Sqlmapper configuration file with XSD checksum mybatis (2)

Source: Internet
Author: User

Write the XSD file and see How to use the XSD file checksum and parse the Sqlmapper file, which is the implementation of the Doparsesqlmapperresourcewithschema () method.

To achieve this function, there are two basic requirements:

(1) Compatibility: Need to be compatible with MyBatis's native configuration, compatible with two levels, one is to use DTD verification, which has been said before, go the original process, compatibility is not a problem, the other is to go XSD checksum, but also need to be compatible with MyBatis native configuration, This compatibility on the one hand from the above modified XSD file to ensure, on the other hand also need to from the XML parsing to ensure.

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

In order to achieve these two basic requirements, here is one of my ideas, the main reference to the spring's custom namespaces:

1, create a entityresolver, read the configuration file of the specified mode under the Classpath, for example: "Classpath*:* */dysd-*-namespaces.ini"

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

Using namespaces as the name of the section, the following schema, parser, represent the XSD file for the namespace and the parser implementation class, so that the checksum file can be found based on the XSD namespace in the XML, and there is a parse entry in place.

Description

    1. Apache Commons-configuration provides the read API for INI format files
    2. Spring uses Meta-inf/spring.schemas and meta-inf/spring.handlers to store XSD files and parser implementation classes, where I modify the configuration to use the INI file centrally
    3. Because the read is the INI file that satisfies the wildcard character under all classpath, it can be very simple to extend the other namespaces, as for the specific use of XSD in Java to verify, here is not detailed

I decomposed the parsing of XML into three elements: parsing context, parser, parsed file. The Doparsesqlmapperresourcewithschema () method is also concise:

protected void Doparsesqlmapperresourcewithschema (Configuration configuration, Resource mapperlocation) {    new  sqlmapperparsercontext (configuration);    Xmlparserutils.parsexml (context, mapperlocation);}

The parser interface is as follows:

 Public Interface extends Iparsercontext> {    publicvoid  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 then divided into three tiers:

(1) General parsing context:

 Public Interface Iparsercontext {     public  problemreporter getproblemreporter ();      Public EventListener Geteventlistener ();      Public sourceextractor getsourceextractor ();      Public Environment getenvironment ();}

The parser implementation class of the corresponding level is mainly responsible for loading the parsed file (for example, Fugazai the string to the Resource object collection), ensuring that the parsing is not repeated, and the concurrency can be executed.

(2) XML parsing context

 Public Interface extends iparsercontext{    publicboolean  isnamespaceaware ();        Public Documentloader Getdocumentloader ();      Public entityresolver getentityresolver ();      Public ErrorHandler Geterrorhandler ();      Public xmlparserdelegate getdelegate ();}

The parser implementation classes at the corresponding level are primarily responsible for converting resource to document objects and verifying them during the conversion process.

(3) Sqlmapper parsing context

 Public Interface extends ixmlparsercontext{    public  Configuration getconfiguration ();}

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

Finally, the parsing is delegated to the Schemasqlmappernamespaceparser class in the INI configuration file, but because this class needs to be configured in a text file, it is not convenient to have a parameter constructor, Thus further entrusted to Schemasqlmapperparserdelegate:

 Public classSchemasqlmappernamespaceparserImplementsInamespaceparser<isqlmapperparsercontext>{@Override Public voidinit () {} @Override Public voidParse (isqlmapperparsercontext parsercontext, document document, Resource Resource) {Schemasqlmapperparserdele Gate Delegate=Newschemasqlmapperparserdelegate (ParserContext, document, Resource);    Delegate.parse (); } @Override Public voiddestory () {}}

At this point, the XSD checksum has been completed, and the XML parsing portal has been found, followed by the real parsing in the schemasqlmapperparserdelegate.

Sqlmapper configuration file with XSD checksum mybatis (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.