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

The following are Jave code examples for showing how to use getNamespaceURI() 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.
+ Save this method
Example 1
Project: OpenJSharp   File: DOMUtil.java   View Source Code Vote up 7 votes
/** Finds and returns the first child node with the given qualified name. */
public static Element getFirstChildElementNS(Node parent,
        String uri, String localpart) {

    // search for node
    Node child = parent.getFirstChild();
    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.getNextSibling();
    }

    // not found
    return null;

}
 
Example 2
Project: gemini.blueprint   File: ParsingUtils.java   View Source Code Vote up 6 votes
public static BeanDefinitionHolder decorateIfRequired(Node node, BeanDefinitionHolder originalDef,
		ParserContext parserContext) {

	String namespaceUri = node.getNamespaceURI();
	if (!parserContext.getDelegate().isDefaultNamespace(namespaceUri) && !isRFC124Namespace(namespaceUri)) {
		NamespaceHandler handler =
				parserContext.getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri);
		if (handler != null) {
			return handler.decorate(node, originalDef, new ParserContext(parserContext.getReaderContext(),
					parserContext.getDelegate()));
		} else if (namespaceUri.startsWith("http://www.springframework.org/")) {
			parserContext.getReaderContext().error(
					"Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]",
					node);
		} else {
			// A custom namespace, not to be handled by Spring - maybe "xml:...".
		}
	}
	return originalDef;
}
 
Example 3
Project: OpenJSharp   File: DOMUtil.java   View Source Code Vote up 6 votes
/** 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 4
Project: javaide   File: ExtraPackage.java   View Source Code Vote up 6 votes
private String[] parseProjectFiles(Node projectFilesNode) {
    ArrayList<String> paths = new ArrayList<String>();

    if (projectFilesNode != null) {
        String nsUri = projectFilesNode.getNamespaceURI();
        for(Node child = projectFilesNode.getFirstChild();
                 child != null;
                 child = child.getNextSibling()) {

            if (child.getNodeType() == Node.ELEMENT_NODE &&
                    nsUri.equals(child.getNamespaceURI()) &&
                    RepoConstants.NODE_PATH.equals(child.getLocalName())) {
                String path = child.getTextContent();
                if (path != null) {
                    path = path.trim();
                    if (path.length() > 0) {
                        paths.add(path);
                    }
                }
            }
        }
    }

    return paths.toArray(new String[paths.size()]);
}
 
Example 5
Project: OpenJSharp   File: DOMUtil.java   View Source Code Vote up 6 votes
/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent,
        String[][] elemNames) {

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

    // not found
    return null;

}
 
Example 6
Project: OpenJSharp   File: DOMUtil.java   View Source Code Vote up 6 votes
/** Finds and returns the first child node with the given qualified name. */
public static Element getFirstChildElementNS(Node parent,
        String[][] elemNames) {

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

    // not found
    return null;

}
 
Example 7
Project: OpenJSharp   File: DTMNodeProxy.java   View Source Code Vote up 5 votes
/**
 *
 * @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 8
Project: OpenJSharp   File: AttList.java   View Source Code Vote up 5 votes
/**
 * Look up the index of an attribute by Namespace name.
 *
 * @param uri The Namespace URI, or the empty string if
 *        the name has no Namespace URI.
 * @param localPart The attribute's local name.
 * @return The index of the attribute, or -1 if it does not
 *         appear in the list.
 */
public int getIndex(String uri, String localPart)
{
  for(int i=m_attrs.getLength()-1;i>=0;--i)
  {
    Node a=m_attrs.item(i);
    String u=a.getNamespaceURI();
    if( (u==null ? uri==null : u.equals(uri))
        &&
        a.getLocalName().equals(localPart) )
      return i;
  }
  return -1;
}
 
Example 9
Project: openjdk-jdk10   File: AttList.java   View Source Code Vote up 5 votes
/**
 * Look up the index of an attribute by Namespace name.
 *
 * @param uri The Namespace URI, or the empty string if
 *        the name has no Namespace URI.
 * @param localPart The attribute's local name.
 * @return The index of the attribute, or -1 if it does not
 *         appear in the list.
 */
public int getIndex(String uri, String localPart)
{
  for(int i=m_attrs.getLength()-1;i>=0;--i)
  {
    Node a=m_attrs.item(i);
    String u=a.getNamespaceURI();
    if( (u==null ? uri==null : u.equals(uri))
        &&
        a.getLocalName().equals(localPart) )
      return i;
  }
  return -1;
}
 
Example 10
Project: convertigo-engine   File: XMLUtils.java   View Source Code Vote up 5 votes
public static void spreadNamespaces(Node node, String tns, boolean overwrite) {
	Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument();
	boolean isParent = false;
	while (node != null) {
		Node next = null;
		if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) {
			if (node.getNamespaceURI() == null) {
				node = doc.renameNode(node, tns, node.getNodeName());
			} else {
				if (overwrite) {
					tns = node.getNamespaceURI();
				}
			}
			NamedNodeMap nodeMap = node.getAttributes();
			int nodeMapLengthl = nodeMap.getLength();
			for (int i = 0; i < nodeMapLengthl; i++) {
				Node attr = nodeMap.item(i);
				if (attr.getNamespaceURI() == null) {
					doc.renameNode(attr, tns, attr.getNodeName());
				}
			}
		}
		isParent = (isParent || (next = node.getFirstChild()) == null) && (next = node.getNextSibling()) == null;
		node = isParent ? node.getParentNode() : next;
		if (isParent && node != null) {
			if (overwrite) {
				tns = node.getNamespaceURI();
			}
		}
	}
}
 
Example 11
Project: openjdk-jdk10   File: XMLUtils.java   View Source Code Vote up 5 votes
/**
 * @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 12
Project: OpenJSharp   File: AttList.java   View Source Code Vote up 5 votes
/**
 * Look up the index of an attribute by Namespace name.
 *
 * @param uri The Namespace URI, or the empty string if
 *        the name has no Namespace URI.
 * @param localPart The attribute's local name.
 * @return The index of the attribute, or -1 if it does not
 *         appear in the list.
 */
public int getIndex(String uri, String localPart)
{
  for(int i=m_attrs.getLength()-1;i>=0;--i)
  {
    Node a=m_attrs.item(i);
    String u=a.getNamespaceURI();
    if( (u==null ? uri==null : u.equals(uri))
    &&
    a.getLocalName().equals(localPart) )
  return i;
  }
  return -1;
}
 
Example 13
Project: lams   File: XMLHelper.java   View Source Code 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 14
Project: lams   File: XMLHelper.java   View Source Code 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 15
Project: OpenJSharp   File: DOMNormalizer.java   View Source Code 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 16
Project: openjdk-jdk10   File: SOAPFaultBuilder.java   View Source Code Vote up 5 votes
/**
 * 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 17
Project: OpenJSharp   File: XMLUtils.java   View Source Code Vote up 5 votes
/**
 * @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 18
Project: openjdk-jdk10   File: PrefixResolverDefault.java   View Source Code Vote up 4 votes
/**
 * Given a namespace, get the corrisponding prefix.
 * Warning: This will not work correctly if namespaceContext
 * is an attribute node.
 * @param prefix Prefix to resolve.
 * @param namespaceContext Node from which to start searching for a
 * xmlns attribute that binds a prefix to a namespace.
 * @return Namespace that prefix resolves to, or null if prefix
 * is not bound.
 */
public String getNamespaceForPrefix(String prefix,
                                    org.w3c.dom.Node namespaceContext)
{

  Node parent = namespaceContext;
  String namespace = null;

  if (prefix.equals("xml"))
  {
    namespace = Constants.S_XMLNAMESPACEURI;
  }
  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;
}
 
Example 19
Project: openjdk-jdk10   File: DOMUtil.java   View Source Code Vote up 4 votes
public static String getNamespaceURI(Node node) {
    return node.getNamespaceURI();
}
 
Example 20
Project: openjdk-jdk10   File: DOM2Helper.java   View Source Code Vote up 2 votes
/**
 * Returns the Namespace Name (Namespace URI) for the given node. In a Level
 * 2 DOM, you can ask the node itself. Note, however, that doing so
 * conflicts with our decision in getLocalNameOfNode not to trust the that
 * the DOM was indeed created using the Level 2 methods. If Level 1 methods
 * were used, these two functions will disagree with each other.
 * <p>
 * TODO: Reconcile with getLocalNameOfNode.
 *
 * @param n Node to be examined
 *
 * @return String containing the Namespace URI bound to this DOM node at the
 * time the Node was created.
 */
public static String getNamespaceOfNode(Node n) {
    return n.getNamespaceURI();
}