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

📄 xmlwriternamespacebase.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2004-2005, Dennis M. Sosnoski.All rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this   list of conditions and the following disclaimer. * 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. * Neither the name of JiBX nor the names of its contributors may be used   to endorse or promote products derived from this software without specific   prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY 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 ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.runtime.impl;import java.io.IOException;import java.util.Stack;import org.jibx.runtime.IXMLWriter;/** * Base implementation of XML writer interface namespace handling. This tracks * only the namespace declarations and the element nesting depth. It can be used * as a base class for all forms of output. * * @author Dennis M. Sosnoski * @version 1.0 */public abstract class XMLWriterNamespaceBase implements IXMLWriter{    /** Empty array for default return. */    private static final int[] EMPTY_INT_ARRAY = new int[0];        /** URIs for namespaces. */    protected String[] m_uris;        /** Prefixes currently defined for namespaces. */    protected String[] m_prefixes;        /** Depth of nested tags. */    private int m_nestingDepth;        /** Stack of information for namespace declarations. */    private Stack m_namespaceStack;        /** Depth of top namespace declaration level. */    private int m_namespaceDepth;        /** Extension namespace URIs (<code>null</code> if not in use). */    private String[][] m_extensionUris;        /** Extension namespace prefixes (<code>null</code> if not in use). */    private String[][] m_extensionPrefixes;        /**     * Constructor.     *     * @param uris ordered array of URIs for namespaces used in document (must     * be constant; the value in position 0 must always be the empty string "",     * and the value in position 1 must always be the XML namespace     * "http://www.w3.org/XML/1998/namespace")     */    public XMLWriterNamespaceBase(String[] uris) {        m_uris = uris;        m_prefixes = new String[uris.length];        m_prefixes[0] = "";        m_prefixes[1] = "xml";        m_namespaceStack = new Stack();        m_namespaceDepth = -1;    }        /**     * Copy constructor. This initializes the extension namespace information     * from an existing instance.     *     * @param base existing instance     * @param uris ordered array of URIs for namespaces used in document     */    public XMLWriterNamespaceBase(XMLWriterNamespaceBase base, String[] uris) {        this(uris);        m_extensionUris = base.m_extensionUris;        m_extensionPrefixes = base.m_extensionPrefixes;        m_nestingDepth = base.m_nestingDepth;    }    /**     * Report to subclass that namespace has been defined.     *     * @param index namespace URI index number     * @param prefix prefix used for namespace     * @throws IOException if error writing to document     */    protected abstract void defineNamespace(int index, String prefix)        throws IOException;        /**     * Report to subclass that namespace has been undefined.     *     * @param index namespace URI index number     */    protected abstract void undefineNamespace(int index);        /**     * Set prefix for namespace.     *     * @param index namespace URI index number     */    private void setNamespacePrefix(int index, String prefix) {        if (index < m_prefixes.length) {            m_prefixes[index] = prefix;        } else if (m_extensionUris != null) {            index -= m_prefixes.length;            for (int i = 0; i < m_extensionUris.length; i++) {                int length = m_extensionUris[i].length;                if (index < length) {                    m_extensionPrefixes[i][index] = prefix;                    break;                } else {                    index -= length;                }            }        }    }        /**     * Open the specified namespaces. Previously active namespace declarations     * are not duplicated.     *     * @param nums array of namespace indexes defined by this element (must     * be constant, reference is kept until end of element)     * @param prefs array of namespace prefixes mapped by this element (no     * <code>null</code> values, use "" for default namespace declaration)     * @return array of indexes for namespaces not previously active (the ones     * actually needing to be declared, in the case of text output)     * @throws IOException on error writing to document     */    public int[] openNamespaces(int[] nums, String[] prefs) throws IOException {                // find the number of namespaces actually being declared        int count = 0;        for (int i = 0; i < nums.length; i++) {                        // set prefix only if different and not changing prefix to default            String newpref = prefs[i];            String oldpref = getNamespacePrefix(nums[i]);            boolean use = false;            if (!newpref.equals(oldpref) &&                (!"".equals(newpref) || oldpref == null)) {                use = true;                for (int j = 0; j < i; j++) {                    if (nums[i] == nums[j]) {                        if (prefs[i] == null) {                            nums[i] = -1;                        } else {                            use = false;                            break;                        }                    }                }            }            if (use) {                count++;            } else {                nums[i] = -1;            }        }                // check if there's actually any change        int[] deltas = EMPTY_INT_ARRAY;        if (count > 0) {                        // get the set of namespace indexes that are changing            String[] priors = new String[count];            if (count == nums.length) {                                // replace the full set, tracking the prior values                deltas = nums;                for (int i = 0; i < count; i++) {                    int slot = deltas[i];                    priors[i] = getNamespacePrefix(slot);                    setNamespacePrefix(slot, prefs[i]);                    defineNamespace(slot, prefs[i]);                }                            } else {                                // replace only changed ones, tracking both indexes and priors                int fill = 0;                deltas = new int[count];                for (int i = 0; i < nums.length; i++) {                    int slot = nums[i];                    if (slot >= 0) {                        deltas[fill] = slot;                        priors[fill++] = getNamespacePrefix(slot);                        setNamespacePrefix(slot, prefs[i]);                        defineNamespace(slot, prefs[i]);                    }                }            }                        // set up for undeclaring namespaces on close of element            m_namespaceStack.push                (new DeclarationInfo(m_nestingDepth, deltas, priors));            m_namespaceDepth = m_nestingDepth;                    }        return deltas;    }        /**     * Ends the current innermost set of nested namespace definitions. Reverts     * the namespaces involved to their previously-declared prefixes, and sets     * up for ending the new innermost set.     */    private void closeNamespaces() {                // revert prefixes for namespaces included in last declaration        DeclarationInfo info = (DeclarationInfo)m_namespaceStack.pop();        int[] deltas = info.m_deltas;        String[] priors = info.m_priors;        for (int i = 0; i < deltas.length; i++) {            int index = deltas[i];            undefineNamespace(index);            if (index < m_prefixes.length) {                m_prefixes[index] = priors[i];            } else if (m_extensionUris != null) {                index -= m_prefixes.length;                for (int j = 0; j < m_extensionUris.length; j++) {                    int length = m_extensionUris[j].length;                    if (index < length) {                        m_extensionPrefixes[j][index] = priors[i];                    } else {                        index -= length;                    }                }            }        }                // set up for clearing next nested set        if (m_namespaceStack.empty()) {

⌨️ 快捷键说明

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