The path problem of the DTD in the XML parsing of spring-

Source: Internet
Author: User
Tags baseuri relative
The simplest XML in spring is as follows: <?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" > <beans >
<bean id= "Hello" class= "Com.test.Hello" singleton= "true"/>
</beans>
But the DTD here uses the network path, if the computer does not have the Internet, then spring read the XML will certainly be problematic, because the network address can not be reached, the solution is to use the local DTD, then the path should be set. Generally our configuration files are placed in the classes directory, that is, under the classpath, then is not to put SPRING-BEANS.DTD into the classes inside it. No, look at the source code for this class in spring Org.springframework.beans.factory.xml.BeansDtdResolver:/*
* Copyright 2002-2004 The original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You are not a use this file except in compliance with the License.
* Obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "as is" BASIS,
* Without warranties or CONDITIONS of any KIND, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/Package org.springframework.beans.factory.xml; Import java.io.IOException; Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.xml.sax.EntityResolver;
Import Org.xml.sax.InputSource; Import Org.springframework.core.io.ClassPathResource;
Import Org.springframework.core.io.Resource; /**
* Entityresolver implementation for the Spring beans DTD,
* To-load the DTD from the Spring classpath resp. JAR file.
*
* <p>fetches "SPRING-BEANS.DTD" from the CLASSPATH resource
* "/org/springframework/beans/factory/xml/spring-beans.dtd",
* No matter if specified as some local URL or as
* "Http://www.springframework.org/dtd/spring-beans.dtd".
*
* @author Juergen Hoeller
* @since 04.06.2003
*/
public class Beansdtdresolver implements Entityresolver {private static final String Dtd_name = "Spring-beans";  private static final String search_package = "/org/springframework/beans/factory/xml/";  Protected final Log logger = Logfactory.getlog (GetClass ()); Public InputSource resolveentity (string publicid, String systemid) throws IOException {
Logger.debug ("Trying to resolve XML entity with public ID [" + publicID +
"] and System ID [" + SystemID + "]");
if (systemid! = null && systemid.indexof (dtd_name) > Systemid.lastindexof ("/")) {
String dtdfile = systemid.substring (Systemid.indexof (dtd_name));
Logger.debug ("Trying to locate [" + Dtdfile + "] under [" + Search_package + "]");
try {
Resource Resource = new Classpathresource (Search_package + dtdfile, getclass ());
InputSource Source = new InputSource (Resource.getinputstream ());
Source.setpublicid (PUBLICID);
Source.setsystemid (SystemID);
Logger.debug ("Found beans DTD [" + SystemID + "] in Classpath");
return source;
}
catch (IOException ex) {
Logger.debug ("Could not resolve beans DTD [" + SystemID + "]: No found in Classpath", ex);
}
}
Use the default behaviour, download from website or wherever
return null;
}} It is clear that spring has written the path of/org/springframework/beans/factory/xml/to the code, So we can only put Spring-beans.dtd into this directory, to be guaranteed by the XML parsing engine parsing, of course, here can also use absolute path, but it is difficult to transplant, so it is better to use relative path,   And spring has packed this spring-beans.dtd into the Spring.jar file. OK, now that the relative path is used, the relative path should be written. Before you see what the parsing engine is for spring, because Spring uses JAXP parsing XML files, the JDK uses the Crimson parsing engine without special declarations, as described in Robbin's analysis:/http Forum.javaeye.com/viewtopic.php?t=75 Well, now look at crimson this parsing engine, in parsing the DTD code: private String ResolveUri (string uri)
Throws Saxexception
{
int temp = Uri.indexof (': '); Resolve relative URIs ... must do it here since
It ' s relative to the source file holding the uri! "New Java.net.URL (URL, String)" conforms to RFC 1630,
But we can ' t use this except when the URI is a URL.
The entity resolver is allowed to handle URIs
Not URLs, so we pass URIs through with scheme intact
if (temp = =-1 | | uri.indexof ('/') < temp) {
String BaseURI; BaseURI = In.getsystemid ();
if (BaseURI = = null)
Fatal ("P-055", new Object [] {URI});
if (uri.length () = = 0)
URI = ".";
BaseURI = baseuri.substring (0, Baseuri.lastindexof ('/') + 1);
if (Uri.charat (0)! = '/')
URI = BaseURI + uri;
else {
We have relative URIs that begins with a '/'//Extract scheme including colon from BaseURI
String Baseurischeme;
int colonindex = Baseuri.indexof (': ');
if (Colonindex = =-1) {
Base URI does not has a scheme so default to
"File:" scheme
Baseurischeme = "File:";
} else {
Baseurischeme = baseuri.substring (0, Colonindex + 1);
} URI = Baseurischeme + uri;
}//Letting other code map "/xxx/". /"or"/./"to"/",
Since all URIs must handle it the same.
}
Check for fragment ID in URI
if (Uri.indexof (' # ')! =-1)
Error ("P-056", new Object [] {URI});
return URI;
The path must contain ":" and "/" and ":" must also be in front of "/", so this path we can write the simplest ":/spring-beans.dtd" on it. Of course ":" before adding any other path does not affect the parsing. The path problem of the DTD in the XML parsing of spring-

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.