src/share/jaxws_classes/javax/xml/bind/helpers/AbstractMarshallerImpl.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.helpers;
ohair@286 27
ohair@286 28 import javax.xml.bind.JAXBException;
ohair@286 29 import javax.xml.bind.Marshaller;
ohair@286 30 import javax.xml.bind.PropertyException;
ohair@286 31 import javax.xml.bind.ValidationEventHandler;
ohair@286 32 import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286 33 import javax.xml.bind.attachment.AttachmentMarshaller;
ohair@286 34 import javax.xml.stream.XMLEventWriter;
ohair@286 35 import javax.xml.stream.XMLStreamWriter;
ohair@286 36 import javax.xml.transform.dom.DOMResult;
ohair@286 37 import javax.xml.transform.sax.SAXResult;
ohair@286 38 import javax.xml.transform.stream.StreamResult;
ohair@286 39 import javax.xml.validation.Schema;
ohair@286 40 import java.io.UnsupportedEncodingException;
ohair@286 41 import java.io.File;
ohair@286 42 import java.io.OutputStream;
ohair@286 43 import java.io.FileOutputStream;
ohair@286 44 import java.io.BufferedOutputStream;
ohair@286 45 import java.io.IOException;
ohair@286 46 // J2SE1.4 feature
ohair@286 47 // import java.nio.charset.Charset;
ohair@286 48 // import java.nio.charset.UnsupportedCharsetException;
ohair@286 49
ohair@286 50 /**
ohair@286 51 * Partial default <tt>Marshaller</tt> implementation.
ohair@286 52 *
ohair@286 53 * <p>
ohair@286 54 * This class provides a partial default implementation for the
ohair@286 55 * {@link javax.xml.bind.Marshaller} interface.
ohair@286 56 *
ohair@286 57 * <p>
ohair@286 58 * The only methods that a JAXB Provider has to implement are
ohair@286 59 * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.transform.Result)},
ohair@286 60 * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLStreamWriter)}, and
ohair@286 61 * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLEventWriter)}.
ohair@286 62 *
ohair@286 63 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
ohair@286 64 * @see javax.xml.bind.Marshaller
ohair@286 65 * @since JAXB1.0
ohair@286 66 */
ohair@286 67 public abstract class AbstractMarshallerImpl implements Marshaller
ohair@286 68 {
ohair@286 69 /** handler that will be used to process errors and warnings during marshal */
ohair@286 70 private ValidationEventHandler eventHandler =
ohair@286 71 new DefaultValidationEventHandler();
ohair@286 72
ohair@286 73 //J2SE1.4 feature
ohair@286 74 //private Charset encoding = null;
ohair@286 75
ohair@286 76 /** store the value of the encoding property. */
ohair@286 77 private String encoding = "UTF-8";
ohair@286 78
ohair@286 79 /** store the value of the schemaLocation property. */
ohair@286 80 private String schemaLocation = null;
ohair@286 81
ohair@286 82 /** store the value of the noNamespaceSchemaLocation property. */
ohair@286 83 private String noNSSchemaLocation = null;
ohair@286 84
ohair@286 85 /** store the value of the formattedOutput property. */
ohair@286 86 private boolean formattedOutput = false;
ohair@286 87
ohair@286 88 /** store the value of the fragment property. */
ohair@286 89 private boolean fragment = false;
ohair@286 90
ohair@286 91 public final void marshal( Object obj, java.io.OutputStream os )
ohair@286 92 throws JAXBException {
ohair@286 93
ohair@286 94 checkNotNull( obj, "obj", os, "os" );
ohair@286 95 marshal( obj, new StreamResult(os) );
ohair@286 96 }
ohair@286 97
ohair@286 98 public void marshal(Object jaxbElement, File output) throws JAXBException {
ohair@286 99 checkNotNull(jaxbElement, "jaxbElement", output, "output" );
ohair@286 100 try {
ohair@286 101 OutputStream os = new BufferedOutputStream(new FileOutputStream(output));
ohair@286 102 try {
ohair@286 103 marshal( jaxbElement, new StreamResult(os) );
ohair@286 104 } finally {
ohair@286 105 os.close();
ohair@286 106 }
ohair@286 107 } catch (IOException e) {
ohair@286 108 throw new JAXBException(e);
ohair@286 109 }
ohair@286 110 }
ohair@286 111
ohair@286 112 public final void marshal( Object obj, java.io.Writer w )
ohair@286 113 throws JAXBException {
ohair@286 114
ohair@286 115 checkNotNull( obj, "obj", w, "writer" );
ohair@286 116 marshal( obj, new StreamResult(w) );
ohair@286 117 }
ohair@286 118
ohair@286 119 public final void marshal( Object obj, org.xml.sax.ContentHandler handler )
ohair@286 120 throws JAXBException {
ohair@286 121
ohair@286 122 checkNotNull( obj, "obj", handler, "handler" );
ohair@286 123 marshal( obj, new SAXResult(handler) );
ohair@286 124 }
ohair@286 125
ohair@286 126 public final void marshal( Object obj, org.w3c.dom.Node node )
ohair@286 127 throws JAXBException {
ohair@286 128
ohair@286 129 checkNotNull( obj, "obj", node, "node" );
ohair@286 130 marshal( obj, new DOMResult(node) );
ohair@286 131 }
ohair@286 132
ohair@286 133 /**
ohair@286 134 * By default, the getNode method is unsupported and throw
ohair@286 135 * an {@link java.lang.UnsupportedOperationException}.
ohair@286 136 *
ohair@286 137 * Implementations that choose to support this method must
ohair@286 138 * override this method.
ohair@286 139 */
ohair@286 140 public org.w3c.dom.Node getNode( Object obj ) throws JAXBException {
ohair@286 141
ohair@286 142 checkNotNull( obj, "obj", Boolean.TRUE, "foo" );
ohair@286 143
ohair@286 144 throw new UnsupportedOperationException();
ohair@286 145 }
ohair@286 146
ohair@286 147 /**
ohair@286 148 * Convenience method for getting the current output encoding.
ohair@286 149 *
ohair@286 150 * @return the current encoding or "UTF-8" if it hasn't been set.
ohair@286 151 */
ohair@286 152 protected String getEncoding() {
ohair@286 153 return encoding;
ohair@286 154 }
ohair@286 155
ohair@286 156 /**
ohair@286 157 * Convenience method for setting the output encoding.
ohair@286 158 *
ohair@286 159 * @param encoding a valid encoding as specified in the Marshaller class
ohair@286 160 * documentation
ohair@286 161 */
ohair@286 162 protected void setEncoding( String encoding ) {
ohair@286 163 this.encoding = encoding;
ohair@286 164 }
ohair@286 165
ohair@286 166 /**
ohair@286 167 * Convenience method for getting the current schemaLocation.
ohair@286 168 *
ohair@286 169 * @return the current schemaLocation or null if it hasn't been set
ohair@286 170 */
ohair@286 171 protected String getSchemaLocation() {
ohair@286 172 return schemaLocation;
ohair@286 173 }
ohair@286 174
ohair@286 175 /**
ohair@286 176 * Convenience method for setting the schemaLocation.
ohair@286 177 *
ohair@286 178 * @param location the schemaLocation value
ohair@286 179 */
ohair@286 180 protected void setSchemaLocation( String location ) {
ohair@286 181 schemaLocation = location;
ohair@286 182 }
ohair@286 183
ohair@286 184 /**
ohair@286 185 * Convenience method for getting the current noNamespaceSchemaLocation.
ohair@286 186 *
ohair@286 187 * @return the current noNamespaceSchemaLocation or null if it hasn't
ohair@286 188 * been set
ohair@286 189 */
ohair@286 190 protected String getNoNSSchemaLocation() {
ohair@286 191 return noNSSchemaLocation;
ohair@286 192 }
ohair@286 193
ohair@286 194 /**
ohair@286 195 * Convenience method for setting the noNamespaceSchemaLocation.
ohair@286 196 *
ohair@286 197 * @param location the noNamespaceSchemaLocation value
ohair@286 198 */
ohair@286 199 protected void setNoNSSchemaLocation( String location ) {
ohair@286 200 noNSSchemaLocation = location;
ohair@286 201 }
ohair@286 202
ohair@286 203 /**
ohair@286 204 * Convenience method for getting the formatted output flag.
ohair@286 205 *
ohair@286 206 * @return the current value of the formatted output flag or false if
ohair@286 207 * it hasn't been set.
ohair@286 208 */
ohair@286 209 protected boolean isFormattedOutput() {
ohair@286 210 return formattedOutput;
ohair@286 211 }
ohair@286 212
ohair@286 213 /**
ohair@286 214 * Convenience method for setting the formatted output flag.
ohair@286 215 *
ohair@286 216 * @param v value of the formatted output flag.
ohair@286 217 */
ohair@286 218 protected void setFormattedOutput( boolean v ) {
ohair@286 219 formattedOutput = v;
ohair@286 220 }
ohair@286 221
ohair@286 222
ohair@286 223 /**
ohair@286 224 * Convenience method for getting the fragment flag.
ohair@286 225 *
ohair@286 226 * @return the current value of the fragment flag or false if
ohair@286 227 * it hasn't been set.
ohair@286 228 */
ohair@286 229 protected boolean isFragment() {
ohair@286 230 return fragment;
ohair@286 231 }
ohair@286 232
ohair@286 233 /**
ohair@286 234 * Convenience method for setting the fragment flag.
ohair@286 235 *
ohair@286 236 * @param v value of the fragment flag.
ohair@286 237 */
ohair@286 238 protected void setFragment( boolean v ) {
ohair@286 239 fragment = v;
ohair@286 240 }
ohair@286 241
ohair@286 242
ohair@286 243 static String[] aliases = {
ohair@286 244 "UTF-8", "UTF8",
ohair@286 245 "UTF-16", "Unicode",
ohair@286 246 "UTF-16BE", "UnicodeBigUnmarked",
ohair@286 247 "UTF-16LE", "UnicodeLittleUnmarked",
ohair@286 248 "US-ASCII", "ASCII",
ohair@286 249 "TIS-620", "TIS620",
ohair@286 250
ohair@286 251 // taken from the project-X parser
ohair@286 252 "ISO-10646-UCS-2", "Unicode",
ohair@286 253
ohair@286 254 "EBCDIC-CP-US", "cp037",
ohair@286 255 "EBCDIC-CP-CA", "cp037",
ohair@286 256 "EBCDIC-CP-NL", "cp037",
ohair@286 257 "EBCDIC-CP-WT", "cp037",
ohair@286 258
ohair@286 259 "EBCDIC-CP-DK", "cp277",
ohair@286 260 "EBCDIC-CP-NO", "cp277",
ohair@286 261 "EBCDIC-CP-FI", "cp278",
ohair@286 262 "EBCDIC-CP-SE", "cp278",
ohair@286 263
ohair@286 264 "EBCDIC-CP-IT", "cp280",
ohair@286 265 "EBCDIC-CP-ES", "cp284",
ohair@286 266 "EBCDIC-CP-GB", "cp285",
ohair@286 267 "EBCDIC-CP-FR", "cp297",
ohair@286 268
ohair@286 269 "EBCDIC-CP-AR1", "cp420",
ohair@286 270 "EBCDIC-CP-HE", "cp424",
ohair@286 271 "EBCDIC-CP-BE", "cp500",
ohair@286 272 "EBCDIC-CP-CH", "cp500",
ohair@286 273
ohair@286 274 "EBCDIC-CP-ROECE", "cp870",
ohair@286 275 "EBCDIC-CP-YU", "cp870",
ohair@286 276 "EBCDIC-CP-IS", "cp871",
ohair@286 277 "EBCDIC-CP-AR2", "cp918",
ohair@286 278
ohair@286 279 // IANA also defines two that JDK 1.2 doesn't handle:
ohair@286 280 // EBCDIC-CP-GR --> CP423
ohair@286 281 // EBCDIC-CP-TR --> CP905
ohair@286 282 };
ohair@286 283
ohair@286 284 /**
ohair@286 285 * Gets the corresponding Java encoding name from an IANA name.
ohair@286 286 *
ohair@286 287 * This method is a helper method for the derived class to convert
ohair@286 288 * encoding names.
ohair@286 289 *
ohair@286 290 * @exception UnsupportedEncodingException
ohair@286 291 * If this implementation couldn't find the Java encoding name.
ohair@286 292 */
ohair@286 293 protected String getJavaEncoding( String encoding ) throws UnsupportedEncodingException {
ohair@286 294 try {
ohair@286 295 "1".getBytes(encoding);
ohair@286 296 return encoding;
ohair@286 297 } catch( UnsupportedEncodingException e ) {
ohair@286 298 // try known alias
ohair@286 299 for( int i=0; i<aliases.length; i+=2 ) {
ohair@286 300 if(encoding.equals(aliases[i])) {
ohair@286 301 "1".getBytes(aliases[i+1]);
ohair@286 302 return aliases[i+1];
ohair@286 303 }
ohair@286 304 }
ohair@286 305
ohair@286 306 throw new UnsupportedEncodingException(encoding);
ohair@286 307 }
ohair@286 308 /* J2SE1.4 feature
ohair@286 309 try {
ohair@286 310 this.encoding = Charset.forName( _encoding );
ohair@286 311 } catch( UnsupportedCharsetException uce ) {
ohair@286 312 throw new JAXBException( uce );
ohair@286 313 }
ohair@286 314 */
ohair@286 315 }
ohair@286 316
ohair@286 317 /**
ohair@286 318 * Default implementation of the setProperty method handles
ohair@286 319 * the four defined properties in Marshaller. If a provider
ohair@286 320 * needs to handle additional properties, it should override
ohair@286 321 * this method in a derived class.
ohair@286 322 */
ohair@286 323 public void setProperty( String name, Object value )
ohair@286 324 throws PropertyException {
ohair@286 325
ohair@286 326 if( name == null ) {
ohair@286 327 throw new IllegalArgumentException(
ohair@286 328 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286 329 }
ohair@286 330
ohair@286 331 // recognize and handle four pre-defined properties.
ohair@286 332 if( JAXB_ENCODING.equals(name) ) {
ohair@286 333 checkString( name, value );
ohair@286 334 setEncoding( (String)value );
ohair@286 335 return;
ohair@286 336 }
ohair@286 337 if( JAXB_FORMATTED_OUTPUT.equals(name) ) {
ohair@286 338 checkBoolean( name, value );
ohair@286 339 setFormattedOutput((Boolean) value );
ohair@286 340 return;
ohair@286 341 }
ohair@286 342 if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) {
ohair@286 343 checkString( name, value );
ohair@286 344 setNoNSSchemaLocation( (String)value );
ohair@286 345 return;
ohair@286 346 }
ohair@286 347 if( JAXB_SCHEMA_LOCATION.equals(name) ) {
ohair@286 348 checkString( name, value );
ohair@286 349 setSchemaLocation( (String)value );
ohair@286 350 return;
ohair@286 351 }
ohair@286 352 if( JAXB_FRAGMENT.equals(name) ) {
ohair@286 353 checkBoolean(name, value);
ohair@286 354 setFragment((Boolean) value );
ohair@286 355 return;
ohair@286 356 }
ohair@286 357
ohair@286 358 throw new PropertyException(name, value);
ohair@286 359 }
ohair@286 360
ohair@286 361 /**
ohair@286 362 * Default implementation of the getProperty method handles
ohair@286 363 * the four defined properties in Marshaller. If a provider
ohair@286 364 * needs to support additional provider specific properties,
ohair@286 365 * it should override this method in a derived class.
ohair@286 366 */
ohair@286 367 public Object getProperty( String name )
ohair@286 368 throws PropertyException {
ohair@286 369
ohair@286 370 if( name == null ) {
ohair@286 371 throw new IllegalArgumentException(
ohair@286 372 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286 373 }
ohair@286 374
ohair@286 375 // recognize and handle four pre-defined properties.
ohair@286 376 if( JAXB_ENCODING.equals(name) )
ohair@286 377 return getEncoding();
ohair@286 378 if( JAXB_FORMATTED_OUTPUT.equals(name) )
ohair@286 379 return isFormattedOutput()?Boolean.TRUE:Boolean.FALSE;
ohair@286 380 if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) )
ohair@286 381 return getNoNSSchemaLocation();
ohair@286 382 if( JAXB_SCHEMA_LOCATION.equals(name) )
ohair@286 383 return getSchemaLocation();
ohair@286 384 if( JAXB_FRAGMENT.equals(name) )
ohair@286 385 return isFragment()?Boolean.TRUE:Boolean.FALSE;
ohair@286 386
ohair@286 387 throw new PropertyException(name);
ohair@286 388 }
ohair@286 389 /**
ohair@286 390 * @see javax.xml.bind.Marshaller#getEventHandler()
ohair@286 391 */
ohair@286 392 public ValidationEventHandler getEventHandler() throws JAXBException {
ohair@286 393 return eventHandler;
ohair@286 394 }
ohair@286 395
ohair@286 396 /**
ohair@286 397 * @see javax.xml.bind.Marshaller#setEventHandler(ValidationEventHandler)
ohair@286 398 */
ohair@286 399 public void setEventHandler(ValidationEventHandler handler)
ohair@286 400 throws JAXBException {
ohair@286 401
ohair@286 402 if( handler == null ) {
ohair@286 403 eventHandler = new DefaultValidationEventHandler();
ohair@286 404 } else {
ohair@286 405 eventHandler = handler;
ohair@286 406 }
ohair@286 407 }
ohair@286 408
ohair@286 409
ohair@286 410
ohair@286 411
ohair@286 412 /*
ohair@286 413 * assert that the given object is a Boolean
ohair@286 414 */
ohair@286 415 private void checkBoolean( String name, Object value ) throws PropertyException {
ohair@286 416 if(!(value instanceof Boolean))
ohair@286 417 throw new PropertyException(
ohair@286 418 Messages.format( Messages.MUST_BE_BOOLEAN, name ) );
ohair@286 419 }
ohair@286 420
ohair@286 421 /*
ohair@286 422 * assert that the given object is a String
ohair@286 423 */
ohair@286 424 private void checkString( String name, Object value ) throws PropertyException {
ohair@286 425 if(!(value instanceof String))
ohair@286 426 throw new PropertyException(
ohair@286 427 Messages.format( Messages.MUST_BE_STRING, name ) );
ohair@286 428 }
ohair@286 429
ohair@286 430 /*
ohair@286 431 * assert that the parameters are not null
ohair@286 432 */
ohair@286 433 private void checkNotNull( Object o1, String o1Name,
ohair@286 434 Object o2, String o2Name ) {
ohair@286 435
ohair@286 436 if( o1 == null ) {
ohair@286 437 throw new IllegalArgumentException(
ohair@286 438 Messages.format( Messages.MUST_NOT_BE_NULL, o1Name ) );
ohair@286 439 }
ohair@286 440 if( o2 == null ) {
ohair@286 441 throw new IllegalArgumentException(
ohair@286 442 Messages.format( Messages.MUST_NOT_BE_NULL, o2Name ) );
ohair@286 443 }
ohair@286 444 }
ohair@286 445
ohair@286 446 public void marshal(Object obj, XMLEventWriter writer)
ohair@286 447 throws JAXBException {
ohair@286 448
ohair@286 449 throw new UnsupportedOperationException();
ohair@286 450 }
ohair@286 451
ohair@286 452 public void marshal(Object obj, XMLStreamWriter writer)
ohair@286 453 throws JAXBException {
ohair@286 454
ohair@286 455 throw new UnsupportedOperationException();
ohair@286 456 }
ohair@286 457
ohair@286 458 public void setSchema(Schema schema) {
ohair@286 459 throw new UnsupportedOperationException();
ohair@286 460 }
ohair@286 461
ohair@286 462 public Schema getSchema() {
ohair@286 463 throw new UnsupportedOperationException();
ohair@286 464 }
ohair@286 465
ohair@286 466 public void setAdapter(XmlAdapter adapter) {
ohair@286 467 if(adapter==null)
ohair@286 468 throw new IllegalArgumentException();
ohair@286 469 setAdapter((Class)adapter.getClass(),adapter);
ohair@286 470 }
ohair@286 471
ohair@286 472 public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
ohair@286 473 throw new UnsupportedOperationException();
ohair@286 474 }
ohair@286 475
ohair@286 476 public <A extends XmlAdapter> A getAdapter(Class<A> type) {
ohair@286 477 throw new UnsupportedOperationException();
ohair@286 478 }
ohair@286 479
ohair@286 480 public void setAttachmentMarshaller(AttachmentMarshaller am) {
ohair@286 481 throw new UnsupportedOperationException();
ohair@286 482 }
ohair@286 483
ohair@286 484 public AttachmentMarshaller getAttachmentMarshaller() {
ohair@286 485 throw new UnsupportedOperationException();
ohair@286 486 }
ohair@286 487
ohair@286 488 public void setListener(Listener listener) {
ohair@286 489 throw new UnsupportedOperationException();
ohair@286 490 }
ohair@286 491
ohair@286 492 public Listener getListener() {
ohair@286 493 throw new UnsupportedOperationException();
ohair@286 494 }
ohair@286 495 }

mercurial