Use XSD to verify Mybatis's SqlMapper configuration file with of 2

  • 2020-05-12 02:46:14
  • OfStack

In the last article, I introduced the method of using XSD to verify Mybatis's SqlMapper configuration file (1). Those who need it can refer to it.

Write the XSD file, then see how to use the XSD file for validation, and parse the SqlMapper file, which is to implement the doParseSqlMapperResourceWithSchema() method.

To do this, there are two basic requirements:

(1) compatibility: the native configuration of mybatis needs to be compatible. There are two levels of compatibility. One is to use DTD for verification. The other is to go through XSD verification, but also need to be compatible with mybatis native configuration, this compatibility 1 from the above modified XSD file to ensure, another 1 from the XML parsing to ensure.

(2) extensibility: the purpose of modification is for extensibility, so extensibility is also a basic requirement. But extensibility is not arbitrary, and it needs to be extended according to the specification, which is a custom XSD file.

In order to achieve these two basic requirements, the following is one of my ideas, mainly referring to the custom namespace of Spring:

1. Create an EntityResolver and read the configuration file of the specified mode in the classpath, such as "classpath*:**/dysd-* -namespaces.ini ".

2. Define the namespace meta-information in the ini file, such as:

Using the namespace as the name of Section, the following schema and parser represent the xsd file of the namespace and the parser implementation class, respectively, so that the validation file can be found according to the XSD namespace in XML and there is a parse entry.

Description:

apache's commons-configuration provides API for reading files in ini format

Spring USES META-INF/spring. schemas and META-INF/spring. handlers to store XSD files and parser implementation classes

Since you are reading all the ini files in the classpath that satisfy the wildcard, you can easily extend other namespaces. How do you use XSD in Java to verify, I won't go into details here

I've broken down XML parsing into three elements: parsing the context, the parser, and the parsed file. The doParseSqlMapperResourceWithSchema() method is also simple:


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 in turn divided into three levels:

(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 the string wildcards as a collection of Resource objects), ensuring that they are not parsed repeatedly and can be executed concurrently.

(2) XML parses the context


public interface IXmlParserContext extends IParserContext{
public boolean isNamespaceAware(); 
public DocumentLoader getDocumentLoader();
public EntityResolver getEntityResolver();
public ErrorHandler getErrorHandler();
public XmlParserDelegate getDelegate();
}

The corresponding parser implementation class is mainly responsible for converting Resource into Document objects and validating them during the transformation.

(3) SqlMapper parses context


public interface ISqlMapperParserContext extends IXmlParserContext{
public Configuration getConfiguration();
}

The parser implementation class at the corresponding level is primarily responsible for finding the parser in the namespace where the root element resides and parsing Document using the parser.

Finally, the resolution is delegated to the SchemaSqlMapperNamespaceParser class in the ini configuration file, but since this class needs to be configured in a text file and is not convenient for the constructor with arguments, it is further delegated to SchemaSqlMapperParserDelegate:


public class SchemaSqlMapperNamespaceParser implements INamespaceParser<ISqlMapperParserContext> {
@Override
public void init() {
}
@Override
public void parse(ISqlMapperParserContext parserContext, Document document, Resource resource) {
SchemaSqlMapperParserDelegate delegate = new SchemaSqlMapperParserDelegate(parserContext, document, resource);
delegate.parse();
}
@Override
public void destory() {
}
}

So far, the XSD verification has been completed and the XML parsing entry has been found. The following is the real parsing in SchemaSqlMapperParserDelegate.


Related articles: