Java Code Examples for org.w3c.dom.Node#getPrefix()

The following examples show how to use org.w3c.dom.Node#getPrefix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XAdESSignature.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * This method sets the namespace which will determinate the {@code XAdESPaths} to use. The content of the
 * Transform element is ignored.
 *
 * @param element
 */
public void recursiveNamespaceBrowser(final Element element) {
	for (int ii = 0; ii < element.getChildNodes().getLength(); ii++) {
		final Node node = element.getChildNodes().item(ii);
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			final String prefix = node.getPrefix();
			final Element childElement = (Element) node;
			final String namespaceURI = childElement.getNamespaceURI();
			final String localName = childElement.getLocalName();
			if (XMLDSigElement.TRANSFORM.isSameTagName(localName) && XMLDSigElement.TRANSFORM.getURI().equals(namespaceURI)) {
				xmldSigNamespace = new DSSNamespace(namespaceURI, prefix);
				continue;
			} else if (XAdES132Element.QUALIFYING_PROPERTIES.isSameTagName(localName)) {
				setXAdESPathAndNamespace(prefix, namespaceURI);
				return;
			}
			recursiveNamespaceBrowser(childElement);
		}
	}
}
 
Example 2
Source File: AbstractDocumentComponent.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
   * @return mapping from prefix to namespace.
   */
  public Map<String, String> getPrefixes() {
      Map<String,String> prefixes = new HashMap<String,String>();
      NamedNodeMap nodes = getPeer().getAttributes();
      for (int i = 0; i < nodes.getLength(); i++) {
          Node n = nodes.item(i);
          String name = n.getLocalName();
   String prefix = n.getPrefix();
   final String xmlns = XMLConstants.XMLNS_ATTRIBUTE; //NOI18N
   if (xmlns.equals(name) || // default namespace
xmlns.equals(prefix)) { // namespace prefix
String ns = n.getNodeValue();
prefixes.put(name, ns);
   }
      }
      String defaultNamespace = prefixes.remove(XMLConstants.XMLNS_ATTRIBUTE);
      if (defaultNamespace != null) {
          prefixes.put(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespace);
      }
      return prefixes;
  }
 
Example 3
Source File: DOMValidatorHelper.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void fillQName(QName toFill, Node node) {
    final String prefix = node.getPrefix();
    final String localName = node.getLocalName();
    final String rawName = node.getNodeName();
    final String namespace = node.getNamespaceURI();

    toFill.uri = (namespace != null && namespace.length() > 0) ? fSymbolTable.addSymbol(namespace) : null;
    toFill.rawname = (rawName != null) ? fSymbolTable.addSymbol(rawName) : XMLSymbols.EMPTY_STRING;

    // Is this a DOM level1 document?
    if (localName == null) {
        int k = rawName.indexOf(':');
        if (k > 0) {
            toFill.prefix = fSymbolTable.addSymbol(rawName.substring(0, k));
            toFill.localpart = fSymbolTable.addSymbol(rawName.substring(k + 1));
        }
        else {
            toFill.prefix = XMLSymbols.EMPTY_STRING;
            toFill.localpart = toFill.rawname;
        }
    }
    else {
        toFill.prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
        toFill.localpart = (localName != null) ? fSymbolTable.addSymbol(localName) : XMLSymbols.EMPTY_STRING;
    }
}
 
Example 4
Source File: DOMValidatorHelper.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void fillQName(QName toFill, Node node) {
    final String prefix = node.getPrefix();
    final String localName = node.getLocalName();
    final String rawName = node.getNodeName();
    final String namespace = node.getNamespaceURI();

    toFill.uri = (namespace != null && namespace.length() > 0) ? fSymbolTable.addSymbol(namespace) : null;
    toFill.rawname = (rawName != null) ? fSymbolTable.addSymbol(rawName) : XMLSymbols.EMPTY_STRING;

    // Is this a DOM level1 document?
    if (localName == null) {
        int k = rawName.indexOf(':');
        if (k > 0) {
            toFill.prefix = fSymbolTable.addSymbol(rawName.substring(0, k));
            toFill.localpart = fSymbolTable.addSymbol(rawName.substring(k + 1));
        }
        else {
            toFill.prefix = XMLSymbols.EMPTY_STRING;
            toFill.localpart = toFill.rawname;
        }
    }
    else {
        toFill.prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
        toFill.localpart = (localName != null) ? fSymbolTable.addSymbol(localName) : XMLSymbols.EMPTY_STRING;
    }
}
 
Example 5
Source File: DOMStreamReader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return an attribute's qname. Handle the case of DOM level 1 nodes.
 */
public QName getAttributeName(int index) {
    if (_state == START_ELEMENT) {
        Node attr = _currentAttributes.get(index);
        String localName = attr.getLocalName();
        if (localName != null) {
            String prefix = attr.getPrefix();
            String uri = attr.getNamespaceURI();
            return new QName(fixNull(uri), localName, fixNull(prefix));
        }
        else {
            return QName.valueOf(attr.getNodeName());
        }
    }
    throw new IllegalStateException("DOMStreamReader: getAttributeName() called in illegal state");
}
 
Example 6
Source File: DOMValidatorHelper.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void fillQName(QName toFill, Node node) {
    final String prefix = node.getPrefix();
    final String localName = node.getLocalName();
    final String rawName = node.getNodeName();
    final String namespace = node.getNamespaceURI();

    toFill.uri = (namespace != null && namespace.length() > 0) ? fSymbolTable.addSymbol(namespace) : null;
    toFill.rawname = (rawName != null) ? fSymbolTable.addSymbol(rawName) : XMLSymbols.EMPTY_STRING;

    // Is this a DOM level1 document?
    if (localName == null) {
        int k = rawName.indexOf(':');
        if (k > 0) {
            toFill.prefix = fSymbolTable.addSymbol(rawName.substring(0, k));
            toFill.localpart = fSymbolTable.addSymbol(rawName.substring(k + 1));
        }
        else {
            toFill.prefix = XMLSymbols.EMPTY_STRING;
            toFill.localpart = toFill.rawname;
        }
    }
    else {
        toFill.prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
        toFill.localpart = (localName != null) ? fSymbolTable.addSymbol(localName) : XMLSymbols.EMPTY_STRING;
    }
}
 
Example 7
Source File: ReadXml.java    From opentest with MIT License 5 votes vote down vote up
private Object processNode(Node xmlNode) {
    if (xmlNode.getNodeType() == Node.ELEMENT_NODE) {
        Map<String, Object> nodeObject = new HashMap<>();

        processAttributes(xmlNode, nodeObject);

        StringBuilder nodeText = null;
        NodeList nodeList = xmlNode.getChildNodes();
        int childCount = nodeList.getLength();
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            Node childNode = nodeList.item(childIndex);
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                String prefix = childNode.getPrefix() != null ? childNode.getPrefix() : "";
                nodeObject.put(prefix + childNode.getNodeName(), processNode(childNode));
            } else if (childNode.getNodeType() == Node.TEXT_NODE) {
                if (nodeText == null) {
                    nodeText = new StringBuilder();
                }

                nodeText.append(childNode.getTextContent());
            }
        }

        if (nodeText != null) {
            nodeObject.put("$$text", nodeText.toString());
        }

        return nodeObject;
    } else if (xmlNode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return xmlNode.getNodeValue();
    } else if (xmlNode.getNodeType() == Node.TEXT_NODE) {
        return xmlNode.getNodeValue();
    } else {
        return null;
    }
}
 
Example 8
Source File: DOMNormalizer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected final void updateQName (Node node, QName qname){

        String prefix    = node.getPrefix();
        String namespace = node.getNamespaceURI();
        String localName = node.getLocalName();
        // REVISIT: the symbols are added too often: start/endElement
        //          and in the namespaceFixup. Should reduce number of calls to symbol table.
        qname.prefix = (prefix!=null && prefix.length()!=0)?fSymbolTable.addSymbol(prefix):null;
        qname.localpart = (localName != null)?fSymbolTable.addSymbol(localName):null;
        qname.rawname = fSymbolTable.addSymbol(node.getNodeName());
        qname.uri =  (namespace != null)?fSymbolTable.addSymbol(namespace):null;
    }
 
Example 9
Source File: Svg2Vector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void printlnCommon(Node n) {
    logger.log(Level.FINE, " nodeName=\"" + n.getNodeName() + "\"");

    String val = n.getNamespaceURI();
    if (val != null) {
        logger.log(Level.FINE, " uri=\"" + val + "\"");
    }

    val = n.getPrefix();

    if (val != null) {
        logger.log(Level.FINE, " pre=\"" + val + "\"");
    }

    val = n.getLocalName();
    if (val != null) {
        logger.log(Level.FINE, " local=\"" + val + "\"");
    }

    val = n.getNodeValue();
    if (val != null) {
        logger.log(Level.FINE, " nodeValue=");
        if (val.trim().equals("")) {
            // Whitespace
            logger.log(Level.FINE, "[WS]");
        } else {
            logger.log(Level.FINE, "\"" + n.getNodeValue() + "\"");
        }
    }
}
 
Example 10
Source File: UnsupportedDecoderXmlInputException.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private static String getName(Node n) {
    if (n.getPrefix() == null || n.getPrefix().isEmpty()) {
        return n.getLocalName();
    } else {
        return n.getPrefix() + ":" + n.getLocalName();
    }
}
 
Example 11
Source File: DTMNodeProxy.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
     * DOM Level 3
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    @Override
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 12
Source File: DOM2DTMdefaultNamespaceDeclarationNode.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 13
Source File: NodeImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Tests whether two nodes are equal.
 * <br>This method tests for equality of nodes, not sameness (i.e.,
 * whether the two nodes are references to the same object) which can be
 * tested with <code>Node.isSameNode</code>. All nodes that are the same
 * will also be equal, though the reverse may not be true.
 * <br>Two nodes are equal if and only if the following conditions are
 * satisfied: The two nodes are of the same type.The following string
 * attributes are equal: <code>nodeName</code>, <code>localName</code>,
 * <code>namespaceURI</code>, <code>prefix</code>, <code>nodeValue</code>
 * , <code>baseURI</code>. This is: they are both <code>null</code>, or
 * they have the same length and are character for character identical.
 * The <code>attributes</code> <code>NamedNodeMaps</code> are equal.
 * This is: they are both <code>null</code>, or they have the same
 * length and for each node that exists in one map there is a node that
 * exists in the other map and is equal, although not necessarily at the
 * same index.The <code>childNodes</code> <code>NodeLists</code> are
 * equal. This is: they are both <code>null</code>, or they have the
 * same length and contain equal nodes at the same index. This is true
 * for <code>Attr</code> nodes as for any other type of node. Note that
 * normalization can affect equality; to avoid this, nodes should be
 * normalized before being compared.
 * <br>For two <code>DocumentType</code> nodes to be equal, the following
 * conditions must also be satisfied: The following string attributes
 * are equal: <code>publicId</code>, <code>systemId</code>,
 * <code>internalSubset</code>.The <code>entities</code>
 * <code>NamedNodeMaps</code> are equal.The <code>notations</code>
 * <code>NamedNodeMaps</code> are equal.
 * <br>On the other hand, the following do not affect equality: the
 * <code>ownerDocument</code> attribute, the <code>specified</code>
 * attribute for <code>Attr</code> nodes, the
 * <code>isWhitespaceInElementContent</code> attribute for
 * <code>Text</code> nodes, as well as any user data or event listeners
 * registered on the nodes.
 * @param arg The node to compare equality with.
 * @param deep If <code>true</code>, recursively compare the subtrees; if
 *   <code>false</code>, compare only the nodes themselves (and its
 *   attributes, if it is an <code>Element</code>).
 * @return If the nodes, and possibly subtrees are equal,
 *   <code>true</code> otherwise <code>false</code>.
 * @since DOM Level 3
 */
public boolean isEqualNode(Node arg) {
    if (arg == this) {
        return true;
    }
    if (arg.getNodeType() != getNodeType()) {
        return false;
    }
    // in theory nodeName can't be null but better be careful
    // who knows what other implementations may be doing?...
    if (getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    }
    else if (!getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    }
    else if (!getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    }
    else if (!getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    }
    else if (!getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    }
    else if (!getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }


    return true;
}
 
Example 14
Source File: XMLUtil.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI) throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns")) continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI) &&
                !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}
 
Example 15
Source File: UnImplNode.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 16
Source File: DOM2DTMdefaultNamespaceDeclarationNode.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 17
Source File: UnImplNode.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 18
Source File: NodeImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests whether two nodes are equal.
 * <br>This method tests for equality of nodes, not sameness (i.e.,
 * whether the two nodes are references to the same object) which can be
 * tested with <code>Node.isSameNode</code>. All nodes that are the same
 * will also be equal, though the reverse may not be true.
 * <br>Two nodes are equal if and only if the following conditions are
 * satisfied: The two nodes are of the same type.The following string
 * attributes are equal: <code>nodeName</code>, <code>localName</code>,
 * <code>namespaceURI</code>, <code>prefix</code>, <code>nodeValue</code>
 * , <code>baseURI</code>. This is: they are both <code>null</code>, or
 * they have the same length and are character for character identical.
 * The <code>attributes</code> <code>NamedNodeMaps</code> are equal.
 * This is: they are both <code>null</code>, or they have the same
 * length and for each node that exists in one map there is a node that
 * exists in the other map and is equal, although not necessarily at the
 * same index.The <code>childNodes</code> <code>NodeLists</code> are
 * equal. This is: they are both <code>null</code>, or they have the
 * same length and contain equal nodes at the same index. This is true
 * for <code>Attr</code> nodes as for any other type of node. Note that
 * normalization can affect equality; to avoid this, nodes should be
 * normalized before being compared.
 * <br>For two <code>DocumentType</code> nodes to be equal, the following
 * conditions must also be satisfied: The following string attributes
 * are equal: <code>publicId</code>, <code>systemId</code>,
 * <code>internalSubset</code>.The <code>entities</code>
 * <code>NamedNodeMaps</code> are equal.The <code>notations</code>
 * <code>NamedNodeMaps</code> are equal.
 * <br>On the other hand, the following do not affect equality: the
 * <code>ownerDocument</code> attribute, the <code>specified</code>
 * attribute for <code>Attr</code> nodes, the
 * <code>isWhitespaceInElementContent</code> attribute for
 * <code>Text</code> nodes, as well as any user data or event listeners
 * registered on the nodes.
 * @param arg The node to compare equality with.
 * @param deep If <code>true</code>, recursively compare the subtrees; if
 *   <code>false</code>, compare only the nodes themselves (and its
 *   attributes, if it is an <code>Element</code>).
 * @return If the nodes, and possibly subtrees are equal,
 *   <code>true</code> otherwise <code>false</code>.
 * @since DOM Level 3
 */
public boolean isEqualNode(Node arg) {
    if (arg == this) {
        return true;
    }
    if (arg.getNodeType() != getNodeType()) {
        return false;
    }
    // in theory nodeName can't be null but better be careful
    // who knows what other implementations may be doing?...
    if (getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    }
    else if (!getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    }
    else if (!getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    }
    else if (!getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    }
    else if (!getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    }
    else if (!getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }


    return true;
}
 
Example 19
Source File: NodeImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests whether two nodes are equal.
 * <br>This method tests for equality of nodes, not sameness (i.e.,
 * whether the two nodes are references to the same object) which can be
 * tested with <code>Node.isSameNode</code>. All nodes that are the same
 * will also be equal, though the reverse may not be true.
 * <br>Two nodes are equal if and only if the following conditions are
 * satisfied: The two nodes are of the same type.The following string
 * attributes are equal: <code>nodeName</code>, <code>localName</code>,
 * <code>namespaceURI</code>, <code>prefix</code>, <code>nodeValue</code>
 * , <code>baseURI</code>. This is: they are both <code>null</code>, or
 * they have the same length and are character for character identical.
 * The <code>attributes</code> <code>NamedNodeMaps</code> are equal.
 * This is: they are both <code>null</code>, or they have the same
 * length and for each node that exists in one map there is a node that
 * exists in the other map and is equal, although not necessarily at the
 * same index.The <code>childNodes</code> <code>NodeLists</code> are
 * equal. This is: they are both <code>null</code>, or they have the
 * same length and contain equal nodes at the same index. This is true
 * for <code>Attr</code> nodes as for any other type of node. Note that
 * normalization can affect equality; to avoid this, nodes should be
 * normalized before being compared.
 * <br>For two <code>DocumentType</code> nodes to be equal, the following
 * conditions must also be satisfied: The following string attributes
 * are equal: <code>publicId</code>, <code>systemId</code>,
 * <code>internalSubset</code>.The <code>entities</code>
 * <code>NamedNodeMaps</code> are equal.The <code>notations</code>
 * <code>NamedNodeMaps</code> are equal.
 * <br>On the other hand, the following do not affect equality: the
 * <code>ownerDocument</code> attribute, the <code>specified</code>
 * attribute for <code>Attr</code> nodes, the
 * <code>isWhitespaceInElementContent</code> attribute for
 * <code>Text</code> nodes, as well as any user data or event listeners
 * registered on the nodes.
 * @param arg The node to compare equality with.
 * @param deep If <code>true</code>, recursively compare the subtrees; if
 *   <code>false</code>, compare only the nodes themselves (and its
 *   attributes, if it is an <code>Element</code>).
 * @return If the nodes, and possibly subtrees are equal,
 *   <code>true</code> otherwise <code>false</code>.
 * @since DOM Level 3
 */
public boolean isEqualNode(Node arg) {
    if (arg == this) {
        return true;
    }
    if (arg.getNodeType() != getNodeType()) {
        return false;
    }
    // in theory nodeName can't be null but better be careful
    // who knows what other implementations may be doing?...
    if (getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    }
    else if (!getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    }
    else if (!getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    }
    else if (!getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    }
    else if (!getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    }
    else if (!getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }


    return true;
}
 
Example 20
Source File: UnImplNode.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }