Example usage for org.w3c.dom NamedNodeMap item

List of usage examples for org.w3c.dom NamedNodeMap item

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the map.

Usage

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

public static Document insertImport(Document doc, String resource) {
    List<Node> targetList = new ArrayList<Node>();

    // First, delete old lines if any.

    NodeList list = doc.getElementsByTagName("import");
    Node node = null;//  ww  w  . ja  va  2s.c  o  m
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);
        NamedNodeMap attributes = node.getAttributes();
        for (int j = 0; j < attributes.getLength(); j++) {
            Node attr = attributes.item(j);
            if (attr.getNodeName().equals("resource") && attr.getNodeValue().equals(resource)) {
                targetList.add(node);
                break;
            }
        }
    }

    NodeList beans_list = doc.getElementsByTagName("beans");
    Node beans_node = beans_list.item(0);

    if (targetList.size() > 0) {
        for (Node target : targetList) {
            beans_node.removeChild(target);
        }
    }

    // Now, add the new line

    NodeList list1 = beans_node.getChildNodes();
    Node bean_node = null;
    for (int i = 0; i < list1.getLength(); i++) {
        Node node1 = list1.item(i);
        if (node1.getNodeName().equals("bean")) {
            bean_node = node1;
            break;
        }
    }

    Element elem = doc.createElement("import");
    elem.setAttribute("resource", resource);

    try {
        if (bean_node != null) {
            beans_node.insertBefore(elem, bean_node);
        } else {
            beans_node.appendChild(elem);
        }
    } catch (DOMException ex) {
        ex.printStackTrace();
    }

    return doc;
}

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  va2  s  .c o  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:com.netsteadfast.greenstep.util.BusinessProcessManagementUtils.java

public static String getResourceProcessId(File activitiBpmnFile) throws Exception {
    if (null == activitiBpmnFile || !activitiBpmnFile.exists()) {
        throw new Exception("file no exists!");
    }/*from   w ww  . j  a  v  a 2s . c  om*/
    if (!activitiBpmnFile.getName().endsWith(_SUB_NAME)) {
        throw new Exception("not resource file.");
    }
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(activitiBpmnFile);
    Element element = doc.getDocumentElement();
    if (!"definitions".equals(element.getNodeName())) {
        throw new Exception("not resource file.");
    }
    String processId = activitiBpmnFile.getName();
    if (processId.endsWith(_SUB_NAME)) {
        processId = processId.substring(0, processId.length() - _SUB_NAME.length());
    }
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if ("process".equals(node.getNodeName())) {
            NamedNodeMap nodeMap = node.getAttributes();
            for (int attr = 0; attr < nodeMap.getLength(); attr++) {
                Node processAttr = nodeMap.item(attr);
                if ("id".equals(processAttr.getNodeName())) {
                    processId = processAttr.getNodeValue();
                }
            }
        }
    }
    return processId;
}

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());/*from   w  w w  .  ja v  a2s .  c om*/
    }

    return attributes;
}

From source file:Main.java

/**
 * Copies all attributes from one element to another in the official way.
 *///from w  w w. j  a v  a 2  s.  co  m
public static void copyAttributes(Element elementFrom, Element elementTo) {
    NamedNodeMap nodeList = elementFrom.getAttributes();
    if (nodeList == null) {
        // No attributes to copy: just return
        return;
    }

    Attr attrFrom = null;
    Attr attrTo = null;

    // Needed as factory to create attrs
    Document documentTo = elementTo.getOwnerDocument();
    int len = nodeList.getLength();

    // Copy each attr by making/setting a new one and
    // adding to the target element.
    for (int i = 0; i < len; i++) {
        attrFrom = (Attr) nodeList.item(i);

        // Create an set value
        attrTo = documentTo.createAttribute(attrFrom.getName());
        attrTo.setValue(attrFrom.getValue());

        // Set in target element
        elementTo.setAttributeNode(attrTo);
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.xml.XmlNode.java

/**
 * @param xmlNode the {@code XmlNode} to which the element is added
 * @param element the {@code Node} to be copied
 *//*w w w.jav  a2s .  com*/
private static void recursiveCopy(final XmlNode xmlNode, final Node element) {

    final String name = element.getNodeName();
    final XmlNode _xmlNode = new XmlNode(name);
    final NamedNodeMap attributes = element.getAttributes();
    for (int jj = 0; jj < attributes.getLength(); jj++) {

        final Node attrNode = attributes.item(jj);
        final String attrName = attrNode.getNodeName();
        if (!"xmlns".equals(attrName)) {

            _xmlNode.setAttribute(attrName, attrNode.getNodeValue());
        }
    }

    final NodeList nodes = element.getChildNodes();
    boolean hasElementNodes = false;
    for (int ii = 0; ii < nodes.getLength(); ii++) {

        final Node node = nodes.item(ii);
        if (node.getNodeType() == Node.ELEMENT_NODE) {

            hasElementNodes = true;
            recursiveCopy(_xmlNode, node);
        }
    }
    if (!hasElementNodes) {

        final String value = element.getTextContent();
        _xmlNode.setValue(value);
    }
    _xmlNode.setParent(xmlNode);
}

From source file:ee.ria.xroad.proxy.util.MetaserviceTestUtil.java

/** Merge xroad-specific {@link SoapHeader} to the generic {@link SOAPHeader}
 * @param header//from   w ww  .  j  a  v  a 2 s . co m
 * @param xrHeader
 * @throws JAXBException
 * @throws ParserConfigurationException
 * @throws SOAPException
 */
public static void mergeHeaders(SOAPHeader header, SoapHeader xrHeader)
        throws JAXBException, ParserConfigurationException, SOAPException {

    Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
    final DocumentFragment documentFragment = document.createDocumentFragment();
    // marshalling on the header would add the xroad header as a child of the header
    // (i.e. two nested header elements)
    marshaller.marshal(xrHeader, documentFragment);

    Document headerDocument = header.getOwnerDocument();
    Node xrHeaderElement = documentFragment.getFirstChild();

    assertTrue("test setup failed: assumed had header element but did not",
            xrHeaderElement.getNodeType() == Node.ELEMENT_NODE
                    && xrHeaderElement.getLocalName().equals("Header"));

    final NamedNodeMap attributes = xrHeaderElement.getAttributes();

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            final Attr attribute = (Attr) attributes.item(i);
            header.setAttributeNodeNS((Attr) headerDocument.importNode(attribute, false));
        }
    }

    final NodeList childNodes = xrHeaderElement.getChildNodes();

    if (childNodes != null) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            final Node node = childNodes.item(i);
            header.appendChild(headerDocument.importNode(node, true));
        }
    }

}

