KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > UnmarshallerImpl


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.xb.binding;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26
27 import org.jboss.xb.binding.parser.JBossXBParser;
28 import org.jboss.xb.binding.parser.sax.SaxJBossXBParser;
29 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
30 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
31 import org.jboss.xb.binding.sunday.unmarshalling.SundayContentHandler;
32 import org.jboss.xb.binding.metadata.unmarshalling.DocumentBinding;
33 import org.xml.sax.EntityResolver JavaDoc;
34 import org.xml.sax.ErrorHandler JavaDoc;
35
36 /**
37  * Unmarshaller implementation.
38  * WARNING: this implementation is not thread-safe.
39  *
40  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
41  * @version <tt>$Revision: 2063 $</tt>
42  */

43 public class UnmarshallerImpl implements Unmarshaller
44 {
45    private ObjectModelBuilder builder = new ObjectModelBuilder();
46    private final JBossXBParser parser;
47
48    // Constructor
49

50    /**
51     * The constructor for DTD and XSD client awareness.
52     */

53    public UnmarshallerImpl() throws JBossXBException
54    {
55       parser = new SaxJBossXBParser();
56    }
57
58    public void setValidation(boolean validation) throws JBossXBException
59    {
60       parser.setFeature(VALIDATION, validation);
61    }
62
63    public void setSchemaValidation(boolean validation) throws JBossXBException
64    {
65       parser.setFeature(SCHEMA_VALIDATION, validation);
66    }
67
68    public void setFeature(String JavaDoc feature, boolean value) throws JBossXBException
69    {
70       parser.setFeature(feature, value);
71    }
72
73    public void setNamespaceAware(boolean namespaces) throws JBossXBException
74    {
75       parser.setFeature(NAMESPACES, namespaces);
76    }
77
78    public void setEntityResolver(EntityResolver JavaDoc entityResolver) throws JBossXBException
79    {
80       parser.setEntityResolver(entityResolver);
81    }
82
83    public void setErrorHandler(ErrorHandler JavaDoc errorHandler)
84    {
85       // todo reader.setErrorHandler(errorHandler);
86
}
87
88    public void mapFactoryToNamespace(ObjectModelFactory factory, String JavaDoc namespaceUri)
89    {
90       if(builder == null)
91       {
92          builder = new ObjectModelBuilder();
93       }
94       builder.mapFactoryToNamespace(factory, namespaceUri);
95    }
96
97    public Object JavaDoc unmarshal(String JavaDoc xmlFile) throws JBossXBException
98    {
99       // todo
100
throw new UnsupportedOperationException JavaDoc();
101    }
102
103    public Object JavaDoc unmarshal(String JavaDoc xmlFile, JBossXBParser.ContentHandler handler) throws JBossXBException
104    {
105       parser.parse(xmlFile, handler);
106       return handler.getRoot();
107    }
108
109    public Object JavaDoc unmarshal(String JavaDoc xml, SchemaBinding schemaBinding) throws JBossXBException
110    {
111       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaBinding);
112       parser.parse(xml, cHandler);
113       return cHandler.getRoot();
114    }
115
116    public Object JavaDoc unmarshal(Reader JavaDoc xmlReader, SchemaBinding schemaBinding) throws JBossXBException
117    {
118       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaBinding);
119       parser.parse(xmlReader, cHandler);
120       return cHandler.getRoot();
121    }
122
123    public Object JavaDoc unmarshal(InputStream JavaDoc xmlStream, SchemaBinding schemaBinding) throws JBossXBException
124    {
125       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaBinding);
126       parser.parse(xmlStream, cHandler);
127       return cHandler.getRoot();
128    }
129
130    public Object JavaDoc unmarshal(String JavaDoc xml, SchemaBindingResolver schemaResolver) throws JBossXBException
131    {
132       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaResolver);
133       parser.parse(xml, cHandler);
134       return cHandler.getRoot();
135    }
136
137    public Object JavaDoc unmarshal(Reader JavaDoc xmlReader, SchemaBindingResolver schemaResolver) throws JBossXBException
138    {
139       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaResolver);
140       parser.parse(xmlReader, cHandler);
141       return cHandler.getRoot();
142    }
143
144    public Object JavaDoc unmarshal(InputStream JavaDoc xmlStream, SchemaBindingResolver schemaResolver) throws JBossXBException
145    {
146       JBossXBParser.ContentHandler cHandler = new SundayContentHandler(schemaResolver);
147       parser.parse(xmlStream, cHandler);
148       return cHandler.getRoot();
149    }
150
151    public Object JavaDoc unmarshal(Reader JavaDoc reader, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
152    {
153       if(builder == null)
154       {
155          builder = new ObjectModelBuilder();
156       }
157       builder.init(factory, root);
158       parser.parse(reader, builder);
159       return builder.getRoot();
160    }
161
162    public Object JavaDoc unmarshal(InputStream JavaDoc is, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
163    {
164       if(builder == null)
165       {
166          builder = new ObjectModelBuilder();
167       }
168       builder.init(factory, root);
169       parser.parse(is, builder);
170       return builder.getRoot();
171    }
172
173    public Object JavaDoc unmarshal(String JavaDoc systemId, ObjectModelFactory factory, Object JavaDoc root) throws JBossXBException
174    {
175       if(builder == null)
176       {
177          builder = new ObjectModelBuilder();
178       }
179       builder.init(factory, root);
180       parser.parse(systemId, builder);
181       return builder.getRoot();
182    }
183
184    public Object JavaDoc unmarshal(String JavaDoc systemId, ObjectModelFactory factory, DocumentBinding binding)
185       throws JBossXBException
186    {
187       if(binding != null)
188       {
189          throw new IllegalStateException JavaDoc("DocumentBinding API is not supported anymore!");
190       }
191       return unmarshal(systemId, factory, (Object JavaDoc)null);
192    }
193
194    public Object JavaDoc unmarshal(Reader JavaDoc reader, ObjectModelFactory factory, DocumentBinding binding) throws JBossXBException
195    {
196       if(binding != null)
197       {
198          throw new IllegalStateException JavaDoc("DocumentBinding API is not supported anymore!");
199       }
200       return unmarshal(reader, factory, (Object JavaDoc)null);
201    }
202
203    JBossXBParser getParser()
204    {
205       return parser;
206    }
207 }
208
Popular Tags