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.evolveum.midpoint.util.DOMUtil.java

public static boolean isPrefixUsed(Element targetElement, String prefix) {
    if (comparePrefix(prefix, targetElement.getPrefix())) {
        return true;
    }//from ww w .  j  ava  2 s  .  c  o  m
    NamedNodeMap attributes = targetElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (comparePrefix(prefix, attr.getPrefix())) {
            return true;
        }
    }
    NodeList childNodes = targetElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            if (isPrefixUsed(element, prefix)) {
                return true;
            }
        }
    }
    return false;
}

From source file:de.elbe5.base.data.XmlData.java

public Map<String, String> getAttributes(Node node) {
    Map<String, String> map = new HashMap<>();
    if (node.hasAttributes()) {
        NamedNodeMap attrMap = node.getAttributes();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Node attr = attrMap.item(i);
            map.put(attr.getNodeName(), attr.getNodeValue());
        }//w w  w .  j  a  v  a 2s.  c  om
    }
    return map;
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

private static void fixNamespaceDeclarations(Element targetElement, Element currentElement) {
    NamedNodeMap attributes = currentElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (isNamespaceDefinition(attr)) {
            String prefix = getNamespaceDeclarationPrefix(attr);
            String namespace = getNamespaceDeclarationNamespace(attr);
            if (hasNamespaceDeclarationForPrefix(targetElement, prefix)) {
                if (targetElement != currentElement) {
                    // We are processing parent element, while the original element already
                    // has prefix declaration. That means it must have been processed before
                    // we can skip the usage check
                    continue;
                }/*from   w  w w  . ja v a  2  s.co  m*/
            } else {
                setNamespaceDeclaration(targetElement, prefix, getNamespaceDeclarationNamespace(attr));
            }
        }
    }
    Node parentNode = currentElement.getParentNode();
    if (parentNode instanceof Element) {
        fixNamespaceDeclarations(targetElement, (Element) parentNode);
    }
}

From source file:com.nridge.core.base.io.xml.DataBagXML.java

/**
 * Parses an XML DOM element and loads it into a bag/table.
 *
 * @param anElement DOM element.// ww  w . j av  a  2  s.  co  m
 * @throws java.io.IOException I/O related exception.
 */
@Override
public void load(Element anElement) throws IOException {
    Node nodeItem;
    Attr nodeAttr;
    Element nodeElement;
    DataField dataField;
    String nodeName, nodeValue;

    String className = mBag.getClass().getName();
    String attrValue = anElement.getAttribute("type");
    if ((StringUtils.isNotEmpty(attrValue)) && (!IO.isTypesEqual(attrValue, className)))
        throw new IOException("Unsupported type: " + attrValue);

    attrValue = anElement.getAttribute("name");
    if (StringUtils.isNotEmpty(attrValue))
        mBag.setName(attrValue);
    attrValue = anElement.getAttribute("title");
    if (StringUtils.isNotEmpty(attrValue))
        mBag.setTitle(attrValue);

    NamedNodeMap namedNodeMap = anElement.getAttributes();
    int attrCount = namedNodeMap.getLength();
    for (int attrOffset = 0; attrOffset < attrCount; attrOffset++) {
        nodeAttr = (Attr) namedNodeMap.item(attrOffset);
        nodeName = nodeAttr.getNodeName();
        nodeValue = nodeAttr.getNodeValue();

        if (StringUtils.isNotEmpty(nodeValue)) {
            if ((StringUtils.equalsIgnoreCase(nodeName, "name"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "type"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "count"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "title"))
                    || (StringUtils.equalsIgnoreCase(nodeName, "version")))
                continue;
            else
                mBag.addFeature(nodeName, nodeValue);
        }
    }

    NodeList nodeList = anElement.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        nodeItem = nodeList.item(i);

        if (nodeItem.getNodeType() != Node.ELEMENT_NODE)
            continue;

        nodeName = nodeItem.getNodeName();
        if (nodeName.equalsIgnoreCase(IO.XML_FIELD_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            dataField = mDataFieldXML.load(nodeElement);
            if (dataField != null)
                mBag.add(dataField);
        }
    }
}

From source file:com.subgraph.vega.impl.scanner.urls.ResponseAnalyzer.java

