1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/helpers/AbstractMarshallerImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,495 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.xml.bind.helpers; 1.30 + 1.31 +import javax.xml.bind.JAXBException; 1.32 +import javax.xml.bind.Marshaller; 1.33 +import javax.xml.bind.PropertyException; 1.34 +import javax.xml.bind.ValidationEventHandler; 1.35 +import javax.xml.bind.annotation.adapters.XmlAdapter; 1.36 +import javax.xml.bind.attachment.AttachmentMarshaller; 1.37 +import javax.xml.stream.XMLEventWriter; 1.38 +import javax.xml.stream.XMLStreamWriter; 1.39 +import javax.xml.transform.dom.DOMResult; 1.40 +import javax.xml.transform.sax.SAXResult; 1.41 +import javax.xml.transform.stream.StreamResult; 1.42 +import javax.xml.validation.Schema; 1.43 +import java.io.UnsupportedEncodingException; 1.44 +import java.io.File; 1.45 +import java.io.OutputStream; 1.46 +import java.io.FileOutputStream; 1.47 +import java.io.BufferedOutputStream; 1.48 +import java.io.IOException; 1.49 +// J2SE1.4 feature 1.50 +// import java.nio.charset.Charset; 1.51 +// import java.nio.charset.UnsupportedCharsetException; 1.52 + 1.53 +/** 1.54 + * Partial default <tt>Marshaller</tt> implementation. 1.55 + * 1.56 + * <p> 1.57 + * This class provides a partial default implementation for the 1.58 + * {@link javax.xml.bind.Marshaller} interface. 1.59 + * 1.60 + * <p> 1.61 + * The only methods that a JAXB Provider has to implement are 1.62 + * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.transform.Result)}, 1.63 + * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLStreamWriter)}, and 1.64 + * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLEventWriter)}. 1.65 + * 1.66 + * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul> 1.67 + * @see javax.xml.bind.Marshaller 1.68 + * @since JAXB1.0 1.69 + */ 1.70 +public abstract class AbstractMarshallerImpl implements Marshaller 1.71 +{ 1.72 + /** handler that will be used to process errors and warnings during marshal */ 1.73 + private ValidationEventHandler eventHandler = 1.74 + new DefaultValidationEventHandler(); 1.75 + 1.76 + //J2SE1.4 feature 1.77 + //private Charset encoding = null; 1.78 + 1.79 + /** store the value of the encoding property. */ 1.80 + private String encoding = "UTF-8"; 1.81 + 1.82 + /** store the value of the schemaLocation property. */ 1.83 + private String schemaLocation = null; 1.84 + 1.85 + /** store the value of the noNamespaceSchemaLocation property. */ 1.86 + private String noNSSchemaLocation = null; 1.87 + 1.88 + /** store the value of the formattedOutput property. */ 1.89 + private boolean formattedOutput = false; 1.90 + 1.91 + /** store the value of the fragment property. */ 1.92 + private boolean fragment = false; 1.93 + 1.94 + public final void marshal( Object obj, java.io.OutputStream os ) 1.95 + throws JAXBException { 1.96 + 1.97 + checkNotNull( obj, "obj", os, "os" ); 1.98 + marshal( obj, new StreamResult(os) ); 1.99 + } 1.100 + 1.101 + public void marshal(Object jaxbElement, File output) throws JAXBException { 1.102 + checkNotNull(jaxbElement, "jaxbElement", output, "output" ); 1.103 + try { 1.104 + OutputStream os = new BufferedOutputStream(new FileOutputStream(output)); 1.105 + try { 1.106 + marshal( jaxbElement, new StreamResult(os) ); 1.107 + } finally { 1.108 + os.close(); 1.109 + } 1.110 + } catch (IOException e) { 1.111 + throw new JAXBException(e); 1.112 + } 1.113 + } 1.114 + 1.115 + public final void marshal( Object obj, java.io.Writer w ) 1.116 + throws JAXBException { 1.117 + 1.118 + checkNotNull( obj, "obj", w, "writer" ); 1.119 + marshal( obj, new StreamResult(w) ); 1.120 + } 1.121 + 1.122 + public final void marshal( Object obj, org.xml.sax.ContentHandler handler ) 1.123 + throws JAXBException { 1.124 + 1.125 + checkNotNull( obj, "obj", handler, "handler" ); 1.126 + marshal( obj, new SAXResult(handler) ); 1.127 + } 1.128 + 1.129 + public final void marshal( Object obj, org.w3c.dom.Node node ) 1.130 + throws JAXBException { 1.131 + 1.132 + checkNotNull( obj, "obj", node, "node" ); 1.133 + marshal( obj, new DOMResult(node) ); 1.134 + } 1.135 + 1.136 + /** 1.137 + * By default, the getNode method is unsupported and throw 1.138 + * an {@link java.lang.UnsupportedOperationException}. 1.139 + * 1.140 + * Implementations that choose to support this method must 1.141 + * override this method. 1.142 + */ 1.143 + public org.w3c.dom.Node getNode( Object obj ) throws JAXBException { 1.144 + 1.145 + checkNotNull( obj, "obj", Boolean.TRUE, "foo" ); 1.146 + 1.147 + throw new UnsupportedOperationException(); 1.148 + } 1.149 + 1.150 + /** 1.151 + * Convenience method for getting the current output encoding. 1.152 + * 1.153 + * @return the current encoding or "UTF-8" if it hasn't been set. 1.154 + */ 1.155 + protected String getEncoding() { 1.156 + return encoding; 1.157 + } 1.158 + 1.159 + /** 1.160 + * Convenience method for setting the output encoding. 1.161 + * 1.162 + * @param encoding a valid encoding as specified in the Marshaller class 1.163 + * documentation 1.164 + */ 1.165 + protected void setEncoding( String encoding ) { 1.166 + this.encoding = encoding; 1.167 + } 1.168 + 1.169 + /** 1.170 + * Convenience method for getting the current schemaLocation. 1.171 + * 1.172 + * @return the current schemaLocation or null if it hasn't been set 1.173 + */ 1.174 + protected String getSchemaLocation() { 1.175 + return schemaLocation; 1.176 + } 1.177 + 1.178 + /** 1.179 + * Convenience method for setting the schemaLocation. 1.180 + * 1.181 + * @param location the schemaLocation value 1.182 + */ 1.183 + protected void setSchemaLocation( String location ) { 1.184 + schemaLocation = location; 1.185 + } 1.186 + 1.187 + /** 1.188 + * Convenience method for getting the current noNamespaceSchemaLocation. 1.189 + * 1.190 + * @return the current noNamespaceSchemaLocation or null if it hasn't 1.191 + * been set 1.192 + */ 1.193 + protected String getNoNSSchemaLocation() { 1.194 + return noNSSchemaLocation; 1.195 + } 1.196 + 1.197 + /** 1.198 + * Convenience method for setting the noNamespaceSchemaLocation. 1.199 + * 1.200 + * @param location the noNamespaceSchemaLocation value 1.201 + */ 1.202 + protected void setNoNSSchemaLocation( String location ) { 1.203 + noNSSchemaLocation = location; 1.204 + } 1.205 + 1.206 + /** 1.207 + * Convenience method for getting the formatted output flag. 1.208 + * 1.209 + * @return the current value of the formatted output flag or false if 1.210 + * it hasn't been set. 1.211 + */ 1.212 + protected boolean isFormattedOutput() { 1.213 + return formattedOutput; 1.214 + } 1.215 + 1.216 + /** 1.217 + * Convenience method for setting the formatted output flag. 1.218 + * 1.219 + * @param v value of the formatted output flag. 1.220 + */ 1.221 + protected void setFormattedOutput( boolean v ) { 1.222 + formattedOutput = v; 1.223 + } 1.224 + 1.225 + 1.226 + /** 1.227 + * Convenience method for getting the fragment flag. 1.228 + * 1.229 + * @return the current value of the fragment flag or false if 1.230 + * it hasn't been set. 1.231 + */ 1.232 + protected boolean isFragment() { 1.233 + return fragment; 1.234 + } 1.235 + 1.236 + /** 1.237 + * Convenience method for setting the fragment flag. 1.238 + * 1.239 + * @param v value of the fragment flag. 1.240 + */ 1.241 + protected void setFragment( boolean v ) { 1.242 + fragment = v; 1.243 + } 1.244 + 1.245 + 1.246 + static String[] aliases = { 1.247 + "UTF-8", "UTF8", 1.248 + "UTF-16", "Unicode", 1.249 + "UTF-16BE", "UnicodeBigUnmarked", 1.250 + "UTF-16LE", "UnicodeLittleUnmarked", 1.251 + "US-ASCII", "ASCII", 1.252 + "TIS-620", "TIS620", 1.253 + 1.254 + // taken from the project-X parser 1.255 + "ISO-10646-UCS-2", "Unicode", 1.256 + 1.257 + "EBCDIC-CP-US", "cp037", 1.258 + "EBCDIC-CP-CA", "cp037", 1.259 + "EBCDIC-CP-NL", "cp037", 1.260 + "EBCDIC-CP-WT", "cp037", 1.261 + 1.262 + "EBCDIC-CP-DK", "cp277", 1.263 + "EBCDIC-CP-NO", "cp277", 1.264 + "EBCDIC-CP-FI", "cp278", 1.265 + "EBCDIC-CP-SE", "cp278", 1.266 + 1.267 + "EBCDIC-CP-IT", "cp280", 1.268 + "EBCDIC-CP-ES", "cp284", 1.269 + "EBCDIC-CP-GB", "cp285", 1.270 + "EBCDIC-CP-FR", "cp297", 1.271 + 1.272 + "EBCDIC-CP-AR1", "cp420", 1.273 + "EBCDIC-CP-HE", "cp424", 1.274 + "EBCDIC-CP-BE", "cp500", 1.275 + "EBCDIC-CP-CH", "cp500", 1.276 + 1.277 + "EBCDIC-CP-ROECE", "cp870", 1.278 + "EBCDIC-CP-YU", "cp870", 1.279 + "EBCDIC-CP-IS", "cp871", 1.280 + "EBCDIC-CP-AR2", "cp918", 1.281 + 1.282 + // IANA also defines two that JDK 1.2 doesn't handle: 1.283 + // EBCDIC-CP-GR --> CP423 1.284 + // EBCDIC-CP-TR --> CP905 1.285 + }; 1.286 + 1.287 + /** 1.288 + * Gets the corresponding Java encoding name from an IANA name. 1.289 + * 1.290 + * This method is a helper method for the derived class to convert 1.291 + * encoding names. 1.292 + * 1.293 + * @exception UnsupportedEncodingException 1.294 + * If this implementation couldn't find the Java encoding name. 1.295 + */ 1.296 + protected String getJavaEncoding( String encoding ) throws UnsupportedEncodingException { 1.297 + try { 1.298 + "1".getBytes(encoding); 1.299 + return encoding; 1.300 + } catch( UnsupportedEncodingException e ) { 1.301 + // try known alias 1.302 + for( int i=0; i<aliases.length; i+=2 ) { 1.303 + if(encoding.equals(aliases[i])) { 1.304 + "1".getBytes(aliases[i+1]); 1.305 + return aliases[i+1]; 1.306 + } 1.307 + } 1.308 + 1.309 + throw new UnsupportedEncodingException(encoding); 1.310 + } 1.311 + /* J2SE1.4 feature 1.312 + try { 1.313 + this.encoding = Charset.forName( _encoding ); 1.314 + } catch( UnsupportedCharsetException uce ) { 1.315 + throw new JAXBException( uce ); 1.316 + } 1.317 + */ 1.318 + } 1.319 + 1.320 + /** 1.321 + * Default implementation of the setProperty method handles 1.322 + * the four defined properties in Marshaller. If a provider 1.323 + * needs to handle additional properties, it should override 1.324 + * this method in a derived class. 1.325 + */ 1.326 + public void setProperty( String name, Object value ) 1.327 + throws PropertyException { 1.328 + 1.329 + if( name == null ) { 1.330 + throw new IllegalArgumentException( 1.331 + Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 1.332 + } 1.333 + 1.334 + // recognize and handle four pre-defined properties. 1.335 + if( JAXB_ENCODING.equals(name) ) { 1.336 + checkString( name, value ); 1.337 + setEncoding( (String)value ); 1.338 + return; 1.339 + } 1.340 + if( JAXB_FORMATTED_OUTPUT.equals(name) ) { 1.341 + checkBoolean( name, value ); 1.342 + setFormattedOutput((Boolean) value ); 1.343 + return; 1.344 + } 1.345 + if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) { 1.346 + checkString( name, value ); 1.347 + setNoNSSchemaLocation( (String)value ); 1.348 + return; 1.349 + } 1.350 + if( JAXB_SCHEMA_LOCATION.equals(name) ) { 1.351 + checkString( name, value ); 1.352 + setSchemaLocation( (String)value ); 1.353 + return; 1.354 + } 1.355 + if( JAXB_FRAGMENT.equals(name) ) { 1.356 + checkBoolean(name, value); 1.357 + setFragment((Boolean) value ); 1.358 + return; 1.359 + } 1.360 + 1.361 + throw new PropertyException(name, value); 1.362 + } 1.363 + 1.364 + /** 1.365 + * Default implementation of the getProperty method handles 1.366 + * the four defined properties in Marshaller. If a provider 1.367 + * needs to support additional provider specific properties, 1.368 + * it should override this method in a derived class. 1.369 + */ 1.370 + public Object getProperty( String name ) 1.371 + throws PropertyException { 1.372 + 1.373 + if( name == null ) { 1.374 + throw new IllegalArgumentException( 1.375 + Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 1.376 + } 1.377 + 1.378 + // recognize and handle four pre-defined properties. 1.379 + if( JAXB_ENCODING.equals(name) ) 1.380 + return getEncoding(); 1.381 + if( JAXB_FORMATTED_OUTPUT.equals(name) ) 1.382 + return isFormattedOutput()?Boolean.TRUE:Boolean.FALSE; 1.383 + if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) 1.384 + return getNoNSSchemaLocation(); 1.385 + if( JAXB_SCHEMA_LOCATION.equals(name) ) 1.386 + return getSchemaLocation(); 1.387 + if( JAXB_FRAGMENT.equals(name) ) 1.388 + return isFragment()?Boolean.TRUE:Boolean.FALSE; 1.389 + 1.390 + throw new PropertyException(name); 1.391 + } 1.392 + /** 1.393 + * @see javax.xml.bind.Marshaller#getEventHandler() 1.394 + */ 1.395 + public ValidationEventHandler getEventHandler() throws JAXBException { 1.396 + return eventHandler; 1.397 + } 1.398 + 1.399 + /** 1.400 + * @see javax.xml.bind.Marshaller#setEventHandler(ValidationEventHandler) 1.401 + */ 1.402 + public void setEventHandler(ValidationEventHandler handler) 1.403 + throws JAXBException { 1.404 + 1.405 + if( handler == null ) { 1.406 + eventHandler = new DefaultValidationEventHandler(); 1.407 + } else { 1.408 + eventHandler = handler; 1.409 + } 1.410 + } 1.411 + 1.412 + 1.413 + 1.414 + 1.415 + /* 1.416 + * assert that the given object is a Boolean 1.417 + */ 1.418 + private void checkBoolean( String name, Object value ) throws PropertyException { 1.419 + if(!(value instanceof Boolean)) 1.420 + throw new PropertyException( 1.421 + Messages.format( Messages.MUST_BE_BOOLEAN, name ) ); 1.422 + } 1.423 + 1.424 + /* 1.425 + * assert that the given object is a String 1.426 + */ 1.427 + private void checkString( String name, Object value ) throws PropertyException { 1.428 + if(!(value instanceof String)) 1.429 + throw new PropertyException( 1.430 + Messages.format( Messages.MUST_BE_STRING, name ) ); 1.431 + } 1.432 + 1.433 + /* 1.434 + * assert that the parameters are not null 1.435 + */ 1.436 + private void checkNotNull( Object o1, String o1Name, 1.437 + Object o2, String o2Name ) { 1.438 + 1.439 + if( o1 == null ) { 1.440 + throw new IllegalArgumentException( 1.441 + Messages.format( Messages.MUST_NOT_BE_NULL, o1Name ) ); 1.442 + } 1.443 + if( o2 == null ) { 1.444 + throw new IllegalArgumentException( 1.445 + Messages.format( Messages.MUST_NOT_BE_NULL, o2Name ) ); 1.446 + } 1.447 + } 1.448 + 1.449 + public void marshal(Object obj, XMLEventWriter writer) 1.450 + throws JAXBException { 1.451 + 1.452 + throw new UnsupportedOperationException(); 1.453 + } 1.454 + 1.455 + public void marshal(Object obj, XMLStreamWriter writer) 1.456 + throws JAXBException { 1.457 + 1.458 + throw new UnsupportedOperationException(); 1.459 + } 1.460 + 1.461 + public void setSchema(Schema schema) { 1.462 + throw new UnsupportedOperationException(); 1.463 + } 1.464 + 1.465 + public Schema getSchema() { 1.466 + throw new UnsupportedOperationException(); 1.467 + } 1.468 + 1.469 + public void setAdapter(XmlAdapter adapter) { 1.470 + if(adapter==null) 1.471 + throw new IllegalArgumentException(); 1.472 + setAdapter((Class)adapter.getClass(),adapter); 1.473 + } 1.474 + 1.475 + public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { 1.476 + throw new UnsupportedOperationException(); 1.477 + } 1.478 + 1.479 + public <A extends XmlAdapter> A getAdapter(Class<A> type) { 1.480 + throw new UnsupportedOperationException(); 1.481 + } 1.482 + 1.483 + public void setAttachmentMarshaller(AttachmentMarshaller am) { 1.484 + throw new UnsupportedOperationException(); 1.485 + } 1.486 + 1.487 + public AttachmentMarshaller getAttachmentMarshaller() { 1.488 + throw new UnsupportedOperationException(); 1.489 + } 1.490 + 1.491 + public void setListener(Listener listener) { 1.492 + throw new UnsupportedOperationException(); 1.493 + } 1.494 + 1.495 + public Listener getListener() { 1.496 + throw new UnsupportedOperationException(); 1.497 + } 1.498 +}