Spring的schemaResolver

Spring解析xml可以参考以上,可以指定自定义的schema,使用Jdk提供的xml API解析xml.
接下来Spring针对自己的schema,针对自己定义的xml元素,解析并注入到Spring的Bean中。

为了保持Spring的高可扩展性,用户可以在Spring的基础上最大限度的开放,这里采用了Schema Resolver,解析器采用最基本的Document Element.

这里给个例子并不是基本Spring的插件体系,不过原因相同。


package org.frame.base.xml.jdk.bk;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.frame.base.xml.jdk.Contact;
import org.frame.base.xml.jdk.ContactName;
import org.frame.base.xml.jdk.MaySimpleSaxErrorHandler;
import org.frame.base.xml.jdk.MyPluggableSchemaResolver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
* Spring提供自定义schema,并提供自定义schema解析类, 并集成到Spring框架中
*
* @author ycl
* @version 1.0 2012-12-17 下午4:59:08
* @since 1.0
*
*/
public class TestSpringNamespaceHandler {
protected final static Log logger = LogFactory.getLog(TestSpringNamespaceHandler.class);

public static MyNamespaceHandlerResolver namespaceHandlerResolver = new DefaultMyNamespaceHandlerResolver();

public static void main(String[] args) {
// set jaxp debug
System.setProperty("jaxp.debug", "1");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();

// 设置使用命名空间
builderFactory.setNamespaceAware(true);
builderFactory.setValidating(true);
builderFactory.setIgnoringComments(true);
builderFactory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
// 如果使用xsd,一船需要设置schemaLanguage的schema版本.

ErrorHandler errorHandler = new MaySimpleSaxErrorHandler(logger);
// 异常处理类
EntityResolver entityResolver = new MyPluggableSchemaResolver(
TestDocumentBuilderFactory.class.getClassLoader());
// 实体分解类

Document document = parse(builderFactory, getInputSource(),
entityResolver, errorHandler);

print(document);
printByHander(document);
}

private static void printByHander(Document document){
Map<String,Object> context = new HashMap<String,Object>();
Element root = document.getDocumentElement();

String namespaceUri = getNamespaceURI(root);
MyNamespaceHandler handler = namespaceHandlerResolver.resolve(namespaceUri);
handler.parse((Element) root,context);
//根元素解析
parserElement(root,context);
Contact contact = (Contact)context.get("contact");
System.out.println(contact);
}

private static void parserElement(Element root,Map<String,Object> context) {
NodeList nodes = root.getChildNodes();
if(nodes!=null&&nodes.getLength()>0){
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String namespaceUri = getNamespaceURI(node);
if(namespaceUri!=null){
MyNamespaceHandler handler = namespaceHandlerResolver.resolve(namespaceUri);
handler.parse((Element) node,context);
parserElement((Element)node,context);
}
}
}
}

private static void print(Document document) {
Element root = document.getDocumentElement();

List<ContactName> contactNameList = new ArrayList<ContactName>();

ContactName contactItem;

// 子元素列表
NodeList nodes = root.getChildNodes();

/**
* code this is so tied.
*/
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
// a child element to process
Element child = (Element) node;
String width = child.getAttribute("width");
contactItem = new ContactName();
contactItem.setWidth(width);
NodeList itemSub = node.getChildNodes();
for (int j = 0; j < itemSub.getLength(); j++) {
Node itemSubNode = itemSub.item(j);
if (itemSubNode instanceof Element) {
if (((Element) itemSubNode).getTagName().equals("uic")) {
contactItem.setUid(itemSubNode.getTextContent());
} else if (((Element) itemSubNode).getTagName().equals(
"fullName")) {
contactItem.setFullName(itemSubNode
.getTextContent());
}
}
}
contactNameList.add(contactItem);
}
}
System.out.println(contactNameList);
}

/**
* 获取节点的namespace
* @param node
* @return
*/
public static String getNamespaceURI(Node node) {
return node.getNamespaceURI();
}


private static InputSource getInputSource() {
StringBuffer xml = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<contact xmlns=\"http://www.ycl.com/schema/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.ycl.com/schema/schema http://www.ycl.com/schema/schema.xsd\" >");
xml.append("<!--ycl is good -->");
xml.append("<item width=\"10\">");
xml.append("<uic>1</uic>");
xml.append("<fullName>ycl1</fullName>");
xml.append("</item>");

xml.append("<item width=\"11\">");
xml.append("<uic>1 <![CDATA[06:00 Vesti<br>06:05 Jutarnji]]> 2</uic>");
xml.append("<fullName>ycl2</fullName>");
xml.append("</item>");

xml.append("</contact>");

InputSource is = new InputSource(new StringReader(xml.toString()));
return is;
}

