/* * $Id$ * * Copyright (c) 2007 by Tmaxsoft co., Ltd. * All rights reserved. * * This software is the confidential and proprietary information * of Tmaxsoft co., Ltd("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Tmaxsoft co., Ltd. */ package com.ctc.wstx.dom; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.stream.XMLStreamException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import com.ctc.wstx.util.BijectiveNsMap; import com.ctc.wstx.util.EmptyIterator; /** * * * @version $Revision$
* created at ¿ÀÈÄ 3:25:13 * @author Yoon-Je Choi */ public class DOMOutputElement implements NamespaceContext { public final static int PREFIX_UNBOUND = 0; public final static int PREFIX_OK = 1; public final static int PREFIX_MISBOUND = 2; final static String sXmlNsPrefix = XMLConstants.XML_NS_PREFIX; final static String sXmlNsURI = XMLConstants.XML_NS_URI; private DOMOutputElement mParent; private final Document mDocument; private Element mElement; /* //////////////////////////////////////////// // Namespace binding/mapping information //////////////////////////////////////////// */ /** * Namespace context end application may have supplied, and that * (if given) should be used to augment explicitly defined bindings. */ NamespaceContext mRootNsContext; String mDefaultNsURI; /** * True, if the default namespace URI has been explicitly specified * for this element; false if it was inherited from the parent * element */ boolean mDefaultNsSet; /** * Mapping of namespace prefixes to URIs and back. */ BijectiveNsMap mNsMapping; /** * True, if {@link #mNsMapping} is a shared copy from the parent; * false if a local copy was created (which happens when namespaces * get bound etc). */ boolean mNsMapShared; /* //////////////////////////////////////////// // Attribute information //////////////////////////////////////////// */ /** * Map used to check for duplicate attribute declarations, if * feature is enabled. */ HashMap mAttrMap = null; /** * Constructor for the virtual root element */ private DOMOutputElement(Document doc) { mDocument = doc; mParent = null; mElement = null; mNsMapping = null; mNsMapShared = false; mDefaultNsURI = ""; mRootNsContext = null; mDefaultNsSet = false; } private DOMOutputElement(DOMOutputElement parent, Element element, BijectiveNsMap ns) { mDocument = parent.getDocument(); mParent = parent; mElement = element; mNsMapping = ns; mNsMapShared = (ns != null); mDefaultNsURI = parent.mDefaultNsURI; mRootNsContext = parent.mRootNsContext; mDefaultNsSet = false; } /** * Method called to reuse a pooled instance. * * @return Chained pooled instance that should now be head of the * reuse chain */ private void relink(DOMOutputElement parent, Element element) { parent.getElement().appendChild(element); mParent = parent; mElement = element; mNsMapping = parent.mNsMapping; mNsMapShared = (mNsMapping != null); mDefaultNsURI = parent.mDefaultNsURI; mRootNsContext = parent.mRootNsContext; mDefaultNsSet = false; } public static DOMOutputElement createRoot(Document doc) { return new DOMOutputElement(doc); } /** * Simplest factory method, which gets called when a 1-argument * element output method is called. It is, then, assumed to * use the default namespace. */ protected DOMOutputElement createChild(Element element) { /* At this point we can also discard attribute Map; it is assumed * that when a child element has been opened, no more attributes * can be output. */ mAttrMap = null; if(isRoot()) { mDocument.appendChild(element); // mDocument.adoptNode(element); } else { mElement.appendChild(element); } return new DOMOutputElement(this, element, mNsMapping); } protected DOMOutputElement createChild() { /* At this point we can also discard attribute Map; it is assumed * that when a child element has been opened, no more attributes * can be output. */ mAttrMap = null; // if(isRoot()) { //// mDocument.appendChild(element); // mDocument.adoptNode(element); // } else { // mElement.appendChild(element); // } return new DOMOutputElement(this, null, mNsMapping); } /** * @return New head of the recycle pool */ protected DOMOutputElement reuseAsChild(DOMOutputElement parent, Element element) { mAttrMap = null; DOMOutputElement poolHead = mParent; relink(parent, element); return poolHead; } protected void setRootNsContext(NamespaceContext ctxt) { mRootNsContext = ctxt; /* Let's also see if we have an active default ns mapping: * (provided it hasn't yet explicitly been set for this element) */ if (!mDefaultNsSet) { String defURI = ctxt.getNamespaceURI(""); if (defURI != null && defURI.length() > 0) { mDefaultNsURI = defURI; } } } /** * Method called to temporarily link this instance to a pool, to * allow reusing of instances with the same reader. */ protected void addToPool(DOMOutputElement poolHead) { mParent = poolHead; } /* //////////////////////////////////////////// // Public API, accessors //////////////////////////////////////////// */ public DOMOutputElement getParent() { return mParent; } public boolean isRoot() { // (Virtual) Root element has no parent... return (mParent == null); } public Document getDocument() { return mDocument; } public Element getElement() { return mElement; } public String getDefaultNsUri() { return mDefaultNsURI; } /** * @return String presentation of the fully-qualified name, in * "prefix:localName" format (no URI). Useful for error and * debugging messages. */ public String getNameDesc() { if(mElement != null) { return mElement.getLocalName(); } return "#error"; // unexpected case } /* //////////////////////////////////////////// // Public API, mutators //////////////////////////////////////////// */ public void setElement(Element element) { mElement = element; } public void setDefaultNsUri(String uri) { mDefaultNsURI = uri; mDefaultNsSet = true; } public void setPrefix(String prefix) { mElement.setPrefix(prefix); } public String generateMapping(String prefixBase, String uri, int[] seqArr) { // This is mostly cut'n pasted from addPrefix()... if (mNsMapping == null) { // Didn't have a mapping yet? Need to create one... mNsMapping = BijectiveNsMap.createEmpty(); } else if (mNsMapShared) { /* Was shared with parent(s)? Need to create a derivative, to * allow for nesting/scoping of new prefix */ mNsMapping = mNsMapping.createChild(); mNsMapShared = false; } return mNsMapping.addGeneratedMapping(prefixBase, mRootNsContext, uri, seqArr); } public void addPrefix(String prefix, String uri) { if (mNsMapping == null) { // Didn't have a mapping yet? Need to create one... mNsMapping = BijectiveNsMap.createEmpty(); } else if (mNsMapShared) { /* Was shared with parent(s)? Need to create a derivative, to * allow for nesting/scoping of new prefix */ mNsMapping = mNsMapping.createChild(); mNsMapShared = false; } mNsMapping.addMapping(prefix, uri); } /* ////////////////////////////////////////////////// // NamespaceContext implementation ////////////////////////////////////////////////// */ public String getNamespaceURI(String prefix) { if (prefix.length() == 0) { //default NS return mDefaultNsURI; } if (mNsMapping != null) { String uri = mNsMapping.findUriByPrefix(prefix); if (uri != null) { return uri; } } return (mRootNsContext != null) ? mRootNsContext.getNamespaceURI(prefix) : null; } public String getPrefix(String uri) { if (mDefaultNsURI.equals(uri)) { return ""; } if (mNsMapping != null) { String prefix = mNsMapping.findPrefixByUri(uri); if (prefix != null) { return prefix; } } return (mRootNsContext != null) ? mRootNsContext.getPrefix(uri) : null; } public Iterator getPrefixes(String uri) { List l = null; if (mDefaultNsURI.equals(uri)) { l = new ArrayList(); l.add(""); } if (mNsMapping != null) { l = mNsMapping.getPrefixesBoundToUri(uri, l); } // How about the root namespace context? (if any) /* Note: it's quite difficult to properly resolve masking, when * combining these things (not impossible, just tricky); for now * let's do best effort without worrying about masking: */ if (mRootNsContext != null) { Iterator it = mRootNsContext.getPrefixes(uri); while (it.hasNext()) { String prefix = (String) it.next(); if (prefix.length() == 0) { // default NS already checked continue; } // slow check... but what the heck if (l == null) { l = new ArrayList(); } else if (l.contains(prefix)) { // double-defined... continue; } l.add(prefix); } } return (l == null) ? EmptyIterator.getInstance() : l.iterator(); } public void appendChild(Node n) { mElement.appendChild(n); } /* //////////////////////////////////////////// // Internal methods //////////////////////////////////////////// */ private void throwOutputError(String msg) throws XMLStreamException { throw new XMLStreamException(msg); } /* //////////////////////////////////////////// // Public API, ns binding, checking //////////////////////////////////////////// */ /** * Method similar to {@link #getPrefix}, but one that will not accept * the default namespace, only an explicit one. Usually used when * trying to find a prefix for attributes. */ public String getExplicitPrefix(String uri) { if (mNsMapping != null) { String prefix = mNsMapping.findPrefixByUri(uri); if (prefix != null) { return prefix; } } if (mRootNsContext != null) { String prefix = mRootNsContext.getPrefix(uri); if (prefix != null) { // Hmmh... still can't use the default NS: if (prefix.length() > 0) { return prefix; } // ... should we try to find an explicit one? } } return null; } /** * Method that verifies that passed-in prefix indeed maps to the specified * namespace URI; and depending on how it goes returns a status for * caller. * * @param isElement If true, rules for the default NS are those of elements * (ie. empty prefix can map to non-default namespace); if false, * rules are those of attributes (only non-default prefix can map to * a non-default namespace). * * @return PREFIX_OK, if passed-in prefix matches matched-in namespace URI * in current scope; PREFIX_UNBOUND if it's not bound to anything, * and PREFIX_MISBOUND if it's bound to another URI. * * @throws XMLStreamException True if default (no) prefix is allowed to * match a non-default URI (elements); false if not (attributes) */ public int isPrefixValid(String prefix, String nsURI, boolean isElement) throws XMLStreamException { // Hmmm.... caller shouldn't really pass null. if (nsURI == null) { nsURI = ""; } /* First thing is to see if specified prefix is bound to a namespace; * and if so, verify it matches with data passed in: */ // Checking default namespace? if (prefix == null || prefix.length() == 0) { if (isElement) { // It's fine for elements only if the URI actually matches: if (nsURI == mDefaultNsURI || nsURI.equals(mDefaultNsURI)) { return PREFIX_OK; } } else { /* Attributes never use the default namespace: "no prefix" * can only mean "no namespace" */ if (nsURI.length() == 0) { return PREFIX_OK; } } return PREFIX_MISBOUND; } /* Need to handle 'xml' prefix and its associated * URI; they are always declared by default */ if (prefix.equals(sXmlNsPrefix)) { // Should we thoroughly verify its namespace matches...? // 01-Apr-2005, TSa: Yes, let's always check this if (!nsURI.equals(sXmlNsURI)) { throwOutputError("Namespace prefix '"+sXmlNsPrefix +"' can not be bound to non-default namespace ('"+nsURI+"'); has to be the default '" +sXmlNsURI+"'"); } return PREFIX_OK; } // Nope checking some other namespace String act; if (mNsMapping != null) { act = mNsMapping.findUriByPrefix(prefix); } else { act = null; } if (act == null && mRootNsContext != null) { act = mRootNsContext.getNamespaceURI(prefix); } // Not (yet) bound... if (act == null) { return PREFIX_UNBOUND; } return (act == nsURI || act.equals(nsURI)) ? PREFIX_OK : PREFIX_MISBOUND; } }