Example usage for org.w3c.dom Node getLocalName

List of usage examples for org.w3c.dom Node getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Node getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java

private static Attributes namedNodeMapToSaxAttributes(final NamedNodeMap attributesMap) {
    final AttributesImpl attributes = new AttributesImpl();
    final int length = attributesMap.getLength();
    for (int i = 0; i < length; i++) {
        final Node attr = attributesMap.item(i);
        attributes.addAttribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getNodeName(), null,
                attr.getNodeValue());/*  w w w  .  j  av a2  s.c  o m*/
    }

    return attributes;
}

From source file:Main.java

/**
 * Node is element whose namespace is "namespace" and localname is "localname"?
 * /*from  w  w w  .  j  a v a  2s  .  co  m*/
 * @param node XML node.
 * @param namespace Namespace
 * @param localname Local name
 * @return Node is element whose namespace is "namespace" and localname is "localname"?
 */
public static boolean nodeIsElementOf(Node node, String namespace, String localname) {

    if (node.getNodeType() != Node.ELEMENT_NODE) {
        return false;
    }
    String ns = node.getNamespaceURI();

    if (((ns != null) && (namespace != null) && (ns.equals(namespace)))
            || ((ns == null) && (namespace == null))) {
    } else {
        return false;
    }

    String ln = node.getLocalName();
    if (!ln.equals(localname)) {
        return false;
    }
    return true;
}

From source file:com.weibo.api.motan.config.springsupport.MotanBeanDefinitionParser.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass,
        boolean required) throws ClassNotFoundException {
    RootBeanDefinition bd = new RootBeanDefinition();
    bd.setBeanClass(beanClass);//from w  w w .  j a v  a 2  s .co m
    // ??lazy init
    bd.setLazyInit(false);

    // id?id,idcontext
    String id = element.getAttribute("id");
    if ((id == null || id.length() == 0) && required) {
        String generatedBeanName = element.getAttribute("name");
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = element.getAttribute("class");
        }
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = beanClass.getName();
        }
        id = generatedBeanName;
        int counter = 2;
        while (parserContext.getRegistry().containsBeanDefinition(id)) {
            id = generatedBeanName + (counter++);
        }
    }
    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new IllegalStateException("Duplicate spring bean id " + id);
        }
        parserContext.getRegistry().registerBeanDefinition(id, bd);
    }
    bd.getPropertyValues().addPropertyValue("id", id);
    if (ProtocolConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.protocolDefineNames.add(id);

    } else if (RegistryConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.registryDefineNames.add(id);

    } else if (BasicServiceInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicServiceConfigDefineNames.add(id);

    } else if (BasicRefererInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicRefererConfigDefineNames.add(id);

    } else if (ServiceConfigBean.class.equals(beanClass)) {
        String className = element.getAttribute("class");
        if (className != null && className.length() > 0) {
            RootBeanDefinition classDefinition = new RootBeanDefinition();
            classDefinition.setBeanClass(
                    Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
            classDefinition.setLazyInit(false);
            parseProperties(element.getChildNodes(), classDefinition);
            bd.getPropertyValues().addPropertyValue("ref",
                    new BeanDefinitionHolder(classDefinition, id + "Impl"));
        }
    }

    Set<String> props = new HashSet<String>();
    ManagedMap parameters = null;
    // ??setbd
    for (Method setter : beanClass.getMethods()) {
        String name = setter.getName();
        // setXXX
        if (name.length() <= 3 || !name.startsWith("set") || !Modifier.isPublic(setter.getModifiers())
                || setter.getParameterTypes().length != 1) {
            continue;
        }
        String property = (name.substring(3, 4).toLowerCase() + name.substring(4)).replaceAll("_", "-");
        props.add(property);
        if ("id".equals(property)) {
            bd.getPropertyValues().addPropertyValue("id", id);
            continue;
        }
        String value = element.getAttribute(property);
        if ("methods".equals(property)) {
            parseMethods(id, element.getChildNodes(), bd, parserContext);
        }
        if (StringUtils.isBlank(value)) {
            continue;
        }
        value = value.trim();
        if (value.length() == 0) {
            continue;
        }
        Object reference;
        if ("ref".equals(property)) {
            if (parserContext.getRegistry().containsBeanDefinition(value)) {
                BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                if (!refBean.isSingleton()) {
                    throw new IllegalStateException(
                            "The exported service ref " + value + " must be singleton! Please set the " + value
                                    + " bean scope to singleton, eg: <bean id=\"" + value
                                    + "\" scope=\"singleton\" ...>");
                }
            }
            reference = new RuntimeBeanReference(value);
        } else if ("protocol".equals(property) && !StringUtils.isBlank(value)) {
            if (!value.contains(",")) {
                reference = new RuntimeBeanReference(value);
            } else {
                parseMultiRef("protocols", value, bd, parserContext);
                reference = null;
            }
        } else if ("registry".equals(property)) {
            parseMultiRef("registries", value, bd, parserContext);
            reference = null;
        } else if ("basicService".equals(property)) {
            reference = new RuntimeBeanReference(value);

        } else if ("basicReferer".equals(property)) {
            reference = new RuntimeBeanReference(value);

        } else if ("extConfig".equals(property)) {
            reference = new RuntimeBeanReference(value);
        } else {
            reference = new TypedStringValue(value);
        }

        if (reference != null) {
            bd.getPropertyValues().addPropertyValue(property, reference);
        }
    }
    if (ProtocolConfig.class.equals(beanClass)) {
        // protocolparameters?
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        bd.getPropertyValues().addPropertyValue("parameters", parameters);
    }
    return bd;
}

