In the previous article, I introduced to you the method of using XSD to verify Mybatis' SqlMapper configuration file (1). Friends who need it can refer to it.
Write the XSD file, then see how to use the XSD file to verify it, and parse the SqlMapper file, that is, implement the doParseSqlMapperResourceWithSchema() method.
In order to implement this function, there are two basic requirements:
(1) Compatibility: It needs to be compatible with the native configuration of mybatis. There are two levels of compatibility. One is to use DTD verification. As mentioned earlier, there is no problem with compatibility when following the original process; the other is to use XSD verification, but it also needs to be compatible with the native configuration of mybatis. On the one hand, this compatibility is guaranteed from the XSD file modified above, and on the other hand, it also needs to be guaranteed from XML parsing.
(2) Extensibility: The purpose of modification is to expandability, so extensibility is also a basic requirement. However, the scalability is not arbitrary, and it also needs to be extended according to the specification, which is a custom XSD file.
In order to meet these two basic requirements, the following is my idea, mainly drawing on Spring's custom namespace:
1. Create an EntityResolver and read the configuration file of the specified mode under the classpath, such as: "classpath*:**/dysd-*-namespaces.ini"
2. Define namespace meta information in the ini file, such as:
Use the namespace as the name of the Section. The schema and parser below represent the xsd file and parser implementation class of the namespace respectively. In this way, you can find the verification file based on the XSD namespace in XML and have a parsing entry.
illustrate:
Apache's commons-configuration provides a reading API for ini format files
In Spring, I used META-INF/spring.schemas and META-INF/spring.handlers to store XSD files and parser implementation classes. Here I modified it to use ini file central configuration
Because it is reading an ini file that satisfies wildcard characters under all classpaths, it can easily expand other namespaces. As for how to use XSD to verify in Java, I won't go into details here
I decompose the parsing of XML into three elements: parsing the context, parsing the file. The doParseSqlMapperResourceWithSchema() method is also very concise:
protected void doParseSqlMapperResourceWithSchema(Configuration configuration, Resource mapperLocation){ISqlMapperParserContext context = new SqlMapperParserContext(configuration);XmlParserUtils.parseXml(context, mapperLocation);}The parser interface is as follows:
public interface IParser<E extends IParserContext> {public void parse(E parserContext, String location);public void parse(E parserContext, String[] locationPatterns);public void parse(E parserContext, InputStream inputStream);public void parse(E parserContext, Resource resource);}The parsing context and parser implementation classes are divided into three levels in turn:
(1) General parsing context:
public interface IParserContext { public ProblemReporter getProblemReporter();public EventListener getEventListener();public SourceExtractor getSourceExtractor();public Environment getEnvironment();}The parser implementation class at the corresponding level is mainly responsible for loading the parsed files (such as loading string wildcards into a collection of Resource objects), ensuring that no repeated parsing is performed, and ensuring concurrent execution.
(2) XML parsing context
public interface IXmlParserContext extends IParserContext{public boolean isNamespaceAware(); public DocumentLoader getDocumentLoader();public EntityResolver getEntityResolver();public ErrorHandler getErrorHandler();public XmlParserDelegate getDelegate();}The parser implementation class at the corresponding level is mainly responsible for converting the Resource into a Document object and verifying it during the conversion process.
(3) SqlMapper parsing context
public interface ISqlMapperParserContext extends IXmlParserContext{public Configuration getConfiguration();}The parser implementation class at the corresponding level is mainly responsible for finding the parser in the namespace where the root element is located, and using the parser to parse the Document.
Finally, the parsing is delegated to the SchemaSqlMapperNamespaceParser class in the ini configuration file. However, because this class needs to be configured in a text file, it is not convenient to constructors with parameters, so it is further delegated to SchemaSqlMapperParserDelegate:
public class SchemaSqlMapperNamespaceParser implements INamespaceParser<ISqlMapperParserContext> {@Overridepublic void init() {}@Overridepublic void parse(ISqlMapperParserContext parserContext, Document document, Resource resource) {SchemaSqlMapperParserDelegate delegate = new SchemaSqlMapperParserDelegate(parserContext, document, resource);delegate.parse();}@Overridepublic void destory() {}}At this point, the XSD verification has been completed and the XML parsing portal has been found. The subsequent real parsing is in SchemaSqlMapperParserDelegate.
The above is the method (2) for using XSD to verify Mybatis' SqlMapper configuration file that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!