Example usage for org.w3c.dom Attr getValue

List of usage examples for org.w3c.dom Attr getValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr getValue.

Prototype

public String getValue();

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

From source file:org.apache.shindig.gadgets.rewrite.AbsolutePathReferenceVisitor.java

/**
 * Returns href value of the base tag./*from ww  w.ja v  a2  s.c o m*/
 * @param doc The document to process.
 * @return Value of href attribute of the base tag.
 */
@VisibleForTesting
String getBaseHref(Document doc) {
    NodeList list = doc.getElementsByTagName("base");
    if (list.getLength() == 0) {
        return null;
    }

    NamedNodeMap nodeMap = list.item(0).getAttributes();
    if (nodeMap == null) {
        return null;
    }
    Attr attr = (Attr) nodeMap.getNamedItem("href");
    return attr != null ? attr.getValue() : null;
}

From source file:org.apache.shindig.gadgets.rewrite.ResourceMutateVisitor.java

/**
 * {@inheritDoc}//from w  ww.  ja v  a  2s .co m
 */
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException {
    String nodeName = node.getNodeName().toLowerCase();
    if (node.getNodeType() == Node.ELEMENT_NODE && resourceTags.containsKey(nodeName)
            && featureConfig.shouldRewriteTag(nodeName)) {
        if ("link".equals(nodeName)) {
            // Rewrite link only when it is for css.
            String type = ((Element) node).getAttribute("type");
            String rel = ((Element) node).getAttribute("rel");
            if (!"stylesheet".equalsIgnoreCase(rel) || !"text/css".equalsIgnoreCase(type)) {
                return VisitStatus.BYPASS;
            }
        }

        Attr attr = (Attr) node.getAttributes().getNamedItem(resourceTags.get(nodeName));
        if (attr != null) {
            String urlValue = attr.getValue();
            if (!StringUtils.isEmpty(urlValue) && featureConfig.shouldRewriteURL(urlValue)) {
                return VisitStatus.RESERVE_NODE;
            }
        }
    }
    return VisitStatus.BYPASS;
}

From source file:org.apache.strutsel.taglib.utils.DOMHelper.java

public static void recordFoundAttributes(org.w3c.dom.Document document, String xpath, Map map)
        throws TransformerException {

    CachedXPathAPI xpathAPI = new CachedXPathAPI();
    Node foundNode = xpathAPI.selectSingleNode(document.getDocumentElement(), xpath);

    if (foundNode != null) {

        NamedNodeMap attrMap = foundNode.getAttributes();

        for (int ctr = 0; ctr < attrMap.getLength(); ++ctr) {

            Attr attrNode = (Attr) attrMap.item(ctr);
            map.put(attrNode.getName(), attrNode.getValue());
        }/*  www. ja  v a 2  s  .  co m*/
    }
}

From source file:org.apache.strutsel.taglib.utils.DOMHelper.java

public static void printNode(Node node, int level) {

    if (node == null)
        return;//from ww  w .  j  a  v  a  2s  .  c o  m

    String nodeName = node.getNodeName();
    NodeList children = node.getChildNodes();

    if (children != null) {

        short nodeType = node.getNodeType();

        if (nodeType == Node.TEXT_NODE) {

            String text = ((Text) node).getData();
            System.out.print(text);
        } else {
            System.out.print(spaces.substring(0, level) + "<" + nodeName);

            NamedNodeMap nodeMap = node.getAttributes();
            if (nodeMap.getLength() > 0) {
                StringBuffer sb = new StringBuffer();

                for (int ctr = 0; ctr < nodeMap.getLength(); ++ctr) {
                    Attr attrnode = (Attr) nodeMap.item(ctr);
                    String name = attrnode.getName();
                    String value = attrnode.getValue();

                    sb.append(" " + name + "=\"" + value + "\"");
                }

                System.out.print(sb.toString());
            }

            System.out.println(">");
        }

        for (int ctr = 0; ctr < children.getLength(); ++ctr) {

            Node child = children.item(ctr);
            printNode(child, level + 1);
        }

        if (nodeType != Node.TEXT_NODE)
            System.out.println(spaces.substring(0, level) + "</" + nodeName + ">");
    } else
        System.out.println(spaces.substring(0, level) + "<" + nodeName + "/>");
}

From source file:org.apache.woden.internal.DOMWSDLReader.java

