1 package com.sun.tools.xjc.runtime; 2 3 import java.io.BufferedWriter ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.io.OutputStream ; 7 import java.io.OutputStreamWriter ; 8 import java.io.UnsupportedEncodingException ; 9 import java.io.Writer ; 10 11 import javax.xml.bind.DatatypeConverter; 12 import javax.xml.bind.JAXBException; 13 import javax.xml.bind.MarshalException; 14 import javax.xml.bind.PropertyException; 15 import javax.xml.bind.helpers.AbstractMarshallerImpl; 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 import javax.xml.transform.Result ; 20 import javax.xml.transform.dom.DOMResult ; 21 import javax.xml.transform.sax.SAXResult ; 22 import javax.xml.transform.stream.StreamResult ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Node ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.helpers.LocatorImpl ; 29 30 import com.sun.xml.bind.DatatypeConverterImpl; 31 import com.sun.xml.bind.JAXBAssertionError; 32 import com.sun.xml.bind.marshaller.CharacterEscapeHandler; 33 import com.sun.xml.bind.marshaller.DataWriter; 34 import com.sun.xml.bind.marshaller.DumbEscapeHandler; 35 import com.sun.xml.bind.marshaller.Messages; 36 import com.sun.xml.bind.marshaller.MinimumEscapeHandler; 37 import com.sun.xml.bind.marshaller.NamespacePrefixMapper; 38 import com.sun.xml.bind.marshaller.NioEscapeHandler; 39 import com.sun.xml.bind.marshaller.SAX2DOMEx; 40 import com.sun.xml.bind.marshaller.SchemaLocationFilter; 41 import com.sun.xml.bind.marshaller.XMLWriter; 42 43 49 public class MarshallerImpl extends AbstractMarshallerImpl 50 { 51 52 private String indent = " "; 53 54 55 private NamespacePrefixMapper prefixMapper = null; 56 57 58 private CharacterEscapeHandler escapeHandler = null; 59 60 61 private boolean printXmlDeclaration = true; 62 63 64 private String header=null; 65 66 67 final DefaultJAXBContextImpl context; 68 69 public MarshallerImpl( DefaultJAXBContextImpl c ) { 70 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 72 73 context = c; 74 } 75 76 public void marshal(Object obj, Result result) throws JAXBException { 77 XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj); 79 80 if(so==null) 81 throw new MarshalException( 82 Messages.format( Messages.NOT_MARSHALLABLE ) ); 83 84 85 if (result instanceof SAXResult ) { 86 write(so, ((SAXResult ) result).getHandler()); 87 return; 88 } 89 if (result instanceof DOMResult ) { 90 Node node = ((DOMResult ) result).getNode(); 91 92 if (node == null) { 93 try { 94 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 95 dbf.setNamespaceAware(true); 96 DocumentBuilder db = dbf.newDocumentBuilder(); 97 Document doc = db.newDocument(); 98 ((DOMResult ) result).setNode(doc); 99 write(so, new SAX2DOMEx(doc)); 100 } catch (ParserConfigurationException pce) { 101 throw new JAXBAssertionError(pce); 102 } 103 } else { 104 write(so, new SAX2DOMEx(node)); 105 } 106 107 return; 108 } 109 if (result instanceof StreamResult ) { 110 StreamResult sr = (StreamResult ) result; 111 XMLWriter w = null; 112 113 if (sr.getWriter() != null) 114 w = createWriter(sr.getWriter()); 115 else if (sr.getOutputStream() != null) 116 w = createWriter(sr.getOutputStream()); 117 else if (sr.getSystemId() != null) { 118 String fileURL = sr.getSystemId(); 119 120 if (fileURL.startsWith("file:///")) { 121 if (fileURL.substring(8).indexOf(":") > 0) 122 fileURL = fileURL.substring(8); 123 else 124 fileURL = fileURL.substring(7); 125 } 127 try { 128 w = createWriter(new FileOutputStream (fileURL)); 129 } catch (IOException e) { 130 throw new MarshalException(e); 131 } 132 } 133 134 if (w == null) 135 throw new IllegalArgumentException (); 136 137 write(so, w); 138 return; 139 } 140 141 throw new MarshalException( 143 Messages.format( Messages.UNSUPPORTED_RESULT ) ); 144 } 145 146 private void write( XMLSerializable obj, ContentHandler writer ) 147 throws JAXBException { 148 149 try { 150 if( getSchemaLocation()!=null || getNoNSSchemaLocation()!=null ) { 151 writer = new SchemaLocationFilter( 154 getSchemaLocation(), 155 getNoNSSchemaLocation(), 156 writer ); 157 } 158 159 SAXMarshaller serializer = new SAXMarshaller(writer,prefixMapper,this); 160 161 writer.setDocumentLocator( new LocatorImpl () ); 163 writer.startDocument(); 164 serializer.childAsBody(obj,null); 165 writer.endDocument(); 166 167 serializer.reconcileID(); } catch( SAXException e ) { 169 throw new MarshalException(e); 170 } 171 } 172 173 174 180 protected CharacterEscapeHandler createEscapeHandler( String encoding ) { 181 if( escapeHandler!=null ) 182 return escapeHandler; 184 185 if( encoding.startsWith("UTF") ) 186 return MinimumEscapeHandler.theInstance; 189 190 try { 192 return new NioEscapeHandler( getJavaEncoding(encoding) ); 194 } catch( Throwable e ) { 195 return DumbEscapeHandler.theInstance; 197 } 198 } 199 200 public XMLWriter createWriter( Writer w, String encoding ) throws JAXBException { 201 202 w = new BufferedWriter (w); 204 205 CharacterEscapeHandler ceh = createEscapeHandler(encoding); 206 XMLWriter xw; 207 208 if(isFormattedOutput()) { 209 DataWriter d = new DataWriter(w,encoding,ceh); 210 d.setIndentStep(indent); 211 xw=d; 212 } 213 else 214 xw = new XMLWriter(w,encoding,ceh); 215 216 xw.setXmlDecl(printXmlDeclaration); 217 xw.setHeader(header); 218 return xw; 219 } 220 221 public XMLWriter createWriter(Writer w) throws JAXBException{ 222 return createWriter(w, getEncoding()); 223 } 224 225 public XMLWriter createWriter( OutputStream os ) throws JAXBException { 226 return createWriter(os, getEncoding()); 227 } 228 229 public XMLWriter createWriter( OutputStream os, String encoding ) throws JAXBException { 230 try { 231 return createWriter( 232 new OutputStreamWriter (os,getJavaEncoding(encoding)), 233 encoding ); 234 } catch( UnsupportedEncodingException e ) { 235 throw new MarshalException( 236 Messages.format( Messages.UNSUPPORTED_ENCODING, encoding ), 237 e ); 238 } 239 } 240 241 242 public Object getProperty(String name) throws PropertyException { 243 if( INDENT_STRING.equals(name) ) 244 return indent; 245 if( ENCODING_HANDLER.equals(name) ) 246 return escapeHandler; 247 if( PREFIX_MAPPER.equals(name) ) 248 return prefixMapper; 249 if( XMLDECLARATION.equals(name) ) 250 return printXmlDeclaration ? Boolean.TRUE : Boolean.FALSE; 251 if( XML_HEADERS.equals(name) ) 252 return header; 253 254 return super.getProperty(name); 255 } 256 257 public void setProperty(String name, Object value) throws PropertyException { 258 if( INDENT_STRING.equals(name) && value instanceof String ) { 259 indent = (String )value; 260 return; 261 } 262 if( ENCODING_HANDLER.equals(name) ) { 263 escapeHandler = (CharacterEscapeHandler)value; 264 return; 265 } 266 if( PREFIX_MAPPER.equals(name) ) { 267 prefixMapper = (NamespacePrefixMapper)value; 268 return; 269 } 270 if( XMLDECLARATION.equals(name) ) { 271 printXmlDeclaration = ((Boolean )value).booleanValue(); 272 return; 273 } 274 if( XML_HEADERS.equals(name) ) { 275 header = (String )value; 276 return; 277 } 278 279 super.setProperty(name, value); 280 } 281 282 private static final String INDENT_STRING = "com.sun.xml.bind.indentString"; 283 private static final String PREFIX_MAPPER = "com.sun.xml.bind.namespacePrefixMapper"; 284 private static final String ENCODING_HANDLER = "com.sun.xml.bind.characterEscapeHandler"; 285 private static final String XMLDECLARATION = "com.sun.xml.bind.xmlDeclaration"; 286 private static final String XML_HEADERS = "com.sun.xml.bind.xmlHeaders"; 287 } 288 | Popular Tags |