From source file:eidassaml.starterkit.EidasMetadataNode.java

/**
 * Parse an metadata.xml/*from  ww  w .  j av  a2s .c om*/
 * 
 * @param is
 * @return
 * @throws XMLParserException
 * @throws UnmarshallingException
 * @throws CertificateException
 * @throws IOException
 * @throws ErrorCodeException 
 * @throws DOMException 
 */
public static EidasMetadataNode Parse(InputStream is) throws XMLParserException, UnmarshallingException,
        CertificateException, IOException, DOMException, ErrorCodeException {
    EidasMetadataNode eidasMetadataService = new EidasMetadataNode();
    BasicParserPool ppMgr = new BasicParserPool();
    Document inCommonMDDoc = ppMgr.parse(is);
    Element metadataRoot = inCommonMDDoc.getDocumentElement();
    UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
    Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);
    EntityDescriptor metaData = (EntityDescriptor) unmarshaller.unmarshall(metadataRoot);

    eidasMetadataService.setId(metaData.getID());
    eidasMetadataService.setEntityId(metaData.getEntityID());
    eidasMetadataService.setValidUntil(metaData.getValidUntil().toDate());
    if (metaData.getExtensions() != null) {
        Element extension = metaData.getExtensions().getDOM();
        for (int i = 0; i < extension.getChildNodes().getLength(); i++) {
            Node n = extension.getChildNodes().item(i);
            if ("SPType".equals(n.getLocalName())) {
                eidasMetadataService.spType = EidasRequestSectorType.GetValueOf(n.getTextContent());
                break;
            }
        }
    }

    SPSSODescriptor ssoDescriptor = metaData.getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol");

    ssoDescriptor.getAssertionConsumerServices().forEach(s -> {
        String bindString = s.getBinding();
        if ("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST".equals(bindString)) {
            eidasMetadataService.setPostEndpoint(s.getLocation());
        }
    });

    for (KeyDescriptor k : ssoDescriptor.getKeyDescriptors()) {
        if (k.getUse() == UsageType.ENCRYPTION) {
            eidasMetadataService.encCert = GetFirstCertFromKeyDescriptor(k);
        } else if (k.getUse() == UsageType.SIGNING) {
            eidasMetadataService.sigCert = GetFirstCertFromKeyDescriptor(k);
        }
    }

    return eidasMetadataService;
}

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

public static List<TimeStampToken> verify(XAdESTimeStampType sigAndRefsTimeStamp, Element signatureElement)
        throws XAdESValidationException {

    LOG.debug("validate SigAndRefsTimeStamp...");

    List<TimeStampToken> timeStampTokens = XAdESUtils.getTimeStampTokens(sigAndRefsTimeStamp);
    if (timeStampTokens.isEmpty()) {
        LOG.error("No timestamp tokens present in SigAndRefsTimeStamp");
        throw new XAdESValidationException("No timestamp tokens present in SigAndRefsTimeStamp");
    }//from w ww .  j a v  a2  s  . co m

    TimeStampDigestInput digestInput = new TimeStampDigestInput(
            sigAndRefsTimeStamp.getCanonicalizationMethod().getAlgorithm());

    /*
     * 2. check ds:SignatureValue present 3. take ds:SignatureValue,
     * cannonicalize and concatenate bytes.
     */
    NodeList signatureValueNodeList = signatureElement.getElementsByTagNameNS(XMLSignature.XMLNS,
            "SignatureValue");
    if (0 == signatureValueNodeList.getLength()) {
        LOG.error("no XML signature valuefound");
        throw new XAdESValidationException("no XML signature valuefound");
    }
    digestInput.addNode(signatureValueNodeList.item(0));

    /*
     * 4. check SignatureTimeStamp(s), CompleteCertificateRefs,
     * CompleteRevocationRefs, AttributeCertificateRefs,
     * AttributeRevocationRefs 5. canonicalize these and concatenate to
     * bytestream from step 3 These nodes should be added in their order of
     * appearance.
     */

    NodeList unsignedSignaturePropertiesNodeList = signatureElement
            .getElementsByTagNameNS(XAdESUtils.XADES_132_NS_URI, "UnsignedSignatureProperties");
    if (unsignedSignaturePropertiesNodeList.getLength() == 0) {
        throw new XAdESValidationException("UnsignedSignatureProperties node not present");
    }
    Node unsignedSignaturePropertiesNode = unsignedSignaturePropertiesNodeList.item(0);
    NodeList childNodes = unsignedSignaturePropertiesNode.getChildNodes();
    int childNodesCount = childNodes.getLength();
    for (int idx = 0; idx < childNodesCount; idx++) {
        Node childNode = childNodes.item(idx);
        if (Node.ELEMENT_NODE != childNode.getNodeType()) {
            continue;
        }
        if (!XAdESUtils.XADES_132_NS_URI.equals(childNode.getNamespaceURI())) {
            continue;
        }
        String localName = childNode.getLocalName();
        if ("SignatureTimeStamp".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("CompleteCertificateRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("CompleteRevocationRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("AttributeCertificateRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("AttributeRevocationRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
    }

    for (TimeStampToken timeStampToken : timeStampTokens) {

        // 1. verify signature in timestamp token
        XAdESUtils.verifyTimeStampTokenSignature(timeStampToken);

        // 6. compute digest and compare with token
        XAdESUtils.verifyTimeStampTokenDigest(timeStampToken, digestInput);
    }

    return timeStampTokens;
}

From source file:Main.java

public static String getXPathForElement(Node e, NamespaceContext ctx) {
    StringBuffer sb = new StringBuffer();
    List<Node> path = new ArrayList<Node>();

    Node currentNode = e;/*from  w ww.  j a va 2  s.  c o  m*/
    while (currentNode.getParentNode() != currentNode.getOwnerDocument()) {
        path.add(0, currentNode);
        if (currentNode instanceof Attr) {
            Attr a = (Attr) currentNode;
            currentNode = a.getOwnerElement();
        } else {
            currentNode = currentNode.getParentNode();
        }
    }
    path.add(0, currentNode); // We need the root element

    for (Node n : path) {
        sb.append("/");

        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            sb.append("@");
        }

        String namespaceURI = n.getNamespaceURI();
        if (namespaceURI != null && !namespaceURI.equals("")) {
            sb.append(ctx.getPrefix(namespaceURI)).append(":");
        }
        sb.append(n.getLocalName());

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            appendElementQualifier(sb, (Element) n);
        }
    }

    return sb.toString();
}

From source file:Main.java

private static boolean isEqualNode(Node arg0, Node arg) {
    if (arg == arg0) {
        return true;
    }/*from   w w w . j  a v a2s  .  co  m*/
    if (arg.getNodeType() != arg0.getNodeType()) {
        return false;
    }

    if (arg0.getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    } else if (!arg0.getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (arg0.getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    } else if (!arg0.getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (arg0.getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    } else if (!arg0.getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (arg0.getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    } else if (!arg0.getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (arg0.getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    } else if (!arg0.getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }
    return true;
}

From source file:com.weibo.api.motan.config.springsupport.MotanBeanDefinitionParser.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
        ParserContext parserContext) throws ClassNotFoundException {
    if (nodeList != null && nodeList.getLength() > 0) {
        ManagedList methods = null;/*from w  w w.j a  va2s  .c o  m*/
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {
                    String methodName = element.getAttribute("name");
                    if (methodName == null || methodName.length() == 0) {
                        throw new IllegalStateException("<motan:method> name attribute == null");
                    }
                    if (methods == null) {
                        methods = new ManagedList();
                    }
                    BeanDefinition methodBeanDefinition = parse((Element) node, parserContext,
                            MethodConfig.class, false);
                    String name = id + "." + methodName;
                    BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(
                            methodBeanDefinition, name);
                    methods.add(methodBeanDefinitionHolder);
                }
            }
        }
        if (methods != null) {
            beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
        }
    }
}

From source file:kenh.xscript.ScriptUtils.java

/**
 * Use <code>Node</code> to initial an <code>Element</code>.
 * @param node/* w ww. j a  v  a 2 s.  com*/
 * @param env
 * @return
 */
private static final Element getElement(Node node, Environment env) throws UnsupportedScriptException {
    if (env == null)
        return null;

    String ns = node.getNamespaceURI(); // name space
    String name = node.getLocalName(); // name
    //String prefix = node.getPrefix(); // prefix

    Element element = env.getElement(ns, name);
    if (element == null) {
        throw new UnsupportedScriptException(null,
                "Could't find the element.[" + (StringUtils.isBlank(ns) ? name : ns + ":" + name) + "]");
    }
    element.setEnvironment(env);

    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            String attrName = attr.getNodeName();
            String attrValue = attr.getNodeValue();

            if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                if (attrName.startsWith("xmlns:")) {
                    // to add function package
                    String abbr = StringUtils.substringAfter(attrName, "xmlns:");
                    if (StringUtils.startsWithAny(abbr, "f.", "func.", "function.")) {
                        abbr = StringUtils.substringAfter(abbr, ".");
                        env.setFunctionPackage(abbr, attrValue);
                    }
                }
            } else {
                element.setAttribute(attrName, attrValue);
            }
        }
    }

    if (includeTextNode(node)) {
        String text = node.getTextContent();
        element.setText(text);
    }

    NodeList nodes = node.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element child = getElement(n, env);
                element.addChild(child);
            }
        }
    }

    return element;
}

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

