Example usage for org.w3c.dom Element getAttributeNS

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

Introduction

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

Prototype

public String getAttributeNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Retrieves an attribute value by local name and namespace URI.

Usage

From source file:org.exist.xquery.modules.sql.ExecuteFunction.java

private void setParametersOnPreparedStatement(Statement stmt, Element parametersElement)
        throws SQLException, XPathException {
    if (parametersElement.getNamespaceURI().equals(SQLModule.NAMESPACE_URI)
            && parametersElement.getLocalName().equals(PARAMETERS_ELEMENT_NAME)) {
        NodeList paramElements = parametersElement.getElementsByTagNameNS(SQLModule.NAMESPACE_URI,
                PARAM_ELEMENT_NAME);/* w w w .j ava2 s .  c  om*/

        for (int i = 0; i < paramElements.getLength(); i++) {
            Element param = ((Element) paramElements.item(i));
            Node child = param.getFirstChild();

            // Prevent NPE
            if (child != null) {
                if (child instanceof ReferenceNode) {
                    child = ((ReferenceNode) child).getReference().getNode();
                }

                final String value = child.getNodeValue();
                final String type = param.getAttributeNS(SQLModule.NAMESPACE_URI, TYPE_ATTRIBUTE_NAME);
                final int sqlType = SQLUtils.sqlTypeFromString(type);

                if (sqlType == Types.TIMESTAMP) {
                    final DateTimeValue dv = new DateTimeValue(value);
                    final Timestamp timestampValue = new Timestamp(dv.getDate().getTime());
                    ((PreparedStatement) stmt).setTimestamp(i + 1, timestampValue);

                } else {
                    ((PreparedStatement) stmt).setObject(i + 1, value, sqlType);
                }
            }

        }
    }
}

From source file:org.jboss.bpm.console.server.util.DOMUtils.java

/** Get the value from the given attribute
 *
 * @return null if the attribute value is empty or the attribute is not present
 *///from w w  w.j a v a 2 s .  c om
public static String getAttributeValue(Element el, QName attrName) {
    String attr = null;
    if ("".equals(attrName.getNamespaceURI()))
        attr = el.getAttribute(attrName.getLocalPart());
    else
        attr = el.getAttributeNS(attrName.getNamespaceURI(), attrName.getLocalPart());

    if ("".equals(attr))
        attr = null;

    return attr;
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyChildElement(SOAPElement parent, Element source) throws SOAPException {
    String localName = source.getLocalName();
    String prefix = source.getPrefix();
    String namespaceURI = source.getNamespaceURI();

    Name targetName;//w  ww. j a va 2 s . c o  m
    SOAPEnvelope envelope = findEnvelope(parent);

    if (prefix == null || prefix.length() == 0) {
        // source has no prefix, distinguish between no namespace and default namespace
        if (namespaceURI == null || namespaceURI.length() == 0) {
            // no namespace
            targetName = envelope.createName(localName);
            if (traceEnabled)
                log.trace("appended element: " + localName);
        } else {
            // default namespace, look for existing prefix at target
            prefix = getPrefix(namespaceURI, parent);

            // no prefix for that namespace?
            if (prefix == null) {
                prefix = XmlUtil.generatePrefix(DEFAULT_NAMESPACE_PREFIX, source);
            }
            // BPEL-195 source maps prefix to another URI?
            else if (!namespaceURI.equals(source.getAttributeNS(BpelConstants.NS_XMLNS, prefix))) {
                prefix = XmlUtil.generatePrefix(prefix, source);
            }

            targetName = envelope.createName(localName, prefix, namespaceURI);
            if (traceEnabled)
                log.trace("added child element: {" + namespaceURI + '}' + prefix + ':' + localName);
        }
    } else {
        // source has prefix
        targetName = envelope.createName(localName, prefix, namespaceURI);
        if (traceEnabled)
            log.trace("added child element: {" + namespaceURI + '}' + prefix + ':' + localName);
    }

    SOAPElement target;
    if (parent instanceof SOAPBody) {
        /*
         * jboss-ws4ee throws ClassCastException upon calling the remote endpoint if child elements
         * other than SOAPBodyElements are added to SOAPBody
         */
        SOAPBody body = (SOAPBody) parent;
        target = body.addBodyElement(targetName);
    } else
        target = parent.addChildElement(targetName);

    // namespaces
    copyNamespaces(target, source);
    ensureOwnNamespaceDeclared(target);
    // attributes
    copyAttributes(target, source);
    // child nodes
    copyChildNodes(target, source);
}

From source file:org.jbpm.bpel.integration.soap.SoapUtilTest.java

public void testCopyAttributes_domSoap() throws Exception {
    String xml = "<soap:Envelope xmlns:soap='" + SOAPConstants.URI_NS_SOAP_ENVELOPE + "'>"
            + " <soap:Body xmlns:fish='urn:example:fish'>"
            + "  <lunch time='1200' produce:lettuce='0.1lb' fish:fillet='0.25lb' "
            + "   xmlns:produce='urn:example:produce' />" + " </soap:Body>" + "</soap:Envelope>";
    SOAPMessage soapMessage = parseSoap(xml);
    SOAPElement source = SoapUtil.getElement(soapMessage.getSOAPBody(), "lunch");
    Element target = XmlUtil.createElement("detail");
    // perform the copy
    SoapUtil.copyAttributes(target, source);
    // qualified attributes
    assertEquals("0.1lb", target.getAttributeNS("urn:example:produce", "lettuce"));
    assertEquals("0.25lb", target.getAttributeNS("urn:example:fish", "fillet"));
    // local attribute
    assertEquals("1200", target.getAttribute("time"));
}

From source file:org.jbpm.bpel.integration.soap.SoapUtilTest.java

public void testCopyVisibleNamespaces_domSoap_targetMatch() throws Exception {
    String xml = "<soap:Envelope xmlns:soap='" + SOAPConstants.URI_NS_SOAP_ENVELOPE + "'>"
            + " <soap:Body xmlns:produce='urn:example:produce'>"
            + "  <meal:lunch produce:lettuce='0.1lb' fish:fillet='0.25lb' "
            + "   xmlns:fish='urn:example:fish' xmlns:meal='urn:example:meal'/>" + " </soap:Body>"
            + "</soap:Envelope>";
    SOAPMessage soapMessage = parseSoap(xml);
    SOAPElement source = SoapUtil.getElement(soapMessage.getSOAPBody(), "urn:example:meal", "lunch");

    String targetXml = "<detail xmlns:produce='urn:example:produce'>"
            + " <other:target xmlns:other='urn:example:other'/>" + "</detail>";
    Element target = XmlUtil.getElement(XmlUtil.parseText(targetXml), "urn:example:other", "target");

    // perform the copy
    SoapUtil.copyVisibleNamespaces(target, source);

    // prefixed declaration
    assertEquals("urn:example:fish", target.getAttributeNS(BpelConstants.NS_XMLNS, "fish"));
    assertEquals("urn:example:meal", target.getAttributeNS(BpelConstants.NS_XMLNS, "meal"));
    // parent prefixed declaration
    assertNull(target.getAttributeNodeNS(BpelConstants.NS_XMLNS, "produce"));
    assertEquals(SOAPConstants.URI_NS_SOAP_ENVELOPE, target.getAttributeNS(BpelConstants.NS_XMLNS, "soap"));
}

From source file:org.jbpm.bpel.integration.soap.SoapUtilTest.java

public void testCopy_domSoap_qualifiedNoPrefix() throws Exception {
    String xml = "<soapenv:Envelope xmlns:soapenv='" + SOAPConstants.URI_NS_SOAP_ENVELOPE + "'>"
            + " <soapenv:Body>" + "  <response xmlns='" + BpelConstants.NS_EXAMPLES + "'>" + "   <return>"
            + "    <amount>0.0</amount>" + "    <branch>140</branch>" + "    <capital>10419.18</capital>"
            + "    <status>1</status>" + "    <transaction xmlns:xsi='" + BpelConstants.NS_XML_SCHEMA_INSTANCE
            + "' xsi:nil='true'/>" + "   </return>" + "  </response>" + " </soapenv:Body>"
            + "</soapenv:Envelope>";
    SOAPMessage soapMessage = parseSoap(xml);
    SOAPElement source = SoapUtil.getElement(soapMessage.getSOAPBody(), BpelConstants.NS_EXAMPLES, "response");
    Element target = XmlUtil.createElement("detail");

    // perform the copy
    SoapUtil.copy(target, source);/*from  ww w  . ja v a 2s  .  com*/
    target = XmlUtilTest.writeAndRead(target);

    // namespaces
    assertEquals(SOAPConstants.URI_NS_SOAP_ENVELOPE, target.getAttributeNS(BpelConstants.NS_XMLNS, "soapenv"));

    // child elements
    Element returnElem = XmlUtil.getElement(target, BpelConstants.NS_EXAMPLES, "return");

    // namespaces
    assertEquals(BpelConstants.NS_EXAMPLES, returnElem.getAttributeNS(BpelConstants.NS_XMLNS, "xmlns"));

    // child elements
    assertEquals("0.0",
            DatatypeUtil.toString(XmlUtil.getElement(returnElem, BpelConstants.NS_EXAMPLES, "amount")));
    assertEquals("140",
            DatatypeUtil.toString(XmlUtil.getElement(returnElem, BpelConstants.NS_EXAMPLES, "branch")));
    assertEquals("10419.18",
            DatatypeUtil.toString(XmlUtil.getElement(returnElem, BpelConstants.NS_EXAMPLES, "capital")));
    assertEquals("1",
            DatatypeUtil.toString(XmlUtil.getElement(returnElem, BpelConstants.NS_EXAMPLES, "status")));

    Element transactionElem = XmlUtil.getElement(returnElem, BpelConstants.NS_EXAMPLES, "transaction");

    // namespaces
    assertEquals(BpelConstants.NS_XML_SCHEMA_INSTANCE,
            transactionElem.getAttributeNS(BpelConstants.NS_XMLNS, "xsi"));

    // attributes
    assertEquals("true", transactionElem.getAttributeNS(BpelConstants.NS_XML_SCHEMA_INSTANCE, "nil"));
}

From source file:org.ojbc.util.xml.TestOjbcNamespaceContext.java

@Test
public void testRootNamespacePopulation() throws Exception {

    Document d = db.newDocument();
    Element root = d.createElementNS(OjbcNamespaceContext.NS_PERSON_SEARCH_RESULTS_EXT, "e1");
    root.setPrefix(OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT);
    d.appendChild(root);//from  w w  w .  j av a 2  s  .  com
    Element child = XmlUtils.appendElement(root, OjbcNamespaceContext.NS_JXDM_41, "foo");
    XmlUtils.addAttribute(child, OjbcNamespaceContext.NS_STRUCTURES, "id", "I1");
    //XmlUtils.printNode(d);
    XmlUtils.OJBC_NAMESPACE_CONTEXT.populateRootNamespaceDeclarations(root);
    //XmlUtils.printNode(d);
    assertNotNull(XmlUtils.xPathNodeSearch(root,
            "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT));
    assertEquals(
            root.getAttributeNS("http://www.w3.org/2000/xmlns/",
                    OjbcNamespaceContext.NS_PREFIX_PERSON_SEARCH_RESULTS_EXT),
            OjbcNamespaceContext.NS_PERSON_SEARCH_RESULTS_EXT);
    assertNotNull(
            XmlUtils.xPathNodeSearch(root, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_STRUCTURES));
    assertEquals(
            root.getAttributeNS("http://www.w3.org/2000/xmlns/", OjbcNamespaceContext.NS_PREFIX_STRUCTURES),
            OjbcNamespaceContext.NS_STRUCTURES);
    assertNull(XmlUtils.xPathNodeSearch(root, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_ANSI_NIST));
    assertNotNull(
            XmlUtils.xPathNodeSearch(child, "namespace::xmlns:" + OjbcNamespaceContext.NS_PREFIX_JXDM_41));
    assertNull(
            child.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", OjbcNamespaceContext.NS_PREFIX_JXDM_41));
    assertNull(child.getAttributeNodeNS("http://www.w3.org/2000/xmlns/",
            OjbcNamespaceContext.NS_PREFIX_STRUCTURES));

}

From source file:org.onehippo.cms7.autoexport.InitializeItem.java

private DeltaInstruction parseInstructionElement(Element element, DeltaInstruction parent) {

    boolean isNode = element.getTagName().equals(QNODE);
    String name = element.getAttribute(QNAME);
    String directive = element.getAttributeNS(DELTA_URI, MERGE);

    DeltaInstruction instruction;//from ww w.  j  a  va  2 s. c  o  m
    if (parent == null) {
        instruction = new DeltaInstruction(isNode, name, directive, contextPath);
    } else {
        instruction = new DeltaInstruction(isNode, name, directive, parent);
    }
    if (instruction.isCombineDirective()) {
        final NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            final Node item = childNodes.item(i);
            if (item instanceof Element) {
                DeltaInstruction child = parseInstructionElement((Element) item, instruction);
                if (child == null) {
                    return null;
                }
                instruction.addInstruction(child);
            }
        }
    }
    if (instruction.isUnsupportedDirective()) {
        return null;
    }
    return instruction;
}

From source file:org.osaf.cosmo.calendar.hcalendar.HCalendarParser.java

private void buildProperty(Element element, String propName, ContentHandler handler) throws ParserException {
    if (element == null)
        return;//from   w  w  w  .  j av a2 s  .c  om

    if (log.isDebugEnabled())
        log.debug("Building property " + propName);

    String className = _className(propName);
    String elementName = element.getLocalName().toLowerCase();

    String value = null;
    if (elementName.equals("abbr")) {
        // "If an <abbr> element is used for a property, then the 'title'
        // attribute of the <abbr> element is the value of the property,
        // instead of the contents of the element, which instead provide a
        // human presentable version of the value."
        value = element.getAttribute("title");
        if (StringUtils.isBlank(value))
            throw new ParserException("Abbr element '" + className + "' requires a non-empty title", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from title attribute");
    } else if (isHeaderElement(elementName)) {
        // try title first. if that's not set, fall back to text content.
        value = element.getAttribute("title");
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from title attribute");
        } else {
            value = getTextContent(element);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    } else if (elementName.equals("a") && isUrlProperty(propName)) {
        value = element.getAttribute("href");
        if (StringUtils.isBlank(value))
            throw new ParserException("A element '" + className + "' requires a non-empty href", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from href attribute");
    } else if (elementName.equals("img")) {
        if (isUrlProperty(propName)) {
            value = element.getAttribute("src");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty src", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from src attribute");
        } else {
            value = element.getAttribute("alt");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty alt", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from alt attribute");
        }
    } else {
        value = getTextContent(element);
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    }

    if (StringUtils.isBlank(value)) {
        if (log.isDebugEnabled())
            log.debug("Skipping property with empty value");
        return;
    }

    handler.startProperty(propName);

    // if it's a date property, we have to convert from the
    // hCalendar-formatted date (RFC 3339) to an iCalendar-formatted date
    if (isDateProperty(propName)) {
        try {
            Date date = _icalDate(value);
            value = date.toString();

            if (!(date instanceof DateTime))
                try {
                    handler.parameter(Parameter.VALUE, Value.DATE.getValue());
                } catch (Exception e) {
                }
        } catch (ParseException e) {
            throw new ParserException("Malformed date value for element '" + className + "'", -1, e);
        }
    }

    if (isTextProperty(propName)) {
        String lang = element.getAttributeNS(XMLConstants.XML_NS_URI, "lang");
        if (!StringUtils.isBlank(lang))
            try {
                handler.parameter(Parameter.LANGUAGE, lang);
            } catch (Exception e) {
            }
    }

    // XXX: other parameters?

    try {
        handler.propertyValue(value);
    } catch (URISyntaxException e) {
        throw new ParserException("Malformed URI value for element '" + className + "'", -1, e);
    } catch (ParseException e) {
        throw new ParserException("Malformed value for element '" + className + "'", -1, e);
    } catch (IOException e) {
        throw new RuntimeException("Unknown error setting property value for element '" + className + "'", e);
    }

    handler.endProperty(propName);
}

From source file:org.regenstrief.util.XMLUtil.java

public final static String getAttributeIgnoreNamespace(final Element xml, final String attr,
        final String namespaceURI) {
    String attrValue = xml.getAttribute(attr);
    if (Util.isEmpty(attrValue)) {
        attrValue = xml.getAttributeNS(namespaceURI, attr);
    }/* w w w. ja  v a 2  s  .  c o  m*/
    return attrValue;
}