Example usage for org.w3c.dom NamedNodeMap getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

From source file:Main.java

private static void renderNode(StringBuffer sb, Node node) {
    if (node == null) {
        sb.append("null");
        return;/*from w  ww  . j av  a 2 s  .  c o  m*/
    }
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        Node root = ((Document) node).getDocumentElement();
        renderNode(sb, root);
        break;

    case Node.ELEMENT_NODE:
        String name = getNodeNameWithNamespace(node);
        NamedNodeMap attributes = node.getAttributes();
        if (attributes.getLength() == 0) {
            sb.append("<" + name + ">");
        } else {
            sb.append("<" + name + " ");
            int attrlen = attributes.getLength();
            for (int i = 0; i < attrlen; i++) {
                Node attr = attributes.item(i);
                String attrName = getNodeNameWithNamespace(attr);
                sb.append(attrName + "=\"" + escapeChars(attr.getNodeValue()));
                if (i < attrlen - 1)
                    sb.append("\" ");
                else
                    sb.append("\">");
            }
        }
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                renderNode(sb, children.item(i));
            }
        }
        sb.append("</" + name + ">");
        break;

    case Node.TEXT_NODE:
        sb.append(escapeChars(node.getNodeValue()));
        break;

    case Node.CDATA_SECTION_NODE:
        sb.append("<![CDATA[" + node.getNodeValue() + "]]>");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        sb.append("<?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?>");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        sb.append("&" + node.getNodeName() + ";");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        // Ignore document type nodes
        break;

    case Node.COMMENT_NODE:
        sb.append("<!--" + node.getNodeValue() + "-->");
        break;
    }
    return;
}

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  a2 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:kenh.xscript.ScriptUtils.java

/**
 * Use <code>Node</code> to initial an <code>Element</code>.
 * @param node// w w w.  jav  a  2  s.co  m
 * @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: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   ww w  . j  a va2s. c  o  m*/
    }

    return attributes;
}

From source file:Main.java

private static String getMetaScriptType(List metaNodeList) {
    Node meta = null;/*from ww w  .  j  ava2 s. co  m*/
    NamedNodeMap attributes = null;
    boolean httpEquiv = false;
    String contentScriptType = null;

    for (int i = metaNodeList.size() - 1; i >= 0; i--) {
        meta = (Node) metaNodeList.get(i);
        attributes = meta.getAttributes();
        httpEquiv = false;
        contentScriptType = null;
        for (int j = 0; j < attributes.getLength(); j++) {
            if (attributes.item(j).getNodeName().equalsIgnoreCase(HTTP_EQUIV)) {
                httpEquiv = attributes.item(j).getNodeValue().equalsIgnoreCase(CONTENT_SCRIPT_TYPE);
            } else if (attributes.item(j).getNodeName().equalsIgnoreCase(CONTENT)) {
                contentScriptType = attributes.item(j).getNodeValue();
            }
        }
        if (httpEquiv && (contentScriptType != null)) {
            return contentScriptType;
        }
    }
    return null;
}

From source file:com.git.original.common.config.XMLFileConfigDocument.java

/**
 * XML??/*from  w w  w.ja v a2 s . c o m*/
 * 
 * @param elem
 *            XML
 * @return ?
 */
static ConfigNode convertElement(Element elem) {
    if (elem == null) {
        return null;
    }

    ConfigNode cn = new ConfigNode(elem.getTagName(), null);

    NamedNodeMap attrNodeMap = elem.getAttributes();
    if (attrNodeMap != null) {
        for (int i = 0; i < attrNodeMap.getLength(); i++) {
            Node node = attrNodeMap.item(i);
            cn.addAttribute(node.getNodeName(), node.getNodeValue());
        }
    }

    NodeList nodeList = elem.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            switch (node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                cn.addAttribute(node.getNodeName(), node.getNodeValue());
                break;
            case Node.ELEMENT_NODE:
                ConfigNode child = convertElement((Element) node);
                cn.addChild(child);
                break;
            case Node.TEXT_NODE:
                cn.value = node.getNodeValue();
                break;
            default:
                continue;
            }
        }
    }

    return cn;
}

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  . j  av  a2  s.  c om
    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:jp.go.nict.langrid.bpel.ProcessAnalyzer.java

private static void addNamespaceMappings(NamespaceContextImpl nsc, NamedNodeMap attrs) {
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        String[] names = attr.getName().split(":", 2);
        if (names.length == 2) {
            if (names[0].equals("xmlns")) {
                nsc.addMapping(names[1], attr.getValue());
            }/*from w ww . j  av a 2s .  c  o m*/
        }
    }
}

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));
        }// www  . j  a  va  2s .  c o 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.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 va 2 s .c o m*/
    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;
}