📄 staxbuilder.java
字号:
/*-- Copyright (C) 2000-2004 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 <request_AT_jdom_DOT_org>. 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 <request_AT_jdom_DOT_org>. 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 <jhunter_AT_jdom_DOT_org> and Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information on the JDOM Project, please see <http://www.jdom.org/>. */package org.codehaus.xfire.util.jdom;import java.io.InputStream;import java.io.Reader;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import org.codehaus.xfire.util.STAXUtils;import org.jdom.Attribute;import org.jdom.Content;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMFactory;import org.jdom.Namespace;import org.jdom.UncheckedJDOMFactory;/** * Builds a JDOM {@link org.jdom.Document org.jdom.Document} using a * {@link javax.xml.stream.XMLStreamReader}. * * @version $Revision: 1198 $, $Date: 2006-02-15 21:21:25 +0100 (Wed, 15 Feb 2006) $ * @author Tatu Saloranta * @author Bradley S. Huffman */public class StaxBuilder{ /** * Map that contains conversion from textual attribute types StAX uses, to * int values JDOM uses. */ final static HashMap attrTypes = new HashMap(32); static { attrTypes.put("CDATA", new Integer(Attribute.CDATA_TYPE)); attrTypes.put("cdata", new Integer(Attribute.CDATA_TYPE)); attrTypes.put("ID", new Integer(Attribute.ID_TYPE)); attrTypes.put("id", new Integer(Attribute.ID_TYPE)); attrTypes.put("IDREF", new Integer(Attribute.IDREF_TYPE)); attrTypes.put("idref", new Integer(Attribute.IDREF_TYPE)); attrTypes.put("IDREFS", new Integer(Attribute.IDREFS_TYPE)); attrTypes.put("idrefs", new Integer(Attribute.IDREFS_TYPE)); attrTypes.put("ENTITY", new Integer(Attribute.ENTITY_TYPE)); attrTypes.put("entity", new Integer(Attribute.ENTITY_TYPE)); attrTypes.put("ENTITIES", new Integer(Attribute.ENTITIES_TYPE)); attrTypes.put("entities", new Integer(Attribute.ENTITIES_TYPE)); attrTypes.put("NMTOKEN", new Integer(Attribute.NMTOKEN_TYPE)); attrTypes.put("nmtoken", new Integer(Attribute.NMTOKEN_TYPE)); attrTypes.put("NMTOKENS", new Integer(Attribute.NMTOKENS_TYPE)); attrTypes.put("nmtokens", new Integer(Attribute.NMTOKENS_TYPE)); attrTypes.put("NOTATION", new Integer(Attribute.NOTATION_TYPE)); attrTypes.put("notation", new Integer(Attribute.NOTATION_TYPE)); attrTypes.put("ENUMERATED", new Integer(Attribute.ENUMERATED_TYPE)); attrTypes.put("enumerated", new Integer(Attribute.ENUMERATED_TYPE)); } /** The factory for creating new JDOM objects */ private JDOMFactory factory = null; private XMLInputFactory xifactory; /** * Whether ignorable white space should be ignored, ie not added in the * resulting JDOM tree. If true, it will be ignored; if false, it will be * added in the tree. Default value if false. */ protected boolean cfgIgnoreWS = false; private Map additionalNamespaces = null; public Map getAdditionalNamespaces() { return additionalNamespaces; } public void setAdditionalNamespaces(Map additionalNamespaces) { this.additionalNamespaces = additionalNamespaces; } /** * Default constructor. */ public StaxBuilder() { xifactory = STAXUtils.getXMLInputFactory(null); } public StaxBuilder(Map namespaces) { xifactory = STAXUtils.getXMLInputFactory(null); this.additionalNamespaces = namespaces; } public StaxBuilder(XMLInputFactory xifactory) { this.xifactory = xifactory; } /* * This sets a custom JDOMFactory for the builder. Use this to build the * tree with your own subclasses of the JDOM classes. * * @param factory <code>JDOMFactory</code> to use */ public void setFactory(JDOMFactory f) { factory = f; } public void setIgnoreWhitespace(boolean state) { cfgIgnoreWS = state; } /** * Returns the current {@link org.jdom.JDOMFactory} in use, if one has been * previously set with {@link #setFactory}, otherwise null. * * @return the factory builder will use */ public JDOMFactory getFactory() { return factory; } /** * This will build a JDOM tree given a StAX stream reader. * * @param r * Stream reader from which input is read. * @return <code>Document</code> - JDOM document object. * @throws XMLStreamException * If the reader threw such exception (to indicate a parsing or * I/O problem) */ public Document build(XMLStreamReader r) throws XMLStreamException { /* * Should we do sanity checking to see that r is positioned at * beginning? Not doing so will allow creating documents from sub-trees, * though? */ JDOMFactory f = factory; if (f == null) { f = new UncheckedJDOMFactory(); } Document doc = f.document(null); buildTree(f, r, doc); return doc; } public Document build(InputStream is) throws XMLStreamException { return build(xifactory.createXMLStreamReader(is)); } public Document build(Reader reader) throws XMLStreamException { return build(xifactory.createXMLStreamReader(reader)); } /** * This takes a <code>XMLStreamReader</code> and builds up a JDOM tree. * Recursion has been eliminated by using local stack of open elements; this * improves performance somewhat (classic * recursion-by-iteration-and-explicit stack transformation) * * @param node * <code>Code</node> to examine. * @param doc JDOM <code>Document</code> being built. */ private void buildTree(JDOMFactory f, XMLStreamReader r, Document doc) throws XMLStreamException { Element current = null; // At top level int event = r.getEventType();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -