/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Implementation of {@link Marshaller} interface for the JAXB RI.
*
* <p>
* Eventually all the {@link #marshal} methods call into
* the {@link #write} method.
*
* @author Kohsuke Kawaguchi
* @author Vivek Pandey
*/
public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
{
/** Indentation string. Default is four whitespaces. */
/** Used to assign prefixes to namespace URIs. */
/** Object that handles character escaping. */
/** XML BLOB written after the XML declaration. */
/** reference to the context that created this object */
/**
* Non-null if we do the marshal-time validation.
*/
/** Marshaller.Listener */
/** Configured for c14n? */
private boolean c14nSupport;
// while createing XmlOutput those values may be set.
// if these are non-null they need to be cleaned up
/**
* @param assoc
* non-null if the marshaller is working inside {@link BinderImpl}.
*/
context = c;
serializer = new XMLSerializer(this);
try {
setEventHandler(this);
} catch (JAXBException e) {
throw new AssertionError(e); // impossible
}
}
return context;
}
/**
* Marshals to {@link OutputStream} with the given in-scope namespaces
* taken into account.
*
* @since 2.1.5
*/
public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
}
write(obj, XMLStreamWriterOutput.create(writer,context), new StAXPostInitAction(writer,serializer));
}
}
}
/**
* Creates {@link XmlOutput} from the given {@link Result} object.
*/
} else {
}
}
if (result instanceof StreamResult) {
try {
} catch (URISyntaxException use) {
// otherwise assume that it's a file name
}
try {
assert toBeClosed==null;
toBeClosed = fos;
return createWriter(fos);
} catch (IOException e) {
throw new MarshalException(e);
}
}
}
// unsupported parameter type
}
/**
* Creates an appropriate post-init action object.
*/
}
return null;
}
}
/**
* Used by {@link BridgeImpl} to write an arbitrary object as a fragment.
*/
protected final <T> void write(Name rootTagName, JaxBeanInfo<T> bi, T obj, XmlOutput out,Runnable postInitAction) throws JAXBException {
try {
try {
// special case for void
} else { // normal cases
else
}
postwrite();
} catch( SAXException e ) {
throw new MarshalException(e);
} catch (IOException e) {
throw new MarshalException(e);
} catch (XMLStreamException e) {
throw new MarshalException(e);
} finally {
serializer.close();
}
} finally {
cleanUp();
}
}
/**
* All the marshal method invocation eventually comes down to this call.
*/
try {
// send the output to the validator as well
// work around a bug in JAXP validator in Tiger
XMLFilterImpl f = new XMLFilterImpl() {
}
};
public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException {
}
super.endDocument(false);
}
}, out );
}
try {
postwrite();
} catch( SAXException e ) {
throw new MarshalException(e);
} catch (IOException e) {
throw new MarshalException(e);
} catch (XMLStreamException e) {
throw new MarshalException(e);
} finally {
serializer.close();
}
} finally {
cleanUp();
}
}
private void cleanUp() {
if(toBeFlushed!=null)
try {
toBeFlushed.flush();
} catch (IOException e) {
// ignore
}
if(toBeClosed!=null)
try {
toBeClosed.close();
} catch (IOException e) {
// ignore
}
toBeFlushed = null;
toBeClosed = null;
}
// common parts between two write methods.
private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException {
if(prefixMapper!=null) {
// be defensive as we work with the user's code
}
}
}
}
}
//
//
// create XMLWriter by specifing various type of output.
//
//
if( escapeHandler!=null )
// user-specified one takes precedence.
return escapeHandler;
// no need for character reference. Use the handler
// optimized for that pattern.
return MinimumEscapeHandler.theInstance;
// otherwise try to find one from the encoding
try {
// try new JDK1.4 NIO
} catch( Throwable e ) {
// if that fails, fall back to the dumb mode
return DumbEscapeHandler.theInstance;
}
}
// XMLWriter doesn't do buffering, so do it here if it looks like a good idea
if(!(w instanceof BufferedWriter))
w = new BufferedWriter(w);
assert toBeFlushed==null;
toBeFlushed = w;
if(isFormattedOutput()) {
d.setIndentStep(indent);
xw=d;
} else
}
return createWriter(w, getEncoding());
}
}
// UTF8XmlOutput does buffering on its own, and
// otherwise createWriter(Writer) inserts a buffering,
// so no point in doing a buffering here.
final UTF8XmlOutput out;
if(isFormattedOutput())
else {
if(c14nSupport)
else
}
return out;
}
try {
return createWriter(
encoding );
} catch( UnsupportedEncodingException e ) {
throw new MarshalException(
e );
}
}
return indent;
return escapeHandler;
return prefixMapper;
return !isFragment();
return header;
return c14nSupport;
return serializer.getObjectIdentityCycleDetection();
return super.getProperty(name);
}
return;
}
if(!(value instanceof CharacterEscapeHandler))
throw new PropertyException(
name,
CharacterEscapeHandler.class.getName(),
return;
}
if(!(value instanceof NamespacePrefixMapper))
throw new PropertyException(
name,
NamespacePrefixMapper.class.getName(),
return;
}
// com.sun.xml.internal.bind.xmlDeclaration is an alias for JAXB_FRAGMENT
// setting it to false is treated the same as setting fragment to true.
return;
}
return;
}
return;
}
return;
}
}
/*
* assert that the given object is a Boolean
*/
throw new PropertyException(
name,
}
/*
* assert that the given object is a String
*/
throw new PropertyException(
name,
}
throw new IllegalArgumentException();
}
throw new IllegalArgumentException();
// so as not to create a new instance when this method is called
else
return null;
}
}
return serializer.attachmentMarshaller;
}
return schema;
}
this.schema = s;
}
/**
* Default error handling behavior fot {@link Marshaller}.
*/
// draconian by default
return false;
}
return externalListener;
}
}
// features supported
protected static final String ENCODING_HANDLER = "com.sun.xml.internal.bind.characterEscapeHandler";
protected static final String ENCODING_HANDLER2 = "com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler";
protected static final String OBJECT_IDENTITY_CYCLE_DETECTION = "com.sun.xml.internal.bind.objectIdentitityCycleDetection";
}