1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xml.internal.serializer.utils;
  23 
  24 import java.io.IOException;
  25 
  26 import javax.xml.parsers.DocumentBuilder;
  27 import javax.xml.parsers.DocumentBuilderFactory;
  28 import javax.xml.parsers.ParserConfigurationException;
  29 import javax.xml.transform.TransformerException;
  30 
  31 import org.w3c.dom.Attr;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.Element;
  34 import org.w3c.dom.Node;
  35 
  36 import org.xml.sax.InputSource;
  37 
  38 /**
  39  * This class provides a DOM level 2 "helper", which provides services currently
  40  * not provided be the DOM standard.
  41  *
  42  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
  43  * It exists to cut the serializers dependancy on that package.
  44  *
  45  * The differences from the original class are:
  46  * it doesn't extend DOMHelper, not depricated,
  47  * dropped method isNodeAfter(Node node1, Node node2)
  48  * dropped method parse(InputSource)
  49  * dropped method supportSAX()
  50  * dropped method setDocument(doc)
  51  * dropped method checkNode(Node)
  52  * dropped method getDocument()
  53  * dropped method getElementByID(String id, Document doc)
  54  * dropped method getParentOfNode(Node node)
  55  * dropped field Document m_doc;
  56  * made class non-public
  57  *
  58  * This class is not a public API, it is only public because it is
  59  * used in com.sun.org.apache.xml.internal.serializer.
  60  *
  61  * @xsl.usage internal
  62  */
  63 public final class DOM2Helper
  64 {
  65 
  66   /**
  67    * Construct an instance.
  68    */
  69   public DOM2Helper(){}
  70 
  71   /**
  72    * Returns the local name of the given node, as defined by the
  73    * XML Namespaces specification. This is prepared to handle documents
  74    * built using DOM Level 1 methods by falling back upon explicitly
  75    * parsing the node name.
  76    *
  77    * @param n Node to be examined
  78    *
  79    * @return String containing the local name, or null if the node
  80    * was not assigned a Namespace.
  81    */
  82   public String getLocalNameOfNode(Node n)
  83   {
  84 
  85     String name = n.getLocalName();
  86 
  87     return (null == name) ? getLocalNameOfNodeFallback(n) : name;
  88   }
  89 
  90   /**
  91    * Returns the local name of the given node. If the node's name begins
  92    * with a namespace prefix, this is the part after the colon; otherwise
  93    * it's the full node name.
  94    *
  95    * This method is copied from com.sun.org.apache.xml.internal.utils.DOMHelper
  96    *
  97    * @param n the node to be examined.
  98    *
  99    * @return String containing the Local Name
 100    */
 101   private String getLocalNameOfNodeFallback(Node n)
 102   {
 103 
 104     String qname = n.getNodeName();
 105     int index = qname.indexOf(':');
 106 
 107     return (index < 0) ? qname : qname.substring(index + 1);
 108   }
 109 
 110   /**
 111    * Returns the Namespace Name (Namespace URI) for the given node.
 112    * In a Level 2 DOM, you can ask the node itself. Note, however, that
 113    * doing so conflicts with our decision in getLocalNameOfNode not
 114    * to trust the that the DOM was indeed created using the Level 2
 115    * methods. If Level 1 methods were used, these two functions will
 116    * disagree with each other.
 117    * <p>
 118    * TODO: Reconcile with getLocalNameOfNode.
 119    *
 120    * @param n Node to be examined
 121    *
 122    * @return String containing the Namespace URI bound to this DOM node
 123    * at the time the Node was created.
 124    */
 125   public String getNamespaceOfNode(Node n)
 126   {
 127     return n.getNamespaceURI();
 128   }
 129 
 130   /** Field m_useDOM2getNamespaceURI is a compile-time flag which
 131    *  gates some of the parser options used to build a DOM -- but
 132    * that code is commented out at this time and nobody else
 133    * references it, so I've commented this out as well. */
 134   //private boolean m_useDOM2getNamespaceURI = false;
 135 }