definitioncontext.java
来自「JiBX是一个为Java提供的XML数据绑定框架。它可以和现存的类一起运行」· Java 代码 · 共 473 行 · 第 1/2 页
JAVA
473 行
/*Copyright (c) 2004-2005, 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;/** * 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; /** 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; } /** * 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 definition for element name. * TODO: handle multiple prefixes for namespace, proper screening * * @param name attribute group defining name * @return namespace definition, or <code>null</code> if none that matches */ public NamespaceElement getElementNamespace(NameAttributes name) { String uri = name.getUri(); String prefix = name.getPrefix(); NamespaceElement ns = null; if (uri != null) { if (m_uriMap != null) { ns = (NamespaceElement)m_uriMap.get(uri); if (ns != null && prefix != null) { if (!prefix.equals(ns.getPrefix())) { ns = null; } } } } else if (prefix != null) { if (m_prefixMap != null) { ns = (NamespaceElement)m_prefixMap.get(prefix); } } else { ns = m_elementDefault; } if (ns == null && m_outerContext != null) { ns = m_outerContext.getElementNamespace(name); } return ns; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?