001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.xml.bind.helpers;
031:
032: import org.w3c.dom.Node;
033: import org.xml.sax.ContentHandler;
034:
035: import javax.xml.bind.JAXBException;
036: import javax.xml.bind.Marshaller;
037: import javax.xml.bind.PropertyException;
038: import javax.xml.bind.ValidationEvent;
039: import javax.xml.bind.ValidationEventHandler;
040: import javax.xml.bind.annotation.adapters.XmlAdapter;
041: import javax.xml.bind.attachment.AttachmentMarshaller;
042: import javax.xml.stream.XMLEventWriter;
043: import javax.xml.stream.XMLOutputFactory;
044: import javax.xml.stream.XMLStreamException;
045: import javax.xml.stream.XMLStreamWriter;
046: import javax.xml.transform.dom.DOMResult;
047: import javax.xml.transform.sax.SAXResult;
048: import javax.xml.transform.stream.StreamResult;
049: import javax.xml.validation.Schema;
050: import java.io.OutputStream;
051: import java.io.UnsupportedEncodingException;
052: import java.io.Writer;
053: import java.util.HashMap;
054:
055: public abstract class AbstractMarshallerImpl implements Marshaller {
056: private XMLOutputFactory _factory;
057: private AttachmentMarshaller _attachmentMarshaller;
058: private String _encoding = "UTF-8";
059: private ValidationEventHandler _validationEventHandler = _defaultValidationEventHandler;
060: private Listener _listener;
061: private boolean _formattedOutput = false;
062: private boolean _fragment = false;
063: private String _noNSSchemaLocation;
064: private Schema _schema;
065: private String _schemaLocation;
066: private HashMap<String, Object> _properties = new HashMap<String, Object>();
067: private HashMap<Class, XmlAdapter> _adapters = new HashMap<Class, XmlAdapter>();
068:
069: private static final ValidationEventHandler _defaultValidationEventHandler = new DefaultValidationEventHandler() {
070: public boolean handleEvent(ValidationEvent event) {
071: if (event == null)
072: throw new IllegalArgumentException();
073:
074: return event.getSeverity() != ValidationEvent.FATAL_ERROR;
075: }
076: };
077:
078: private final HashMap<String, String> _ianaToJavaEncoding = new HashMap<String, String>();
079:
080: public AbstractMarshallerImpl() {
081: _ianaToJavaEncoding.put("UTF-8", "UTF8");
082: // XXX add more encodings
083: }
084:
085: public <A extends XmlAdapter> A getAdapter(Class<A> type) {
086: return (A) _adapters.get(type);
087: }
088:
089: public AttachmentMarshaller getAttachmentMarshaller() {
090: return _attachmentMarshaller;
091: }
092:
093: protected String getEncoding() {
094: return _encoding;
095: }
096:
097: public ValidationEventHandler getEventHandler()
098: throws JAXBException {
099: return _validationEventHandler;
100: }
101:
102: protected String getJavaEncoding(String encoding)
103: throws UnsupportedEncodingException {
104: if (_ianaToJavaEncoding.containsKey(encoding))
105: return _ianaToJavaEncoding.get(encoding);
106:
107: throw new UnsupportedEncodingException(encoding);
108: }
109:
110: public Listener getListener() {
111: return _listener;
112: }
113:
114: public Node getNode(Object obj) throws JAXBException {
115: throw new UnsupportedOperationException();
116: }
117:
118: protected String getNoNSSchemaLocation() {
119: return _noNSSchemaLocation;
120: }
121:
122: public Object getProperty(String name) throws PropertyException {
123: if (name.equals("jaxb.encoding")) {
124: return getEncoding();
125: } else if (name.equals("jaxb.formatted.output")) {
126: return _formattedOutput;
127: } else if (name.equals("jaxb.schemaLocation")) {
128: return getSchemaLocation();
129: } else if (name.equals("jaxb.noNamespaceSchemaLocation")) {
130: return getNoNSSchemaLocation();
131: } else if (name.equals("jaxb.fragment")) {
132: return _fragment;
133: } else if (_properties.containsKey(name)) {
134: return _properties.get(name);
135: }
136:
137: throw new PropertyException(name);
138: }
139:
140: public Schema getSchema() {
141: return _schema;
142: }
143:
144: protected String getSchemaLocation() {
145: return _schemaLocation;
146: }
147:
148: protected boolean isFormattedOutput() {
149: return _formattedOutput;
150: }
151:
152: protected boolean isFragment() {
153: return _fragment;
154: }
155:
156: public final void marshal(Object obj, ContentHandler handler)
157: throws JAXBException {
158: SAXResult result = new SAXResult(handler);
159:
160: marshal(obj, result);
161: }
162:
163: public final void marshal(Object obj, Node node)
164: throws JAXBException {
165: DOMResult result = new DOMResult(node);
166:
167: marshal(obj, result);
168: }
169:
170: private XMLOutputFactory getXMLOutputFactory() {
171: if (_factory == null) {
172: _factory = XMLOutputFactory.newInstance();
173: _factory.setProperty(
174: XMLOutputFactory.IS_REPAIRING_NAMESPACES,
175: Boolean.TRUE);
176: }
177:
178: return _factory;
179: }
180:
181: public final void marshal(Object obj, OutputStream os)
182: throws JAXBException {
183: marshal(obj, new StreamResult(os));
184: }
185:
186: public final void marshal(Object obj, Writer w)
187: throws JAXBException {
188: marshal(obj, new StreamResult(w));
189: }
190:
191: public <A extends XmlAdapter> void setAdapter(Class<A> type,
192: A adapter) {
193: _adapters.put(type, adapter);
194: }
195:
196: public void setAdapter(XmlAdapter adapter) {
197: _adapters.put((Class) adapter.getClass(), adapter);
198: }
199:
200: public void setAttachmentMarshaller(AttachmentMarshaller am) {
201: _attachmentMarshaller = am;
202: }
203:
204: protected void setEncoding(String encoding) {
205: _encoding = encoding;
206: }
207:
208: public void setEventHandler(ValidationEventHandler handler)
209: throws JAXBException {
210: _validationEventHandler = handler;
211: }
212:
213: protected void setFormattedOutput(boolean v) {
214: _formattedOutput = v;
215: }
216:
217: protected void setFragment(boolean v) {
218: _fragment = v;
219: }
220:
221: public void setListener(Listener listener) {
222: _listener = listener;
223: }
224:
225: protected void setNoNSSchemaLocation(String location) {
226: _noNSSchemaLocation = location;
227: }
228:
229: public void setProperty(String name, Object value)
230: throws PropertyException {
231: if (name.equals("jaxb.encoding")) {
232: setEncoding((String) value);
233: } else if (name.equals("jaxb.formatted.output")) {
234: setFormattedOutput(((Boolean) value).booleanValue());
235: } else if (name.equals("jaxb.schemaLocation")) {
236: setSchemaLocation((String) value);
237: } else if (name.equals("jaxb.noNamespaceSchemaLocation")) {
238: setNoNSSchemaLocation((String) value);
239: } else if (name.equals("jaxb.fragment")) {
240: setFragment(((Boolean) value).booleanValue());
241: } else {
242: _properties.put(name, value);
243: }
244: }
245:
246: public void setSchema(Schema schema) {
247: _schema = schema;
248: }
249:
250: protected void setSchemaLocation(String location) {
251: _schemaLocation = location;
252: }
253:
254: public void marshal(Object obj, XMLEventWriter writer)
255: throws JAXBException {
256: throw new UnsupportedOperationException(
257: "subclasses must override this");
258: }
259:
260: public void marshal(Object obj, XMLStreamWriter writer)
261: throws JAXBException {
262: throw new UnsupportedOperationException(
263: "subclasses must override this");
264: }
265: }
|