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

📄 definitioncontext.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2003-2006, Dennis M. SosnoskiAll 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.binding.def;import java.util.ArrayList;import java.util.HashMap;import org.jibx.binding.classes.ClassFile;import org.jibx.binding.classes.MethodBuilder;import org.jibx.binding.util.ArrayMap;import org.jibx.runtime.JiBXException;import org.jibx.runtime.QName;/** * Nesting level for definitions in binding. This tracks namespace and mapping * definitions that apply to all enclosed items. * * @author Dennis M. Sosnoski * @version 1.0 */public class DefinitionContext{    /** Containing binding definition component. */    private final IContainer m_container;    /** Containing definition context. */    private final DefinitionContext m_context;    /** Namespace used by default at this level for attributes. */    private NamespaceDefinition m_attributeDefault;    /** Namespace used by default at this level for elements. */    private NamespaceDefinition m_elementDefault;    /** Namespaces defined at level (lazy create). */    private ArrayList m_namespaces;    /** Mapping from prefix to namespace definition (lazy create). */    private HashMap m_prefixMap;    /** Mapping from URI to namespace definition (lazy create). */    private HashMap m_uriMap;    /** Mapping from fully qualified class name to mapping index (lazy     create). */    private ArrayMap m_classMap;    /** Class mappings defined at level (lazy create). */    private ArrayList m_mappings;        /** Map from signatures to <code>String</code> conversions. */    private HashMap m_convertMap;        /** Map from format qnames to <code>String</code> conversions. */    private HashMap m_formatMap;        /** Named binding components (only for root context of a binding). */    private HashMap m_namedStructureMap;    /**     * Constructor. Uses the containing context to establish the hierarchy for     * resolving namespaces and class mappings.     *     * @param contain containing binding definition component     */    public DefinitionContext(IContainer contain) {        m_container = contain;        m_context = contain.getDefinitionContext();        // TODO: make these lazy        m_convertMap = new HashMap();        m_formatMap = new HashMap();        if (m_context == null) {            m_namedStructureMap = new HashMap();        }    }    /**     * Check for duplicate or conflicting namespace. This also intializes the     * namespace structures for this context the first time the method is     * called.     *     * @param def     * @return duplicate flag (either complete duplicate, or prior definition     * of same URI with prefix is present)     * @throws JiBXException on conflicting prefix     */    private boolean checkDuplicateNamespace(NamespaceDefinition def)        throws JiBXException {                // create structures if not already done        if (m_namespaces == null) {            m_namespaces = new ArrayList();            m_prefixMap = new HashMap();            m_uriMap = new HashMap();        }                // check for conflict (or duplicate) on prefix        String uri = def.getUri();        String prefix = def.getPrefix();        NamespaceDefinition dup = (NamespaceDefinition)m_prefixMap.get(prefix);        DefinitionContext ctx = this;        while (dup == null && (ctx = ctx.m_context) != null) {            if (ctx.m_prefixMap != null) {                dup = (NamespaceDefinition)ctx.m_prefixMap.get(prefix);            }        }        if (dup == null) {                        // check for duplicate definition of same URI, but with prefix            NamespaceDefinition prior = (NamespaceDefinition)m_uriMap.get(uri);            if (prior != null && prior.getPrefix() != null) {                return true;            } else {                return false;            }                    } else {                        // check for repeated definition of same namespace            if (uri.equals(dup.getUri())) {                return true;            } else {                throw new JiBXException("Namespace prefix conflict");            }        }    }    /**     * Add namespace to internal tables.     *     * @param def     */    private void internalAddNamespace(NamespaceDefinition def) {        String uri = def.getUri();        String prefix = def.getPrefix();        def.setIndex(m_container.getBindingRoot().            getNamespaceUriIndex(uri, prefix));        m_namespaces.add(def);        m_prefixMap.put(prefix, def);        m_uriMap.put(uri, def);    }    /**     * Add namespace to set defined at this level. If the new namespace     * conflicts with an existing namespace at this level (in terms of default     * usage or prefix) this throws an exception.     *     * @param def namespace definition to be added (duplicates ignored)     * @throws JiBXException on namespace definition conflict     */    public void addNamespace(NamespaceDefinition def) throws JiBXException {        if (!checkDuplicateNamespace(def)) {            // check for conflict as default for attributes            if (def.isAttributeDefault()) {                if (m_attributeDefault == null) {                    m_attributeDefault = def;                } else {                    throw new JiBXException                        ("Multiple default attribute namespaces at level");                }            }            // check for conflict as default for elements            if (def.isElementDefault()) {                if (m_elementDefault == null) {                    m_elementDefault = def;                } else {                    throw new JiBXException                        ("Multiple default element namespaces at level");                }            }                        // no conflicts, add it            internalAddNamespace(def);        }    }    /**     * Add namespace declaration to set defined at this level. This method     * treats all namespaces as though they were declared with default="none".     * If the new namespace prefix conflicts with an existing namespace this     * throws an exception.     *     * @param def namespace definition to be added (duplicates ignored)     * @throws JiBXException on namespace definition conflict     */    public void addImpliedNamespace(NamespaceDefinition def) throws JiBXException {        if (!checkDuplicateNamespace(def)) {            internalAddNamespace(def);        }    }    /**     * Add class mapping to set defined at this level. If the new mapping     * conflicts with an existing one at this level it throws an exception.     *     * @param def mapping definition to be added     * @throws JiBXException on mapping definition conflict     */    public void addMapping(IMapping def) throws JiBXException {        // create structure if not already done        if (m_mappings == null) {            m_classMap = new ArrayMap();            m_mappings = new ArrayList();        }        // check for conflict on class name before adding to definitions        String name = def.getTypeName();        if (name == null) {            name = def.getReferenceType();        }        int index = m_classMap.findOrAdd(name);        if (index < m_mappings.size()) {            if (def.getTypeName() == null) {                throw new JiBXException                    ("Conflicting mappings for class " + name);            } else {                throw new JiBXException                    ("Conflicting mappings for type name " + name);            }        } else {            m_mappings.add(def);        }    }    /**     * Add named structure component to set defined at this level. If the name     * conflicts with an existing one at this level it throws an exception.     *     * @param name component name to be set     * @param comp named component     * @throws JiBXException on mapping definition conflict     */    public void addNamedStructure(String name, IComponent comp)        throws JiBXException {        if (m_namedStructureMap == null) {            m_context.addNamedStructure(name, comp);        } else {            m_namedStructureMap.put(name, comp);        }    }    /**     * Get the default namespace for a contained name. Elements and attributes     * are treated separately, since namespace handling differs between the two.     *     * @param attr flag for attribute name     * @return default namespace URI, or <code>null</code> if none     */    private NamespaceDefinition getDefaultNamespace(boolean attr) {        NamespaceDefinition ns;        if (attr) {            ns = m_attributeDefault;        } else {            ns = m_elementDefault;        }        if (ns == null && m_context != null) {            ns = m_context.getDefaultNamespace(attr);        }        return ns;    }    /**     * Get the default namespace URI for a contained name. Elements and     * attributes are treated separately, since namespace handling differs     * between the two.     *     * @param attr flag for attribute name     * @return default namespace URI, or <code>null</code> if none     */    public String getDefaultURI(boolean attr) {        NamespaceDefinition ns = getDefaultNamespace(attr);        if (ns == null) {            return null;        } else {            return ns.getUri();        }    }    /**     * Get the default namespace index for a contained name. Elements and     * attributes are treated separately, since namespace handling differs     * between the two.     *     * @param attr flag for attribute name     * @return default namespace index     */    public int getDefaultIndex(boolean attr) {        NamespaceDefinition ns = getDefaultNamespace(attr);        if (ns == null) {            return 0;        } else {            return ns.getIndex();        }    }    /**     * Get namespace index for a given URI. Finds the prefix for a URI in a     * name contained by this level, throwing an exception if the URI is not     * found or does not have a prefix.     *     * @param uri namespace URI to be found     * @param attr flag for attribute name     * @return namespace index for URI     * @throws JiBXException if URI not defined or not usable     */    public int getNamespaceIndex(String uri, boolean attr)        throws JiBXException {        // check for namespace URI defined at this level        if (m_uriMap != null) {            Object value = m_uriMap.get(uri);            if (value != null) {                if (!attr || ((NamespaceDefinition)value).getPrefix() != null) {                    return ((NamespaceDefinition)value).getIndex();                }            }        }        // if all else fails, try the higher level        if (m_context == null) {            throw new JiBXException("Namespace URI \"" + uri +                "\" not defined or not usable");        } else {            return m_context.getNamespaceIndex(uri, attr);        }    }    /**     * Get mapping definition for class if defined at this level.     *     * @param name fully qualified class name     * @return mapping definition for class, or <code>null</code> if not defined     */    public IMapping getMappingAtLevel(String name) {        // check for class mapping defined at this level        if (m_classMap != null) {                        // check for definition at this level            int index = m_classMap.find(name);            if (index >= 0) {                return (IMapping)m_mappings.get(index);

⌨️ 快捷键说明

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