Method of using XSD to validate MyBatis sqlmapper configuration files (2) _java

Source: Internet
Author: User

In the previous article to introduce the use of XSD checksum mybatis sqlmapper configuration file Method (1), the need for friends can refer to the following.

Write the XSD file, then see how to use the XSD file checksum, and parse the Sqlmapper file, that is, implement the Doparsesqlmapperresourcewithschema () method.

To achieve this function, there are two basic requirements:

(1) Compatibility: the need for compatible mybatis of the original configuration, compatible with two levels, one is the use of DTD checksum, which has been said before, go the original process, compatibility is no problem; the other is to take an XSD checksum, but it also requires a compatible mybatis native configuration. This compatibility on the one hand from the above modified XSD file to ensure, on the other hand, the need for 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 as a specification, which is a custom XSD file.

To achieve these two basic requirements, here's a thought for me, drawing on the custom namespaces of spring:

1, create a entityresolver, read the classpath under the specified mode of configuration files, such as: "Classpath*:* */dysd-*-namespaces.ini"

2, in the INI file to define namespace meta information, such as:

Using namespaces as the name of the section, the following schema, parser represents the XSD file for the namespace and the parser implementation class, so that you can find the checksum file based on the XSD namespace in the XML and have a parse entry.

Description

Apache's Commons-configuration provides a read API for INI format files

Spring uses Meta-inf/spring.schemas and meta-inf/spring.handlers to store XSD file and parser implementation classes, which I modified to use in the INI file set configuration

Because you read the INI file that satisfies the wildcard characters under all the classpath, you can extend the other namespaces very simply, and how to use XSD to validate in Java is not detailed here.

I decompose XML parsing into three elements: parsing context, parser, and parsed file. The Doparsesqlmapperresourcewithschema () method is also 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 the parser implementation class are divided into three levels in turn:

(1) General resolution context:

Public interface Iparsercontext {public 
problemreporter getproblemreporter ();
Public EventListener Geteventlistener ();
Public Sourceextractor getsourceextractor ();
Public environment getenvironment ();
}

The corresponding level of the parser implementation class is mainly responsible for loading the parsed file (such as the string wildcard Fugazai as a resource object set), to ensure that the resolution is not repeated, to ensure 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 corresponding level of the parser implementation class is primarily responsible for converting resource to 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 primarily responsible for finding the parser for the namespace in which the root element resides and parsing the document using the parser.

Ultimately, the Schemasqlmappernamespaceparser class that is delegated to the INI configuration file is resolved, but because this class needs to be configured in a text file, it is inconvenient to have a parameter constructor. Thus further entrusted to the Schemasqlmapperparserdelegate:

public class Schemasqlmappernamespaceparser implements inamespaceparser<isqlmapperparsercontext> {
@ Override public
void init () {
}
@Override
the public void parse (Isqlmapperparsercontext parsercontext, Document document, Resource Resource) {
schemasqlmapperparserdelegate delegate = new Schemasqlmapperparserdelegate (ParserContext, document, Resource);
Delegate.parse ();
}
@Override public
void Destory () {
}
}

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.

The above is a small part of the description of the use of XSD checksum mybatis sqlmapper configuration file Method (2), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.