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

📄 saxwriter.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setXMLReader(XMLReader xmlReader) {
        setContentHandler(xmlReader.getContentHandler());
        setDTDHandler(xmlReader.getDTDHandler());
        setEntityResolver(xmlReader.getEntityResolver());
        setErrorHandler(xmlReader.getErrorHandler());
    }

    /**
     * Looks up the value of a feature.
     * 
     * @param name
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     * 
     * @throws SAXNotRecognizedException
     *             DOCUMENT ME!
     * @throws SAXNotSupportedException
     *             DOCUMENT ME!
     */
    public boolean getFeature(String name) throws SAXNotRecognizedException,
            SAXNotSupportedException {
        Boolean answer = (Boolean) features.get(name);

        return (answer != null) && answer.booleanValue();
    }

    /**
     * This implementation does actually use any features but just stores them
     * for later retrieval
     * 
     * @param name
     *            DOCUMENT ME!
     * @param value
     *            DOCUMENT ME!
     * 
     * @throws SAXNotRecognizedException
     *             DOCUMENT ME!
     * @throws SAXNotSupportedException
     *             DOCUMENT ME!
     */
    public void setFeature(String name, boolean value)
            throws SAXNotRecognizedException, SAXNotSupportedException {
        if (FEATURE_NAMESPACE_PREFIXES.equals(name)) {
            setDeclareNamespaceAttributes(value);
        } else if (FEATURE_NAMESPACE_PREFIXES.equals(name)) {
            if (!value) {
                String msg = "Namespace feature is always supported in dom4j";
                throw new SAXNotSupportedException(msg);
            }
        }

        features.put(name, (value) ? Boolean.TRUE : Boolean.FALSE);
    }

    /**
     * Sets the given SAX property
     * 
     * @param name
     *            DOCUMENT ME!
     * @param value
     *            DOCUMENT ME!
     */
    public void setProperty(String name, Object value) {
        for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
            if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
                setLexicalHandler((LexicalHandler) value);

                return;
            }
        }

        properties.put(name, value);
    }

    /**
     * Gets the given SAX property
     * 
     * @param name
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     * 
     * @throws SAXNotRecognizedException
     *             DOCUMENT ME!
     * @throws SAXNotSupportedException
     *             DOCUMENT ME!
     */
    public Object getProperty(String name) throws SAXNotRecognizedException,
            SAXNotSupportedException {
        for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
            if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
                return getLexicalHandler();
            }
        }

        return properties.get(name);
    }

    /**
     * This method is not supported.
     * 
     * @param systemId
     *            DOCUMENT ME!
     * 
     * @throws SAXNotSupportedException
     *             DOCUMENT ME!
     */
    public void parse(String systemId) throws SAXNotSupportedException {
        throw new SAXNotSupportedException("This XMLReader can only accept"
                + " <dom4j> InputSource objects");
    }

    /**
     * Parses an XML document. This method can only accept DocumentInputSource
     * inputs otherwise a {@link SAXNotSupportedException}exception is thrown.
     * 
     * @param input
     *            DOCUMENT ME!
     * 
     * @throws SAXException
     *             DOCUMENT ME!
     * @throws SAXNotSupportedException
     *             if the input source is not wrapping a dom4j document
     */
    public void parse(InputSource input) throws SAXException {
        if (input instanceof DocumentInputSource) {
            DocumentInputSource documentInput = (DocumentInputSource) input;
            Document document = documentInput.getDocument();
            write(document);
        } else {
            throw new SAXNotSupportedException(
                    "This XMLReader can only accept "
                            + "<dom4j> InputSource objects");
        }
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected void writeContent(Branch branch, NamespaceStack namespaceStack)
            throws SAXException {
        for (Iterator iter = branch.nodeIterator(); iter.hasNext();) {
            Object object = iter.next();

            if (object instanceof Element) {
                write((Element) object, namespaceStack);
            } else if (object instanceof CharacterData) {
                if (object instanceof Text) {
                    Text text = (Text) object;
                    write(text.getText());
                } else if (object instanceof CDATA) {
                    write((CDATA) object);
                } else if (object instanceof Comment) {
                    write((Comment) object);
                } else {
                    throw new SAXException("Invalid Node in DOM4J content: "
                            + object + " of type: " + object.getClass());
                }
            } else if (object instanceof String) {
                write((String) object);
            } else if (object instanceof Entity) {
                write((Entity) object);
            } else if (object instanceof ProcessingInstruction) {
                write((ProcessingInstruction) object);
            } else if (object instanceof Namespace) {
                write((Namespace) object);
            } else {
                throw new SAXException("Invalid Node in DOM4J content: "
                        + object);
            }
        }
    }

    /**
     * The {@link org.xml.sax.Locator}is only really useful when parsing a
     * textual document as its main purpose is to identify the line and column
     * number. Since we are processing an in memory tree which will probably
     * have its line number information removed, we'll just use -1 for the line
     * and column numbers.
     * 
     * @param document
     *            DOCUMENT ME!
     * 
     * @throws SAXException
     *             DOCUMENT ME!
     */
    protected void documentLocator(Document document) throws SAXException {
        LocatorImpl locator = new LocatorImpl();

        String publicID = null;
        String systemID = null;
        DocumentType docType = document.getDocType();

        if (docType != null) {
            publicID = docType.getPublicID();
            systemID = docType.getSystemID();
        }

        if (publicID != null) {
            locator.setPublicId(publicID);
        }

        if (systemID != null) {
            locator.setSystemId(systemID);
        }

        locator.setLineNumber(-1);
        locator.setColumnNumber(-1);

        contentHandler.setDocumentLocator(locator);
    }

    protected void entityResolver(Document document) throws SAXException {
        if (entityResolver != null) {
            DocumentType docType = document.getDocType();

            if (docType != null) {
                String publicID = docType.getPublicID();
                String systemID = docType.getSystemID();

                if ((publicID != null) || (systemID != null)) {
                    try {
                        entityResolver.resolveEntity(publicID, systemID);
                    } catch (IOException e) {
                        throw new SAXException("Could not resolve publicID: "
                                + publicID + " systemID: " + systemID, e);
                    }
                }
            }
        }
    }

    /**
     * We do not yet support DTD or XML Schemas so this method does nothing
     * right now.
     * 
     * @param document
     *            DOCUMENT ME!
     * 
     * @throws SAXException
     *             DOCUMENT ME!
     */
    protected void dtdHandler(Document document) throws SAXException {
    }

    protected void startDocument() throws SAXException {
        contentHandler.startDocument();
    }

    protected void endDocument() throws SAXException {
        contentHandler.endDocument();
    }

    protected void write(Element element, NamespaceStack namespaceStack)
            throws SAXException {
        int stackSize = namespaceStack.size();
        AttributesImpl namespaceAttributes = startPrefixMapping(element,
                namespaceStack);
        startElement(element, namespaceAttributes);
        writeContent(element, namespaceStack);
        endElement(element);
        endPrefixMapping(namespaceStack, stackSize);
    }

    /**
     * Fires a SAX startPrefixMapping event for all the namespaceStack which
     * have just come into scope
     * 
     * @param element
     *            DOCUMENT ME!
     * @param namespaceStack
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     * 
     * @throws SAXException
     *             DOCUMENT ME!
     */
    protected AttributesImpl startPrefixMapping(Element element,
            NamespaceStack namespaceStack) throws SAXException {
        AttributesImpl namespaceAttributes = null;

        // start with the namespace of the element
        Namespace elementNamespace = element.getNamespace();

        if ((elementNamespace != null)
                && !isIgnoreableNamespace(elementNamespace, namespaceStack)) {
            namespaceStack.push(elementNamespace);
            contentHandler.startPrefixMapping(elementNamespace.getPrefix(),
                    elementNamespace.getURI());
            namespaceAttributes = addNamespaceAttribute(namespaceAttributes,
                    elementNamespace);
        }

        List declaredNamespaces = element.declaredNamespaces();

        for (int i = 0, size = declaredNamespaces.size(); i < size; i++) {
            Namespace namespace = (Namespace) declaredNamespaces.get(i);

            if (!isIgnoreableNamespace(namespace, namespaceStack)) {
                namespaceStack.push(namespace);
                contentHandler.startPrefixMapping(namespace.getPrefix(),
                        namespace.getURI());
                namespaceAttributes = addNamespaceAttribute(
                        namespaceAttributes, namespace);
            }
        }

        return namespaceAttributes;
    }

    /**
     * Fires a SAX endPrefixMapping event for all the namespaceStack which have
     * gone out of scope
     * 
     * @param stack
     *            DOCUMENT ME!
     * @param stackSize
     *            DOCUMENT ME!
     * 
     * @throws SAXException
     *             DOCUMENT ME!
     */
    protected void endPrefixMapping(NamespaceStack stack, int stackSize)
            throws SAXException {
        while (stack.size() > stackSize) {
            Namespace namespace = stack.pop();

            if (namespace != null) {
                contentHandler.endPrefixMapping(namespace.getPrefix());
            }
        }
    }

    protected void startElement(Element element,
            AttributesImpl namespaceAttributes) throws SAXException {
        contentHandler.startElement(element.getNamespaceURI(), element
                .getName(), element.getQualifiedName(), createAttributes(
                element, namespaceAttributes));
    }

    protected void endElement(Element element) throws SAXException {
        contentHandler.endElement(element.getNamespaceURI(), element.getName(),
                element.getQualifiedName());
    }

    protected Attributes createAttributes(Element element,
            Attributes namespaceAttributes) throws SAXException {
        attributes.clear();

        if (namespaceAttributes != null) {
            attributes.setAttributes(namespaceAttributes);
        }

        for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
            Attribute attribute = (Attribute) iter.next();
            attributes.addAttribute(attribute.getNamespaceURI(), attribute
                    .getName(), attribute.getQualifiedName(), "CDATA",
                    attribute.getValue());
        }

        return attributes;
    }

    /**
     * If isDelcareNamespaceAttributes() is enabled then this method will add
     * the given namespace declaration to the supplied attributes object,
     * creating one if it does not exist.
     * 
     * @param attrs
     *            DOCUMENT ME!
     * @param namespace
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     */
    protected AttributesImpl addNamespaceAttribute(AttributesImpl attrs,
            Namespace namespace) {
        if (declareNamespaceAttributes) {
            if (attrs == null) {
                attrs = new AttributesImpl();
            }

            String prefix = namespace.getPrefix();
            String qualifiedName = "xmlns";

            if ((prefix != null) && (prefix.length() > 0)) {
                qualifiedName = "xmlns:" + prefix;
            }

            String uri = "";
            String localName = prefix;
            String type = "CDATA";
            String value = namespace.getURI();

            attrs.addAttribute(uri, localName, qualifiedName, type, value);
        }

        return attrs;
    }

    /**
     * DOCUMENT ME!
     * 
     * @param namespace
     *            DOCUMENT ME!
     * @param namespaceStack
     *            DOCUMENT ME!
     * 
     * @return true if the given namespace is an ignorable namespace (such as
     *         Namespace.NO_NAMESPACE or Namespace.XML_NAMESPACE) or if the
     *         namespace has already been declared in the current scope
     */
    protected boolean isIgnoreableNamespace(Namespace namespace,
            NamespaceStack namespaceStack) {
        if (namespace.equals(Namespace.NO_NAMESPACE)
                || namespace.equals(Namespace.XML_NAMESPACE)) {
            return true;
        }

        String uri = namespace.getURI();

        if ((uri == null) || (uri.length() <= 0)) {
            return true;
        }

        return namespaceStack.contains(namespace);
    }

    /**
     * Ensures non-null content handlers?
     */
    protected void checkForNullHandlers() {
    }
}

/*
 * 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 + -