Source: http://cmsblogs.com
In the previous blog "Dead Spring"-----The IOC's load Bean mentions that there are three things to do in the core logic approach doLoadBeanDefinitions()
.
- Call
getValidationModeForResource()
The Get XML file validation mode
- Called
loadDocument()
to obtain the corresponding document instance from the XML file.
- Invokes a
registerBeanDefinitions()
registered Bean instance.
This blog main analysis gets the validation mode of the XML file.
The validation mode of XML file guarantees the correctness of XML file.
The difference between DTD and XSD
A DTD (document type definition), which is a validation mechanism for XML files, is a part of the XML file. A DTD is an effective way of validating the proper format of an XML document, which defines the elements, attributes, arrangement, content type, and hierarchy of elements of an XML document. In fact, the DTD is equivalent to the "vocabulary" and "syntax" in XML, we can compare the XML file and the DTD file to see whether the document conforms to the specification, the elements and labels are used correctly.
To use a DTD in spring, you need to declare it at the head of the spring XML file:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
The DTD pushes the development of XML in a certain phase, but it has some drawbacks in itself:
- It does not use XML format, but instead defines a set of formats, relatively poor reusability of the parser, and there is no standard programming interface for DTD construction and access, so it is difficult for parsers to parse DTD documents easily.
- DTDs have fewer types of elements, while others are also weak.
- DTD has poor scalability.
- The ability to describe a DTD document based on regular expressions is limited.
For the shortcomings of the DTD, the consortium introduced the XSD in 2001. XSD (XML Schemas Definition) is the XML Schema language. The XML Schema itself is an XML document that uses XML syntax so that the XSD document can be easily parsed. The following advantages are provided with respect to dtd,xsd:
- XML Schema is XML-based, with no specific syntax
- XML schemas can parse and process like other XML files
- The XML schema provides a richer data type than the DTD.
- The XML schema provides an extensible data model.
- XML schema supports integrated namespaces
- The XML schema supports attribute groups.
Getvalidationmodeforresource () analysis
protected int getValidationModeForResource(Resource resource) { // 获取指定的验证模式 int validationModeToUse = getValidationMode(); // 如果手动指定,则直接返回 if (validationModeToUse != VALIDATION_AUTO) { return validationModeToUse; } // 通过程序检测 int detectedMode = detectValidationMode(resource); if (detectedMode != VALIDATION_AUTO) { return detectedMode; } // 出现异常,返回 XSD return VALIDATION_XSD; }
If the validation mode (call) that specifies the XML file XmlBeanDefinitionReader.setValidating(boolean validating)
is returned directly to the specified authentication mode, the call detectValidationMode()
gets the appropriate validation mode, as follows:
protected int Detectvalidationmode (Resource Resource) {if (Resource.isopen ()) {throw new Beandefin Itionstoreexception ("passed-in Resource [" + Resource + "] contains an open stream:" + "Cannot determine validation mode automatically. Either pass in a Resource "+" that's able to create fresh streams, or explicitly specify the Validati OnMode "+" on your Xmlbeandefinitionreader instance. "); InputStream InputStream; try {InputStream = Resource.getinputstream (); } catch (IOException ex) {throw new Beandefinitionstoreexception ("Unable to Determi NE validation mode for ["+ Resource +"]: Cannot open inputstream. "+" Do you attempt to load direct Ly from a SAX inputsource without specifying the "+" Validationmode on your Xmlbeandefinitionreader in Stance? ", ex); } try {//core method return This.validationModeDetector.detectValidationMode (InputStream); } catch (IOException ex) {throw new Beandefinitionstoreexception ("Unable to determine validation mode F or ["+ Resource +"]: An error occurred whilst reading from the InputStream. ", ex); } }
The previous heap of code, the core is This.validationModeDetector.detectValidationMode (inputstream)
, Validationmodedetector is defined as xmlvalidationmodedetector
, so the acquisition of the validation mode is delegated to the Xmlvalidationmodedetector The Detectvalidationmode ()
method of the
.
public int Detectvalidationmode (InputStream inputstream) throws IOException {BufferedReader reader = new Buffe Redreader (New InputStreamReader (InputStream)); try {Boolean isdtdvalidated = false; String content; Reads the contents of the XML file one line at a while (content = Reader.readline ()) = null) {content = Consumecommenttoke NS (content); if (this.incomment | |!) Stringutils.hastext (content)) {continue; }//contains DOCTYPE for DTD mode if (Hasdoctype (content)) {isdtdvalidated = True ; Break }//Read < start symbol, validation mode must precede the < symbol if (Hasopeningtag (content)) {//End of meaningful data ... break; }}//For True to return DTD, otherwise return XSD return (isdtdvalidated? VALIDATION_DTD:VALIDATION_XSD); } CatCH (charconversionexception ex) {//exception occurred, for XSD return validation_auto; } finally {reader.close (); } }
From the code, mainly by reading the contents of the XML file to determine whether the content contains DOCTYPE, if it is a DTD, or XSD, of course, will only read to the first "<", because the validation mode must be before the first "<". If an charconversionexception exception occurs, it is XSD mode.
OK, the validation mode analysis of the XML file is complete, the doLoadBeanDefinitions()
second step of the next analysis: Get the document instance.