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