Example usage for org.w3c.dom Element getAttributeNodeNS

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

Introduction

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

Prototype

public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Retrieves an Attr node by local name and namespace URI.

Usage

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Drop the attributes from an element, except possibly an <code>xmlns</code>
 * attribute that declares its namespace.
 * @param target the element whose attributes will be removed.
 * @param flag preserve namespace declaration
 *//*from   ww  w  .j  a  v  a2  s .  com*/
public static void removeAttributes(Element target, boolean flag) {
    if (!target.hasAttributes()) {
        return;
    }
    String prefix = target.getPrefix();
    NamedNodeMap nnm = target.getAttributes();
    Attr toPutBack = null;
    if (flag) {
        if (prefix == null) {
            toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns");
        } else {
            toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns:" + prefix);
        }

    }
    while (nnm.getLength() != 0) {
        target.removeAttributeNode((Attr) nnm.item(0));
    }
    if (toPutBack != null) {
        target.setAttributeNodeNS(toPutBack);
    }
}

From source file:org.apache.ws.security.util.WSSecurityUtil.java

/**
 * Returns the first WS-Security header element for a given actor. Only one
 * WS-Security header is allowed for an actor.
 * /*from  w  w  w  . j  a  v a  2s  .c o m*/
 * @param doc
 * @param actor
 * @return the <code>wsse:Security</code> element or <code>null</code>
 *         if not such element found
 */
public static Element getSecurityHeader(Document doc, String actor, SOAPConstants sc) {
    Element soapHeaderElement = (Element) getDirectChild(doc.getDocumentElement(),
            sc.getHeaderQName().getLocalPart(), sc.getEnvelopeURI());
    if (soapHeaderElement == null) { // no SOAP header at all
        return null;
    }

    // get all wsse:Security nodes
    NodeList list = soapHeaderElement.getElementsByTagNameNS(WSConstants.WSSE_NS, WSConstants.WSSE_LN);
    if (list == null) {
        return null;
    }
    for (int i = 0; i < list.getLength(); i++) {
        Element elem = (Element) list.item(i);
        Attr attr = elem.getAttributeNodeNS(sc.getEnvelopeURI(), sc.getRoleAttributeQName().getLocalPart());
        String hActor = (attr != null) ? attr.getValue() : null;
        if (WSSecurityUtil.isActorEqual(actor, hActor)) {
            return elem;
        }
    }
    return null;
}

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

/**
 * Returns the Attr[]s to be output for the given element.
 * <br>/*from w  w w . j  a v  a  2 s.  c  o  m*/
 * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a 
 * DOM which has been prepared using 
 * {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650(
 * org.w3c.dom.Document)}.
 * 
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
        throws CanonicalizationException {
    // result will contain the attrs which have to be output
    xmlattrStack.push(ns.getLevel());
    boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
    final SortedSet<Attr> result = this.result;
    result.clear();

    if (element.hasAttributes()) {
        NamedNodeMap attrs = element.getAttributes();
        int attrsLength = attrs.getLength();

        for (int i = 0; i < attrsLength; i++) {
            Attr attribute = (Attr) attrs.item(i);
            String NUri = attribute.getNamespaceURI();
            String NName = attribute.getLocalName();
            String NValue = attribute.getValue();

            if (!XMLNS_URI.equals(NUri)) {
                //A non namespace definition node.
                if (XML_LANG_URI.equals(NUri)) {
                    if (NName.equals("id")) {
                        if (isRealVisible) {
                            // treat xml:id like any other attribute 
                            // (emit it, but don't inherit it)
                            result.add(attribute);
                        }
                    } else {
                        xmlattrStack.addXmlnsAttr(attribute);
                    }
                } else if (isRealVisible) {
                    //The node is visible add the attribute to the list of output attributes.
                    result.add(attribute);
                }
            } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) {
                /* except omit namespace node with local name xml, which defines
                 * the xml prefix, if its string value is 
                 * http://www.w3.org/XML/1998/namespace.
                 */
                // add the prefix binding to the ns symb table.
                if (isVisible(attribute)) {
                    if (isRealVisible || !ns.removeMappingIfRender(NName)) {
                        // The xpath select this node output it if needed.
                        Node n = ns.addMappingAndRender(NName, NValue, attribute);
                        if (n != null) {
                            result.add((Attr) n);
                            if (C14nHelper.namespaceIsRelative(attribute)) {
                                Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                                throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace",
                                        exArgs);
                            }
                        }
                    }
                } else {
                    if (isRealVisible && !XMLNS.equals(NName)) {
                        ns.removeMapping(NName);
                    } else {
                        ns.addMapping(NName, NValue, attribute);
                    }
                }
            }
        }
    }

    if (isRealVisible) {
        //The element is visible, handle the xmlns definition        
        Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
        Node n = null;
        if (xmlns == null) {
            //No xmlns def just get the already defined.
            n = ns.getMapping(XMLNS);
        } else if (!isVisible(xmlns)) {
            //There is a definition but the xmlns is not selected by the xpath.
            //then xmlns=""
            n = ns.addMappingAndRender(XMLNS, "", nullNode);
        }
        //output the xmlns def if needed.
        if (n != null) {
            result.add((Attr) n);
        }
        //Float all xml:* attributes of the unselected parent elements to this one. 
        xmlattrStack.getXmlnsAttr(result);
        ns.getUnrenderedNodes(result);
    }

    return result.iterator();
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * This method spreads all namespace attributes in a DOM document to their
 * children. This is needed because the XML Signature XPath transform
 * must evaluate the XPath against all nodes in the input, even against
 * XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are
 * not fully visible in the Xalan XPath model, so we have to do this by
 * hand in DOM spaces so that the nodes become visible in XPath space.
 *
 * @param doc/*from   w  w w.j a v a2s .c o m*/
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
public static void circumventBug2650(Document doc) {

    Element documentElement = doc.getDocumentElement();

    // if the document element has no xmlns definition, we add xmlns=""
    Attr xmlnsAttr = documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns");

    if (xmlnsAttr == null) {
        documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
    }

    XMLUtils.circumventBug2650internal(doc);
}

From source file:org.apereo.portal.layout.dlm.DistributedLayoutManager.java

private void setUserLayoutDOM(DistributedUserLayout userLayout) {

    this.layoutCachingService.cacheLayout(owner, profile, userLayout);
    this.updateCacheKey();

    // determine if this is a layout fragment by looking at the root node
    // for a cp:fragment attribute.
    Element layout = userLayout.getLayout().getDocumentElement();
    Node attr = layout.getAttributeNodeNS(Constants.NS_URI, Constants.LCL_FRAGMENT_NAME);
    this.isFragmentOwner = attr != null;
}

From source file:org.deegree.portal.context.WebMapContextFactory.java

/**
 * creates an instance of a class encapsulating the access the configuration of Module
 * //from  w ww  .j av a 2  s  . com
 * @param element
 * @param xml
 * 
 * @return instance of <tt>ModuleConfiguration</tt>
 * 
 * @throws XMLParsingException
 * @throws MalformedURLException
 */
private static ModuleConfiguration createModuleConfiguration(Element element, XMLFragment xml)
        throws XMLParsingException, MalformedURLException {

    ModuleConfiguration mc = null;
    if (element != null) {
        Element elem = XMLTools.getRequiredElement(element, "cntxt:OnlineResource",
                CommonNamespaces.getNamespaceContext());
        Attr attr = elem.getAttributeNodeNS(CommonNamespaces.XLNNS.toASCIIString(), "href");
        String url = attr.getValue();
        URL u = xml.resolve(url);
        url = u.toExternalForm();
        if (url.endsWith("?")) {
            url = url.substring(0, url.length() - 1);
        }
        attr.setNodeValue(url);
        URL onlineResource = createOnlineResource(elem);
        mc = new ModuleConfiguration(onlineResource);
    }

    return mc;
}

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.jcp.xml.dsig.internal.dom.DOMReference.java

/**
 * Creates a <code>DOMReference</code> from an element.
 *
 * @param refElem a Reference element/*from  w w  w.j  av a2  s . co  m*/
 */
public DOMReference(Element refElem, XMLCryptoContext context, Provider provider) throws MarshalException {
    // unmarshal Transforms, if specified
    Element nextSibling = DOMUtils.getFirstChildElement(refElem);
    List<Transform> transforms = new ArrayList<Transform>(5);
    if (nextSibling.getLocalName().equals("Transforms")) {
        Element transformElem = DOMUtils.getFirstChildElement(nextSibling);
        while (transformElem != null) {
            transforms.add(new DOMTransform(transformElem, context, provider));
            transformElem = DOMUtils.getNextSiblingElement(transformElem);
        }
        nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
    }

    // unmarshal DigestMethod
    Element dmElem = nextSibling;
    this.digestMethod = DOMDigestMethod.unmarshal(dmElem);

    // unmarshal DigestValue
    try {
        Element dvElem = DOMUtils.getNextSiblingElement(dmElem);
        this.digestValue = Base64.decode(dvElem);
    } catch (Base64DecodingException bde) {
        throw new MarshalException(bde);
    }

    // unmarshal attributes
    this.uri = DOMUtils.getAttributeValue(refElem, "URI");
    this.id = DOMUtils.getAttributeValue(refElem, "Id");
    if (this.id != null) {
        DOMCryptoContext dcc = (DOMCryptoContext) context;
        dcc.setIdAttributeNS(refElem, null, "Id");
    }

    this.type = DOMUtils.getAttributeValue(refElem, "Type");
    this.here = refElem.getAttributeNodeNS(null, "URI");
    this.refElem = refElem;
    this.transforms = transforms;
    this.allTransforms = transforms;
    this.appliedTransformData = null;
    this.provider = provider;
}

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);//  w  ww  .  ja  va2s .c o m
    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.wso2.carbon.event.output.adapter.core.internal.util.EventAdapterConfigHelper.java

private static void secureLoadElement(Element element) throws CryptoException {

    Attr secureAttr = element.getAttributeNodeNS(EventAdapterConstants.SECURE_VAULT_NS,
            EventAdapterConstants.SECRET_ALIAS_ATTR_NAME);
    if (secureAttr != null) {
        element.setTextContent(loadFromSecureVault(secureAttr.getValue()));
        element.removeAttributeNode(secureAttr);
    }/*from w  w  w. j  av a  2s . c  o  m*/
    NodeList childNodes = element.getChildNodes();
    int count = childNodes.getLength();
    Node tmpNode;
    for (int i = 0; i < count; i++) {
        tmpNode = childNodes.item(i);
        if (tmpNode instanceof Element) {
            secureLoadElement((Element) tmpNode);
        }
    }
}