KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > Unmarshaller


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
15 import org.xml.sax.ErrorHandler JavaDoc;
16 import org.xml.sax.InputSource JavaDoc;
17
18 import java.io.Reader JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * Unmarshaller implementation.
23  * WARNING: this implementation is not thread-safe.
24  *
25  * @version <tt>$Revision: 1.13.2.6 $</tt>
26  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
27  */

28 public class Unmarshaller
29 {
30    private static final Logger log = Logger.getLogger(Unmarshaller.class);
31
32    public static final String JavaDoc VALIDATION = "http://xml.org/sax/features/validation";
33    public static final String JavaDoc NAMESPACES = "http://xml.org/sax/features/namespaces";
34    public static final String JavaDoc NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
35
36    // set some xerces specific features that allow transparent DTD and Schema validation
37
public static final String JavaDoc DYNAMIC_VALIDATION = "http://apache.org/xml/features/validation/dynamic";
38    public static final String JavaDoc SCHEMA_VALIDATION = "http://apache.org/xml/features/validation/schema";
39    public static final String JavaDoc 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    // Constructor
45

46    /**
47     * The constructor for DTD and XSD client awareness. Invokes this(false).
48     */

49    public Unmarshaller()
50       throws JBossXBException
51    {
52       this(false);
53    }
54    /**
55     * Create a parser with a choice between the XniJBossXBParser and
56     * SaxJBossXBParser.
57     *
58     * @param useXniParser - true to use the XniJBossXBParser, false to use
59     * the SaxJBossXBParser
60     * @throws JBossXBException
61     */

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 JavaDoc errorHandler)
97    {
98       // todo reader.setErrorHandler(errorHandler);
99
}
100
101    public void mapFactoryToNamespace(ObjectModelFactory factory, String JavaDoc namespaceUri)
102    {
103       builder.mapFactoryToNamespace(getGenericObjectModelFactory(factory), namespaceUri);
104    }
105
106    public Object JavaDoc unmarshal(Reader JavaDoc reader, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
107    {
108       builder.init(getGenericObjectModelFactory(factory), root);
109       parser.parse(reader, builder);
110       return builder.getRoot();
111    }
112
113    public Object JavaDoc unmarshal(InputStream JavaDoc is, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
114    {
115       builder.init(getGenericObjectModelFactory(factory), root);
116       parser.parse(is, builder);
117       return builder.getRoot();
118    }
119
120    public Object JavaDoc unmarshal(String JavaDoc systemId, ObjectModelFactory factory, Object JavaDoc root)
121       throws JBossXBException
122    {
123       builder.init(getGenericObjectModelFactory(factory), root);
124       parser.parse(systemId, builder);
125       return builder.getRoot();
126    }
127    public Object JavaDoc unmarshal(InputSource JavaDoc is, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
128    {
129       Object JavaDoc 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