1 16 package org.jahia.utils.xml.betwixt; 17 18 import java.io.InputStream ; 19 20 import org.apache.commons.betwixt.XMLIntrospector; 21 import org.apache.commons.betwixt.io.BeanReader; 22 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; 23 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer; 24 25 29 public class XmlReader 30 { 31 32 private BeanReader beanReader; 33 34 35 private Class clazz; 36 37 40 public XmlReader(Class clazz) 41 { 42 this.clazz = clazz; 43 } 44 45 51 public Object parse(InputStream is) 52 throws Exception 53 { 54 if (beanReader == null) 55 { 56 beanReader = createBeanReader(clazz); 57 } 58 59 return beanReader.parse(is); 60 } 61 62 68 private BeanReader createBeanReader(Class clazz) 69 throws Exception 70 { 71 BeanReader reader = new BeanReader(); 72 reader.setXMLIntrospector(createXMLIntrospector()); 73 reader.registerBeanClass(clazz); 74 return reader; 75 } 76 77 82 private XMLIntrospector createXMLIntrospector() 83 { 84 XMLIntrospector introspector = new XMLIntrospector(); 85 86 introspector.setAttributesForPrimitives(false); 87 introspector.setNameMapper(new DecapitalizeNameMapper()); 88 introspector.setPluralStemmer(new DefaultPluralStemmer()); 89 90 return introspector; 91 } 92 } 93 | Popular Tags |