001: /* $Id: StAXResult.java,v 1.2 2004/06/24 18:04:57 ryan_shoemaker Exp $
002: *
003: * Copyright (c) 2004, Sun Microsystems, Inc.
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials provided
016: * with the distribution.
017: *
018: * * Neither the name of Sun Microsystems, Inc. nor the names of its
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
023: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
024: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
025: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
026: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
027: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
028: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
029: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
030: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
032: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033: */
034: package javanet.staxutils;
035:
036: import javax.xml.stream.XMLEventWriter;
037: import javax.xml.stream.XMLStreamWriter;
038: import javax.xml.transform.sax.SAXResult;
039:
040: /**
041: * A JAXP {@link javax.xml.transform.Result} implementation that produces
042: * a result on the specified {@link javax.xml.stream.XMLStreamWriter} or
043: * {@link javax.xml.stream.XMLEventWriter}.
044: *
045: * <p>
046: * Please note that you may need to call flush() on the underlying
047: * XMLStreamWriter or XMLEventWriter after the transform is complete.
048: * <p>
049: *
050: * The fact that JAXBResult derives from SAXResult is an implementation
051: * detail. Thus in general applications are strongly discouraged from
052: * accessing methods defined on SAXResult.
053: *
054: * <p>
055: * In particular it shall never attempt to call the following methods:
056: *
057: * <ul>
058: * <li>setHandler</li>
059: * <li>setLexicalHandler</li>
060: * <li>setSystemId</li>
061: * </ul>
062: *
063: * <p>
064: * Example:
065: *
066: * <pre>
067: // create a DOMSource
068: Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(...);
069: Source domSource = new DOMSource(doc);
070:
071: // create a StAXResult
072: XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
073: Result staxResult = new StAXResult(writer);
074:
075: // run the transform
076: TransformerFactory.newInstance().newTransformer().transform(domSource, staxResult);
077: * </pre>
078: *
079: * @author Ryan.Shoemaker@Sun.COM
080: * @version 1.0
081: */
082: public class StAXResult extends SAXResult {
083:
084: /**
085: * Create a new {@link javax.xml.transform.Result} that produces
086: * a result on the specified {@link javax.xml.stream.XMLStreamWriter}
087: *
088: * @param writer the XMLStreamWriter
089: * @throws IllegalArgumentException iff the writer is null
090: */
091: public StAXResult(XMLStreamWriter writer) {
092: if (writer == null) {
093: throw new IllegalArgumentException();
094: }
095:
096: super .setHandler(new ContentHandlerToXMLStreamWriter(writer));
097: }
098:
099: /**
100: * Create a new {@link javax.xml.transform.Result} that produces
101: * a result on the specified {@link javax.xml.stream.XMLEventWriter}
102: *
103: * @param writer the XMLEventWriter
104: * @throws IllegalArgumentException iff the writer is null
105: */
106: public StAXResult(XMLEventWriter writer) {
107: if (writer == null) {
108: throw new IllegalArgumentException();
109: }
110:
111: super .setHandler(new ContentHandlerToXMLEventWriter(writer));
112: }
113: }
|