⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 staxeventwriter.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param ns
     *            The {@link Namespace}from which to construct the event.
     * 
     * @return The constructed {@link javax.xml.stream.events.Namespace}event.
     */
    public javax.xml.stream.events.Namespace createNamespace(Namespace ns) {
        String prefix = ns.getPrefix();
        String uri = ns.getURI();

        return factory.createNamespace(prefix, uri);
    }

    /**
     * Writes a DOM4J {@link Text}to the stream.
     * 
     * @param text
     *            The {@link Text}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeText(Text text) throws XMLStreamException {
        consumer.add(createCharacters(text));
    }

    /**
     * Constructs a STAX {@link Characters}event from a DOM4J {@link Text}.
     * 
     * @param text
     *            The {@link Text}from which to construct the event.
     * 
     * @return The constructed {@link Characters}event.
     */
    public Characters createCharacters(Text text) {
        return factory.createCharacters(text.getText());
    }

    /**
     * Writes a DOM4J {@link CDATA}to the event stream.
     * 
     * @param cdata
     *            The {@link CDATA}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeCDATA(CDATA cdata) throws XMLStreamException {
        consumer.add(createCharacters(cdata));
    }

    /**
     * Constructs a STAX {@link Characters}event from a DOM4J {@link CDATA}.
     * 
     * @param cdata
     *            The {@link CDATA}from which to construct the event.
     * 
     * @return The newly constructed {@link Characters}event.
     */
    public Characters createCharacters(CDATA cdata) {
        return factory.createCData(cdata.getText());
    }

    /**
     * Writes a DOM4J {@link Comment}to the stream.
     * 
     * @param comment
     *            The {@link Comment}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeComment(Comment comment) throws XMLStreamException {
        consumer.add(createComment(comment));
    }

    /**
     * Constructs a STAX {@link javax.xml.stream.events.Comment}event from a
     * DOM4J {@link Comment}.
     * 
     * @param comment
     *            The {@link Comment}from which to construct the event.
     * 
     * @return The constructed {@link javax.xml.stream.events.Comment}event.
     */
    public javax.xml.stream.events.Comment createComment(Comment comment) {
        return factory.createComment(comment.getText());
    }

    /**
     * Writes a DOM4J {@link ProcessingInstruction}to the stream.
     * 
     * @param pi
     *            The {@link ProcessingInstruction}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeProcessingInstruction(org.dom4j.ProcessingInstruction pi)
            throws XMLStreamException {
        consumer.add(createProcessingInstruction(pi));
    }

    /**
     * Constructs a STAX {@link javax.xml.stream.events.ProcessingInstruction}
     * event from a DOM4J {@link ProcessingInstruction}.
     * 
     * @param pi
     *            The {@link ProcessingInstruction}from which to construct the
     *            event.
     * 
     * @return The constructed {@link
     *         javax.xml.stream.events.ProcessingInstruction} event.
     */
    public ProcessingInstruction createProcessingInstruction(
            org.dom4j.ProcessingInstruction pi) {
        String target = pi.getTarget();
        String data = pi.getText();

        return factory.createProcessingInstruction(target, data);
    }

    /**
     * Writes a DOM4J {@link Entity}to the stream.
     * 
     * @param entity
     *            The {@link Entity}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeEntity(Entity entity) throws XMLStreamException {
        consumer.add(createEntityReference(entity));
    }

    /**
     * Constructs a STAX {@link EntityReference}event from a DOM4J {@link
     * Entity}.
     * 
     * @param entity
     *            The {@link Entity}from which to construct the event.
     * 
     * @return The constructed {@link EntityReference}event.
     */
    private EntityReference createEntityReference(Entity entity) {
        return factory.createEntityReference(entity.getName(), null);
    }

    /**
     * Writes a DOM4J {@link DocumentType}to the stream.
     * 
     * @param docType
     *            The {@link DocumentType}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeDocumentType(DocumentType docType)
            throws XMLStreamException {
        consumer.add(createDTD(docType));
    }

    /**
     * Constructs a STAX {@link DTD}event from a DOM4J {@link DocumentType}.
     * 
     * @param docType
     *            The {@link DocumentType}from which to construct the event.
     * 
     * @return The constructed {@link DTD}event.
     * 
     * @throws RuntimeException
     *             DOCUMENT ME!
     */
    public DTD createDTD(DocumentType docType) {
        StringWriter decl = new StringWriter();

        try {
            docType.write(decl);
        } catch (IOException e) {
            throw new RuntimeException("Error writing DTD", e);
        }

        return factory.createDTD(decl.toString());
    }

    /**
     * Writes a DOM4J {@link Document}node, and all its contents, to the
     * stream.
     * 
     * @param doc
     *            The {@link Document}to write to the stream.
     * 
     * @throws XMLStreamException
     *             If an error occurs writing to the stream.
     */
    public void writeDocument(Document doc) throws XMLStreamException {
        consumer.add(createStartDocument(doc));

        writeChildNodes(doc);

        consumer.add(createEndDocument(doc));
    }

    /**
     * Constructs a STAX {@link StartDocument}event from a DOM4J {@link
     * Document}.
     * 
     * @param doc
     *            The {@link Document}from which to construct the event.
     * 
     * @return The constructed {@link StartDocument}event.
     */
    public StartDocument createStartDocument(Document doc) {
        String encoding = doc.getXMLEncoding();

        if (encoding != null) {
            return factory.createStartDocument(encoding);
        } else {
            return factory.createStartDocument();
        }
    }

    /**
     * Constructs a STAX {@link EndDocument}event from a DOM4J {@link
     * Document}.
     * 
     * @param doc
     *            The {@link Document}from which to construct the event.
     * 
     * @return The constructed {@link EndDocument}event.
     */
    public EndDocument createEndDocument(Document doc) {
        return factory.createEndDocument();
    }

    /**
     * Constructs a STAX {@link QName}from a DOM4J {@link org.dom4j.QName}.
     * 
     * @param qname
     *            The {@link org.dom4j.QName}from which to construct the STAX
     *            {@link QName}.
     * 
     * @return The constructed {@link QName}.
     */
    public QName createQName(org.dom4j.QName qname) {
        return new QName(qname.getNamespaceURI(), qname.getName(), qname
                .getNamespacePrefix());
    }

    /**
     * Internal {@link Iterator}implementation used to pass DOM4J {@link
     * Attribute}s to the stream.
     */
    private class AttributeIterator implements Iterator {
        /** The underlying DOm4J attribute iterator. */
        private Iterator iter;

        public AttributeIterator(Iterator iter) {
            this.iter = iter;
        }

        public boolean hasNext() {
            return iter.hasNext();
        }

        public Object next() {
            Attribute attr = (Attribute) iter.next();
            QName attrName = createQName(attr.getQName());
            String value = attr.getValue();

            return factory.createAttribute(attrName, value);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    /**
     * Internal {@link Iterator}implementation used to pass DOM4J {@link
     * Namespace}s to the stream.
     */
    private class NamespaceIterator implements Iterator {
        private Iterator iter;

        public NamespaceIterator(Iterator iter) {
            this.iter = iter;
        }

        public boolean hasNext() {
            return iter.hasNext();
        }

        public Object next() {
            Namespace ns = (Namespace) iter.next();
            String prefix = ns.getPrefix();
            String nsURI = ns.getURI();

            return factory.createNamespace(prefix, nsURI);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}

/*
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * 1. Redistributions of source code must retain copyright statements and
 * notices. Redistributions must also contain a copy of this document.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name "DOM4J" must not be used to endorse or promote products derived
 * from this Software without prior written permission of MetaStuff, Ltd. For
 * written permission, please contact dom4j-info@metastuff.com.
 * 
 * 4. Products derived from this Software may not be called "DOM4J" nor may
 * "DOM4J" appear in their names without prior written permission of MetaStuff,
 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
 * 
 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
 * 
 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``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 METASTUFF, LTD. OR ITS 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.
 * 
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -