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

The following examples show how to use org.w3c.dom.Node#getNamespaceURI() . 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: DOMStreamReader.java    From openjdk-jdk8u-backup 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 2
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 3
Source File: PXDOMStyleAdapter.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
@Override
public String getAttributeValue(Object styleable, String attributeName, String namespaceURI) {
    Node node = (Node) styleable;
    if (node.hasAttributes()) {
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            if (attr.getLocalName().equals(attributeName)) {
                // check for any namespace prefix (TODO - we check for the
                // attribute prefix and for the node's namespace URI here.
                // There is a chance that only one way is needed, but for
                // now we keep both)
                if (namespaceURI == null) {
                    return attr.getNamespaceURI() == null ? attr.getNodeValue() : null;
                }
                if ("*".equals(namespaceURI) || namespaceURI.equals(attr.getNamespaceURI())) {
                    return attr.getNodeValue();
                }
            }
        }
    }
    return null;
}
 
Example 4
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 5
Source File: DOMUtil.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the next sibling node with the given qualified name. */
public static Element getNextSiblingElementNS(Node node,
        String[][] elemNames) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = sibling.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0]) &&
                        sibling.getLocalName().equals(elemNames[i][1])) {
                    return (Element)sibling;
                }
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}
 
Example 6
Source File: DOMUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent,
        String uri, String localpart) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String childURI = child.getNamespaceURI();
            if (childURI != null && childURI.equals(uri) &&
                    child.getLocalName().equals(localpart)) {
                return (Element)child;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 7
Source File: XMLUtils.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * @param sibling
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
            && sibling.getLocalName().equals(nodeName)) {
            if (number == 0){
                return (Element)sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}
 
Example 8
Source File: DOMNormalizer.java    From jdk8u60 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: SOAPFaultBuilder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This should be called from the client side to throw an {@link Exception} for a given soap mesage
 */
public Throwable createException(Map<QName, CheckedExceptionImpl> exceptions) throws JAXBException {
    DetailType dt = getDetail();
    Node detail = null;
    if(dt != null)  detail = dt.getDetail(0);

    //return ProtocolException if the detail is not present or there is no checked exception
    if(detail == null || exceptions == null){
        // No soap detail, doesnt look like its a checked exception
        // throw a protocol exception
        return attachServerException(getProtocolException());
    }

    //check if the detail is a checked exception, if not throw a ProtocolException
    QName detailName = new QName(detail.getNamespaceURI(), detail.getLocalName());
    CheckedExceptionImpl ce = exceptions.get(detailName);
    if (ce == null) {
        //No Checked exception for the received detail QName, throw a SOAPFault exception
        return attachServerException(getProtocolException());

    }

    if (ce.getExceptionType().equals(ExceptionType.UserDefined)) {
        return attachServerException(createUserDefinedException(ce));

    }
    Class exceptionClass = ce.getExceptionClass();
    try {
        Constructor constructor = exceptionClass.getConstructor(String.class, (Class) ce.getDetailType().type);
        Exception exception = (Exception) constructor.newInstance(getFaultString(), getJAXBObject(detail, ce));
        return attachServerException(exception);
    } catch (Exception e) {
        throw new WebServiceException(e);
    }
}
 
Example 10
Source File: DTMNodeProxy.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param listVector
 * @param tempNode
 * @param namespaceURI
 * @param localname
 * @param isNamespaceURIWildCard
 * @param isLocalNameWildCard
 *
 * Private method to be used for recursive iterations to obtain elements by tag name
 * and namespaceURI.
 */
private final void traverseChildren
(
 Vector listVector,
 Node tempNode,
 String namespaceURI,
 String localname,
 boolean isNamespaceURIWildCard,
 boolean isLocalNameWildCard)
 {
  if (tempNode == null)
  {
    return;
  }
  else
  {
    if (tempNode.getNodeType() == DTM.ELEMENT_NODE
            && (isLocalNameWildCard
                    || tempNode.getLocalName().equals(localname)))
    {
      String nsURI = tempNode.getNamespaceURI();
      if ((namespaceURI == null && nsURI == null)
             || isNamespaceURIWildCard
             || (namespaceURI != null && namespaceURI.equals(nsURI)))
      {
        listVector.add(tempNode);
      }
    }
    if(tempNode.hasChildNodes())
    {
      NodeList nl = tempNode.getChildNodes();
      for(int i = 0; i < nl.getLength(); i++)
      {
        traverseChildren(listVector, nl.item(i), namespaceURI, localname,
                         isNamespaceURIWildCard, isLocalNameWildCard);
      }
    }
  }
}
 
Example 11
Source File: Xml.java    From RADL with Apache License 2.0 5 votes vote down vote up
private static boolean hasDifferentNamespaceThanParent(Node node) {
  if (node.getNamespaceURI() == null || isNamespaceNode(node)) {
    return false;
  }
  Node parent;
  if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
    parent = ((Attr)node).getOwnerElement();
  } else {
    parent = node.getParentNode();
  }
  if (parent == null) {
    return true;
  }
  return !node.getNamespaceURI().equals(parent.getNamespaceURI());
}
 
Example 12
Source File: XMLUtils.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * @param sibling
 * @param uri
 * @param nodeName
 * @return nodes with the constraint
 */
public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
    List<Element> list = new ArrayList<Element>();
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
            && sibling.getLocalName().equals(nodeName)) {
            list.add((Element)sibling);
        }
        sibling = sibling.getNextSibling();
    }
    return list.toArray(new Element[list.size()]);
}
 
Example 13
Source File: XMLUtils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param sibling
 * @param uri
 * @param nodeName
 * @return nodes with the constraint
 */
public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
    List<Element> list = new ArrayList<Element>();
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
            && sibling.getLocalName().equals(nodeName)) {
            list.add((Element)sibling);
        }
        sibling = sibling.getNextSibling();
    }
    return list.toArray(new Element[list.size()]);
}
 
Example 14
Source File: SOAPFaultBuilder.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This should be called from the client side to throw an {@link Exception} for a given soap mesage
 */
public Throwable createException(Map<QName, CheckedExceptionImpl> exceptions) throws JAXBException {
    DetailType dt = getDetail();
    Node detail = null;
    if(dt != null)  detail = dt.getDetail(0);

    //return ProtocolException if the detail is not present or there is no checked exception
    if(detail == null || exceptions == null){
        // No soap detail, doesnt look like its a checked exception
        // throw a protocol exception
        return attachServerException(getProtocolException());
    }

    //check if the detail is a checked exception, if not throw a ProtocolException
    QName detailName = new QName(detail.getNamespaceURI(), detail.getLocalName());
    CheckedExceptionImpl ce = exceptions.get(detailName);
    if (ce == null) {
        //No Checked exception for the received detail QName, throw a SOAPFault exception
        return attachServerException(getProtocolException());

    }

    if (ce.getExceptionType().equals(ExceptionType.UserDefined)) {
        return attachServerException(createUserDefinedException(ce));

    }
    Class exceptionClass = ce.getExceptionClass();
    try {
        Constructor constructor = exceptionClass.getConstructor(String.class, (Class) ce.getDetailType().type);
        Exception exception = (Exception) constructor.newInstance(getFaultString(), getJAXBObject(detail, ce));
        return attachServerException(exception);
    } catch (Exception e) {
        throw new WebServiceException(e);
    }
}
 
Example 15
Source File: XMLUtils.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param sibling
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
            && sibling.getLocalName().equals(nodeName)) {
            if (number == 0){
                return (Element)sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}
 
Example 16
Source File: DOM2DTM.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Retrieves an attribute node by by qualified name and namespace URI.
 *
 * @param nodeHandle int Handle of the node upon which to look up this attribute..
 * @param namespaceURI The namespace URI of the attribute to
 *   retrieve, or null.
 * @param name The local name of the attribute to
 *   retrieve.
 * @return The attribute node handle with the specified name (
 *   <code>nodeName</code>) or <code>DTM.NULL</code> if there is no such
 *   attribute.
 */
public int getAttributeNode(int nodeHandle, String namespaceURI,
                            String name)
{

  // %OPT% This is probably slower than it needs to be.
  if (null == namespaceURI)
    namespaceURI = "";

  int type = getNodeType(nodeHandle);

  if (DTM.ELEMENT_NODE == type)
  {

    // Assume that attributes immediately follow the element.
    int identity = makeNodeIdentity(nodeHandle);

    while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
    {
      // Assume this can not be null.
      type = _type(identity);

                              // %REVIEW%
                              // Should namespace nodes be retrievable DOM-style as attrs?
                              // If not we need a separate function... which may be desirable
                              // architecturally, but which is ugly from a code point of view.
                              // (If we REALLY insist on it, this code should become a subroutine
                              // of both -- retrieve the node, then test if the type matches
                              // what you're looking for.)
      if (type == DTM.ATTRIBUTE_NODE || type==DTM.NAMESPACE_NODE)
      {
        Node node = lookupNode(identity);
        String nodeuri = node.getNamespaceURI();

        if (null == nodeuri)
          nodeuri = "";

        String nodelocalname = node.getLocalName();

        if (nodeuri.equals(namespaceURI) && name.equals(nodelocalname))
          return makeNodeHandle(identity);
      }

      else // if (DTM.NAMESPACE_NODE != type)
      {
        break;
      }
    }
  }

  return DTM.NULL;
}
 
Example 17
Source File: SamlTokenBuilder.java    From steady with Apache License 2.0 4 votes vote down vote up
public Assertion build(Element element, AssertionBuilderFactory factory) {
    
    SPConstants consts = SP11Constants.SP_NS.equals(element.getNamespaceURI())
        ? SP11Constants.INSTANCE : SP12Constants.INSTANCE;

    SamlToken samlToken = new SamlToken(consts);
    samlToken.setOptional(PolicyConstants.isOptional(element));
    samlToken.setIgnorable(PolicyConstants.isIgnorable(element));

    String attribute = element.getAttributeNS(element.getNamespaceURI(), SPConstants.ATTR_INCLUDE_TOKEN);
    if (attribute != null) {
        samlToken.setInclusion(consts.getInclusionFromAttributeValue(attribute));
    }
    
    Element child = DOMUtils.getFirstElement(element);
    boolean foundPolicy = false;
    while (child != null) {
        String ln = child.getLocalName();
        if (org.apache.neethi.Constants.ELEM_POLICY.equals(ln)) {
            foundPolicy = true;
            NodeList policyChildren = child.getChildNodes();
            if (policyChildren != null) {
                for (int i = 0; i < policyChildren.getLength(); i++) {
                    Node policyChild = policyChildren.item(i);
                    if (policyChild instanceof Element) {
                        QName qname = 
                            new QName(policyChild.getNamespaceURI(), policyChild.getLocalName());
                        String localname = qname.getLocalPart();
                        if (SPConstants.SAML_11_TOKEN_10.equals(localname)) {
                            samlToken.setUseSamlVersion11Profile10(true);
                        } else if (SPConstants.SAML_11_TOKEN_11.equals(localname)) {
                            samlToken.setUseSamlVersion11Profile11(true);
                        } else if (SPConstants.SAML_20_TOKEN_11.equals(localname)) {
                            samlToken.setUseSamlVersion20Profile11(true);
                        } else if (SPConstants.REQUIRE_DERIVED_KEYS.equals(localname)) {
                            samlToken.setDerivedKeys(true);
                        } else if (SPConstants.REQUIRE_EXPLICIT_DERIVED_KEYS.equals(localname)) {
                            samlToken.setExplicitDerivedKeys(true);
                        } else if (SPConstants.REQUIRE_IMPLIED_DERIVED_KEYS.equals(localname)) {
                            samlToken.setImpliedDerivedKeys(true);
                        } else if (SPConstants.REQUIRE_KEY_IDENTIFIER_REFERENCE.equals(localname)) {
                            samlToken.setRequireKeyIdentifierReference(true);
                        }
                    }
                }
            }
        }
        child = DOMUtils.getNextElement(child);
    }
    
    if (!foundPolicy && consts != SP11Constants.INSTANCE) {
        throw new IllegalArgumentException(
            "sp:SpnegoContextToken/wsp:Policy must have a value"
        );
    }
    
    return samlToken;
}
 
Example 18
Source File: ElementImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private Iterator getChildElements(final String nameUri, final String nameLocal) {
    return new Iterator() {
        Iterator eachElement = getChildElementNodes();
        Node next = null;
        Node last = null;

        public boolean hasNext() {
            if (next == null) {
                while (eachElement.hasNext()) {
                    Node element = (Node) eachElement.next();
                    String elementUri = element.getNamespaceURI();
                    elementUri = elementUri == null ? "" : elementUri;
                    String elementName = element.getLocalName();
                    if (elementUri.equals(nameUri)
                        && elementName.equals(nameLocal)) {
                        next = element;
                        break;
                    }
                }
            }
            return next != null;
        }

        public Object next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            last = next;
            next = null;
            return last;
        }

        public void remove() {
            if (last == null) {
                throw new IllegalStateException();
            }
            Node target = last;
            last = null;
            removeChild(target);
        }
    };
}
 
Example 19
Source File: DOM2DTM.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Retrieves an attribute node by by qualified name and namespace URI.
 *
 * @param nodeHandle int Handle of the node upon which to look up this attribute..
 * @param namespaceURI The namespace URI of the attribute to
 *   retrieve, or null.
 * @param name The local name of the attribute to
 *   retrieve.
 * @return The attribute node handle with the specified name (
 *   <code>nodeName</code>) or <code>DTM.NULL</code> if there is no such
 *   attribute.
 */
public int getAttributeNode(int nodeHandle, String namespaceURI,
                            String name)
{

  // %OPT% This is probably slower than it needs to be.
  if (null == namespaceURI)
    namespaceURI = "";

  int type = getNodeType(nodeHandle);

  if (DTM.ELEMENT_NODE == type)
  {

    // Assume that attributes immediately follow the element.
    int identity = makeNodeIdentity(nodeHandle);

    while (DTM.NULL != (identity = getNextNodeIdentity(identity)))
    {
      // Assume this can not be null.
      type = _type(identity);

                              // %REVIEW%
                              // Should namespace nodes be retrievable DOM-style as attrs?
                              // If not we need a separate function... which may be desirable
                              // architecturally, but which is ugly from a code point of view.
                              // (If we REALLY insist on it, this code should become a subroutine
                              // of both -- retrieve the node, then test if the type matches
                              // what you're looking for.)
      if (type == DTM.ATTRIBUTE_NODE || type==DTM.NAMESPACE_NODE)
      {
        Node node = lookupNode(identity);
        String nodeuri = node.getNamespaceURI();

        if (null == nodeuri)
          nodeuri = "";

        String nodelocalname = node.getLocalName();

        if (nodeuri.equals(namespaceURI) && name.equals(nodelocalname))
          return makeNodeHandle(identity);
      }

      else // if (DTM.NAMESPACE_NODE != type)
      {
        break;
      }
    }
  }

  return DTM.NULL;
}
 
Example 20
Source File: NamespaceContextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public String getNamespaceURI(String prefix) {
    Node parent = e;
    String namespace = null;

    if (prefix.equals("xml")) {
        namespace = WellKnownNamespace.XML_NAMESPACE_URI;
    } else {
        int type;

        while ((null != parent) && (null == namespace)
                && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
                || (type == Node.ENTITY_REFERENCE_NODE))) {
            if (type == Node.ELEMENT_NODE) {
                if (parent.getNodeName().indexOf(prefix + ':') == 0)
                    return parent.getNamespaceURI();
                NamedNodeMap nnm = parent.getAttributes();

                for (int i = 0; i < nnm.getLength(); i++) {
                    Node attr = nnm.item(i);
                    String aname = attr.getNodeName();
                    boolean isPrefix = aname.startsWith("xmlns:");

                    if (isPrefix || aname.equals("xmlns")) {
                        int index = aname.indexOf(':');
                        String p = isPrefix ? aname.substring(index + 1) : "";

                        if (p.equals(prefix)) {
                            namespace = attr.getNodeValue();

                            break;
                        }
                    }
                }
            }

            parent = parent.getParentNode();
        }
    }

    return namespace;
}