All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdom2.output.StAXEventOutputter Maven / Gradle / Ivy

Go to download

A complete, Java-based solution for accessing, manipulating, and outputting XML data

There is a newer version: 2.0.2
Show newest version
/*--

 Copyright (C) 2011-2012 Jason Hunter & Brett McLaughlin.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows
    these conditions in the documentation and/or other materials
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact .

 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management .

 In addition, we request (but do not require) that you include in the
 end-user documentation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many
 individuals on behalf of the JDOM Project and was originally
 created by Jason Hunter  and
 Brett McLaughlin .  For more information
 on the JDOM Project, please see .

 */

package org.jdom2.output;

import java.util.List;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.util.XMLEventConsumer;

import org.jdom2.Attribute;
import org.jdom2.CDATA;
import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.EntityRef;
import org.jdom2.ProcessingInstruction;
import org.jdom2.Text;
import org.jdom2.output.support.AbstractStAXEventProcessor;
import org.jdom2.output.support.StAXEventProcessor;
import org.jdom2.output.support.XMLOutputProcessor;

/**
 * Outputs a JDOM document as a StAX XMLEventConsumer of bytes.
 * 

* The StAXStreamOutputter can manage many styles of document formatting, from * untouched to 'pretty' printed. The default is to output the document content * exactly as created, but this can be changed by setting a new Format object: *

    *
  • For pretty-print output, use * {@link Format#getPrettyFormat()}. *
  • For whitespace-normalised output, use * {@link Format#getCompactFormat()}. *
  • For unmodified-format output, use * {@link Format#getRawFormat()}. *
*

* All of the output*(...) methods will flush the * destination XMLEventConsumer before returning, and none of them * will close() the destination. *

* To omit output of the declaration use * {@link Format#setOmitDeclaration}. To omit printing of the * encoding in the declaration use {@link Format#setOmitEncoding}. *

* If changing the {@link Format} settings are insufficient for your output * needs you can customise this StAXStreamOutputter further by setting a different * {@link StAXEventProcessor} with the * {@link #setStAXEventProcessor(StAXEventProcessor)} method or an appropriate * constructor. A fully-enabled Abstract class * {@link AbstractStAXEventProcessor} is available to be further extended to * your needs if all you want to do is tweak some details. * * @since JDOM2 * @author Rolf Lear */ public final class StAXEventOutputter implements Cloneable { /* * ===================================================================== * Static content. * ===================================================================== */ /** * Create a final and static instance of the AbstractStAXEventProcessor The * final part is important because it improves performance. *

* The JDOM user can change the actual XMLOutputProcessor with the * {@link StAXEventOutputter#setStAXEventProcessor(StAXEventProcessor)} method. * * @author rolf */ private static final class DefaultStAXEventProcessor extends AbstractStAXEventProcessor { // nothing } /** * This constant StAXEventProcessor is used for all non-customised * StAXStreamOutputters */ private static final DefaultStAXEventProcessor DEFAULTPROCESSOR = new DefaultStAXEventProcessor(); /** * This constant StAXEventProcessor is used for all non-customised * StAXStreamOutputters */ private static final XMLEventFactory DEFAULTEVENTFACTORY = XMLEventFactory.newInstance(); /* * ===================================================================== * Instance content. * ===================================================================== */ // For normal output private Format myFormat = null; // The actual StAXEventProcessor to delegate to. private StAXEventProcessor myProcessor = null; private XMLEventFactory myEventFactory = null; /* * ===================================================================== * Constructors * ===================================================================== */ /** * This will create an StAXStreamOutputter with the specified format * characteristics. *

* Note: the format object is cloned internally before use. If you * want to modify the Format after constructing the StAXStreamOutputter you can * modify the Format instance {@link #getFormat()} returns. * * @param format * The Format instance to use. This instance will be cloned() and as * a consequence, changes made to the specified format instance * will not be reflected in this StAXStreamOutputter. A null input * format indicates that StAXStreamOutputter should use the default * {@link Format#getRawFormat()} * @param processor * The XMLOutputProcessor to delegate output to. If null the * StAXStreamOutputter will use the default XMLOutputProcessor. * @param eventfactory * The factory to use to create XMLEvent instances. */ public StAXEventOutputter(Format format, StAXEventProcessor processor, XMLEventFactory eventfactory) { myFormat = format == null ? Format.getRawFormat() : format.clone(); myProcessor = processor == null ? DEFAULTPROCESSOR : processor; myEventFactory = eventfactory == null ? DEFAULTEVENTFACTORY : eventfactory; } /** * This will create an StAXStreamOutputter with a default * {@link Format} and {@link XMLOutputProcessor}. */ public StAXEventOutputter() { this(null, null, null); } /** * This will create an StAXStreamOutputter with the specified format * characteristics. *

* Note: the format object is cloned internally before use. * * @param format * The Format instance to use. This instance will be cloned() and as * a consequence, changes made to the specified format instance * will not be reflected in this StAXStreamOutputter. A null input * format indicates that StAXStreamOutputter should use the default * {@link Format#getRawFormat()} */ public StAXEventOutputter(Format format) { this(format, null, null); } /** * This will create an StAXStreamOutputter with the specified * XMLOutputProcessor. * * @param processor * The XMLOutputProcessor to delegate output to. If null the * StAXStreamOutputter will use the default XMLOutputProcessor. */ public StAXEventOutputter(StAXEventProcessor processor) { this(null, processor, null); } /** * This will create an StAXStreamOutputter with the specified * XMLOutputProcessor. * * @param eventfactory * The XMLEventFactory to use to create XMLEvent instances. */ public StAXEventOutputter(XMLEventFactory eventfactory) { this(null, null, eventfactory); } /* * ======================================================================= * API - Settings... * ======================================================================= */ /** * Sets the new format logic for the StAXStreamOutputter. Note the Format object is * cloned internally before use. * * @see #getFormat() * @param newFormat * the format to use for subsequent output */ public void setFormat(Format newFormat) { this.myFormat = newFormat.clone(); } /** * Returns the current format in use by the StAXStreamOutputter. Note the Format * object returned is not a clone of the one used internally, thus, * an StAXStreamOutputter instance is able to have it's Format changed by changing * the settings on the Format instance returned by this method. * * @return the current Format instance used by this StAXStreamOutputter. */ public Format getFormat() { return myFormat; } /** * Returns the current XMLOutputProcessor instance in use by the * StAXStreamOutputter. * * @return the current XMLOutputProcessor instance. */ public StAXEventProcessor getStAXStream() { return myProcessor; } /** * Sets a new XMLOutputProcessor instance for this StAXStreamOutputter. Note the * processor object is expected to be thread-safe. * * @param processor * the new XMLOutputProcesor to use for output */ public void setStAXEventProcessor(StAXEventProcessor processor) { this.myProcessor = processor; } /** * @return the current XMLEventFactory used by this StAXEventOutputter */ public XMLEventFactory getEventFactory() { return myEventFactory; } /** * @param myEventFactory the XMLEventFactory to use for subsequent output. */ public void setEventFactory(XMLEventFactory myEventFactory) { this.myEventFactory = myEventFactory; } /* * ======================================================================== * API - Output to XMLEventConsumer Methods ... These are the core methods that the * Stream and String output methods call. On the other hand, these methods * defer to the protected/override methods. These methods flush the writer. * ======================================================================== */ /** * This will print the Document to the given Writer. *

* Warning: using your own Writer may cause the outputter's preferred * character encoding to be ignored. If you use encodings other than UTF-8, * we recommend using the method that takes an OutputStream instead. *

* * @param doc * Document to format. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(Document doc, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, doc); //out.flush(); } /** * Print out the {@link DocType}. * * @param doctype * DocType to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(DocType doctype, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, doctype); //out.flush(); } /** * Print out an {@link Element}, including its * {@link Attribute}s, and all contained (child) elements, etc. * * @param element * Element to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(Element element, XMLEventConsumer out) throws XMLStreamException { // If this is the root element we could pre-initialize the // namespace stack with the namespaces myProcessor.process(out, myFormat, myEventFactory, element); //out.flush(); } /** * This will handle printing out an {@link * Element}'s content only, not including its tag, and attributes. * This can be useful for printing the content of an element that contains * HTML, like "<description>JDOM is * <b>fun>!</description>". * * @param element * Element to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void outputElementContent(Element element, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, element.getContent()); //out.flush(); } /** * This will handle printing out a list of nodes. This can be useful for * printing the content of an element that contains HTML, like * "<description>JDOM is <b>fun>!</description>". * * @param list * List of nodes. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(List list, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, list); //out.flush(); } /** * Print out a {@link CDATA} node. * * @param cdata * CDATA to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(CDATA cdata, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, cdata); //out.flush(); } /** * Print out a {@link Text} node. Perfoms the necessary entity * escaping and whitespace stripping. * * @param text * Text to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(Text text, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, text); //out.flush(); } /** * Print out a {@link Comment}. * * @param comment * Comment to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(Comment comment, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, comment); //out.flush(); } /** * Print out a {@link ProcessingInstruction}. * * @param pi * ProcessingInstruction to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(ProcessingInstruction pi, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, pi); //out.flush(); } /** * Print out an {@link EntityRef}. * * @param entity * EntityRef to output. * @param out * XMLEventConsumer to use. * @throws XMLStreamException * - if there's any problem writing. * @throws NullPointerException * if the specified content is null. */ public final void output(EntityRef entity, XMLEventConsumer out) throws XMLStreamException { myProcessor.process(out, myFormat, myEventFactory, entity); //out.flush(); } /* * ======================================================================== * Basic Support methods. * ======================================================================== */ /** * Returns a cloned copy of this StAXStreamOutputter. */ @Override public StAXEventOutputter clone() { // Implementation notes: Since all state of an StAXStreamOutputter is // embodied in simple private instance variables, Object.clone // can be used. Note that since Object.clone is totally // broken, we must catch an exception that will never be // thrown. try { return (StAXEventOutputter) super.clone(); } catch (java.lang.CloneNotSupportedException e) { // even though this should never ever happen, it's still // possible to fool Java into throwing a // CloneNotSupportedException. If that happens, we // shouldn't swallow it. throw new RuntimeException(e.toString()); } } /** * Return a string listing of the settings for this StAXStreamOutputter instance. * * @return a string listing the settings for this StAXStreamOutputter instance */ @Override public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append("StAXStreamOutputter[omitDeclaration = "); buffer.append(myFormat.omitDeclaration); buffer.append(", "); buffer.append("encoding = "); buffer.append(myFormat.encoding); buffer.append(", "); buffer.append("omitEncoding = "); buffer.append(myFormat.omitEncoding); buffer.append(", "); buffer.append("indent = '"); buffer.append(myFormat.indent); buffer.append("'"); buffer.append(", "); buffer.append("expandEmptyElements = "); buffer.append(myFormat.expandEmptyElements); buffer.append(", "); buffer.append("lineSeparator = '"); for (char ch : myFormat.lineSeparator.toCharArray()) { switch (ch) { case '\r': buffer.append("\\r"); break; case '\n': buffer.append("\\n"); break; case '\t': buffer.append("\\t"); break; default: buffer.append("[" + ((int) ch) + "]"); break; } } buffer.append("', "); buffer.append("textMode = "); buffer.append(myFormat.mode + "]"); return buffer.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy