Skip to content

Commit

Permalink
Partly reverted mybatis#1393
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harawata committed Jan 9, 2019
1 parent f3f8503 commit 6267204
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 86 deletions.
30 changes: 15 additions & 15 deletions src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -218,7 +218,7 @@ private void parameterMapElement(List<XNode> list) {
String id = parameterMapNode.getStringAttribute("id");
String type = parameterMapNode.getStringAttribute("type");
Class<?> parameterClass = resolveClass(type);
List<XNode> parameterNodes = parameterMapNode.evalNodes("*[local-name()='parameter']");
List<XNode> parameterNodes = parameterMapNode.evalNodes("parameter");
List<ParameterMapping> parameterMappings = new ArrayList<>();
for (XNode parameterNode : parameterNodes) {
String property = parameterNode.getStringAttribute("property");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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;
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -113,7 +113,7 @@ public void parseStatementNode() {
}

private void processSelectKeyNodes(String id, Class<?> parameterTypeClass, LanguageDriver langDriver) {
List<XNode> selectKeyNodes = context.evalNodes("*[local-name()='selectKey']");
List<XNode> selectKeyNodes = context.evalNodes("selectKey");
if (configuration.getDatabaseId() != null) {
parseSelectKeyNodes(id, selectKeyNodes, parameterTypeClass, langDriver, configuration.getDatabaseId());
}
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/org/apache/ibatis/parsing/XPathParser.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -46,18 +45,6 @@
*/
public class XPathParser {

/**
* The special property key that indicate whether use a xsd file on xml parsing.
* <p>
* 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.
* </p>
* @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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)) {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 6267204

Please sign in to comment.