private static Document parse(DocumentBuilderFactory builderFactory,
InputSource is, EntityResolver entityResolver,
ErrorHandler errorHandler) {
DocumentBuilder builder = null;

Document document = null;
try {
builder = builderFactory.newDocumentBuilder();
if (entityResolver != null) {
builder.setEntityResolver(entityResolver);
}
if (errorHandler != null) {
builder.setErrorHandler(errorHandler);
}
document = builder.parse(is);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return document;
}
}


测试类,主要是使用schema,并定义schema Resolver.

package org.frame.base.xml.jdk.bk;


public interface MyNamespaceHandlerResolver {

/**
* 对应schema 对应的 name handler
* @param namespaceUri
* @return
*/
MyNamespaceHandler resolve(String namespaceUri);
}




/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "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.frame.base.xml.jdk.bk;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.beans.factory.xml.NamespaceHandlerResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;

/**
* Default implementation of the {@link NamespaceHandlerResolver} interface.
* Resolves namespace URIs to implementation classes based on the mappings
* contained in mapping file.
*
* <p>By default, this implementation looks for the mapping file at
* <code>META-INF/spring.handlers</code>, but this can be changed using the
* {@link #DefaultNamespaceHandlerResolver(ClassLoader, String)} constructor.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 2.0
* @see NamespaceHandler
* @see DefaultBeanDefinitionDocumentReader
*/
public class DefaultMyNamespaceHandlerResolver implements MyNamespaceHandlerResolver {

/**
* The location to look for the mapping files. Can be present in multiple JAR files.
*/
public static final String DEFAULT_HANDLER_MAPPINGS_LOCATION = "META-INF/myschema.handlers";


/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());

/** ClassLoader to use for NamespaceHandler classes */
private final ClassLoader classLoader;

/** Resource location to search for */
private final String handlerMappingsLocation;

/** Stores the mappings from namespace URI to NamespaceHandler class name / instance */
private volatile Map<String, Object> handlerMappings;


/**
* Create a new <code>DefaultNamespaceHandlerResolver</code> using the
* default mapping file location.
* <p>This constructor will result in the thread context ClassLoader being used
* to load resources.
* @see #DEFAULT_HANDLER_MAPPINGS_LOCATION
*/
public DefaultMyNamespaceHandlerResolver() {
this(null, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}

/**
* Create a new <code>DefaultNamespaceHandlerResolver</code> using the
* default mapping file location.
* @param classLoader the {@link ClassLoader} instance used to load mapping resources
* (may be <code>null</code>, in which case the thread context ClassLoader will be used)
* @see #DEFAULT_HANDLER_MAPPINGS_LOCATION
*/
public DefaultMyNamespaceHandlerResolver(ClassLoader classLoader) {
this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}

/**
* Create a new <code>DefaultNamespaceHandlerResolver</code> using the
* supplied mapping file location.
* @param classLoader the {@link ClassLoader} instance used to load mapping resources
* may be <code>null</code>, in which case the thread context ClassLoader will be used)
* @param handlerMappingsLocation the mapping file location
*/
public DefaultMyNamespaceHandlerResolver(ClassLoader classLoader, String handlerMappingsLocation) {
Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null");
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.handlerMappingsLocation = handlerMappingsLocation;
}


/**
* Locate the {@link NamespaceHandler} for the supplied namespace URI
* from the configured mappings.
* @param namespaceUri the relevant namespace URI
* @return the located {@link NamespaceHandler}, or <code>null</code> if none found
*/
public MyNamespaceHandler resolve(String namespaceUri) {
Map<String, Object> handlerMappings = getHandlerMappings();
Object handlerOrClassName = handlerMappings.get(namespaceUri);
if (handlerOrClassName == null) {
return null;
}
else if (handlerOrClassName instanceof MyNamespaceHandler) {
return (MyNamespaceHandler) handlerOrClassName;
}
else {
String className = (String) handlerOrClassName;
try {
Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
if (!MyNamespaceHandler.class.isAssignableFrom(handlerClass)) {
throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
"] does not implement the [" + MyNamespaceHandler.class.getName() + "] interface");
}
MyNamespaceHandler namespaceHandler = (MyNamespaceHandler) BeanUtils.instantiateClass(handlerClass);
namespaceHandler.init();
handlerMappings.put(namespaceUri, namespaceHandler);
return namespaceHandler;
}
catch (ClassNotFoundException ex) {
throw new FatalBeanException("NamespaceHandler class [" + className + "] for namespace [" +
namespaceUri + "] not found", ex);
}
catch (LinkageError err) {
throw new FatalBeanException("Invalid NamespaceHandler class [" + className + "] for namespace [" +
namespaceUri + "]: problem with handler class file or dependent class", err);
}
}
}

