`

spring4.0源码分析解析xml补充━━━(PluggableSchemaResolver类)

 
阅读更多

        上面一篇文件讲了BeanDefinition的原理。这一篇讲spring解析xml的时候一些小特点。主要是验证,然后不同的xml名称空间会有不同的类来解析xml。解析xml,spring没有用到第三方库,而是直接使用java的api。这里是和spring解析xml类似的代码。我们从这里开始。

@Test
public void testSpringReaderXml() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		//设置解析xml的时候验证xml的格式
		factory.setValidating(true);
		factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
		factory.setExpandEntityReferences(true);
		DocumentBuilder docBuilder = factory.newDocumentBuilder();
//设置解析验证实例
		docBuilder.setEntityResolver(new EntityResolver() {
			@Override
			public InputSource resolveEntity(String publicId, String systemId)
					throws SAXException, IOException {
				//验证xml时要用到的xsd文件,对应schemaLocation的value
				InputSource source = new InputSource(TestXSD.class.getResourceAsStream("D:\\workspaceStudy\\spring_study\\src\\spring-beans-3.0.xsd"));
				source.setPublicId(publicId);
				//验证xml对应的schemaLocation的key
				source.setSystemId("http://www.springframework.org/schema/beans/spring-beans-3.0.xsd");
				return source;
			}

		});
		//设置解析错误时回调方法
		docBuilder.setErrorHandler(new ErrorHandler() {

			@Override
			public void warning(SAXParseException exception)
					throws SAXException {
				exception.printStackTrace();
				throw exception;
			}

			@Override
			public void error(SAXParseException exception) throws SAXException {
				// TODO Auto-generated method stub
				exception.printStackTrace();
				throw exception;
			}

			@Override
			public void fatalError(SAXParseException exception)
					throws SAXException {
				// TODO Auto-generated method stub
				exception.printStackTrace();
				throw exception;
			}

		});

		Document document = docBuilder
				.parse("D:\\workspaceStudy\\spring_study\\src\\beanFactory2.xml");
		Element root = document.getDocumentElement();
		String nodeName = root.getNodeName();
		System.out.println("nodeName:" + nodeName + " ;namespace:"
				+ root.getNamespaceURI());
		NodeList nl = root.getChildNodes();
		for (int i = 0; i < nl.getLength(); i++) {
			Node node = nl.item(i);
			if (node instanceof Element) {
				Element ele = (Element) node;
				System.out.println(ele.getLocalName() + ":"
						+ ele.getAttribute("id"));
			}
		}
		System.out.println("validate true");
}

        spring解析是直接用DocumentBuilder这个类生成Document,然后通过Document得到不同的Element,并且不同的Element,为不同的方法解析。解析xml时验证的实例就会用到PluggableSchemaResolver类。

public class PluggableSchemaResolver implements EntityResolver {

	/**
	 * The location of the file that defines schema mappings.
	 * Can be present in multiple JAR files.
	 */
	public static final String DEFAULT_SCHEMA_MAPPINGS_LOCATION = "META-INF/spring.schemas";


	private static final Log logger = LogFactory.getLog(PluggableSchemaResolver.class);

	private final ClassLoader classLoader;

	private final String schemaMappingsLocation;

	/** Stores the mapping of schema URL -> local schema path */
	private volatile Map<String, String> schemaMappings;

。。。。。
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
		if (logger.isTraceEnabled()) {
			logger.trace("Trying to resolve XML entity with public id [" + publicId +
					"] and system id [" + systemId + "]");
		}

		if (systemId != null) {
			String resourceLocation = getSchemaMappings().get(systemId);
			if (resourceLocation != null) {
				Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
				try {
					InputSource source = new InputSource(resource.getInputStream());
					source.setPublicId(publicId);
					source.setSystemId(systemId);
					if (logger.isDebugEnabled()) {
						logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
					}
					return source;
				}
				catch (FileNotFoundException ex) {
					if (logger.isDebugEnabled()) {
						logger.debug("Couldn't find XML schema [" + systemId + "]: " + resource, ex);
					}
				}
			}
		}
		return null;
	}
   

}

 

上面的schemaMappings就是存放url对应的xsd地址。这里是读classpath路径下所有的META-INF/spring.schemas文件。读的方法就类似下面:

@Test
	public void testMeta() throws IOException {
		ClassLoader cld = Thread.currentThread().getContextClassLoader();
		Enumeration enumURL = cld.getResources("META-INF/spring.schemas");
		int count = 0;
		while (enumURL.hasMoreElements()) {
			URL url = (URL) enumURL.nextElement();
			count++;
			System.out.println(url.getFile());
			InputStream is = url.openConnection().getInputStream();
			int i = is.read();
			while (i != -1) {
				System.out.print((char) i);
				i = is.read();
			}
			System.out.println();
			is.close();
		}
		System.out.println(count);
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics