Home | History | Annotate | Download | only in utils
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: DOM2Helper.java 468654 2006-10-28 07:09:23Z minchau $
     20  */
     21 package org.apache.xml.serializer.utils;
     22 
     23 import org.w3c.dom.Node;
     24 
     25 /**
     26  * This class provides a DOM level 2 "helper", which provides services currently
     27  * not provided be the DOM standard.
     28  *
     29  * This class is a copy of the one in org.apache.xml.utils.
     30  * It exists to cut the serializers dependancy on that package.
     31  *
     32  * The differences from the original class are:
     33  * it doesn't extend DOMHelper, not depricated,
     34  * dropped method isNodeAfter(Node node1, Node node2)
     35  * dropped method parse(InputSource)
     36  * dropped method supportSAX()
     37  * dropped method setDocument(doc)
     38  * dropped method checkNode(Node)
     39  * dropped method getDocument()
     40  * dropped method getElementByID(String id, Document doc)
     41  * dropped method getParentOfNode(Node node)
     42  * dropped field Document m_doc;
     43  * made class non-public
     44  *
     45  * This class is not a public API, it is only public because it is
     46  * used in org.apache.xml.serializer.
     47  *
     48  * @xsl.usage internal
     49  */
     50 public final class DOM2Helper
     51 {
     52 
     53   /**
     54    * Construct an instance.
     55    */
     56   public DOM2Helper(){}
     57 
     58   /**
     59    * Returns the local name of the given node, as defined by the
     60    * XML Namespaces specification. This is prepared to handle documents
     61    * built using DOM Level 1 methods by falling back upon explicitly
     62    * parsing the node name.
     63    *
     64    * @param n Node to be examined
     65    *
     66    * @return String containing the local name, or null if the node
     67    * was not assigned a Namespace.
     68    */
     69   public String getLocalNameOfNode(Node n)
     70   {
     71 
     72     String name = n.getLocalName();
     73 
     74     return (null == name) ? getLocalNameOfNodeFallback(n) : name;
     75   }
     76 
     77   /**
     78    * Returns the local name of the given node. If the node's name begins
     79    * with a namespace prefix, this is the part after the colon; otherwise
     80    * it's the full node name.
     81    *
     82    * This method is copied from org.apache.xml.utils.DOMHelper
     83    *
     84    * @param n the node to be examined.
     85    *
     86    * @return String containing the Local Name
     87    */
     88   private String getLocalNameOfNodeFallback(Node n)
     89   {
     90 
     91     String qname = n.getNodeName();
     92     int index = qname.indexOf(':');
     93 
     94     return (index < 0) ? qname : qname.substring(index + 1);
     95   }
     96 
     97   /**
     98    * Returns the Namespace Name (Namespace URI) for the given node.
     99    * 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 }
    123