001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package javax.xml.bind.helpers;
018
019 import java.io.OutputStream;
020 import java.io.UnsupportedEncodingException;
021 import java.io.Writer;
022 import java.io.File;
023 import java.io.FileOutputStream;
024 import java.io.BufferedOutputStream;
025 import java.io.IOException;
026
027 import javax.xml.bind.JAXBException;
028 import javax.xml.bind.Marshaller;
029 import javax.xml.bind.PropertyException;
030 import javax.xml.bind.ValidationEventHandler;
031 import javax.xml.bind.annotation.adapters.XmlAdapter;
032 import javax.xml.bind.attachment.AttachmentMarshaller;
033 import javax.xml.stream.XMLEventWriter;
034 import javax.xml.stream.XMLStreamWriter;
035 import javax.xml.transform.dom.DOMResult;
036 import javax.xml.transform.sax.SAXResult;
037 import javax.xml.transform.stream.StreamResult;
038 import javax.xml.transform.Result;
039 import javax.xml.validation.Schema;
040
041 import org.w3c.dom.Node;
042
043 import org.xml.sax.ContentHandler;
044
045 public abstract class AbstractMarshallerImpl implements Marshaller {
046
047 static String aliases[] = {
048 "UTF-8", "UTF8",
049 "UTF-16", "Unicode",
050 "UTF-16BE", "UnicodeBigUnmarked",
051 "UTF-16LE", "UnicodeLittleUnmarked",
052 "US-ASCII", "ASCII",
053 "TIS-620", "TIS620",
054 "ISO-10646-UCS-2", "Unicode",
055 "EBCDIC-CP-US", "cp037",
056 "EBCDIC-CP-CA", "cp037",
057 "EBCDIC-CP-NL", "cp037",
058 "EBCDIC-CP-WT", "cp037",
059 "EBCDIC-CP-DK", "cp277",
060 "EBCDIC-CP-NO", "cp277",
061 "EBCDIC-CP-FI", "cp278",
062 "EBCDIC-CP-SE", "cp278",
063 "EBCDIC-CP-IT", "cp280",
064 "EBCDIC-CP-ES", "cp284",
065 "EBCDIC-CP-GB", "cp285",
066 "EBCDIC-CP-FR", "cp297",
067 "EBCDIC-CP-AR1", "cp420",
068 "EBCDIC-CP-HE", "cp424",
069 "EBCDIC-CP-BE", "cp500",
070 "EBCDIC-CP-CH", "cp500",
071 "EBCDIC-CP-ROECE", "cp870",
072 "EBCDIC-CP-YU", "cp870",
073 "EBCDIC-CP-IS", "cp871",
074 "EBCDIC-CP-AR2", "cp918"
075 };
076
077 private ValidationEventHandler eventHandler = new DefaultValidationEventHandler();
078 private String encoding = "UTF-8";
079 private String schemaLocation;
080 private String noNSSchemaLocation;
081 private boolean formattedOutput;
082 private boolean fragment;
083
084 public void marshal(Object obj, File file) throws JAXBException {
085 checkNotNull(obj, "obj", file, "file");
086 try {
087 OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
088 try {
089 marshal(obj, new StreamResult(os));
090 } finally {
091 os.close();
092 }
093 } catch (IOException e) {
094 throw new JAXBException(e);
095 }
096 }
097
098 public final void marshal(Object obj, OutputStream os) throws JAXBException {
099 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 }