1 5 package javax.xml.bind.helpers; 6 7 import javax.xml.bind.JAXBException; 8 import javax.xml.bind.Marshaller; 9 import javax.xml.bind.PropertyException; 10 import javax.xml.bind.ValidationEventHandler; 11 import javax.xml.bind.annotation.adapters.XmlAdapter; 12 import javax.xml.bind.attachment.AttachmentMarshaller; 13 import javax.xml.stream.XMLEventWriter; 14 import javax.xml.stream.XMLStreamWriter; 15 import javax.xml.transform.dom.DOMResult ; 16 import javax.xml.transform.sax.SAXResult ; 17 import javax.xml.transform.stream.StreamResult ; 18 import javax.xml.validation.Schema ; 19 import java.io.UnsupportedEncodingException ; 20 import java.io.File ; 21 import java.io.OutputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.IOException ; 25 29 47 public abstract class AbstractMarshallerImpl implements Marshaller 48 { 49 50 private ValidationEventHandler eventHandler = 51 new DefaultValidationEventHandler(); 52 53 56 57 private String encoding = "UTF-8"; 58 59 60 private String schemaLocation = null; 61 62 63 private String noNSSchemaLocation = null; 64 65 66 private boolean formattedOutput = false; 67 68 69 private boolean fragment = false; 70 71 public final void marshal( Object obj, java.io.OutputStream os ) 72 throws JAXBException { 73 74 checkNotNull( obj, "obj", os, "os" ); 75 marshal( obj, new StreamResult (os) ); 76 } 77 78 public void marshal(Object jaxbElement, File output) throws JAXBException { 79 checkNotNull(jaxbElement, "jaxbElement", output, "output" ); 80 try { 81 OutputStream os = new BufferedOutputStream (new FileOutputStream (output)); 82 try { 83 marshal( jaxbElement, new StreamResult (os) ); 84 } finally { 85 os.close(); 86 } 87 } catch (IOException e) { 88 throw new JAXBException(e); 89 } 90 } 91 92 public final void marshal( Object obj, java.io.Writer w ) 93 throws JAXBException { 94 95 checkNotNull( obj, "obj", w, "writer" ); 96 marshal( obj, new StreamResult (w) ); 97 } 98 99 public final void marshal( Object obj, org.xml.sax.ContentHandler handler ) 100 throws JAXBException { 101 102 checkNotNull( obj, "obj", handler, "handler" ); 103 marshal( obj, new SAXResult (handler) ); 104 } 105 106 public final void marshal( Object obj, org.w3c.dom.Node node ) 107 throws JAXBException { 108 109 checkNotNull( obj, "obj", node, "node" ); 110 marshal( obj, new DOMResult (node) ); 111 } 112 113 120 public org.w3c.dom.Node getNode( Object obj ) throws JAXBException { 121 122 checkNotNull( obj, "obj", Boolean.TRUE, "foo" ); 123 124 throw new UnsupportedOperationException (); 125 } 126 127 132 protected String getEncoding() { 133 return encoding; 134 } 135 136 142 protected void setEncoding( String encoding ) { 143 this.encoding = encoding; 144 } 145 146 151 protected String getSchemaLocation() { 152 return schemaLocation; 153 } 154 155 160 protected void setSchemaLocation( String location ) { 161 schemaLocation = location; 162 } 163 164 170 protected String getNoNSSchemaLocation() { 171 return noNSSchemaLocation; 172 } 173 174 179 protected void setNoNSSchemaLocation( String location ) { 180 noNSSchemaLocation = location; 181 } 182 183 189 protected boolean isFormattedOutput() { 190 return formattedOutput; 191 } 192 193 198 protected void setFormattedOutput( boolean v ) { 199 formattedOutput = v; 200 } 201 202 203 209 protected boolean isFragment() { 210 return fragment; 211 } 212 213 218 protected void setFragment( boolean v ) { 219 fragment = v; 220 } 221 222 223 static String [] aliases = { 224 "UTF-8", "UTF8", 225 "UTF-16", "Unicode", 226 "UTF-16BE", "UnicodeBigUnmarked", 227 "UTF-16LE", "UnicodeLittleUnmarked", 228 "US-ASCII", "ASCII", 229 "TIS-620", "TIS620", 230 231 "ISO-10646-UCS-2", "Unicode", 233 234 "EBCDIC-CP-US", "cp037", 235 "EBCDIC-CP-CA", "cp037", 236 "EBCDIC-CP-NL", "cp037", 237 "EBCDIC-CP-WT", "cp037", 238 239 "EBCDIC-CP-DK", "cp277", 240 "EBCDIC-CP-NO", "cp277", 241 "EBCDIC-CP-FI", "cp278", 242 "EBCDIC-CP-SE", "cp278", 243 244 "EBCDIC-CP-IT", "cp280", 245 "EBCDIC-CP-ES", "cp284", 246 "EBCDIC-CP-GB", "cp285", 247 "EBCDIC-CP-FR", "cp297", 248 249 "EBCDIC-CP-AR1", "cp420", 250 "EBCDIC-CP-HE", "cp424", 251 "EBCDIC-CP-BE", "cp500", 252 "EBCDIC-CP-CH", "cp500", 253 254 "EBCDIC-CP-ROECE", "cp870", 255 "EBCDIC-CP-YU", "cp870", 256 "EBCDIC-CP-IS", "cp871", 257 "EBCDIC-CP-AR2", "cp918", 258 259 }; 263 264 273 protected String getJavaEncoding( String encoding ) throws UnsupportedEncodingException { 274 try { 275 "1".getBytes(encoding); 276 return encoding; 277 } catch( UnsupportedEncodingException e ) { 278 for( int i=0; i<aliases.length; i+=2 ) { 280 if(encoding.equals(aliases[i])) { 281 "1".getBytes(aliases[i+1]); 282 return aliases[i+1]; 283 } 284 } 285 286 throw new UnsupportedEncodingException (encoding); 287 } 288 295 } 296 297 303 public void setProperty( String name, Object value ) 304 throws PropertyException { 305 306 if( name == null ) { 307 throw new IllegalArgumentException ( 308 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 309 } 310 311 if( JAXB_ENCODING.equals(name) ) { 313 checkString( name, value ); 314 setEncoding( (String )value ); 315 return; 316 } 317 if( JAXB_FORMATTED_OUTPUT.equals(name) ) { 318 checkBoolean( name, value ); 319 setFormattedOutput((Boolean ) value ); 320 return; 321 } 322 if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) { 323 checkString( name, value ); 324 setNoNSSchemaLocation( (String )value ); 325 return; 326 } 327 if( JAXB_SCHEMA_LOCATION.equals(name) ) { 328 checkString( name, value ); 329 setSchemaLocation( (String )value ); 330 return; 331 } 332 if( JAXB_FRAGMENT.equals(name) ) { 333 checkBoolean(name, value); 334 setFragment((Boolean ) value ); 335 return; 336 } 337 338 throw new PropertyException(name, value); 339 } 340 341 347 public Object getProperty( String name ) 348 throws PropertyException { 349 350 if( name == null ) { 351 throw new IllegalArgumentException ( 352 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 353 } 354 355 if( JAXB_ENCODING.equals(name) ) 357 return getEncoding(); 358 if( JAXB_FORMATTED_OUTPUT.equals(name) ) 359 return isFormattedOutput()?Boolean.TRUE:Boolean.FALSE; 360 if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) 361 return getNoNSSchemaLocation(); 362 if( JAXB_SCHEMA_LOCATION.equals(name) ) 363 return getSchemaLocation(); 364 if( JAXB_FRAGMENT.equals(name) ) 365 return isFragment()?Boolean.TRUE:Boolean.FALSE; 366 367 throw new PropertyException(name); 368 } 369 372 public ValidationEventHandler getEventHandler() throws JAXBException { 373 return eventHandler; 374 } 375 376 379 public void setEventHandler(ValidationEventHandler handler) 380 throws JAXBException { 381 382 if( handler == null ) { 383 eventHandler = new DefaultValidationEventHandler(); 384 } else { 385 eventHandler = handler; 386 } 387 } 388 389 390 391 392 395 private void checkBoolean( String name, Object value ) throws PropertyException { 396 if(!(value instanceof Boolean )) 397 throw new PropertyException( 398 Messages.format( Messages.MUST_BE_BOOLEAN, name ) ); 399 } 400 401 404 private void checkString( String name, Object value ) throws PropertyException { 405 if(!(value instanceof String )) 406 throw new PropertyException( 407 Messages.format( Messages.MUST_BE_STRING, name ) ); 408 } 409 410 413 private void checkNotNull( Object o1, String o1Name, 414 Object o2, String o2Name ) { 415 416 if( o1 == null ) { 417 throw new IllegalArgumentException ( 418 Messages.format( Messages.MUST_NOT_BE_NULL, o1Name ) ); 419 } 420 if( o2 == null ) { 421 throw new IllegalArgumentException ( 422 Messages.format( Messages.MUST_NOT_BE_NULL, o2Name ) ); 423 } 424 } 425 426 public void marshal(Object obj, XMLEventWriter writer) 427 throws JAXBException { 428 429 throw new UnsupportedOperationException (); 430 } 431 432 public void marshal(Object obj, XMLStreamWriter writer) 433 throws JAXBException { 434 435 throw new UnsupportedOperationException (); 436 } 437 438 public void setSchema(Schema schema) { 439 throw new UnsupportedOperationException (); 440 } 441 442 public Schema getSchema() { 443 throw new UnsupportedOperationException (); 444 } 445 446 public void setAdapter(XmlAdapter adapter) { 447 if(adapter==null) 448 throw new IllegalArgumentException (); 449 setAdapter((Class )adapter.getClass(),adapter); 450 } 451 452 public <A extends XmlAdapter> void setAdapter(Class <A> type, A adapter) { 453 throw new UnsupportedOperationException (); 454 } 455 456 public <A extends XmlAdapter> A getAdapter(Class <A> type) { 457 throw new UnsupportedOperationException (); 458 } 459 460 public void setAttachmentMarshaller(AttachmentMarshaller am) { 461 throw new UnsupportedOperationException (); 462 } 463 464 public AttachmentMarshaller getAttachmentMarshaller() { 465 throw new UnsupportedOperationException (); 466 } 467 468 public void setListener(Listener listener) { 469 throw new UnsupportedOperationException (); 470 } 471 472 public Listener getListener() { 473 throw new UnsupportedOperationException (); 474 } 475 } 476 | Popular Tags |