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

The following are Jave code examples for showing how to use getPrefix() of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: DocumentModelAccess.java   Source Code and License Vote up 6 votes
public String lookupNamespaceURI(Node node, List<? extends Node> pathToRoot) {
    String prefix = node.getPrefix();
    if (prefix == null) prefix = ""; //NOI18N
    String namespace = node.lookupNamespaceURI(prefix);
    if (namespace == null) {
        boolean skipDeeperNodes = true;
        for (Node n : pathToRoot) {
            if (skipDeeperNodes) {
                // The target node has to be inside of pathToRoot. 
                // But it can be not a top element of the list. 
                // It's necessary to skip items until the target node 
                // isn't found in the list.
                if (areSameNodes(n, node)) {
                    skipDeeperNodes = false;
                }
            } else {
                namespace = n.lookupNamespaceURI(prefix);
                if (namespace != null) {
                    break;
                }
            }
        }
    }
    return namespace;
}
 
Example 2
Project: incubator-netbeans   File: AbstractDocumentComponent.java   Source Code and License Vote up 6 votes
/**
   * @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
Project: OpenJSharp   File: DOMStreamReader.java   Source Code and License Vote up 6 votes
/**
 * 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 4
Project: OpenJSharp   File: DOMValidatorHelper.java   Source Code and License Vote up 6 votes
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
Project: openjdk-jdk10   File: DOMStreamReader.java   Source Code and License Vote up 6 votes
/**
 * 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
Project: openjdk-jdk10   File: DOMStreamReader.java   Source Code and License Vote up 6 votes
/**
 * 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 7
Project: openjdk-jdk10   File: DOMValidatorHelper.java   Source Code and License Vote up 6 votes
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 8
Project: incubator-netbeans   File: AbstractDocumentComponent.java   Source Code and License Vote up 5 votes
public static QName getQName(Node n) {
    String namespace = n.getNamespaceURI();
    String localName = n.getLocalName();
    String prefix = n.getPrefix();
    assert(localName != null);
    if (namespace == null && prefix == null) {
        return new QName(localName);
    } else if (namespace != null && prefix == null) {
        return new QName(namespace, localName);
    } else {
        return new QName(namespace, localName, prefix);
    }
}
 
Example 9
Project: convertigo-engine   File: DOMNodePointer.java   Source Code and License Vote up 5 votes
/**
 * Get any prefix from the specified node.
 * @param node the node to check
 * @return String xml prefix
 */
public static String getPrefix(Node node) {
    String prefix = node.getPrefix();
    if (prefix != null) {
        return prefix;
    }

    String name = node.getNodeName();
    int index = name.lastIndexOf(':');
    return index < 0 ? null : name.substring(0, index);
}
 
Example 10
Project: convertigo-engine   File: XMLUtils.java   Source Code and License Vote up 5 votes
public static String getPrefix(Node node) {
	String prefix = node.getPrefix();
	if (prefix == null) {
		prefix = node.getNodeName();
						
		// If the document is not namespace aware, we must split the attribute name
		// with ':' character.
		int i = prefix.indexOf(':');
		if (i != -1) {
			prefix = prefix.substring(0, i);
		}
	}
	
	return prefix;
}
 
Example 11
Project: lams   File: XMLHelper.java   Source Code and License Vote up 5 votes
/**
 * Looks up the namespace URI associated with the given prefix starting at the given element. This method differs
 * from the {@link Node#lookupNamespaceURI(java.lang.String)} in that it only those namespaces declared by an xmlns
 * attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
 * call like {@link Document#createElementNS(java.lang.String, java.lang.String)} even if the resulting element
 * doesn't have an namespace delcaration attribute.
 * 
 * @param startingElement the starting element
 * @param stopingElement the ancestor of the starting element that serves as the upper-bound, inclusive, for the
 *            search
 * @param prefix the prefix to look up
 * 
 * @return the namespace URI for the given prefer or null
 */
public static String lookupNamespaceURI(Element startingElement, Element stopingElement, String prefix) {
    String namespaceURI;

    // This code is a modified version of the lookup code within Xerces
    if (startingElement.hasAttributes()) {
        NamedNodeMap map = startingElement.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();
            namespaceURI = attr.getNamespaceURI();
            if (namespaceURI != null && namespaceURI.equals(XMLConstants.XMLNS_NS)) {
                // at this point we are dealing with DOM Level 2 nodes only
                if (prefix == null && attr.getNodeName().equals(XMLConstants.XMLNS_PREFIX)) {
                    // default namespace
                    return value;
                } else if (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_PREFIX)
                        && attr.getLocalName().equals(prefix)) {
                    // non default namespace
                    return value;
                }
            }
        }
    }

    if (startingElement != stopingElement) {
        Element ancestor = getElementAncestor(startingElement);
        if (ancestor != null) {
            return lookupNamespaceURI(ancestor, stopingElement, prefix);
        }
    }

    return null;
}
 
Example 12
Project: lams   File: XMLHelper.java   Source Code and License Vote up 5 votes
/**
 * Looks up the namespace prefix associated with the given URI starting at the given element. This method differs
 * from the {@link Node#lookupPrefix(java.lang.String)} in that it only those namespaces declared by an xmlns
 * attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
 * call like {@link Document#createElementNS(java.lang.String, java.lang.String)} even if the resulting element
 * doesn't have an namespace delcaration attribute.
 * 
 * @param startingElement the starting element
 * @param stopingElement the ancestor of the starting element that serves as the upper-bound, inclusive, for the
 *            search
 * @param namespaceURI the uri to look up
 * 
 * @return the prefix for the given namespace URI
 */
public static String lookupPrefix(Element startingElement, Element stopingElement, String namespaceURI) {
    String namespace;

    // This code is a modified version of the lookup code within Xerces
    if (startingElement.hasAttributes()) {
        NamedNodeMap map = startingElement.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(XMLConstants.XMLNS_NS)) {
                // DOM Level 2 nodes
                if (attr.getNodeName().equals(XMLConstants.XMLNS_PREFIX)
                        || (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_PREFIX))
                        && value.equals(namespaceURI)) {

                    String localname = attr.getLocalName();
                    String foundNamespace = startingElement.lookupNamespaceURI(localname);
                    if (foundNamespace != null && foundNamespace.equals(namespaceURI)) {
                        return localname;
                    }
                }

            }
        }
    }

    if (startingElement != stopingElement) {
        Element ancestor = getElementAncestor(startingElement);
        if (ancestor != null) {
            return lookupPrefix(ancestor, stopingElement, namespaceURI);
        }
    }

    return null;
}
 
Example 13
Project: opentest   File: ReadXml.java   Source Code and License Vote up 5 votes
private void processAttributes(Node xmlNode, Map<String, Object> nodeObject) {
    NamedNodeMap attributesMap = xmlNode.getAttributes();
    int attrCount = attributesMap.getLength();
    for (int attrIndex = 0; attrIndex < attrCount; attrIndex++) {
        Node attributeNode = attributesMap.item(attrIndex);
        if (attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
            String prefix = attributeNode.getPrefix() != null ? attributeNode.getPrefix() : "";
            nodeObject.put("$" + prefix + attributeNode.getNodeName(), attributeNode.getNodeValue());
        }
    }
}
 
Example 14
Project: opentest   File: ReadXml.java   Source Code and License Vote up 5 votes
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 15
Project: Svg2AndroidXml   File: Svg2Vector.java   Source Code and License Vote up 5 votes
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 16
Project: OpenJSharp   File: DOMNormalizer.java   Source Code and License Vote up 5 votes
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 17
Project: Tarski   File: XMLDOMHelper.java   Source Code and License Vote up 5 votes
public static void printlnCommon(Node n) {
  System.out.print(" nodeName=\"" + n.getNodeName() + "\"");

  String val = n.getNamespaceURI();
  if (val != null) {
    System.out.print(" uri=\"" + val + "\"");
  }

  val = n.getPrefix();

  if (val != null) {
    System.out.print(" pre=\"" + val + "\"");
  }

  val = n.getLocalName();
  if (val != null) {
    System.out.print(" local=\"" + val + "\"");
  }

  val = n.getNodeValue();
  if (val != null) {
    System.out.print(" nodeValue=");
    if (val.trim().equals("")) {
      // Whitespace
      System.out.print("[WS]");
    } else {
      System.out.print("\"" + n.getNodeValue() + "\"");
    }
  }
  System.out.println();
}
 
Example 18
Project: openjdk-jdk10   File: DOMNormalizer.java   Source Code and License Vote up 5 votes
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 19
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 4 votes
public static String getPrefix(Node node) {
    return node.getPrefix();
}
 
Example 20
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 4 votes
public static String getPrefix(Node node) {
    return node.getPrefix();
}