From fa5e1858e8e457aa3b840df7a78bd156acc870c2 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Thu, 10 Jan 2019 00:32:49 +0900 Subject: [PATCH] Partly reverted #1393 We try to find a solution that does not require system property switch. We may be able to reuse the XSD files and the most part of the tests. --- .../ibatis/builder/xml/XMLConfigBuilder.java | 30 +++++++-------- .../ibatis/builder/xml/XMLMapperBuilder.java | 18 ++++----- .../builder/xml/XMLMapperEntityResolver.java | 38 ++++--------------- .../builder/xml/XMLStatementBuilder.java | 4 +- .../apache/ibatis/parsing/XPathParser.java | 21 +--------- .../builder/xsd/XmlConfigBuilderTest.java | 13 ++++--- .../builder/xsd/XmlMapperBuilderTest.java | 9 +++-- 7 files changed, 47 insertions(+), 86 deletions(-) diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java b/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java index 95b76cf982c..a873d96231f 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,28 +95,28 @@ public Configuration parse() { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; - parseConfiguration(parser.evalNode("*[local-name()='configuration']")); + parseConfiguration(parser.evalNode("/configuration")); return configuration; } private void parseConfiguration(XNode root) { try { //issue #117 read properties first - propertiesElement(root.evalNode("*[local-name()='properties']")); - Properties settings = settingsAsProperties(root.evalNode("*[local-name()='settings']")); + propertiesElement(root.evalNode("properties")); + Properties settings = settingsAsProperties(root.evalNode("settings")); loadCustomVfs(settings); loadCustomLogImpl(settings); - typeAliasesElement(root.evalNode("*[local-name()='typeAliases']")); - pluginElement(root.evalNode("*[local-name()='plugins']")); - objectFactoryElement(root.evalNode("*[local-name()='objectFactory']")); - objectWrapperFactoryElement(root.evalNode("*[local-name()='objectWrapperFactory']")); - reflectorFactoryElement(root.evalNode("*[local-name()='reflectorFactory']")); + typeAliasesElement(root.evalNode("typeAliases")); + pluginElement(root.evalNode("plugins")); + objectFactoryElement(root.evalNode("objectFactory")); + objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); + reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 - environmentsElement(root.evalNode("*[local-name()='environments']")); - databaseIdProviderElement(root.evalNode("*[local-name()='databaseIdProvider']")); - typeHandlerElement(root.evalNode("*[local-name()='typeHandlers']")); - mapperElement(root.evalNode("*[local-name()='mappers']")); + environmentsElement(root.evalNode("environments")); + databaseIdProviderElement(root.evalNode("databaseIdProvider")); + typeHandlerElement(root.evalNode("typeHandlers")); + mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } @@ -276,8 +276,8 @@ private void environmentsElement(XNode context) throws Exception { for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); if (isSpecifiedEnvironment(id)) { - TransactionFactory txFactory = transactionManagerElement(child.evalNode("*[local-name()='transactionManager']")); - DataSourceFactory dsFactory = dataSourceElement(child.evalNode("*[local-name()='dataSource']")); + TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); + DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java index f5da50185f9..d5eef32deea 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,7 +91,7 @@ private XMLMapperBuilder(XPathParser parser, Configuration configuration, String public void parse() { if (!configuration.isResourceLoaded(resource)) { - configurationElement(parser.evalNode("*[local-name()='mapper']")); + configurationElement(parser.evalNode("/mapper")); configuration.addLoadedResource(resource); bindMapperForNamespace(); } @@ -112,12 +112,12 @@ private void configurationElement(XNode context) { throw new BuilderException("Mapper's namespace cannot be empty"); } builderAssistant.setCurrentNamespace(namespace); - cacheRefElement(context.evalNode("*[local-name()='cache-ref']")); - cacheElement(context.evalNode("*[local-name()='cache']")); - parameterMapElement(context.evalNodes("*[local-name()='parameterMap']")); - resultMapElements(context.evalNodes("*[local-name()='resultMap']")); - sqlElement(context.evalNodes("*[local-name()='sql']")); - buildStatementFromContext(context.evalNodes("*[local-name()='select' or local-name()='insert' or local-name()='update' or local-name()='delete']")); + cacheRefElement(context.evalNode("cache-ref")); + cacheElement(context.evalNode("cache")); + parameterMapElement(context.evalNodes("/mapper/parameterMap")); + resultMapElements(context.evalNodes("/mapper/resultMap")); + sqlElement(context.evalNodes("/mapper/sql")); + buildStatementFromContext(context.evalNodes("select|insert|update|delete")); } catch (Exception e) { throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e); } @@ -218,7 +218,7 @@ private void parameterMapElement(List list) { String id = parameterMapNode.getStringAttribute("id"); String type = parameterMapNode.getStringAttribute("type"); Class parameterClass = resolveClass(type); - List parameterNodes = parameterMapNode.evalNodes("*[local-name()='parameter']"); + List parameterNodes = parameterMapNode.evalNodes("parameter"); List parameterMappings = new ArrayList<>(); for (XNode parameterNode : parameterNodes) { String property = parameterNode.getStringAttribute("property"); diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperEntityResolver.java b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperEntityResolver.java index fbc3a781c6d..0b35971479a 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperEntityResolver.java +++ b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperEntityResolver.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ import org.xml.sax.SAXException; /** - * Offline entity resolver for the MyBatis DTDs(or XSDs) + * Offline entity resolver for the MyBatis DTDs * * @author Clinton Begin * @author Eduardo Macarron @@ -36,21 +36,16 @@ public class XMLMapperEntityResolver implements EntityResolver { private static final String IBATIS_MAPPER_SYSTEM = "ibatis-3-mapper.dtd"; private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd"; private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd"; - private static final String MYBATIS_CONFIG = "mybatis-config.xsd"; - private static final String MYBATIS_MAPPER = "mybatis-mapper.xsd"; private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd"; private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd"; - private static final String MYBATIS_CONFIG_XSD = "org/apache/ibatis/builder/xml/mybatis-config.xsd"; - private static final String MYBATIS_MAPPER_XSD = "org/apache/ibatis/builder/xml/mybatis-mapper.xsd"; - /** - * Converts a public DTD(XSD) into a local one + * Converts a public DTD into a local one * * @param publicId The public id that is what comes after "PUBLIC" * @param systemId The system id that is what comes after the public id. - * @return The InputSource for the DTD(XSD) + * @return The InputSource for the DTD * * @throws org.xml.sax.SAXException If anything goes wrong */ @@ -60,13 +55,9 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc if (systemId != null) { String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH); if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) { - return getDtdInputSource(MYBATIS_CONFIG_DTD, publicId, systemId); + return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId); } else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) { - return getDtdInputSource(MYBATIS_MAPPER_DTD, publicId, systemId); - } else if (systemId.contains(MYBATIS_CONFIG)) { - return getXsdInputSource(MYBATIS_CONFIG_XSD); - } else if (systemId.contains(MYBATIS_MAPPER)){ - return getXsdInputSource(MYBATIS_MAPPER_XSD); + return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId); } } return null; @@ -75,27 +66,14 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc } } - private InputSource getDtdInputSource(String path, String publicId, String systemId) { + private InputSource getInputSource(String path, String publicId, String systemId) { InputSource source = null; if (path != null) { try { InputStream in = Resources.getResourceAsStream(path); source = new InputSource(in); source.setPublicId(publicId); - source.setSystemId(systemId); - } catch (IOException e) { - // ignore, null is ok - } - } - return source; - } - - private InputSource getXsdInputSource(String path) { - InputSource source = null; - if (path != null) { - try { - InputStream in = Resources.getResourceAsStream(path); - source = new InputSource(in); + source.setSystemId(systemId); } catch (IOException e) { // ignore, null is ok } diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java b/src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java index 12e020c51c7..61298dbeeca 100644 --- a/src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -113,7 +113,7 @@ public void parseStatementNode() { } private void processSelectKeyNodes(String id, Class parameterTypeClass, LanguageDriver langDriver) { - List selectKeyNodes = context.evalNodes("*[local-name()='selectKey']"); + List selectKeyNodes = context.evalNodes("selectKey"); if (configuration.getDatabaseId() != null) { parseSelectKeyNodes(id, selectKeyNodes, parameterTypeClass, langDriver, configuration.getDatabaseId()); } diff --git a/src/main/java/org/apache/ibatis/parsing/XPathParser.java b/src/main/java/org/apache/ibatis/parsing/XPathParser.java index 9c755d5df2f..42981a1633f 100644 --- a/src/main/java/org/apache/ibatis/parsing/XPathParser.java +++ b/src/main/java/org/apache/ibatis/parsing/XPathParser.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import java.util.List; import java.util.Properties; -import javax.xml.XMLConstants; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -46,18 +45,6 @@ */ public class XPathParser { - /** - * The special property key that indicate whether use a xsd file on xml parsing. - *

