1 17 package org.apache.ws.jaxme.impl; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 import java.io.OutputStreamWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.io.Writer ; 24 25 import javax.xml.bind.JAXBException; 26 import javax.xml.bind.MarshalException; 27 import javax.xml.bind.Marshaller; 28 import javax.xml.bind.PropertyException; 29 import javax.xml.namespace.QName ; 30 import javax.xml.transform.Result ; 31 import javax.xml.transform.dom.DOMResult ; 32 import javax.xml.transform.sax.SAXResult ; 33 import javax.xml.transform.stream.StreamResult ; 34 35 import org.apache.ws.jaxme.*; 36 import org.apache.ws.jaxme.JMElement; 37 import org.apache.ws.jaxme.JMMarshaller; 38 import org.apache.ws.jaxme.util.DOMBuilder; 39 import org.w3c.dom.Node ; 40 import org.xml.sax.ContentHandler ; 41 import org.xml.sax.SAXException ; 42 43 44 48 public class JMMarshallerImpl extends JMControllerImpl implements JMMarshaller { 49 52 public static final String DEFAULT_JAXB_ENCODING = "UTF-8"; 53 54 56 public static final String DEFAULT_JAXME_INDENTATION_STRING = " "; 57 58 61 public static final String DEFAULT_JAXME_INDENTATION_SEPARATOR = "\n"; 62 63 71 public static final String JAXME_INDENTATION_STRING = "jaxme.indentation.string"; 72 73 80 public static final String JAXME_INDENTATION_SEPARATOR = "jaxme.indentation.separator"; 81 82 91 public static final String JAXME_XML_DECLARATION = "jaxme.xml.declaration"; 92 93 97 public static final String JAXME_XML_WRITER = "jaxme.xml.writer"; 98 99 private static final Class xmlWriterClassDefault; 100 101 static { 102 Class c; 103 try { 104 c = Class.forName("org.apache.ws.jaxme.impl.CharSetXMLWriter"); 105 } catch (Exception e) { 106 c = XMLWriterImpl.class; 107 } 108 xmlWriterClassDefault = c; 109 } 110 111 private String encoding = DEFAULT_JAXB_ENCODING; 112 private boolean indentation = true; 113 private String indentationString = DEFAULT_JAXME_INDENTATION_STRING; 114 private String indentationSeparator = DEFAULT_JAXME_INDENTATION_SEPARATOR; 115 private boolean xmlDeclaration = true; 116 private Class xmlWriterClass; 117 private String noNamespaceSchemaLocation, schemaLocation; 118 119 125 public void setEncoding(String pEncoding) throws PropertyException { 126 if (pEncoding == null) { 127 pEncoding = DEFAULT_JAXB_ENCODING; 128 } 129 encoding = pEncoding; 130 } 131 132 135 public String getEncoding() { return encoding; } 136 137 143 public void setXMLWriterClass(Class pClass) throws PropertyException { 144 if (pClass == null) { 145 xmlWriterClass = null; 147 } else if (XMLWriter.class.isAssignableFrom(pClass) && !pClass.isInterface()) { 148 xmlWriterClass = pClass; 149 } else { 150 throw new PropertyException("The class " + pClass.getName() + " is not implementing " + XMLWriter.class.getName()); 151 } 152 } 153 154 157 public Class getXMLWriterClass() { 158 return xmlWriterClass == null ? xmlWriterClassDefault : xmlWriterClass; 159 } 160 161 164 public void setIndentation(boolean pIndentation) { 165 indentation = pIndentation; 166 } 167 168 171 public boolean getIndentation() { 172 return indentation; 173 } 174 175 179 public void setXmlDeclaration(boolean pDeclaration) { 180 xmlDeclaration = pDeclaration; 181 } 182 183 187 public boolean getXmlDeclaration() { return xmlDeclaration; } 188 189 197 public void setIndentationString(String pStr) { indentationString = pStr; } 198 199 207 public String getIndentationString() { return indentationString; } 208 209 217 public void setIndentationSeparator(String pStr) { indentationSeparator = pStr; } 218 219 227 public String getIndentationSeparator() { return indentationSeparator; } 228 229 237 public void setSchemaLocation(String pValue) throws PropertyException { 238 if (pValue != null && noNamespaceSchemaLocation != null) { 239 throw new PropertyException("The properties schemaLocation and noNamespaceSchemaLocation are mutually exclusive."); 240 } 241 schemaLocation = pValue; 242 } 243 244 252 public String getSchemaLocation() { 253 return schemaLocation; 254 } 255 256 265 public void setNoNamespaceSchemaLocation(String pValue) throws PropertyException { 266 if (pValue != null && noNamespaceSchemaLocation != null) { 267 throw new PropertyException("The properties schemaLocation and noNamespaceSchemaLocation are mutually exclusive."); 268 } 269 noNamespaceSchemaLocation = pValue; 270 } 271 272 280 public String getNoNamespaceSchemaLocation() { 281 return noNamespaceSchemaLocation; 282 } 283 284 public void setProperty(String pProperty, Object pValue) 285 throws PropertyException { 286 if (pProperty.startsWith("jaxb.")) { 287 if (Marshaller.JAXB_ENCODING.equals(pProperty)) { 288 setEncoding((String ) pValue); 289 return; 290 } else if (Marshaller.JAXB_FORMATTED_OUTPUT.equals(pProperty)) { 291 setIndentation(((Boolean ) pValue).booleanValue()); 292 return; 293 } else if (Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(pProperty)) { 294 setNoNamespaceSchemaLocation((String ) pValue); 295 return; 296 } else if (Marshaller.JAXB_SCHEMA_LOCATION.equals(pProperty)) { 297 setSchemaLocation((String ) pValue); 298 return; 299 } 300 } else if (pProperty.startsWith("jaxme.")) { 301 if (JAXME_XML_WRITER.equals(pProperty)) { 302 setXMLWriterClass((Class ) pValue); 303 return; 304 } else if (JAXME_XML_DECLARATION.equals(pProperty)) { 305 setXmlDeclaration(((Boolean ) pValue).booleanValue()); 306 return; 307 } else if (JAXME_INDENTATION_SEPARATOR.equals(pProperty)) { 308 setIndentationSeparator((String ) pValue); 309 return; 310 } else if (JAXME_INDENTATION_STRING.equals(pProperty)) { 311 setIndentationString((String ) pValue); 312 return; 313 } 314 } 315 super.setProperty(pProperty, pValue); 316 } 317 318 public Object getProperty(String pProperty) throws PropertyException { 319 if (pProperty.startsWith("jaxb.")) { 320 if (Marshaller.JAXB_ENCODING.equals(pProperty)) { 321 return getEncoding(); 322 } else if (Marshaller.JAXB_FORMATTED_OUTPUT.equals(pProperty)) { 323 return new Boolean (getIndentation()); 324 } 325 } else if (pProperty.startsWith("jaxme.")) { 326 if (JAXME_INDENTATION_STRING.equals(pProperty)) { 327 return getIndentationString(); 328 } else if (JAXME_XML_WRITER.equals(pProperty)) { 329 return getXMLWriterClass(); 330 } else if (JAXME_XML_DECLARATION.equals(pProperty)) { 331 return getEncoding(); 332 } else if (JAXME_INDENTATION_SEPARATOR.equals(pProperty)) { 333 return getIndentationSeparator(); 334 } 335 } 336 return super.getProperty(pProperty); 337 } 338 339 340 342 public void marshal(Object pObject, OutputStream pStream) throws JAXBException { 343 Writer writer; 344 try { 345 writer = new OutputStreamWriter (pStream, getEncoding()); 346 } catch(UnsupportedEncodingException e) { 347 throw new MarshalException("Unsupported encoding: " + getEncoding(), e); 348 } 349 marshal(pObject, writer); 350 try { 351 writer.close(); 352 } catch (IOException e) { 353 throw new MarshalException(e); 354 } 355 } 356 357 public void marshal(Object pObject, ContentHandler pHandler) throws JAXBException { 358 JMElement element = (JMElement) pObject; 359 QName qName = element.getQName(); 360 try { 361 JMManager manager = getJAXBContextImpl().getManager(qName); 362 JMSAXDriver driver = manager.getDriver(); 363 JMSAXDriverController controller = new JMSAXDriverController(this, pHandler); 364 controller.marshal(driver, qName.getPrefix(), qName.getNamespaceURI(), qName.getLocalPart(), element); 365 } catch (SAXException e) { 366 throw new MarshalException(e); 367 } 368 } 369 370 public void marshal(Object pObject, Writer pWriter) throws JAXBException { 371 if (getXmlDeclaration()) { 372 try { 373 pWriter.write("<?xml version='1.0' encoding='" + getEncoding() + "'?>"); 374 if (getIndentation()) { 375 pWriter.write(getIndentationSeparator()); 376 } 377 } catch (IOException e) { 378 throw new MarshalException(e); 379 } 380 } 381 XMLWriter w; 382 Class c = getXMLWriterClass(); 383 try { 384 w = (XMLWriter) c.newInstance(); 385 } catch (Exception e) { 386 throw new JAXBException("Failed to instantiate XMLWriter class " + c.getName(), e); 387 } 388 w.init(this); 389 w.setWriter(pWriter); 390 marshal(pObject, w); 391 } 392 393 public void marshal(Object pObject, Node pNode) throws JAXBException { 394 DOMBuilder db = new DOMBuilder(); 395 db.setTarget(pNode); 396 marshal(pObject, db); 397 } 398 399 public void marshal(Object pObject, Result pResult) throws JAXBException { 400 if (pResult instanceof SAXResult ) { 401 ContentHandler ch = ((SAXResult ) pResult).getHandler(); 402 if (ch == null) { 403 throw new MarshalException("The SAXResult doesn't have its ContentHandler set."); 404 } 405 marshal(pObject, ch); 406 } else if (pResult instanceof StreamResult ) { 407 StreamResult sr = (StreamResult ) pResult; 408 Writer w = sr.getWriter(); 409 if (w == null) { 410 OutputStream s = sr.getOutputStream(); 411 if (s == null) { 412 throw new MarshalException("The StreamResult doesn't have its Writer or OutputStream set."); 413 } 414 marshal(pObject, s); 415 } else { 416 marshal(pObject, w); 417 } 418 } else if (pResult instanceof DOMResult ) { 419 Node node = ((DOMResult ) pResult).getNode(); 420 if (node == null) { 421 throw new MarshalException("The DOMResult doesn't have its Node set."); 422 } 423 marshal(pObject, node); 424 } else { 425 throw new MarshalException("Unknown type of Result: " + pResult.getClass().getName() + 426 ", only SAXResult, StreamResult and DOMResult are supported."); 427 } 428 } 429 430 public Node getNode(java.lang.Object contentTree) throws JAXBException { 431 throw new UnsupportedOperationException ("JaxMe doesn't support live DOM views"); 432 } 433 } 434 | Popular Tags |