KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > testsuite > impl > runtime > MarshallerImpl


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.3-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2005.06.14 at 12:02:17 PDT
6
//
7

8 package com.sun.japex.testsuite.impl.runtime;
9
10 import java.io.BufferedWriter JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.io.OutputStreamWriter JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.io.Writer JavaDoc;
17
18 import javax.xml.bind.DatatypeConverter;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.MarshalException;
21 import javax.xml.bind.PropertyException;
22 import javax.xml.bind.helpers.AbstractMarshallerImpl;
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.transform.Result JavaDoc;
27 import javax.xml.transform.dom.DOMResult JavaDoc;
28 import javax.xml.transform.sax.SAXResult JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
30
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.xml.sax.ContentHandler JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.helpers.LocatorImpl JavaDoc;
36
37 import com.sun.xml.bind.DatatypeConverterImpl;
38 import com.sun.xml.bind.JAXBAssertionError;
39 import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
40 import com.sun.xml.bind.marshaller.DataWriter;
41 import com.sun.xml.bind.marshaller.DumbEscapeHandler;
42 import com.sun.xml.bind.marshaller.Messages;
43 import com.sun.xml.bind.marshaller.MinimumEscapeHandler;
44 import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
45 import com.sun.xml.bind.marshaller.NioEscapeHandler;
46 import com.sun.xml.bind.marshaller.SAX2DOMEx;
47 import com.sun.xml.bind.marshaller.SchemaLocationFilter;
48 import com.sun.xml.bind.marshaller.XMLWriter;
49
50 /**
51  * Implementation of {@link Marshaller} interface for JAXB RI.
52  *
53  * @author Kohsuke Kawaguchi
54  * @author Vivek Pandey
55  */