private void analyzeHtmlElement(IInjectionModuleContext ctx, HttpUriRequest req, IHttpResponse res,
        Element elem) {// w  w w.  j a v a  2  s . co m
    boolean remoteScript = false;
    NamedNodeMap attributes = elem.getAttributes();
    final String tag = elem.getTagName().toLowerCase();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node item = attributes.item(i);
        if (item instanceof Attr) {
            String n = ((Attr) item).getName().toLowerCase();
            String v = ((Attr) item).getValue().toLowerCase();
            if (match(tag, "script") && match(n, "src"))
                remoteScript = true;
            if ((v != null) && (match(n, "href", "src", "action", "codebase")
                    || (match(n, "value") && !match(tag, "input")))) {
                if (v.startsWith("vega://"))
                    alert(ctx, "vinfo-url-inject", "URL injection into <" + tag + "> tag", req, res);

                if (v.startsWith("http://vega.invalid/") || v.startsWith("//vega.invalid/")) {
                    if (match(tag, "script", "link"))
                        alert(ctx, "vinfo-url-inject",
                                "URL injection into actively fetched field in tag <" + tag + "> (high risk)",
                                req, res);
                    else if (match(tag, "a"))
                        alert(ctx, "vinfo-url-inject", "URL injection into anchor tag (low risk)", req, res);
                    else
                        alert(ctx, "vinfo-url-inject", "URL injection into tag <" + tag + ">", req, res);
                }

            }

            if ((v != null) && (n.startsWith("on") || n.equals("style"))) {
                checkJavascriptXSS(ctx, req, res, v);
            }

            if (n.contains("vvv"))
                possibleXssAlert(ctx, req, res, n, n.indexOf("vvv"), "xss-inject",
                        "Injected XSS tag into HTML attribute value");

        }
    }

    if (tag.startsWith("vvv"))
        possibleXssAlert(ctx, req, res, tag, 0, "vinfo-xss-inject", "Injected XSS tag into HTML tag name");

    if (tag.equals("style") || (tag.equals("script") && !remoteScript)) {
        String content = elem.getTextContent();
        if (content != null)
            checkJavascriptXSS(ctx, req, res, content);
    }
}

From source file:com.enonic.esl.xml.XMLTool.java

public static Element renameElement(Element elem, String newName) {
    Document doc = elem.getOwnerDocument();

    // Create an element with the new name
    Element elem2 = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        elem2.getAttributes().setNamedItem(attr2);
    }/*from w  ww .  jav  a  2 s.  c o  m*/

    // Move all the children
    while (elem.hasChildNodes()) {
        elem2.appendChild(elem.getFirstChild());
    }

    // Replace the old node with the new node
    elem.getParentNode().replaceChild(elem2, elem);

    return elem2;
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

private static boolean compareAttributesIsSubset(NamedNodeMap subset, NamedNodeMap superset,
        boolean considerNamespacePrefixes) {
    for (int i = 0; i < subset.getLength(); i++) {
        Node aItem = subset.item(i);
        Attr aAttr = (Attr) aItem;
        if (!considerNamespacePrefixes && isNamespaceDefinition(aAttr)) {
            continue;
        }//from   w w  w .  ja  v  a 2  s .c  o  m
        if (StringUtils.isBlank(aAttr.getLocalName())) {
            // this is strange, but it can obviously happen
            continue;
        }
        QName aQname = new QName(aAttr.getNamespaceURI(), aAttr.getLocalName());
        Attr bAttr = findAttributeByQName(superset, aQname);
        if (bAttr == null) {
            return false;
        }
        if (!StringUtils.equals(aAttr.getTextContent(), bAttr.getTextContent())) {
            return false;
        }
    }
    return true;
}

From source file:com.twinsoft.convertigo.engine.util.WsReference.java

private static void extractSoapVariables(XmlSchema xmlSchema, List<RequestableHttpVariable> variables,
        Node node, String longName, boolean isMulti, QName variableType) throws EngineException {
    if (node == null)
        return;//  w  w  w  .j a v  a2s.c o  m
    int type = node.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element != null) {
            String elementName = element.getLocalName();
            if (longName != null)
                elementName = longName + "_" + elementName;

            if (!element.getAttribute("soapenc:arrayType").equals("") && !element.hasChildNodes()) {
                String avalue = element.getAttribute("soapenc:arrayType");
                element.setAttribute("soapenc:arrayType", avalue.replaceAll("\\[\\]", "[1]"));

                Element child = element.getOwnerDocument().createElement("item");
                String atype = avalue.replaceAll("\\[\\]", "");
                child.setAttribute("xsi:type", atype);
                if (atype.startsWith("xsd:")) {
                    String variableName = elementName + "_item";
                    child.appendChild(
                            element.getOwnerDocument().createTextNode("$(" + variableName.toUpperCase() + ")"));
                    RequestableHttpVariable httpVariable = createHttpVariable(true, variableName,
                            new QName(Constants.URI_2001_SCHEMA_XSD, atype.split(":")[1]));
                    variables.add(httpVariable);
                }
                element.appendChild(child);
            }

            // extract from attributes
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node child = map.item(i);
                if (child.getNodeName().equals("soapenc:arrayType"))
                    continue;
                if (child.getNodeName().equals("xsi:type"))
                    continue;
                if (child.getNodeName().equals("soapenv:encodingStyle"))
                    continue;

                String variableName = getVariableName(variables, elementName + "_" + child.getLocalName());

                child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                RequestableHttpVariable httpVariable = createHttpVariable(false, variableName,
                        Constants.XSD_STRING);
                variables.add(httpVariable);
            }

            // extract from children nodes
            boolean multi = false;
            QName qname = Constants.XSD_STRING;
            NodeList children = element.getChildNodes();
            if (children.getLength() > 0) {
                Node child = element.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.COMMENT_NODE) {
                        String value = child.getNodeValue();
                        if (value.startsWith("type:")) {
                            String schemaType = child.getNodeValue().substring("type:".length()).trim();
                            qname = getVariableSchemaType(xmlSchema, schemaType);
                        }
                        if (value.indexOf("repetitions:") != -1) {
                            multi = true;
                        }
                    } else if (child.getNodeType() == Node.TEXT_NODE) {
                        String value = child.getNodeValue().trim();
                        if (value.equals("?") || !value.equals("")) {
                            String variableName = getVariableName(variables, elementName);

                            child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                            RequestableHttpVariable httpVariable = createHttpVariable(isMulti, variableName,
                                    variableType);
                            variables.add(httpVariable);
                        }
                    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
                        extractSoapVariables(xmlSchema, variables, child, elementName, multi, qname);
                        multi = false;
                        qname = Constants.XSD_STRING;
                    }

                    child = child.getNextSibling();
                }
            }

        }
    }
}