public static String getPatientIDFromWsse(String mtom)
        throws IOException, MessagingException, SAXException, ParserConfigurationException {
    String wsseHeader = Parsing.getWsseHeaderFromMTOM(mtom);
    if (wsseHeader == null)
        return ""; // NOSAML
    String patientId = null;/*  w w w .  j  a  va2  s  . c om*/
    //  Node security = JAXB.unmarshal(new StringReader(wsseHeader), Node.class);
    Node securityDoc = MiscUtil.stringToDom(wsseHeader);

    Node security = securityDoc.getFirstChild();

    NodeList securityChildren = security.getChildNodes();
    for (int i = 0; i < securityChildren.getLength(); i++) {
        Node securityChild = securityChildren.item(i);
        if (securityChild.getLocalName() != null && securityChild.getLocalName().equals("Assertion")
                && securityChild.getNamespaceURI().equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
            Node assertion = securityChild;
            NodeList assertionChildren = assertion.getChildNodes();
            for (int j = 0; j < assertionChildren.getLength(); j++) {
                Node assertionChild = assertionChildren.item(j);

                if (assertionChild.getLocalName().equals("AttributeStatement")
                        && assertionChild.getNamespaceURI().equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                    Node attributeStatement = assertionChild;
                    NodeList attributeStatementChildren = attributeStatement.getChildNodes();
                    for (int k = 0; k < attributeStatementChildren.getLength(); k++) {
                        Node attributeStatementChild = attributeStatementChildren.item(k);
                        if (attributeStatementChild.getLocalName().equals("Attribute")
                                && attributeStatementChild.getNamespaceURI()
                                        .equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                            Element attribute = (Element) attributeStatementChild;
                            if (attribute.getAttribute("Name")
                                    .equals("urn:oasis:names:tc:xacml:2.0:resource:resource-id")) {
                                NodeList attributeChildren = attribute.getChildNodes();
                                for (int l = 0; l < attributeChildren.getLength(); l++) {
                                    Node attributeChild = attributeChildren.item(l);
                                    if (attributeChild.getLocalName().equals("AttributeValue")
                                            && attributeChild.getNamespaceURI()
                                                    .equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                                        Node attributeValue = attributeChild;
                                        return attributeValue.getFirstChild().getNodeValue();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return patientId;
}