56 public class MarshallerImpl extends AbstractMarshallerImpl
57 {
58     /** Indentation string. Default is four whitespaces. */
59     private String JavaDoc indent = " ";
60     
61     /** Used to assign prefixes to namespace URIs. */
62     private NamespacePrefixMapper prefixMapper = null;
63     
64     /** Object that handles character escaping. */
65     private CharacterEscapeHandler escapeHandler = null;
66     
67     /** Whether the xml declaration will be printed or not. */
68     private boolean printXmlDeclaration = true;
69     
70     /** XML BLOB written after the XML declaration. */
71     private String JavaDoc header=null;
72     
73     /** reference to the context that created this object */
74     final DefaultJAXBContextImpl context;
75     
76     public MarshallerImpl( DefaultJAXBContextImpl c ) {
77         // initialize datatype converter with ours
78
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
79         
80         context = c;
81     }
82     
83     public void marshal(Object JavaDoc obj, Result JavaDoc result) throws JAXBException {
84         //XMLSerializable so = Util.toXMLSerializable(obj);
85
XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
86
87         if(so==null)
88             throw new MarshalException(
89                 Messages.format( Messages.NOT_MARSHALLABLE ) );
90
91
92         if (result instanceof SAXResult JavaDoc) {
93             write(so, ((SAXResult JavaDoc) result).getHandler());
94             return;
95         }
96         if (result instanceof DOMResult JavaDoc) {
97             Node JavaDoc node = ((DOMResult JavaDoc) result).getNode();
98
99             if (node == null) {
100                 try {
101                     DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
102                     dbf.setNamespaceAware(true);
103                     DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
104                     Document JavaDoc doc = db.newDocument();
105                     ((DOMResult JavaDoc) result).setNode(doc);
106                     write(so, new SAX2DOMEx(doc));
107                 } catch (ParserConfigurationException JavaDoc pce) {
108                     throw new JAXBAssertionError(pce);
109                 }
110             } else {
111                 write(so, new SAX2DOMEx(node));
112             }
113
114             return;
115         }
116         if (result instanceof StreamResult JavaDoc) {
117             StreamResult JavaDoc sr = (StreamResult JavaDoc) result;
118             XMLWriter w = null;
119
120             if (sr.getWriter() != null)
121                 w = createWriter(sr.getWriter());
122             else if (sr.getOutputStream() != null)
123                 w = createWriter(sr.getOutputStream());
124             else if (sr.getSystemId() != null) {
125                 String JavaDoc fileURL = sr.getSystemId();
126
127                 if (fileURL.startsWith("file:///")) {
128                     if (fileURL.substring(8).indexOf(":") > 0)
129                         fileURL = fileURL.substring(8);
130                     else
131                         fileURL = fileURL.substring(7);
132                 } // otherwise assume that it's a file name
133

134                 try {
135                     w = createWriter(new FileOutputStream JavaDoc(fileURL));
136                 } catch (IOException JavaDoc e) {
137                     throw new MarshalException(e);
138                 }
139             }
140
141             if (w == null)
142                 throw new IllegalArgumentException JavaDoc();
143
144             write(so, w);
145             return;
146         }
147
148         // unsupported parameter type
149
throw new MarshalException(
150             Messages.format( Messages.UNSUPPORTED_RESULT ) );
151     }
152     
153     private void write( XMLSerializable obj, ContentHandler JavaDoc writer )
154         throws JAXBException {
155
156         try {
157             if( getSchemaLocation()!=null || getNoNSSchemaLocation()!=null ) {
158                 // if we need to add xsi:schemaLocation or its brother,
159
// throw in the component to do that.
160
writer = new SchemaLocationFilter(
161                     getSchemaLocation(),
162                     getNoNSSchemaLocation(),
163                     writer );
164             }
165             
166             SAXMarshaller serializer = new SAXMarshaller(writer,prefixMapper,this);
167         
168             // set a DocumentLocator that doesn't provide any information
169
writer.setDocumentLocator( new LocatorImpl JavaDoc() );
170             writer.startDocument();
171             obj.serializeBody(serializer);
172             writer.endDocument();
173             
174             serializer.reconcileID(); // extra check
175
} catch( SAXException JavaDoc e ) {
176             throw new MarshalException(e);
177         }
178     }
179     
180     
181     //
182
//
183
// create XMLWriter by specifing various type of output.
184
//
185
//
186

187     protected CharacterEscapeHandler createEscapeHandler( String JavaDoc 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 JavaDoc e ) {
202             // if that fails, fall back to the dumb mode
203
return DumbEscapeHandler.theInstance;
204         }
205     }
206             
207     public XMLWriter createWriter( Writer JavaDoc w, String JavaDoc encoding ) throws JAXBException {
208         
209         // buffering improves the performance
210
w = new BufferedWriter JavaDoc(w);
211         
212         CharacterEscapeHandler ceh = createEscapeHandler(encoding);
213         XMLWriter xw;
214         
215         if(isFormattedOutput()) {
216             DataWriter d = new DataWriter(w,encoding,ceh);
217             d.setIndentStep(indent);
218             xw=d;
219         }
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 JavaDoc w) throws JAXBException{
229         return createWriter(w, getEncoding());
230     }
231     
232     public XMLWriter createWriter( OutputStream JavaDoc os ) throws JAXBException {
233         return createWriter(os, getEncoding());
234     }
235     
236     public XMLWriter createWriter( OutputStream JavaDoc os, String JavaDoc encoding ) throws JAXBException {
237         try {
238             return createWriter(
239                 new OutputStreamWriter JavaDoc(os,getJavaEncoding(encoding)),
240                 encoding );
241         } catch( UnsupportedEncodingException JavaDoc e ) {
242             throw new MarshalException(
243                 Messages.format( Messages.UNSUPPORTED_ENCODING, encoding ),
244                 e );
245         }
246     }
247     
248     
249     public Object JavaDoc getProperty(String JavaDoc name) throws PropertyException {
250         if( INDENT_STRING.equals(name) )
251             return indent;
252         if( ENCODING_HANDLER.equals(name) )
253             return escapeHandler;
254         if( PREFIX_MAPPER.equals(name) )
255             return prefixMapper;
256         if( XMLDECLARATION.equals(name) )
257             return printXmlDeclaration ? Boolean.TRUE : Boolean.FALSE;
258         if( XML_HEADERS.equals(name) )
259             return header;
260         
261         return super.getProperty(name);
262     }
263
264     public void setProperty(String JavaDoc name, Object JavaDoc value) throws PropertyException {
265         if( INDENT_STRING.equals(name) && value instanceof String JavaDoc ) {
266             indent = (String JavaDoc)value;
267             return;
268         }
269         if( ENCODING_HANDLER.equals(name) ) {
270             escapeHandler = (CharacterEscapeHandler)value;
271             return;
272         }
273         if( PREFIX_MAPPER.equals(name) ) {
274             prefixMapper = (NamespacePrefixMapper)value;
275             return;
276         }
277         if( XMLDECLARATION.equals(name) ) {
278             printXmlDeclaration = ((Boolean JavaDoc)value).booleanValue();
279             return;
280         }
281         if( XML_HEADERS.equals(name) ) {
282             header = (String JavaDoc)value;
283             return;
284         }
285             
286         super.setProperty(name, value);
287     }
288     
289     private static final String JavaDoc INDENT_STRING = "com.sun.xml.bind.indentString";
290     private static final String JavaDoc PREFIX_MAPPER = "com.sun.xml.bind.namespacePrefixMapper";
291     private static final String JavaDoc ENCODING_HANDLER = "com.sun.xml.bind.characterEscapeHandler";
292     private static final String JavaDoc XMLDECLARATION = "com.sun.xml.bind.xmlDeclaration";
293     private static final String JavaDoc XML_HEADERS = "com.sun.xml.bind.xmlHeaders";
294 }
295
Popular Tags