19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DOM2Helper.java 468654 2006-10-28 07:09:23Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.serializer.utils;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.w3c.dom.Node;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class provides a DOM level 2 "helper", which provides services currently
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * not provided be the DOM standard.
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class is a copy of the one in org.apache.xml.utils.
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * It exists to cut the serializers dependancy on that package.
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The differences from the original class are:
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * it doesn't extend DOMHelper, not depricated,
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method isNodeAfter(Node node1, Node node2)
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method parse(InputSource)
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method supportSAX()
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method setDocument(doc)
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method checkNode(Node)
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method getDocument()
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method getElementByID(String id, Document doc)
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped method getParentOfNode(Node node)
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dropped field Document m_doc;
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * made class non-public
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class is not a public API, it is only public because it is
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * used in org.apache.xml.serializer.
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage internal
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic final class DOM2Helper
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Construct an instance.
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DOM2Helper(){}
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Returns the local name of the given node, as defined by the
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * XML Namespaces specification. This is prepared to handle documents
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * built using DOM Level 1 methods by falling back upon explicitly
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * parsing the node name.
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param n Node to be examined
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return String containing the local name, or null if the node
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * was not assigned a Namespace.
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public String getLocalNameOfNode(Node n)
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    String name = n.getLocalName();
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return (null == name) ? getLocalNameOfNodeFallback(n) : name;
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Returns the local name of the given node. If the node's name begins
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * with a namespace prefix, this is the part after the colon; otherwise
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * it's the full node name.
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * This method is copied from org.apache.xml.utils.DOMHelper
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param n the node to be examined.
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return String containing the Local Name
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  private String getLocalNameOfNodeFallback(Node n)
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    String qname = n.getNodeName();
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    int index = qname.indexOf(':');
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return (index < 0) ? qname : qname.substring(index + 1);
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Returns the Namespace Name (Namespace URI) for the given node.
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * In a Level 2 DOM, you can ask the node itself. Note, however, that
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * doing so conflicts with our decision in getLocalNameOfNode not
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * to trust the that the DOM was indeed created using the Level 2
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * methods. If Level 1 methods were used, these two functions will
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * disagree with each other.
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * TODO: Reconcile with getLocalNameOfNode.
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param n Node to be examined
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return String containing the Namespace URI bound to this DOM node
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * at the time the Node was created.
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public String getNamespaceOfNode(Node n)
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return n.getNamespaceURI();
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** Field m_useDOM2getNamespaceURI is a compile-time flag which
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  gates some of the parser options used to build a DOM -- but
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * that code is commented out at this time and nobody else
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * references it, so I've commented this out as well. */
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  //private boolean m_useDOM2getNamespaceURI = false;
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
123