1 7 package org.jboss.xml.binding; 8 9 import org.jboss.logging.Logger; 10 import org.jboss.xml.binding.parser.JBossXBParser; 11 import org.jboss.xml.binding.parser.xni.XniJBossXBParser; 12 import org.jboss.xml.binding.parser.sax.SaxJBossXBParser; 13 import org.jboss.util.xml.JBossEntityResolver; 14 import org.xml.sax.EntityResolver ; 15 import org.xml.sax.ErrorHandler ; 16 import org.xml.sax.InputSource ; 17 18 import java.io.Reader ; 19 import java.io.InputStream ; 20 21 28 public class Unmarshaller 29 { 30 private static final Logger log = Logger.getLogger(Unmarshaller.class); 31 32 public static final String VALIDATION = "http://xml.org/sax/features/validation"; 33 public static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; 34 public static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; 35 36 public static final String DYNAMIC_VALIDATION = "http://apache.org/xml/features/validation/dynamic"; 38 public static final String SCHEMA_VALIDATION = "http://apache.org/xml/features/validation/schema"; 39 public static final String SCHEMA_FULL_CHECKING = "http://apache.org/xml/features/validation/schema-full-checking"; 40 41 private ObjectModelBuilder builder = new ObjectModelBuilder(); 42 private final JBossXBParser parser; 43 44 46 49 public Unmarshaller() 50 throws JBossXBException 51 { 52 this(false); 53 } 54 62 public Unmarshaller(boolean useXniParser) 63 throws JBossXBException 64 { 65 if( useXniParser == true ) 66 parser = new XniJBossXBParser(); 67 else 68 parser = new SaxJBossXBParser(); 69 70 parser.setFeature(VALIDATION, true); 71 parser.setFeature(SCHEMA_VALIDATION, true); 72 parser.setFeature(SCHEMA_FULL_CHECKING, true); 73 parser.setFeature(DYNAMIC_VALIDATION, true); 74 parser.setFeature(NAMESPACES, true); 75 76 parser.setEntityResolver(new JBossEntityResolver()); 77 } 78 79 public void setValidation(boolean validation) 80 throws JBossXBException 81 { 82 parser.setFeature(VALIDATION, validation); 83 } 84 85 public void setNamespaceAware(boolean namespaces) 86 throws JBossXBException 87 { 88 parser.setFeature(NAMESPACES, namespaces); 89 } 90 91 public void setEntityResolver(EntityResolver entityResolver) throws JBossXBException 92 { 93 parser.setEntityResolver(entityResolver); 94 } 95 96 public void setErrorHandler(ErrorHandler errorHandler) 97 { 98 } 100 101 public void mapFactoryToNamespace(ObjectModelFactory factory, String namespaceUri) 102 { 103 builder.mapFactoryToNamespace(getGenericObjectModelFactory(factory), namespaceUri); 104 } 105 106 public Object unmarshal(Reader reader, ObjectModelFactory factory, Object root) throws JBossXBException 107 { 108 builder.init(getGenericObjectModelFactory(factory), root); 109 parser.parse(reader, builder); 110 return builder.getRoot(); 111 } 112 113 public Object unmarshal(InputStream is, ObjectModelFactory factory, Object root) throws JBossXBException 114 { 115 builder.init(getGenericObjectModelFactory(factory), root); 116 parser.parse(is, builder); 117 return builder.getRoot(); 118 } 119 120 public Object unmarshal(String systemId, ObjectModelFactory factory, Object root) 121 throws JBossXBException 122 { 123 builder.init(getGenericObjectModelFactory(factory), root); 124 parser.parse(systemId, builder); 125 return builder.getRoot(); 126 } 127 public Object unmarshal(InputSource is, ObjectModelFactory factory, Object root) throws JBossXBException 128 { 129 Object result; 130 if(is.getCharacterStream() != null) 131 { 132 result = unmarshal(is.getCharacterStream(), factory, root); 133 } 134 else if(is.getByteStream() != null) 135 { 136 result = unmarshal(is.getByteStream(), factory, root); 137 } 138 else 139 { 140 result = unmarshal(is.getSystemId(), factory, root); 141 } 142 return result; 143 } 144 145 private static final GenericObjectModelFactory getGenericObjectModelFactory(ObjectModelFactory factory) 146 { 147 if(!(factory instanceof GenericObjectModelFactory)) 148 { 149 factory = new DelegatingObjectModelFactory(factory); 150 } 151 return factory instanceof GenericObjectModelFactory ? (GenericObjectModelFactory)factory : new DelegatingObjectModelFactory(factory); 152 } 153 } 154 | Popular Tags |