001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-fcs
003: // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
004: // Any modifications to this file will be lost upon recompilation of the source schema.
005: // Generated on: 2005.03.04 at 10:20:40 PST
006: //
007:
008: package com.nabhinc.portal.config.impl.runtime;
009:
010: import java.io.BufferedWriter;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013: import java.io.OutputStream;
014: import java.io.OutputStreamWriter;
015: import java.io.UnsupportedEncodingException;
016: import java.io.Writer;
017:
018: import javax.xml.bind.DatatypeConverter;
019: import javax.xml.bind.JAXBException;
020: import javax.xml.bind.MarshalException;
021: import javax.xml.bind.PropertyException;
022: import javax.xml.bind.helpers.AbstractMarshallerImpl;
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.ParserConfigurationException;
026: import javax.xml.transform.Result;
027: import javax.xml.transform.dom.DOMResult;
028: import javax.xml.transform.sax.SAXResult;
029: import javax.xml.transform.stream.StreamResult;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Node;
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.helpers.LocatorImpl;
036:
037: import com.sun.xml.bind.DatatypeConverterImpl;
038: import com.sun.xml.bind.JAXBAssertionError;
039: import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
040: import com.sun.xml.bind.marshaller.DataWriter;
041: import com.sun.xml.bind.marshaller.DumbEscapeHandler;
042: import com.sun.xml.bind.marshaller.Messages;
043: import com.sun.xml.bind.marshaller.MinimumEscapeHandler;
044: import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
045: import com.sun.xml.bind.marshaller.NioEscapeHandler;
046: import com.sun.xml.bind.marshaller.SAX2DOMEx;
047: import com.sun.xml.bind.marshaller.SchemaLocationFilter;
048: import com.sun.xml.bind.marshaller.XMLWriter;
049:
050: /**
051: * Implementation of {@link Marshaller} interface for JAXB RI.
052: *
053: * @author Kohsuke Kawaguchi
054: * @author Vivek Pandey
055: */
056: public class MarshallerImpl extends AbstractMarshallerImpl {
057: /** Indentation string. Default is four whitespaces. */
058: private String indent = " ";
059:
060: /** Used to assign prefixes to namespace URIs. */
061: private NamespacePrefixMapper prefixMapper = null;
062:
063: /** Object that handles character escaping. */
064: private CharacterEscapeHandler escapeHandler = null;
065:
066: /** Whether the xml declaration will be printed or not. */
067: private boolean printXmlDeclaration = true;
068:
069: /** XML BLOB written after the XML declaration. */
070: private String header = null;
071:
072: /** reference to the context that created this object */
073: final DefaultJAXBContextImpl context;
074:
075: public MarshallerImpl(DefaultJAXBContextImpl c) {
076: // initialize datatype converter with ours
077: DatatypeConverter
078: .setDatatypeConverter(DatatypeConverterImpl.theInstance);
079:
080: context = c;
081: }
082:
083: public void marshal(Object obj, Result result) throws JAXBException {
084: //XMLSerializable so = Util.toXMLSerializable(obj);
085: XMLSerializable so = context.getGrammarInfo()
086: .castToXMLSerializable(obj);
087:
088: if (so == null)
089: throw new MarshalException(Messages
090: .format(Messages.NOT_MARSHALLABLE));
091:
092: if (result instanceof SAXResult) {
093: write(so, ((SAXResult) result).getHandler());
094: return;
095: }
096: if (result instanceof DOMResult) {
097: Node node = ((DOMResult) result).getNode();
098:
099: if (node == null) {
100: try {
101: DocumentBuilderFactory dbf = DocumentBuilderFactory
102: .newInstance();
103: dbf.setNamespaceAware(true);
104: DocumentBuilder db = dbf.newDocumentBuilder();
105: Document doc = db.newDocument();
106: ((DOMResult) result).setNode(doc);
107: write(so, new SAX2DOMEx(doc));
108: } catch (ParserConfigurationException pce) {
109: throw new JAXBAssertionError(pce);
110: }
111: } else {
112: write(so, new SAX2DOMEx(node));
113: }
114:
115: return;
116: }
117: if (result instanceof StreamResult) {
118: StreamResult sr = (StreamResult) result;
119: XMLWriter w = null;
120:
121: if (sr.getWriter() != null)
122: w = createWriter(sr.getWriter());
123: else if (sr.getOutputStream() != null)
124: w = createWriter(sr.getOutputStream());
125: else if (sr.getSystemId() != null) {
126: String fileURL = sr.getSystemId();
127:
128: if (fileURL.startsWith("file:///")) {
129: if (fileURL.substring(8).indexOf(":") > 0)
130: fileURL = fileURL.substring(8);
131: else
132: fileURL = fileURL.substring(7);
133: } // otherwise assume that it's a file name
134:
135: try {
136: w = createWriter(new FileOutputStream(fileURL));
137: } catch (IOException e) {
138: throw new MarshalException(e);
139: }
140: }
141:
142: if (w == null)
143: throw new IllegalArgumentException();
144:
145: write(so, w);
146: return;
147: }
148:
149: // unsupported parameter type
150: throw new MarshalException(Messages
151: .format(Messages.UNSUPPORTED_RESULT));
152: }
153:
154: private void write(XMLSerializable obj, ContentHandler writer)
155: throws JAXBException {
156:
157: try {
158: if (getSchemaLocation() != null
159: || getNoNSSchemaLocation() != null) {
160: // if we need to add xsi:schemaLocation or its brother,
161: // throw in the component to do that.
162: writer = new SchemaLocationFilter(getSchemaLocation(),
163: getNoNSSchemaLocation(), writer);
164: }
165:
166: SAXMarshaller serializer = new SAXMarshaller(writer,
167: prefixMapper, this );
168:
169: // set a DocumentLocator that doesn't provide any information
170: writer.setDocumentLocator(new LocatorImpl());
171: writer.startDocument();
172: serializer.childAsBody(obj, null);
173: writer.endDocument();
174:
175: serializer.reconcileID(); // extra check
176: } catch (SAXException e) {
177: throw new MarshalException(e);
178: }
179: }
180:
181: //
182: //
183: // create XMLWriter by specifing various type of output.
184: //
185: //
186:
187: protected CharacterEscapeHandler createEscapeHandler(String encoding) {
188: if (escapeHandler != null)
189: // user-specified one takes precedence.
190: return escapeHandler;
191:
192: if (encoding.startsWith("UTF"))
193: // no need for character reference. Use the handler
194: // optimized for that pattern.
195: return MinimumEscapeHandler.theInstance;
196:
197: // otherwise try to find one from the encoding
198: try {
199: // try new JDK1.4 NIO
200: return new NioEscapeHandler(getJavaEncoding(encoding));
201: } catch (Throwable e) {
202: // if that fails, fall back to the dumb mode
203: return DumbEscapeHandler.theInstance;
204: }
205: }
206:
207: public XMLWriter createWriter(Writer w, String encoding)
208: throws JAXBException {
209:
210: // buffering improves the performance
211: w = new BufferedWriter(w);
212:
213: CharacterEscapeHandler ceh = createEscapeHandler(encoding);
214: XMLWriter xw;
215:
216: if (isFormattedOutput()) {
217: DataWriter d = new DataWriter(w, encoding, ceh);
218: d.setIndentStep(indent);
219: xw = d;
220: } else
221: xw = new XMLWriter(w, encoding, ceh);
222:
223: xw.setXmlDecl(printXmlDeclaration);
224: xw.setHeader(header);
225: return xw;
226: }
227:
228: public XMLWriter createWriter(Writer w) throws JAXBException {
229: return createWriter(w, getEncoding());
230: }
231:
232: public XMLWriter createWriter(OutputStream os) throws JAXBException {
233: return createWriter(os, getEncoding());
234: }
235:
236: public XMLWriter createWriter(OutputStream os, String encoding)
237: throws JAXBException {
238: try {
239: return createWriter(new OutputStreamWriter(os,
240: getJavaEncoding(encoding)), encoding);
241: } catch (UnsupportedEncodingException e) {
242: throw new MarshalException(Messages.format(
243: Messages.UNSUPPORTED_ENCODING, encoding), e);
244: }
245: }
246:
247: public Object getProperty(String name) throws PropertyException {
248: if (INDENT_STRING.equals(name))
249: return indent;
250: if (ENCODING_HANDLER.equals(name))
251: return escapeHandler;
252: if (PREFIX_MAPPER.equals(name))
253: return prefixMapper;
254: if (XMLDECLARATION.equals(name))
255: return printXmlDeclaration ? Boolean.TRUE : Boolean.FALSE;
256: if (XML_HEADERS.equals(name))
257: return header;
258:
259: return super .getProperty(name);
260: }
261:
262: public void setProperty(String name, Object value)
263: throws PropertyException {
264: if (INDENT_STRING.equals(name) && value instanceof String) {
265: indent = (String) value;
266: return;
267: }
268: if (ENCODING_HANDLER.equals(name)) {
269: escapeHandler = (CharacterEscapeHandler) value;
270: return;
271: }
272: if (PREFIX_MAPPER.equals(name)) {
273: prefixMapper = (NamespacePrefixMapper) value;
274: return;
275: }
276: if (XMLDECLARATION.equals(name)) {
277: printXmlDeclaration = ((Boolean) value).booleanValue();
278: return;
279: }
280: if (XML_HEADERS.equals(name)) {
281: header = (String) value;
282: return;
283: }
284:
285: super .setProperty(name, value);
286: }
287:
288: private static final String INDENT_STRING = "com.sun.xml.bind.indentString";
289: private static final String PREFIX_MAPPER = "com.sun.xml.bind.namespacePrefixMapper";
290: private static final String ENCODING_HANDLER = "com.sun.xml.bind.characterEscapeHandler";
291: private static final String XMLDECLARATION = "com.sun.xml.bind.xmlDeclaration";
292: private static final String XML_HEADERS = "com.sun.xml.bind.xmlHeaders";
293: }
|