From source file:info.ajaxplorer.client.model.Node.java

public void initFromXmlNode(org.w3c.dom.Node xmlNode) {
    if (this.resourceType.equals(NODE_TYPE_REPOSITORY)) {
        this.addProperty("icon", "repository.png");
        NodeList children = xmlNode.getChildNodes();
        for (int k = 0; k < children.getLength(); k++) {
            if (children.item(k).getNodeName().equals("label")) {
                this.label = children.item(k).getTextContent();
            } /*else if(children.item(k).getNodeName().equals("client_settings")){
                this.addProperty("icon", children.item(k).getAttributes().getNamedItem("icon").getNodeValue());
              }*///www .j  a  va 2  s  .c o m
        }
    }
    NamedNodeMap map = xmlNode.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        String name = map.item(i).getNodeName();
        String value = map.item(i).getTextContent();
        if (name == "icon" || name == "openicon") {
            value = value.replace("-", "_");
        }
        if (this.resourceType.equals(NODE_TYPE_REPOSITORY)) {
            if (name.equals("id")) {
                this.addProperty("repository_id", value);
            } else if (name.equals("repositorySlug")) {
                this.addProperty("slug", value);
            } else {
                this.addProperty(name, value);
            }
        } else {
            if (name.equals("text")) {
                this.label = value;
            } else if (name.equals("is_file")) {
                if (value.equalsIgnoreCase("true")) {
                    this.setLeaf();
                }
            } else if (name.equals("ajxp_modiftime")) {
                this.lastModified = new Date(Long.parseLong(value) * 1000);
            } else if (name.equals("filename")) {
                this.path = value;
            } else {
                this.addProperty(name, value);
            }
        }
    }
    if (getPropertyValue("ajxp_mime") != null
            && getPropertyValue("ajxp_mime").equalsIgnoreCase("ajxp_browsable_archive")) {
        this.leaf = false;
    }
    this.uuid = UUID.randomUUID();
    this.setStatus(NODE_STATUS_FRESH);
}

From source file:cc.siara.csv_ml.ParsedObject.java

/**
 * Performas any pending activity against an element to finalize it. In this
 * case, it adds any pending attributes needing namespace mapping.
 * /*from w w  w .  ja v a2 s . c o  m*/
 * Also if the node has a namespace attached, it recreates the node with
 * specific URI.
 */
public void finalizeElement() {
    // Add all remaining attributes
    for (String col_name : pendingAttributes.keySet()) {
        String value = pendingAttributes.get(col_name);
        int cIdx = col_name.indexOf(':');
        String ns = col_name.substring(0, cIdx);
        String nsURI = nsMap.get(ns);
        if (nsURI == null)
            nsURI = generalNSURI;
        Attr attr = doc.createAttributeNS(nsURI, col_name.substring(cIdx + 1));
        attr.setPrefix(ns);
        attr.setValue(value);
        ((Element) cur_element).setAttributeNodeNS(attr);
    }
    // If the element had a namespace prefix, it has to be recreated.
    if (!currentElementNS.equals("") && !doc.getDocumentElement().equals(cur_element)) {
        Node parent = cur_element.getParentNode();
        Element cur_ele = (Element) parent.removeChild(cur_element);
        String node_name = cur_ele.getNodeName();
        String nsURI = nsMap.get(currentElementNS);
        if (nsURI == null)
            nsURI = generalNSURI;
        Element new_node = doc.createElementNS(nsURI, currentElementNS + ":" + node_name);
        parent.appendChild(new_node);
        // Add all attributes
        NamedNodeMap attrs = cur_ele.getAttributes();
        while (attrs.getLength() > 0) {
            Attr attr = (Attr) attrs.item(0);
            cur_ele.removeAttributeNode(attr);
            nsURI = attr.getNamespaceURI();
            new_node.setAttributeNodeNS(attr);
        }
        // Add all CData sections
        NodeList childNodes = cur_ele.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.CDATA_SECTION_NODE)
                new_node.appendChild(node);
        }
        cur_element = new_node;
    }
    pendingAttributes = new Hashtable<String, String>();
}