001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: DOM2Helper.java 468654 2006-10-28 07:09:23Z minchau $
020     */
021    package org.apache.xml.serializer.utils;
022    
023    import org.w3c.dom.Node;
024    
025    /**
026     * This class provides a DOM level 2 "helper", which provides services currently 
027     * not provided be the DOM standard.
028     * 
029     * This class is a copy of the one in org.apache.xml.utils. 
030     * It exists to cut the serializers dependancy on that package.
031     * 
032     * The differences from the original class are:
033     * it doesn't extend DOMHelper, not depricated, 
034     * dropped method isNodeAfter(Node node1, Node node2)
035     * dropped method parse(InputSource)
036     * dropped method supportSAX()
037     * dropped method setDocument(doc) 
038     * dropped method checkNode(Node)
039     * dropped method getDocument()
040     * dropped method getElementByID(String id, Document doc)
041     * dropped method getParentOfNode(Node node)
042     * dropped field Document m_doc;
043     * made class non-public
044     *   
045     * This class is not a public API, it is only public because it is 
046     * used in org.apache.xml.serializer.
047     * 
048     * @xsl.usage internal
049     */
050    public final class DOM2Helper
051    {
052    
053      /**
054       * Construct an instance.
055       */
056      public DOM2Helper(){}
057    
058      /**
059       * Returns the local name of the given node, as defined by the
060       * XML Namespaces specification. This is prepared to handle documents
061       * built using DOM Level 1 methods by falling back upon explicitly
062       * parsing the node name.
063       *
064       * @param n Node to be examined
065       *
066       * @return String containing the local name, or null if the node
067       * was not assigned a Namespace.
068       */
069      public String getLocalNameOfNode(Node n)
070      {
071    
072        String name = n.getLocalName();
073    
074        return (null == name) ? getLocalNameOfNodeFallback(n) : name;
075      }
076      
077      /**
078       * Returns the local name of the given node. If the node's name begins
079       * with a namespace prefix, this is the part after the colon; otherwise
080       * it's the full node name.
081       * 
082       * This method is copied from org.apache.xml.utils.DOMHelper
083       *
084       * @param n the node to be examined.
085       *
086       * @return String containing the Local Name
087       */
088      private String getLocalNameOfNodeFallback(Node n)
089      {
090    
091        String qname = n.getNodeName();
092        int index = qname.indexOf(':');
093    
094        return (index < 0) ? qname : qname.substring(index + 1);
095      }
096    
097      /**
098       * Returns the Namespace Name (Namespace URI) for the given node.
099       * In a Level 2 DOM, you can ask the node itself. Note, however, that
100       * doing so conflicts with our decision in getLocalNameOfNode not
101       * to trust the that the DOM was indeed created using the Level 2
102       * methods. If Level 1 methods were used, these two functions will
103       * disagree with each other.
104       * <p>
105       * TODO: Reconcile with getLocalNameOfNode.
106       *
107       * @param n Node to be examined
108       *
109       * @return String containing the Namespace URI bound to this DOM node
110       * at the time the Node was created.
111       */
112      public String getNamespaceOfNode(Node n)
113      {
114        return n.getNamespaceURI();
115      }
116    
117      /** Field m_useDOM2getNamespaceURI is a compile-time flag which
118       *  gates some of the parser options used to build a DOM -- but 
119       * that code is commented out at this time and nobody else
120       * references it, so I've commented this out as well. */
121      //private boolean m_useDOM2getNamespaceURI = false;
122    }