/**
* Load the specified NamespaceHandler mappings lazily.
*/
private Map<String, Object> getHandlerMappings() {
if (this.handlerMappings == null) {
synchronized (this) {
if (this.handlerMappings == null) {
try {
Properties mappings =
PropertiesLoaderUtils.loadAllProperties(this.handlerMappingsLocation, this.classLoader);
if (logger.isDebugEnabled()) {
logger.debug("Loaded NamespaceHandler mappings: " + mappings);
}
Map<String, Object> handlerMappings = new ConcurrentHashMap<String, Object>();
CollectionUtils.mergePropertiesIntoMap(mappings, handlerMappings);
this.handlerMappings = handlerMappings;
}
catch (IOException ex) {
throw new IllegalStateException(
"Unable to load NamespaceHandler mappings from location [" + this.handlerMappingsLocation + "]", ex);
}
}
}
}
return this.handlerMappings;
}


@Override
public String toString() {
return "NamespaceHandlerResolver using mappings " + getHandlerMappings();
}

}


请允许我的偷懒,赤裸裸的copy Spring 源码.

public interface MyNamespaceHandler {

public void init();
public ContactName parse(Element element,Map<String,Object> context);
}



package org.frame.base.xml.jdk.bk;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.frame.base.xml.jdk.ContactName;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.Element;

public class SchemaNamespaceHandler implements MyNamespaceHandler {

/**
* The location to look for the mapping files. Can be present in multiple JAR files.
*/
public static final String DEFAULT_HANDLER_MAPPINGS_LOCATION = "META-INF/schema/contact.handler";

/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());

/** ClassLoader to use for NamespaceHandler classes */
private final ClassLoader classLoader;

/** Resource location to search for */
private final String handlerMappingsLocation;

/** Stores the mappings from namespace URI to NamespaceHandler class name / instance */
private volatile Map<String, Object> handlerMappings;

public SchemaNamespaceHandler(){
this(null, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}

public SchemaNamespaceHandler(ClassLoader classLoader) {
this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}

public SchemaNamespaceHandler(ClassLoader classLoader, String handlerMappingsLocation) {
Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null");
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.handlerMappingsLocation = handlerMappingsLocation;
}


@Override
public void init() {
// TODO Auto-generated method stub
}

@Override
public ContactName parse(Element element,Map<String,Object> context) {
return resolve(element.getLocalName()).parse(element,context);
}

public MyNamespaceHandler resolve(String namespaceUri) {
Map<String, Object> handlerMappings = getHandlerMappings();
Object handlerOrClassName = handlerMappings.get(namespaceUri);
if (handlerOrClassName == null) {
return null;
}
else if (handlerOrClassName instanceof MyNamespaceHandler) {
return (MyNamespaceHandler) handlerOrClassName;
}
else {
String className = (String) handlerOrClassName;
try {
Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
if (!MyNamespaceHandler.class.isAssignableFrom(handlerClass)) {
throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
"] does not implement the [" + MyNamespaceHandler.class.getName() + "] interface");
}
MyNamespaceHandler namespaceHandler = (MyNamespaceHandler) BeanUtils.instantiateClass(handlerClass);
namespaceHandler.init();
handlerMappings.put(namespaceUri, namespaceHandler);
return namespaceHandler;
}
catch (ClassNotFoundException ex) {
throw new FatalBeanException("NamespaceHandler class [" + className + "] for namespace [" +
namespaceUri + "] not found", ex);
}
catch (LinkageError err) {
throw new FatalBeanException("Invalid NamespaceHandler class [" + className + "] for namespace [" +
namespaceUri + "]: problem with handler class file or dependent class", err);
}
}
}

/**
* Load the specified NamespaceHandler mappings lazily.
*/
private Map<String, Object> getHandlerMappings() {
if (this.handlerMappings == null) {
synchronized (this) {
if (this.handlerMappings == null) {
try {
Properties mappings =
PropertiesLoaderUtils.loadAllProperties(this.handlerMappingsLocation, this.classLoader);
if (logger.isDebugEnabled()) {
logger.debug("Loaded NamespaceHandler mappings: " + mappings);
}
Map<String, Object> handlerMappings = new ConcurrentHashMap<String, Object>();
CollectionUtils.mergePropertiesIntoMap(mappings, handlerMappings);
this.handlerMappings = handlerMappings;
}
catch (IOException ex) {
throw new IllegalStateException(
"Unable to load NamespaceHandler mappings from location [" + this.handlerMappingsLocation + "]", ex);
}
}
}
}
return this.handlerMappings;
}


@Override
public String toString() {
return "NamespaceHandlerResolver using mappings " + getHandlerMappings();
}


}



接下来看下我们的配置文件如下:
myschema.handlers

http\://www.ycl.com/schema/schema=org.frame.base.xml.jdk.bk.SchemaNamespaceHandler

contact.handler

