View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package javax.xml.bind.util;
18  
19  import javax.xml.transform.Source;
20  import javax.xml.transform.sax.SAXSource;
21  import javax.xml.bind.Marshaller;
22  import javax.xml.bind.JAXBContext;
23  import javax.xml.bind.JAXBException;
24  
25  import org.xml.sax.XMLReader;
26  import org.xml.sax.SAXNotRecognizedException;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.DTDHandler;
29  import org.xml.sax.ContentHandler;
30  import org.xml.sax.ErrorHandler;
31  import org.xml.sax.SAXException;
32  import org.xml.sax.InputSource;
33  import org.xml.sax.SAXParseException;
34  import org.xml.sax.helpers.XMLFilterImpl;
35  import org.xml.sax.ext.LexicalHandler;
36  
37  public class JAXBSource extends SAXSource {
38  
39      public JAXBSource(JAXBContext context, Object contentObject) throws JAXBException {
40          if (context == null) {
41              throw new JAXBException("context must not be null");
42          }
43          if (contentObject == null) {
44              throw new JAXBException("contentObject must not be null");
45          }
46          setXMLReader(new Reader(context.createMarshaller(), contentObject));
47          setInputSource(new InputSource());
48      }
49  
50      public JAXBSource(final Marshaller marshaller, final Object contentObject) throws JAXBException {
51          if (marshaller == null) {
52              throw new JAXBException("marshaller must not be null");
53          }
54          if (contentObject == null) {
55              throw new JAXBException("contentObject must not be null");
56          }
57          setXMLReader(new Reader(marshaller, contentObject));
58          setInputSource(new InputSource());
59      }
60  
61      private static class Reader implements XMLReader {
62  
63          private LexicalHandler lexicalHandler;
64          private EntityResolver entityResolver;
65          private DTDHandler dtdHandler;
66          private XMLFilterImpl repeater;
67          private ErrorHandler errorHandler;
68          private final Marshaller marshaller;
69          private final Object contentObject;
70  
71          public Reader(Marshaller marshaller, Object contentObject) {
72              this.marshaller = marshaller;
73              this.contentObject = contentObject;
74              repeater = new XMLFilterImpl();
75          }
76  
77          public boolean getFeature(String name) throws SAXNotRecognizedException {
78              if (name.equals("http://xml.org/sax/features/namespaces")) {
79                  return true;
80              }
81              if (name.equals("http://xml.org/sax/features/namespace-prefixes")) {
82                  return false;
83              }
84              throw new SAXNotRecognizedException(name);
85          }
86  
87          public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
88              if (name.equals("http://xml.org/sax/features/namespaces") && value) {
89                  return;
90              }
91              if (name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) {
92                  return;
93              }
94              throw new SAXNotRecognizedException(name);
95          }
96  
97          public Object getProperty(String name) throws SAXNotRecognizedException {
98              if("http://xml.org/sax/properties/lexical-handler".equals(name)) {
99                  return lexicalHandler;
100             }
101             throw new SAXNotRecognizedException(name);
102         }
103 
104         public void setProperty(String name, Object value) throws SAXNotRecognizedException {
105             if("http://xml.org/sax/properties/lexical-handler".equals(name)) {
106                 lexicalHandler = (LexicalHandler) value;
107                 return;
108             }
109             throw new SAXNotRecognizedException(name);
110         }
111 
112         public void setEntityResolver(EntityResolver resolver) {
113             entityResolver = resolver;
114         }
115 
116         public EntityResolver getEntityResolver() {
117             return entityResolver;
118         }
119 
120         public void setDTDHandler(DTDHandler handler) {
121             dtdHandler = handler;
122         }
123 
124         public DTDHandler getDTDHandler() {
125             return dtdHandler;
126         }
127 
128         public void setContentHandler(ContentHandler handler) {
129             repeater.setContentHandler(handler);
130         }
131 
132         public ContentHandler getContentHandler() {
133             return repeater.getContentHandler();
134         }
135 
136         public void setErrorHandler(ErrorHandler handler) {
137             errorHandler = handler;
138         }
139 
140         public ErrorHandler getErrorHandler() {
141             return errorHandler;
142         }
143 
144         public void parse(InputSource input) throws SAXException {
145             parse();
146         }
147 
148         public void parse(String systemId) throws SAXException {
149             parse();
150         }
151 
152         public void parse() throws SAXException {
153             try {
154                 marshaller.marshal(contentObject, repeater);
155             } catch(JAXBException e) {
156                 SAXParseException se = new SAXParseException(e.getMessage(), null, null, -1, -1, e);
157                 if (errorHandler != null) {
158                     errorHandler.fatalError(se);
159                 }
160                 throw se;
161             }
162         }
163 
164     }
165 }