protected void parseExtensionAttributes(XMLElement extEl, Class wsdlClass, WSDLElement wsdlObj,
        DescriptionElement desc) throws WSDLException {
    Element domEl = (Element) extEl.getSource();
    NamedNodeMap nodeMap = domEl.getAttributes();
    int length = nodeMap.getLength();

    for (int i = 0; i < length; i++) {
        Attr domAttr = (Attr) nodeMap.item(i);
        String localName = domAttr.getLocalName();
        String namespaceURI = domAttr.getNamespaceURI();
        String prefix = domAttr.getPrefix();
        QName attrType = new QName(namespaceURI, localName, (prefix != null ? prefix : emptyString));
        String attrValue = domAttr.getValue();

        if (namespaceURI != null && !namespaceURI.equals(Constants.NS_STRING_WSDL20)) {
            if (!namespaceURI.equals(Constants.NS_STRING_XMLNS)
                    && !namespaceURI.equals(Constants.NS_STRING_XSI)) //TODO handle xsi attrs elsewhere, without need to register
            {//from  www  . jav  a 2s  . c o m
                //TODO reg namespaces at appropriate element scope, not just at desc.
                //DOMUtils.registerUniquePrefix(prefix, namespaceURI, desc);

                ExtensionRegistry extReg = fWsdlContext.extensionRegistry;
                XMLAttr xmlAttr = extReg.createExtAttribute(wsdlClass, attrType, extEl, attrValue);
                if (xmlAttr != null) //TODO use an 'UnknownAttr' class in place of null
                {
                    wsdlObj.setExtensionAttribute(attrType, xmlAttr);
                }
            } else {
                //TODO parse xmlns namespace declarations - here or elsewhere?
            }
        } else {
            //TODO confirm non-native attrs in WSDL 2.0 namespace will be detected by schema validation,
            //so no need to handle error here.
        }
    }

}

From source file:org.apache.woden.internal.DOMWSDLReader.java

protected void parseNamespaceDeclarations(XMLElement xmlElem, WSDLElement wsdlElem) throws WSDLException {

    Element elem = (Element) xmlElem.getSource();

    NamedNodeMap attrs = elem.getAttributes();
    int size = attrs.getLength();

    for (int i = 0; i < size; i++) {
        Attr attr = (Attr) attrs.item(i);
        String namespaceURI = attr.getNamespaceURI();
        String localPart = attr.getLocalName();
        String value = attr.getValue();

        if ((Constants.NS_STRING_XMLNS).equals(namespaceURI)) {
            if (!(Constants.ATTR_XMLNS).equals(localPart)) {
                wsdlElem.addNamespace(localPart, getURI(value)); //a prefixed namespace
            } else {
                wsdlElem.addNamespace(null, getURI(value)); //the default namespace
            }/*  ww w  .ja va2  s.c om*/
        }
    }
}

From source file:org.apache.ws.security.message.WSSecEncrypt.java

private Vector doEncryption(Document doc, SecretKey secretKey, KeyInfo keyInfo, Vector references)
        throws WSSecurityException {

    XMLCipher xmlCipher = null;//from   ww  w .ja  v a  2  s . c om
    try {
        xmlCipher = XMLCipher.getInstance(symEncAlgo);
    } catch (XMLEncryptionException e3) {
        throw new WSSecurityException(WSSecurityException.UNSUPPORTED_ALGORITHM, null, null, e3);
    }

    Vector encDataRef = new Vector();

    boolean cloneKeyInfo = false;
    for (int part = 0; part < references.size(); part++) {
        WSEncryptionPart encPart = (WSEncryptionPart) references.get(part);

        String idToEnc = encPart.getId();
        String elemName = encPart.getName();
        String nmSpace = encPart.getNamespace();
        String modifier = encPart.getEncModifier();
        //
        // Third step: get the data to encrypt.
        //
        Element body = null;
        if (idToEnc != null) {
            body = WSSecurityUtil.findElementById(document.getDocumentElement(), idToEnc, WSConstants.WSU_NS);
            if (body == null) {
                body = WSSecurityUtil.findElementById(document.getDocumentElement(), idToEnc, null);
            }
        } else {
            body = (Element) WSSecurityUtil.findElement(document, elemName, nmSpace);
        }
        if (body == null) {
            throw new WSSecurityException(WSSecurityException.FAILURE, "noEncElement",
                    new Object[] { "{" + nmSpace + "}" + elemName });
        }

        boolean content = modifier.equals("Content") ? true : false;
        String xencEncryptedDataId = wssConfig.getIdAllocator().createId("EncDataId-", body);
        encPart.setEncId(xencEncryptedDataId);

        cloneKeyInfo = true;

        if (keyInfo == null) {
            keyInfo = new KeyInfo(document);
            SecurityTokenReference secToken = new SecurityTokenReference(document);

            if (useKeyIdentifier && SecurityTokenReference.SAML_ID_URI.equals(customReferenceValue)) {
                secToken.setSAMLKeyIdentifier((encKeyIdDirectId ? "" : "#") + encKeyId);
            } else {
                Reference ref = new Reference(document);
                if (encKeyIdDirectId) {
                    ref.setURI(encKeyId);
                } else {
                    ref.setURI("#" + encKeyId);
                }
                if (encKeyValueType != null) {
                    ref.setValueType(encKeyValueType);
                }
                secToken.setReference(ref);
            }

            keyInfo.addUnknownElement(secToken.getElement());
            Element keyInfoElement = keyInfo.getElement();
            keyInfoElement.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:" + WSConstants.SIG_PREFIX,
                    WSConstants.SIG_NS);
        }
        //
        // Fourth step: encrypt data, and set necessary attributes in
        // xenc:EncryptedData
        //
        try {
            if (modifier.equals("Header")) {

                Element elem = doc.createElementNS(WSConstants.WSSE11_NS,
                        "wsse11:" + WSConstants.ENCRYPTED_HEADER);
                WSSecurityUtil.setNamespace(elem, WSConstants.WSSE11_NS, WSConstants.WSSE11_PREFIX);
                String wsuPrefix = WSSecurityUtil.setNamespace(elem, WSConstants.WSU_NS,
                        WSConstants.WSU_PREFIX);
                elem.setAttributeNS(WSConstants.WSU_NS, wsuPrefix + ":Id",
                        wssConfig.getIdAllocator().createId("EncHeader-", body));

                NamedNodeMap map = body.getAttributes();

                for (int i = 0; i < map.getLength(); i++) {
                    Attr attr = (Attr) map.item(i);
                    if (attr.getNamespaceURI().equals(WSConstants.URI_SOAP11_ENV)
                            || attr.getNamespaceURI().equals(WSConstants.URI_SOAP12_ENV)) {
                        String soapEnvPrefix = WSSecurityUtil.setNamespace(elem, attr.getNamespaceURI(),
                                WSConstants.DEFAULT_SOAP_PREFIX);
                        elem.setAttributeNS(attr.getNamespaceURI(), soapEnvPrefix + ":" + attr.getLocalName(),
                                attr.getValue());
                    }
                }

                xmlCipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
                EncryptedData encData = xmlCipher.getEncryptedData();
                encData.setId(xencEncryptedDataId);
                encData.setKeyInfo(keyInfo);
                xmlCipher.doFinal(doc, body, content);

                Element encDataElem = WSSecurityUtil.findElementById(document.getDocumentElement(),
                        xencEncryptedDataId, null);
                Node clone = encDataElem.cloneNode(true);
                elem.appendChild(clone);
                encDataElem.getParentNode().appendChild(elem);
                encDataElem.getParentNode().removeChild(encDataElem);
            } else {
                xmlCipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
                EncryptedData encData = xmlCipher.getEncryptedData();
                encData.setId(xencEncryptedDataId);
                encData.setKeyInfo(keyInfo);
                xmlCipher.doFinal(doc, body, content);
            }
            if (cloneKeyInfo) {
                keyInfo = new KeyInfo((Element) keyInfo.getElement().cloneNode(true), null);
            }
        } catch (Exception e2) {
            throw new WSSecurityException(WSSecurityException.FAILED_ENCRYPTION, null, null, e2);
        }
        encDataRef.add("#" + xencEncryptedDataId);
    }
    return encDataRef;
}

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 av a  2  s.co  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.ws.security.util.WSSecurityUtil.java

public static String getNamespace(String prefix, Node e) {
    while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
        Attr attr = null;
        if (prefix == null) {
            attr = ((Element) e).getAttributeNode("xmlns");
        } else {/*w w  w  . j  av a  2 s.c o m*/
            attr = ((Element) e).getAttributeNodeNS(WSConstants.XMLNS_NS, prefix);
        }
        if (attr != null) {
            return attr.getValue();
        }
        e = e.getParentNode();
    }
    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   www. j a  v a  2s.c  o m*/
 * The code of this method is a copy of {@link #handleAttributes(Element,
 * NameSpaceSymbTable)},
 * whereas it takes into account that subtree-c14n is -- well -- 
 * subtree-based.
 * So if the element in question isRoot of c14n, it's parent is not in the
 * node set, as well as all other ancestors.
 *
 * @param element
 * @param ns
 * @return the Attr[]s to be output
 * @throws CanonicalizationException
 */
@Override
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
        throws CanonicalizationException {
    if (!element.hasAttributes() && !firstCall) {
        return null;
    }
    // result will contain the attrs which have to be output
    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)) {
                // It's not a namespace attr node. Add to the result and continue.
                result.add(attribute);
            } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
                // The default mapping for xml must not be output.
                Node n = ns.addMappingAndRender(NName, NValue, attribute);

                if (n != null) {
                    // Render the ns definition
                    result.add((Attr) n);
                    if (C14nHelper.namespaceIsRelative(attribute)) {
                        Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                        throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs);
                    }
                }
            }
        }
    }

    if (firstCall) {
        // It is the first node of the subtree
        // Obtain all the namespaces defined in the parents, and added to the output.
        ns.getUnrenderedNodes(result);
        // output the attributes in the xml namespace.
        xmlattrStack.getXmlnsAttr(result);
        firstCall = false;
    }

    return result.iterator();
}