From source file:Main.java

/**
 * Sarches for ressources that have to be downloaded and creates a list with all download links.
 *
 * @param node to check for download links
 * @return list with all found download links
 *//*w w  w .java 2s  . c  o m*/
private static ArrayList<String> findUrls(Node node) {

    int type = node.getNodeType();
    ArrayList<String> result = new ArrayList<>();
    String temp = null;
    NamedNodeMap atts = node.getAttributes();
    Log.i(TAG, "parsing for ressources.  node: " + node.getNodeName() + " value: " + node.getNodeValue()
            + " atts length: " + atts.getLength() + "type: " + type);
    switch (type) {
    //Element
    case Node.ELEMENT_NODE:
        //TODO: This method is stupid. It just looks for
        // attributes named ressourcepath. What if we have to download several ressources?

        for (int j = 0; j < atts.getLength(); j++) {
            Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue());
            temp = atts.item(j).getNodeValue();

            if (temp != null) {
                result.add(temp);
                Log.i(TAG, "added path: " + temp);
            }

            Log.i(TAG, "parent node:" + node.getParentNode().getNodeName());
            Element parent = (Element) node.getParentNode();
            parent.setAttribute("TESTITEST", "dllink");

        }
        // get the pages, means the children
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            ArrayList<String> childres = findUrls(child);
            if (childres.size() > 0) {
                result.addAll(childres);
            }
        }
        break;
    }
    return result;
}

From source file:Main.java

private static String toString(Node node, int level, boolean indent) {

    StringBuilder nodeBuilder = new StringBuilder();

    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        if (indent) {
            nodeBuilder.append(setIndent(level));
        }/* w  w  w  .j av  a2 s  .co m*/
        nodeBuilder.append(node.getNodeValue());
        if (indent) {
            nodeBuilder.append("\n");
        }
        break;
    case Node.ELEMENT_NODE:

        NamedNodeMap attributeMap;
        NodeList childList;

        if (indent) {
            nodeBuilder.append(setIndent(level));
        }
        nodeBuilder.append("<");
        nodeBuilder.append(((Element) node).getTagName());

        attributeMap = node.getAttributes();
        for (int loop = 0; loop < attributeMap.getLength(); loop++) {
            nodeBuilder.append(" ");
            nodeBuilder.append(attributeMap.item(loop).getNodeName());
            nodeBuilder.append("=\"");
            nodeBuilder.append(attributeMap.item(loop).getNodeValue());
            nodeBuilder.append("\"");
        }

        if (node.hasChildNodes()) {
            nodeBuilder.append(">");
            if (indent) {
                nodeBuilder.append("\n");
            }

            childList = node.getChildNodes();
            for (int loop = 0; loop < childList.getLength(); loop++) {
                nodeBuilder.append(toString(childList.item(loop), level + 1, indent));
            }

            if (indent) {
                nodeBuilder.append(setIndent(level));
            }
            nodeBuilder.append("</");
            nodeBuilder.append(((Element) node).getTagName());
            nodeBuilder.append(">");
            if (indent) {
                nodeBuilder.append("\n");
            }
        } else {
            nodeBuilder.append("/>");
            if (indent) {
                nodeBuilder.append("\n");
            }
        }
        break;
    default:
        nodeBuilder.append("<Unkown Node Type (");
        nodeBuilder.append(node.getNodeType());
        nodeBuilder.append(")/>");
        if (indent) {
            nodeBuilder.append("\n");
        }
    }

    return nodeBuilder.toString();
}

From source file:com.portfolio.data.utils.DomUtils.java

public static String getNodeAttributesString(Node node) {
    String ret = "";
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attribute = (Attr) attributes.item(i);
        ret += attribute.getName().trim() + "=\"" + StringEscapeUtils.escapeXml11(attribute.getValue().trim())
                + "\" ";
    }//from  w w  w.j  a v a 2s. c  om
    return ret;
}