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.helpers;
18  
19  import java.io.OutputStream;
20  import java.io.UnsupportedEncodingException;
21  import java.io.Writer;
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.BufferedOutputStream;
25  import java.io.IOException;
26  
27  import javax.xml.bind.JAXBException;
28  import javax.xml.bind.Marshaller;
29  import javax.xml.bind.PropertyException;
30  import javax.xml.bind.ValidationEventHandler;
31  import javax.xml.bind.annotation.adapters.XmlAdapter;
32  import javax.xml.bind.attachment.AttachmentMarshaller;
33  import javax.xml.stream.XMLEventWriter;
34  import javax.xml.stream.XMLStreamWriter;
35  import javax.xml.transform.dom.DOMResult;
36  import javax.xml.transform.sax.SAXResult;
37  import javax.xml.transform.stream.StreamResult;
38  import javax.xml.transform.Result;
39  import javax.xml.validation.Schema;
40  
41  import org.w3c.dom.Node;
42  
43  import org.xml.sax.ContentHandler;
44  
45  public abstract class AbstractMarshallerImpl implements Marshaller {
46  
47      static String aliases[] = {
48              "UTF-8", "UTF8",
49              "UTF-16", "Unicode",
50              "UTF-16BE", "UnicodeBigUnmarked",
51              "UTF-16LE", "UnicodeLittleUnmarked",
52              "US-ASCII", "ASCII",
53              "TIS-620", "TIS620",
54              "ISO-10646-UCS-2", "Unicode",
55              "EBCDIC-CP-US", "cp037",
56              "EBCDIC-CP-CA", "cp037",
57              "EBCDIC-CP-NL", "cp037",
58              "EBCDIC-CP-WT", "cp037",
59              "EBCDIC-CP-DK", "cp277",
60              "EBCDIC-CP-NO", "cp277",
61              "EBCDIC-CP-FI", "cp278",
62              "EBCDIC-CP-SE", "cp278",
63              "EBCDIC-CP-IT", "cp280",
64              "EBCDIC-CP-ES", "cp284",
65              "EBCDIC-CP-GB", "cp285",
66              "EBCDIC-CP-FR", "cp297",
67              "EBCDIC-CP-AR1", "cp420",
68              "EBCDIC-CP-HE", "cp424",
69              "EBCDIC-CP-BE", "cp500",
70              "EBCDIC-CP-CH", "cp500",
71              "EBCDIC-CP-ROECE", "cp870",
72              "EBCDIC-CP-YU", "cp870",
73              "EBCDIC-CP-IS", "cp871",
74              "EBCDIC-CP-AR2", "cp918"
75      };
76  
77      private ValidationEventHandler eventHandler = new DefaultValidationEventHandler();
78      private String encoding = "UTF-8";
79      private String schemaLocation;
80      private String noNSSchemaLocation;
81      private boolean formattedOutput;
82      private boolean fragment;
83  
84      public void marshal(Object obj, File file) throws JAXBException {
85          checkNotNull(obj, "obj", file, "file");
86          try {
87              OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
88              try {
89                  marshal(obj, new StreamResult(os));
90              } finally {
91                  os.close();
92              }
93          } catch (IOException e) {
94              throw new JAXBException(e);
95          }
96      }
97  
98      public final void marshal(Object obj, OutputStream os) throws JAXBException {
99          checkNotNull(obj, "obj", os, "os");
100         marshal(obj, new StreamResult(os));
101     }
102 
103     public final void marshal(Object obj, Writer w) throws JAXBException {
104         checkNotNull(obj, "obj", w, "writer");
105         marshal(obj, new StreamResult(w));
106     }
107 
108     public final void marshal(Object obj, ContentHandler handler) throws JAXBException {
109         checkNotNull(obj, "obj", handler, "handler");
110         marshal(obj, new SAXResult(handler));
111     }
112 
113     public final void marshal(Object obj, Node node) throws JAXBException {
114         checkNotNull(obj, "obj", node, "node");
115         marshal(obj, new DOMResult(node));
116     }
117 
118     public Node getNode(Object obj) throws JAXBException {
119         checkNotNull(obj, "obj", "foo", "bar");
120         throw new UnsupportedOperationException();
121     }
122 
123     protected String getEncoding() {
124         return encoding;
125     }
126 
127     protected void setEncoding(String encoding) {
128         this.encoding = encoding;
129     }
130 
131     protected String getSchemaLocation() {
132         return schemaLocation;
133     }
134 
135     protected void setSchemaLocation(String location) {
136         schemaLocation = location;
137     }
138 
139     protected String getNoNSSchemaLocation() {
140         return noNSSchemaLocation;
141     }
142 
143     protected void setNoNSSchemaLocation(String location) {
144         noNSSchemaLocation = location;
145     }
146 
147     protected boolean isFormattedOutput() {
148         return formattedOutput;
149     }
150 
151     protected void setFormattedOutput(boolean v) {
152         formattedOutput = v;
153     }
154 
155     protected boolean isFragment() {
156         return fragment;
157     }
158 
159     protected void setFragment(boolean v) {
160         fragment = v;
161     }
162 
163     protected String getJavaEncoding(String encoding) throws UnsupportedEncodingException {
164         try {
165             "dummy".getBytes(encoding);
166             return encoding;
167         }
168         catch (UnsupportedEncodingException e) {
169         }
170         for (int i = 0; i < aliases.length; i += 2) {
171             if (encoding.equals(aliases[i])) {
172                 "dummy".getBytes(aliases[i + 1]);
173                 return aliases[i + 1];
174             }
175         }
176         throw new UnsupportedEncodingException(encoding);
177     }
178 
179     public void setProperty(String name, Object value) throws PropertyException {
180         if (name == null) {
181             throw new IllegalArgumentException("name must not be null");
182         }
183         if (JAXB_ENCODING.equals(name)) {
184             checkString(name, value);
185             setEncoding((String) value);
186         } else if (JAXB_FORMATTED_OUTPUT.equals(name)) {
187             checkBoolean(name, value);
188             setFormattedOutput(((Boolean) value).booleanValue());
189         } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name)) {
190             checkString(name, value);
191             setNoNSSchemaLocation((String) value);
192         } else if (JAXB_SCHEMA_LOCATION.equals(name)) {
193             checkString(name, value);
194             setSchemaLocation((String) value);
195         } else if (JAXB_FRAGMENT.equals(name)) {
196             checkBoolean(name, value);
197             setFragment(((Boolean) value).booleanValue());
198         } else {
199             throw new PropertyException(name, value);
200         }
201     }
202 
203     public Object getProperty(String name) throws PropertyException {
204         if (name == null) {
205             throw new IllegalArgumentException("name must not be null");
206         }
207         if (JAXB_ENCODING.equals(name)) {
208             return getEncoding();
209         } else if (JAXB_FORMATTED_OUTPUT.equals(name)) {
210             return isFormattedOutput() ? Boolean.TRUE : Boolean.FALSE;
211         } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name)) {
212             return getNoNSSchemaLocation();
213         } else if (JAXB_SCHEMA_LOCATION.equals(name)) {
214             return getSchemaLocation();
215         } else if (JAXB_FRAGMENT.equals(name)) {
216             return isFragment() ? Boolean.TRUE : Boolean.FALSE;
217         } else {
218             throw new PropertyException(name);
219         }
220     }
221 
222     public ValidationEventHandler getEventHandler() throws JAXBException {
223         return eventHandler;
224     }
225 
226     public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
227         if (handler == null) {
228             eventHandler = new DefaultValidationEventHandler();
229         } else {
230             eventHandler = handler;
231         }
232     }
233 
234     private void checkBoolean(String name, Object value) throws PropertyException {
235         if (!(value instanceof Boolean)) {
236             throw new PropertyException(name + " must be a boolean");
237         }
238     }
239 
240     private void checkString(String name, Object value) throws PropertyException {
241         if (!(value instanceof String)) {
242             throw new PropertyException(name + " must be a string");
243         }
244     }
245 
246     private void checkNotNull(Object o1, String o1Name, Object o2, String o2Name) {
247         if (o1 == null) {
248             throw new IllegalArgumentException(o1Name + " must not be null");
249         }
250         if (o2 == null) {
251             throw new IllegalArgumentException(o2Name + " must not be null");
252         }
253     }
254 
255     public void marshal(Object obj, XMLEventWriter writer)
256             throws JAXBException {
257         throw new UnsupportedOperationException();
258     }
259 
260     public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
261         throw new UnsupportedOperationException();
262     }
263 
264     public void setSchema(Schema schema) {
265         throw new UnsupportedOperationException();
266     }
267 
268     public Schema getSchema() {
269         throw new UnsupportedOperationException();
270     }
271 
272     public void setAdapter(XmlAdapter adapter) {
273         if (adapter == null) {
274             throw new IllegalArgumentException();
275         }
276         setAdapter((Class<XmlAdapter>) adapter.getClass(), adapter);
277     }
278 
279     public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
280         throw new UnsupportedOperationException();
281     }
282 
283     public <A extends XmlAdapter> A getAdapter(Class<A> type) {
284         throw new UnsupportedOperationException();
285     }
286 
287     public void setAttachmentMarshaller(AttachmentMarshaller am) {
288         throw new UnsupportedOperationException();
289     }
290 
291     public AttachmentMarshaller getAttachmentMarshaller() {
292         throw new UnsupportedOperationException();
293     }
294 
295     public void setListener(javax.xml.bind.Marshaller.Listener listener) {
296         throw new UnsupportedOperationException();
297     }
298 
299     public javax.xml.bind.Marshaller.Listener getListener() {
300         throw new UnsupportedOperationException();
301     }
302 }