contact=org.frame.base.xml.jdk.bk.ContactHandler
item=org.frame.base.xml.jdk.bk.ItemHandler
uic=org.frame.base.xml.jdk.bk.UicHandler
fullName=org.frame.base.xml.jdk.bk.FullNameHandler

我这里对xml中的每一个元素都定义了Handler

package org.frame.base.xml.jdk.bk;

import java.util.Map;

import org.frame.base.xml.jdk.Contact;
import org.frame.base.xml.jdk.ContactName;
import org.w3c.dom.Element;

/**
* 这里可以包含其他元素进行解析
*
* @author ycl
* @version 1.0 2012-12-18 上午9:32:12
* @since 1.0
*
*/
public class ContactHandler implements MyNamespaceHandler {

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public ContactName parse(Element element,Map<String,Object> context) {
Contact contactName = new Contact();
context.put("contact", contactName);
return null;
}

}



package org.frame.base.xml.jdk.bk;

import java.util.Map;

import org.frame.base.xml.jdk.Contact;
import org.frame.base.xml.jdk.ContactName;
import org.frame.base.xml.jdk.Item;
import org.w3c.dom.Element;

public class ItemHandler implements MyNamespaceHandler {

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public ContactName parse(Element element, Map<String, Object> context) {
Contact contactName = (Contact) context.get("contact");
String width = element.getAttribute("width");
Item item = new Item();
item.setWidth(width);
contactName.addItem(item);
context.put("curentItem", item);
return null;
}

}



package org.frame.base.xml.jdk.bk;

import java.util.Map;

import org.frame.base.xml.jdk.Contact;
import org.frame.base.xml.jdk.ContactName;
import org.frame.base.xml.jdk.Item;
import org.w3c.dom.Element;

public class UicHandler implements MyNamespaceHandler {

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public ContactName parse(Element element, Map<String, Object> context) {
Item item = (Item)context.get("curentItem");
String uic = element.getTextContent();
item.setUic(uic);
return null;
}

}




package org.frame.base.xml.jdk.bk;

import java.util.Map;

import org.frame.base.xml.jdk.ContactName;
import org.frame.base.xml.jdk.Item;
import org.w3c.dom.Element;

public class FullNameHandler implements MyNamespaceHandler {

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public ContactName parse(Element element, Map<String, Object> context) {
Item item = (Item)context.get("curentItem");
String fullName = element.getTextContent();
item.setFullName(fullName);
return null;
}

}



当然也,这里图个简单,解析整个xml,而在resolver中可以解析自己需要的xml元素到自己的Context中,context可以放任何中东西,这是一个上下文。

输出数据如下:

20121218-10:56:08 main org.springframework.beans.factory.xml.PluggableSchemaResolver Loading schema mappings from [META-INF/myschema.schemas]
20121218-10:56:08 main org.springframework.beans.factory.xml.PluggableSchemaResolver Loaded schema mappings: {http://www.ycl.com/schema/schema.xsd=org/frame/base/xml/jdk/schema.xsd}
20121218-10:56:08 main org.springframework.beans.factory.xml.PluggableSchemaResolver Found XML schema [http://www.ycl.com/schema/schema.xsd] in classpath: org/frame/base/xml/jdk/schema.xsd
[uid:1,fullName:ycl1,width:10, uid:1 06:00 Vesti<br>06:05 Jutarnji 2,fullName:ycl2,width:11]
20121218-10:56:08 main org.frame.base.xml.jdk.bk.DefaultMyNamespaceHandlerResolver Loaded NamespaceHandler mappings: {http://www.ycl.com/schema/schema=org.frame.base.xml.jdk.bk.SchemaNamespaceHandler}
20121218-10:56:08 main org.frame.base.xml.jdk.bk.SchemaNamespaceHandler Loaded NamespaceHandler mappings: {contact=org.frame.base.xml.jdk.bk.ContactHandler, uic=org.frame.base.xml.jdk.bk.UicHandler, item=org.frame.base.xml.jdk.bk.ItemHandler, fullName=org.frame.base.xml.jdk.bk.FullNameHandler}
[width:10,uic:1,fullName:ycl1,sex:null, width:11,uic:1 06:00 Vesti<br>06:05 Jutarnji 2,fullName:ycl2,sex:null]



这里可以输出自定义的handler,和自己想要的结果。
使用resolver 这种解析模式,你不需要关系xml结构,不需要关系xml到Java对象的关系。
你关心的只是xml元素,我需要解析xml中的元素到我的对象中,而xml元素和Java对象不存在必然的联系,这也是传说中的”解耦“吗.

我这里只是把每个元素都设计了Handler,如果使用这种解析模式,使用属性会让代码引人缩短,而且更易理解(怪不得bean这么多属性).

Spring对于Bean的设计也允许有子元素,那就是Properties,其实也是属性,只是放在子元素上看起来也更美观,逻辑更清晰,也更突显了Bean注入的强烈优势.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值