- * The default value is {@code false} (indicate disable using a xsd file xml parsing) - * If you specify the {@code true}, you can use a xsd file on config xml or mapper xml. - *

- * @since 3.5.0 - */ - public static final String KEY_USE_XSD = "org.mybatis.useXsd"; - - private static final String USE_XSD_DEFAULT_VALUE = "false"; - private final Document document; private boolean validation; private EntityResolver entityResolver; @@ -251,12 +238,6 @@ private Document createDocument(InputSource inputSource) { factory.setCoalescing(false); factory.setExpandEntityReferences(true); - boolean useXsd = Boolean.parseBoolean(System.getProperty(KEY_USE_XSD, USE_XSD_DEFAULT_VALUE)); - if (useXsd) { - factory.setNamespaceAware(true); - factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); - } - DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(entityResolver); builder.setErrorHandler(new ErrorHandler() { diff --git a/src/test/java/org/apache/ibatis/builder/xsd/XmlConfigBuilderTest.java b/src/test/java/org/apache/ibatis/builder/xsd/XmlConfigBuilderTest.java index 8d4e6b5a7d3..8647a7cbb59 100644 --- a/src/test/java/org/apache/ibatis/builder/xsd/XmlConfigBuilderTest.java +++ b/src/test/java/org/apache/ibatis/builder/xsd/XmlConfigBuilderTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,12 +36,12 @@ import org.apache.ibatis.io.Resources; import org.apache.ibatis.logging.slf4j.Slf4jImpl; import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.parsing.XPathParser; import org.apache.ibatis.scripting.defaults.RawLanguageDriver; import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver; import org.apache.ibatis.session.*; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.apache.ibatis.type.JdbcType; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.io.InputStream; @@ -51,11 +51,12 @@ import static org.junit.jupiter.api.Assertions.*; +@Disabled("We'll try a different approach. See #1393") public class XmlConfigBuilderTest { @Test public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception { - System.setProperty(XPathParser.KEY_USE_XSD, "true"); + // System.setProperty(XPathParser.KEY_USE_XSD, "true"); String resource = "org/apache/ibatis/builder/xsd/MinimalMapperConfig.xml"; try (InputStream inputStream = Resources.getResourceAsStream(resource)) { XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); @@ -85,13 +86,13 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception { assertNull(config.getLogImpl()); assertNull(config.getConfigurationFactory()); } finally { - System.clearProperty(XPathParser.KEY_USE_XSD); + // System.clearProperty(XPathParser.KEY_USE_XSD); } } @Test public void shouldSuccessfullyLoadXMLConfigFitle() throws Exception { - System.setProperty(XPathParser.KEY_USE_XSD, "true"); + // System.setProperty(XPathParser.KEY_USE_XSD, "true"); String resource = "org/apache/ibatis/builder/xsd/CustomizedSettingsMapperConfig.xml"; try (InputStream inputStream = Resources.getResourceAsStream(resource)) { XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); @@ -156,7 +157,7 @@ public void shouldSuccessfullyLoadXMLConfigFitle() throws Exception { assertTrue(config.getMapperRegistry().hasMapper(BlogMapper.class)); assertTrue(config.getMapperRegistry().hasMapper(NestedBlogMapper.class)); } finally { - System.clearProperty(XPathParser.KEY_USE_XSD); + // System.clearProperty(XPathParser.KEY_USE_XSD); } } diff --git a/src/test/java/org/apache/ibatis/builder/xsd/XmlMapperBuilderTest.java b/src/test/java/org/apache/ibatis/builder/xsd/XmlMapperBuilderTest.java index e19d9891cef..cb0d69d96da 100644 --- a/src/test/java/org/apache/ibatis/builder/xsd/XmlMapperBuilderTest.java +++ b/src/test/java/org/apache/ibatis/builder/xsd/XmlMapperBuilderTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,18 +20,19 @@ import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.mapping.StatementType; -import org.apache.ibatis.parsing.XPathParser; import org.apache.ibatis.session.Configuration; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.io.InputStream; +@Disabled("We'll try a different approach. See #1393") public class XmlMapperBuilderTest { @Test public void mappedStatementWithOptions() throws Exception { - System.setProperty(XPathParser.KEY_USE_XSD, "true"); + // System.setProperty(XPathParser.KEY_USE_XSD, "true"); Configuration configuration = new Configuration(); String resource = "org/apache/ibatis/builder/xsd/AuthorMapper.xml"; try (InputStream inputStream = Resources.getResourceAsStream(resource)) { @@ -46,7 +47,7 @@ public void mappedStatementWithOptions() throws Exception { Assertions.assertFalse(mappedStatement.isFlushCacheRequired()); Assertions.assertFalse(mappedStatement.isUseCache()); } finally { - System.clearProperty(XPathParser.KEY_USE_XSD); + // System.clearProperty(XPathParser.KEY_USE_XSD); } }