📄 definitioncontext.java
字号:
/*Copyright (c) 2004-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.model;import java.util.ArrayList;import java.util.HashMap;import org.jibx.binding.def.NamespaceDefinition;import org.jibx.runtime.JiBXException;/** * Definition context information. This is used to track definitions of items * that can be referenced by other items. The contexts are nested, so that names * not found in a context may be defined by a containing context. The access * methods take this into account, automatically delegating to the containing * context (if defined) when a lookup fails. * * @author Dennis M. Sosnoski * @version 1.0 */public class DefinitionContext{ /** Link to containing definition context. */ private final DefinitionContext m_outerContext; /** Namespace used by default at this level for attributes. */ private NamespaceElement m_attributeDefault; /** Namespace used by default at this level for elements. */ private NamespaceElement 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; /** Map from element names to mappings defined at level (lazy create). */ private HashMap m_mappingMap; /** Class hierarchy context for format definitions (lazy create). */ private ClassHierarchyContext m_formatContext; /** Class hierarchy context for template definitions (lazy create). */ private ClassHierarchyContext m_templateContext; /** Named binding components (lazy create). */ private HashMap m_namedStructureMap; /** * Constructor. * * @param outer containing definition context (<code>null</code> if * at root of tree) */ protected DefinitionContext(DefinitionContext outer) { m_outerContext = outer; } /** * Copy a context for use by an included binding. The duplicated context has * the same containing context as the original, and shared reference * structures for formats and templates, but only a static copy of the * namespace definitions. * * @return context copy for included binding */ /*package*/ DefinitionContext getIncludeCopy() { DefinitionContext dupl = new DefinitionContext(m_outerContext); if (m_mappingMap == null) { m_mappingMap = new HashMap(); } dupl.m_mappingMap = m_mappingMap; if (m_formatContext == null) { m_formatContext = new ClassHierarchyContext(getContainingFormatContext()); } dupl.m_formatContext = m_formatContext; if (m_templateContext == null) { m_templateContext = new ClassHierarchyContext(getContainingTemplateContext()); } dupl.m_templateContext = m_templateContext; return dupl; } /** * Inject namespaces from this context into another context. This is * intended for includes, where the included binding inherits the * namespace declarations of the containing binding. * * @param to */ /*package*/ void injectNamespaces(DefinitionContext to) { to.m_attributeDefault = m_attributeDefault; to.m_elementDefault = m_elementDefault; if (m_namespaces != null) { to.m_namespaces = new ArrayList(m_namespaces); to.m_prefixMap = new HashMap(m_prefixMap); to.m_uriMap = new HashMap(m_uriMap); } } /** * Get containing context. * * @return containing context information (<code>null</code> if at root of * tree) */ public DefinitionContext getContaining() { return m_outerContext; } /** * Get containing format context. * * @return innermost containing context for format definitions * (<code>null</code> none defined) */ private ClassHierarchyContext getContainingFormatContext() { if (m_outerContext == null) { return null; } else { return m_outerContext.getFormatContext(); } } /** * Get current format context. * * @return innermost context for format definitions (<code>null</code> none * defined) */ private ClassHierarchyContext getFormatContext() { if (m_formatContext == null) { return getContainingFormatContext(); } else { return m_formatContext; } } /** * Get containing template context. * * @return innermost containing context for template definitions * (<code>null</code> none defined) */ private ClassHierarchyContext getContainingTemplateContext() { if (m_outerContext == null) { return null; } else { return m_outerContext.getTemplateContext(); } } /** * Get current template context. * * @return innermost context for template definitions (<code>null</code> none * defined) */ private ClassHierarchyContext getTemplateContext() { if (m_templateContext == null) { return getContainingTemplateContext(); } else { return m_templateContext; } } /** * Add namespace to set defined at this level. * * @param def namespace definition element to be added * @return problem information, or <code>null</code> if no problem *//* public ValidationProblem addNamespace(NamespaceElement def) { // initialize structures if first namespace definition if (m_namespaces == null) { m_namespaces = new ArrayList(); m_prefixMap = new HashMap(); m_uriMap = new HashMap(); } // check for conflict as default for attributes if (def.isAttributeDefault()) { if (m_attributeDefault == null) { m_attributeDefault = def; } else { return new ValidationProblem ("Conflicting attribute namespaces", def); } } // check for conflict as default for elements if (def.isElementDefault()) { if (m_elementDefault == null) { m_elementDefault = def; } else { return new ValidationProblem ("Conflicting element namespaces", def); } } // check for conflict on prefix String prefix = def.getPrefix(); if (m_prefixMap.get(prefix) != null) { return new ValidationProblem("Namespace prefix conflict", def); } // check for duplicate definition of same URI String uri = def.getUri(); Object prior = m_uriMap.get(uri); if (prior != null && ((NamespaceElement)prior).getPrefix() != null) { // TODO: is this needed? multiple prefixes should be allowed return null; } // add only if successful in all tests m_namespaces.add(def); m_prefixMap.put(prefix, def); m_uriMap.put(uri, def); return null; } */ /** * Get namespace for prefix. * * @param prefix * @return namespace definition in this context, <code>null</code> if none */ public NamespaceElement getNamespaceForPrefix(String prefix) { if (m_prefixMap == null) { return null; } else { return (NamespaceElement)m_prefixMap.get(prefix); } } /** * Check for namespace using the same prefix. This also intializes the * namespace structures for this context the first time the method is * called. * * @param def * @return namespace definition using same prefix, <code>null</code> if none */ private NamespaceElement checkDuplicatePrefix(NamespaceElement def) { // 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 prefix = def.getPrefix(); return (NamespaceElement)m_prefixMap.get(prefix); } /** * Add namespace to internal tables. * * @param def */ private void internalAddNamespace(NamespaceElement def) { String uri = def.getUri(); String prefix = def.getPrefix(); m_namespaces.add(def); m_prefixMap.put(prefix, def); m_uriMap.put(uri, def); } /** * Add namespace to set defined at this level. * * @param def namespace definition element to be added (duplicates ignored) * @return problem information, or <code>null</code> if no problem */ public ValidationProblem addNamespace(NamespaceElement def) { String uri = def.getUri(); NamespaceElement dup = checkDuplicatePrefix(def); if (dup == null) { // check for no definition of same URI with prefix NamespaceElement prior = (NamespaceElement)m_uriMap.get(uri); if (prior == null || prior.getPrefix() == null) { // check for conflict as default for attributes if (def.isAttributeDefault()) { if (m_attributeDefault == null) { m_attributeDefault = def; } else { return new ValidationProblem ("Conflicting attribute namespaces", def); } } // check for conflict as default for elements if (def.isElementDefault()) { if (m_elementDefault == null) { m_elementDefault = def; } else { return new ValidationProblem ("Conflicting element namespaces", def); } } // no conflicts, add it internalAddNamespace(def); } } else if (!uri.equals(dup.getUri())) { String prefix = def.getPrefix(); if (prefix == null) { return new ValidationProblem ("Default namespace conflict", def); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -