Example usage for org.w3c.dom Element getNamespaceURI

List of usage examples for org.w3c.dom Element getNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Element getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

public static DirectAddressing getDirectAddressing(String mtom) throws MessagingException, IOException {

    SOAPWithAttachment swa = Parsing.parseMtom(mtom);
    DirectAddressing directAddressing = new DirectAddressing();
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(swa.getSoap()), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return directAddressing;
    }/*  ww w . j a  v a 2 s .c  om*/
    Iterator it = headers.iterator();
    boolean foundDirectAddressBlock = false;
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_DIRECT_ADDRESS_BLOCK)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            foundDirectAddressBlock = true;
            NodeList directFrom = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_FROM);
            NodeList directTo = header.getElementsByTagNameNS(NAMESPACE_DIRECT, ELEMENT_NAME_DIRECT_TO);

            directAddressing.setDirectFrom(directFrom.item(0).getFirstChild().getNodeValue());
            directAddressing.setDirectTo(directTo.item(0).getFirstChild().getNodeValue());
        } else if (header.getLocalName().equals(ELEMENT_NAME_WSA_MESSAGEID)
                && header.getNamespaceURI().equals(NAMESPACE_WSA)) {
            directAddressing.setMessageID(header.getFirstChild().getNodeValue());
        }
    }
    return directAddressing;
}

From source file:gov.nist.healthcare.ttt.parsing.Parsing.java

private static MetadataLevel getMetadataLevelFromSoap(String soap) {
    Envelope env = (Envelope) JAXB.unmarshal(new StringReader(soap), Envelope.class);
    List<Object> headers = env.getHeader().getAny();
    if (headers == null) {
        return MetadataLevel.XDS;
    }/*from   w  w w  .  j a  v  a  2 s  . c om*/
    Iterator it = headers.iterator();
    while (it.hasNext()) {
        Element header = (Element) it.next();
        if (header.getLocalName().equals(ELEMENT_NAME_METADATA_LEVEL)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            String metadataLevel = header.getFirstChild().getTextContent();
            if (metadataLevel.equals(METADATA_LEVEL_MINIMAL)) {
                return MetadataLevel.MINIMAL;
            } else if (metadataLevel.equals(METADATA_LEVEL_XDS)) {
                return MetadataLevel.XDS;
            }
        } else if (header.getLocalName().equals(ELEMENT_NAME_DIRECT_ADDRESS_BLOCK)
                && header.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
            Element directAddressBlock = header;
            NodeList childrenDirectAddressBlock = directAddressBlock.getChildNodes();
            for (int i = 0; i < childrenDirectAddressBlock.getLength(); i++) {
                Element child = null;
                if (childrenDirectAddressBlock.item(i) instanceof Element) {
                    child = (Element) childrenDirectAddressBlock.item(i);
                    if (child.getLocalName().equals(ELEMENT_NAME_METADATA_LEVEL)
                            && child.getNamespaceURI().equals(NAMESPACE_DIRECT)) {
                        String metadataLevel = child.getFirstChild().getTextContent();
                        if (metadataLevel.equals(METADATA_LEVEL_MINIMAL)) {
                            return MetadataLevel.MINIMAL;
                        } else if (metadataLevel.equals(METADATA_LEVEL_XDS)) {
                            return MetadataLevel.XDS;
                        }
                    }
                }
            }
        }
    }
    return MetadataLevel.XDS;
}

From source file:DOMUtils.java

public static void compareNodes(Node expected, Node actual) throws Exception {
    if (expected.getNodeType() != actual.getNodeType()) {
        throw new Exception("Different types of nodes: " + expected + " " + actual);
    }/*  w ww  .  j a  v  a2 s. c om*/
    if (expected instanceof Document) {
        Document expectedDoc = (Document) expected;
        Document actualDoc = (Document) actual;
        compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
    } else if (expected instanceof Element) {
        Element expectedElement = (Element) expected;
        Element actualElement = (Element) actual;

        // compare element names
        if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) {
            throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " "
                    + actualElement.getLocalName());
        }
        // compare element ns
        String expectedNS = expectedElement.getNamespaceURI();
        String actualNS = actualElement.getNamespaceURI();
        if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) {
            throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS);
        }

        String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();

        // compare attributes
        NamedNodeMap expectedAttrs = expectedElement.getAttributes();
        NamedNodeMap actualAttrs = actualElement.getAttributes();
        if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) {
            throw new Exception(elementName + ": Number of attributes do not match up: "
                    + countNonNamespaceAttribures(expectedAttrs) + " "
                    + countNonNamespaceAttribures(actualAttrs));
        }
        for (int i = 0; i < expectedAttrs.getLength(); i++) {
            Attr expectedAttr = (Attr) expectedAttrs.item(i);
            if (expectedAttr.getName().startsWith("xmlns")) {
                continue;
            }
            Attr actualAttr = null;
            if (expectedAttr.getNamespaceURI() == null) {
                actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName());
            } else {
                actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(),
                        expectedAttr.getLocalName());
            }
            if (actualAttr == null) {
                throw new Exception(elementName + ": No attribute found:" + expectedAttr);
            }
            if (!expectedAttr.getValue().equals(actualAttr.getValue())) {
                throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue()
                        + " " + actualAttr.getValue());
            }
        }

        // compare children
        NodeList expectedChildren = expectedElement.getChildNodes();
        NodeList actualChildren = actualElement.getChildNodes();
        if (expectedChildren.getLength() != actualChildren.getLength()) {
            throw new Exception(elementName + ": Number of children do not match up: "
                    + expectedChildren.getLength() + " " + actualChildren.getLength());
        }
        for (int i = 0; i < expectedChildren.getLength(); i++) {
            Node expectedChild = expectedChildren.item(i);
            Node actualChild = actualChildren.item(i);
            compareNodes(expectedChild, actualChild);
        }
    } else if (expected instanceof Text) {
        String expectedData = ((Text) expected).getData().trim();
        String actualData = ((Text) actual).getData().trim();

        if (!expectedData.equals(actualData)) {
            throw new Exception("Text does not match: " + expectedData + " " + actualData);
        }
    }
}

From source file:microsoft.exchange.webservices.data.core.EwsServiceXmlWriter.java

/**
 * @param element DOM element//from   ww  w.j a  v a 2s  .co m
 * @param writer XML stream writer
 * @throws XMLStreamException the XML stream exception
 */
public static void addElement(Element element, XMLStreamWriter writer) throws XMLStreamException {
    String nameSpace = element.getNamespaceURI();
    String prefix = element.getPrefix();
    String localName = element.getLocalName();
    if (prefix == null) {
        prefix = "";
    }
    if (localName == null) {
        localName = element.getNodeName();

        if (localName == null) {
            throw new IllegalStateException("Element's local name cannot be null!");
        }
    }

    String decUri = writer.getNamespaceContext().getNamespaceURI(prefix);
    boolean declareNamespace = decUri == null || !decUri.equals(nameSpace);

    if (nameSpace == null || nameSpace.length() == 0) {
        writer.writeStartElement(localName);
    } else {
        writer.writeStartElement(prefix, localName, nameSpace);
    }

    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);

        String name = attr.getNodeName();
        String attrPrefix = "";
        int prefixIndex = name.indexOf(':');
        if (prefixIndex != -1) {
            attrPrefix = name.substring(0, prefixIndex);
            name = name.substring(prefixIndex + 1);
        }

        if ("xmlns".equals(attrPrefix)) {
            writer.writeNamespace(name, attr.getNodeValue());
            if (name.equals(prefix) && attr.getNodeValue().equals(nameSpace)) {
                declareNamespace = false;
            }
        } else {
            if ("xmlns".equals(name) && "".equals(attrPrefix)) {
                writer.writeNamespace("", attr.getNodeValue());
                if (attr.getNodeValue().equals(nameSpace)) {
                    declareNamespace = false;
                }
            } else {
                writer.writeAttribute(attrPrefix, attr.getNamespaceURI(), name, attr.getNodeValue());
            }
        }
    }

    if (declareNamespace) {
        if (nameSpace == null) {
            writer.writeNamespace(prefix, "");
        } else {
            writer.writeNamespace(prefix, nameSpace);
        }
    }

    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        writeNode(n, writer);
    }

    writer.writeEndElement();

}

From source file:eu.esdihumboldt.hale.common.core.io.HaleIO.java

/**
 * Get the value of a complex property represented as a DOM element.
 * //from  w w w  .j  a v  a 2  s  . com
 * @param element the DOM element
 * @param expectedType the expected parameter type, this must be either
 *            {@link String}, DOM {@link Element} or a complex value type
 *            defined in the {@link ComplexValueExtension}
 * @param context the context object, may be <code>null</code>
 * @return the complex value or <code>null</code> if it could not be created
 *         from the element
 */
@SuppressWarnings("unchecked")
public static <T> T getComplexValue(Element element, Class<T> expectedType, Object context) {
    if (element == null) {
        return null;
    }

    QName name;
    if (element.getNamespaceURI() != null && !element.getNamespaceURI().isEmpty()) {
        name = new QName(element.getNamespaceURI(), element.getLocalName());
    } else {
        String ln = element.getTagName(); // .getLocalName();
        name = new QName(ln);
    }
    ComplexValueDefinition cvt = ComplexValueExtension.getInstance().getDefinition(name);
    Object value = null;
    if (cvt != null) {
        try {
            value = cvt.fromDOM(element, context);
        } catch (Exception e) {
            throw new IllegalStateException("Failed to load complex value from DOM", e);
        }
    }

    if (value != null && expectedType.isAssignableFrom(value.getClass())) {
        return (T) value;
    }

    // maybe the element itself is OK
    if (expectedType.isAssignableFrom(element.getClass())) {
        return (T) element;
    }

    if (expectedType.isAssignableFrom(String.class)) {
        // FIXME use legacy complex value if possible
    }

    return null;
}

From source file:XMLUtils.java

/**
 * Get the first child element to match a given name. 
 * Look for a child element in the same namespace as the parent.
 * //from  w  w w .  ja  v  a 2 s  .  co m
 * @param parent
 * @param name
 * @return
 */
public static Element getChild(Element parent, String ns, String name) {
    Element child = getFirstChild(parent);
    while (child != null) {
        if (child.getLocalName().equals(name)) {
            if (ns == null && child.getNamespaceURI() == null) {
                return child;
            } else if (ns != null && ns.equals(child.getNamespaceURI())) {
                return child;
            }
        }
        child = getNext(child);
    }
    return child;
}

From source file:be.fedict.eid.dss.spi.utils.XAdESUtils.java

/**
 * Find the next sibling at DOM level of the given XAdES DOM element.
 * /*from w ww. java  2 s. c o  m*/
 * @param xadesElement
 * @param namespace
 * @param localName
 * @param jaxbType
 * @return
 * @throws XAdESValidationException
 */
public static <T> T findNextSibling(Element xadesElement, String namespace, String localName, Class<T> jaxbType)
        throws XAdESValidationException {
    Node siblingNode = xadesElement.getNextSibling();
    while (siblingNode != null && siblingNode.getNodeType() != Node.ELEMENT_NODE) {
        /*
         * Can happen as shown during latest ETSI XAdES plugtests.
         */
        LOG.debug("skipping a non-Element sibling: " + siblingNode.getNodeType());
        if (Node.TEXT_NODE == siblingNode.getNodeType()) {
            LOG.debug("TEXT node sibling: \"" + siblingNode.getNodeValue() + "\"");
        }
        siblingNode = siblingNode.getNextSibling();
    }
    if (null == siblingNode) {
        return null;
    }
    Element element = (Element) siblingNode;
    if (false == namespace.equals(element.getNamespaceURI())) {
        return null;
    }
    if (false == localName.equals(element.getLocalName())) {
        return null;
    }
    return unmarshall(element, jaxbType);
}

From source file:XMLUtils.java

/**
 * Adds an element as a child of a given element. 
 * The child is created with the same namespace as the parent. 
 * @param parent/*from   w  w w  .  j  a v  a  2  s  . co  m*/
 * @param name
 * @return
 */
public static Element addElement(Element parent, String name) {
    Document doc = parent.getOwnerDocument();
    String qname;
    if (parent.getPrefix() != null) {
        qname = parent.getPrefix() + ":" + name;
    } else {
        qname = name;
    }
    Element child = doc.createElementNS(parent.getNamespaceURI(), qname);
    parent.appendChild(child);
    return child;
}

From source file:org.escidoc.browser.elabsmodul.service.ELabsService.java

private static InvestigationBean resolveInvestigation(final ContainerProxy containerProxy) {
    final String URI_DC = "http://purl.org/dc/elements/1.1/";
    final String URI_EL = "http://escidoc.org/ontologies/bw-elabs/re#";
    final String URI_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

    if (containerProxy == null) {
        throw new NullPointerException("Container Proxy is null.");
    }/*  w w w .j ava  2  s .c  o m*/
    final InvestigationBean investigationBean = new InvestigationBean();
    final Element e = containerProxy.getMetadataRecords().get("escidoc").getContent();
    investigationBean.setObjid(containerProxy.getId());

    if (!(("Investigation".equals(e.getLocalName()) && URI_EL.equals(e.getNamespaceURI()))
            || "el:Investigation".equals(e.getTagName()))) {
        LOG.error("Container is not an eLabs Investigation");
        return investigationBean;
    }

    final NodeList nodeList = e.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        final String nodeName = node.getLocalName();
        final String nsUri = node.getNamespaceURI();

        if (nodeName == null || nsUri == null) {
            continue;
        }

        if ("title".equals(nodeName) && URI_DC.equals(nsUri)) {
            investigationBean.setName(node.getTextContent());
        } else if ("description".equals(nodeName) && URI_DC.equals(nsUri)) {
            investigationBean.setDescription(node.getTextContent());
        } else if ("max-runtime".equals(nodeName) && URI_EL.equals(nsUri)) {
            investigationBean.setMaxRuntime("<<not used>>");
            try {
                investigationBean.setMaxRuntimeInMin(Integer.valueOf(node.getTextContent()));
            } catch (final NumberFormatException nfe) {
                LOG.error(nfe.getMessage());
                investigationBean.setMaxRuntimeInMin(0);
            }
        } else if ("deposit-endpoint".equals(nodeName) && URI_EL.equals(nsUri)) {
            investigationBean.setDepositEndpoint(node.getTextContent());
        } else if ("investigator".equals(nodeName) && URI_EL.equals(nsUri)) {
            final String investigatorId = node.getAttributes().getNamedItemNS(URI_RDF, "resource")
                    .getTextContent();
            investigationBean.setInvestigator(investigatorId);
        } else if ("rig".equals(nodeName) && URI_EL.equals(nsUri)) {
            final String rigId = node.getAttributes().getNamedItemNS(URI_RDF, "resource").getTextContent();
            if (StringUtils.notEmpty(rigId)) {
                final RigBean rigBean = new RigBean();
                rigBean.setObjectId(rigId);
                investigationBean.setRigBean(rigBean);
            }
        } else if ("instrument".equals(nodeName) && URI_EL.equals(nsUri)) {
            final String instrument = node.getAttributes().getNamedItemNS(URI_RDF, "resource").getTextContent();
            final String folder = node.getTextContent().trim();
            investigationBean.getInstrumentFolder().put(instrument, folder);
        }
    }
    return investigationBean;
}

From source file:eu.semaine.util.XMLTool.java

/**
 * For the given element, return the value of the given attribute if it exists,
 * or complain with a MessageFormatException if it doesn't exist.
 * @param e the element whose attribute to return
 * @param attributeName the name of the attribute to look up.
 * @return the String value of the attribute if it exists
 * @throws MessageFormatException if the attribute doesn't exist.
 *///  w  w  w.j  av a  2s.  c  om
public static String needAttribute(Element e, String attributeName) throws MessageFormatException {
    if (!e.hasAttribute(attributeName)) {
        throw new MessageFormatException("Element '" + e.getTagName() + "' in namespace '" + e.getNamespaceURI()
                + "' needs an attribute '" + attributeName + "'");
    }
    return e.getAttribute(attributeName);
}