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.nridge.core.base.io.xml.DataFieldXML.java

public DataField load(Element anElement) throws IOException {
    Attr nodeAttr;/*from www . j a  v a  2s . c  o  m*/
    Node nodeItem;
    DataField dataField;
    Element nodeElement;
    Field.Type fieldType;
    String nodeName, nodeValue;

    String attrValue = anElement.getAttribute("name");
    if (StringUtils.isNotEmpty(attrValue)) {
        String fieldName = attrValue;
        attrValue = anElement.getAttribute("type");
        if (StringUtils.isNotEmpty(attrValue))
            fieldType = Field.stringToType(attrValue);
        else
            fieldType = Field.Type.Text;
        dataField = new DataField(fieldType, fieldName);

        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, "rangeType")))
                    continue;
                else if (StringUtils.equalsIgnoreCase(nodeName, "title"))
                    dataField.setTitle(nodeValue);
                else if (StringUtils.equalsIgnoreCase(nodeName, "isMultiValue"))
                    dataField.setMultiValueFlag(StrUtl.stringToBoolean(nodeValue));
                else if (StringUtils.equalsIgnoreCase(nodeName, "displaySize"))
                    dataField.setDisplaySize(Field.createInt(nodeValue));
                else if (StringUtils.equalsIgnoreCase(nodeName, "sortOrder"))
                    dataField.setSortOrder(Field.Order.valueOf(nodeValue));
                else if (StringUtils.equalsIgnoreCase(nodeName, "defaultValue"))
                    dataField.setDefaultValue(nodeValue);
                else
                    dataField.addFeature(nodeName, nodeValue);
            }
        }
        String rangeType = anElement.getAttribute("rangeType");
        if (StringUtils.isNotEmpty(rangeType)) {
            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 (StringUtils.equalsIgnoreCase(nodeName, "Range")) {
                    nodeElement = (Element) nodeItem;
                    dataField.setRange(mRangeXML.load(nodeElement));
                } else if (StringUtils.equalsIgnoreCase(nodeName, "Value")) {
                    nodeValue = XMLUtl.getNodeStrValue(nodeItem);
                    String mvDelimiter = dataField.getFeature(Field.FEATURE_MV_DELIMITER);
                    if (StringUtils.isNotEmpty(mvDelimiter))
                        dataField.expand(nodeValue, mvDelimiter.charAt(0));
                    else
                        dataField.expand(nodeValue);
                }
            }
        } else {
            nodeItem = (Node) anElement;
            if (dataField.isFeatureTrue(Field.FEATURE_IS_CONTENT))
                nodeValue = XMLUtl.getNodeCDATAValue(nodeItem);
            else
                nodeValue = XMLUtl.getNodeStrValue(nodeItem);
            if (dataField.isMultiValue()) {
                String mvDelimiter = dataField.getFeature(Field.FEATURE_MV_DELIMITER);
                if (StringUtils.isNotEmpty(mvDelimiter))
                    dataField.expand(nodeValue, mvDelimiter.charAt(0));
                else
                    dataField.expand(nodeValue);
            } else
                dataField.setValue(nodeValue);
        }
    } else
        dataField = null;

    return dataField;
}

From source file:jp.igapyon.selecrawler.SeleCrawlerWebContentAnalyzer.java

public void processFile(final File file, final String urlString) throws IOException {
    final List<String> anchorList = new ArrayList<String>();
    final List<String> headList = new ArrayList<String>();
    final List<String> scriptSrcList = new ArrayList<String>();

    final String contents = FileUtils.readFileToString(
            new File(file.getParentFile(), file.getName() + SeleCrawlerConstants.EXT_SC_NORMAL), "UTF-8");

    final Document document = SimpleMyXmlUtil.string2Document(contents);
    {//ww w  . j a v  a2  s . c o  m
        final String title = SimpleMyXmlUtil.getXPathString(document, "/html/head/title/text()");
        if (title != null && title.trim().length() > 0) {
            headList.add("title: " + title);
        }
    }

    {
        final NodeList nodes = SimpleMyXmlUtil.getXPathNodeList(document, "/html/head/meta");
        for (int index = 0; index < nodes.getLength(); index++) {
            if (nodes.item(index) instanceof Element) {
                final Element eleMeta = (Element) nodes.item(index);
                headList.add("meta:");

                final NamedNodeMap nnm = eleMeta.getAttributes();
                for (int indexNnm = 0; indexNnm < nnm.getLength(); indexNnm++) {
                    final Attr attr = (Attr) nnm.item(indexNnm);

                    headList.add("  " + attr.getName() + " [" + attr.getValue() + "]");
                }
            }
        }
    }

    {
        final NodeList nodes = SimpleMyXmlUtil.getXPathNodeList(document, "/html/head/link");
        for (int index = 0; index < nodes.getLength(); index++) {
            if (nodes.item(index) instanceof Element) {
                final Element eleMeta = (Element) nodes.item(index);
                headList.add("link:");

                final NamedNodeMap nnm = eleMeta.getAttributes();
                for (int indexNnm = 0; indexNnm < nnm.getLength(); indexNnm++) {
                    final Attr attr = (Attr) nnm.item(indexNnm);

                    headList.add("  " + attr.getName() + " [" + attr.getValue() + "]");
                }
            }
        }
    }

    {
        // search anchor with href
        final NodeList nodes = SimpleMyXmlUtil.getXPathNodeList(document, "//a");
        for (int index = 0; index < nodes.getLength(); index++) {
            if (nodes.item(index) instanceof Element) {
                final Element eleAnchor = (Element) nodes.item(index);
                String href = eleAnchor.getAttribute("href");
                href = adjustAnchorUrl(href, urlString);
                if (href == null) {
                    continue;
                }
                anchorList.add(href);
            }
        }
    }

    {
        // search script
        final NodeList nodes = SimpleMyXmlUtil.getXPathNodeList(document, "//script");
        for (int index = 0; index < nodes.getLength(); index++) {
            if (nodes.item(index) instanceof Element) {
                final Element eleScript = (Element) nodes.item(index);
                String srcString = eleScript.getAttribute("src");
                srcString = adjustAnchorUrl(srcString, urlString);
                if (srcString == null) {
                    continue;
                }
                scriptSrcList.add(srcString);
            }
        }
    }

    {
        final File fileMetaAnchor = new File(file.getParentFile(),
                file.getName() + SeleCrawlerConstants.EXT_SC_ANCHOR);
        FileUtils.writeLines(fileMetaAnchor, "UTF-8", anchorList);
    }

    {
        final File fileMetaHead = new File(file.getParentFile(),
                file.getName() + SeleCrawlerConstants.EXT_SC_HEAD);
        FileUtils.writeLines(fileMetaHead, "UTF-8", headList);
    }

    {
        final File fileScriptSrc = new File(file.getParentFile(),
                file.getName() + SeleCrawlerConstants.EXT_SC_SCRIPTSRC);
        FileUtils.writeLines(fileScriptSrc, "UTF-8", scriptSrcList);
    }
}

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

/**
 * Parses an XML DOM element and loads it into a relationship.
 *
 * @param anElement DOM element.//from ww w .  j  av a  2 s . c o m
 *
 * @throws java.io.IOException I/O related exception.
 */
@Override
public void load(Element anElement) throws IOException {
    Node nodeItem;
    Attr nodeAttr;
    Element nodeElement;
    DataBagXML dataBagXML;
    DocumentXML documentXML;
    String nodeName, nodeValue;

    mRelationship.reset();
    String attrValue = anElement.getAttribute("type");
    if (StringUtils.isEmpty(attrValue))
        throw new IOException("Relationship is missing type attribute.");
    mRelationship.setType(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, "type")))
                mRelationship.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_PROPERTIES_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            dataBagXML = new DataBagXML();
            dataBagXML.load(nodeElement);
            mRelationship.setBag(dataBagXML.getBag());
        } else {
            nodeElement = (Element) nodeItem;
            documentXML = new DocumentXML();
            documentXML.load(nodeElement);
            mRelationship.add(documentXML.getDocument());
        }
    }
}

From source file:net.algart.simagis.imageio.IIOMetadataToJsonConverter.java

private JSONObject compactTIFFSimilarNodes(Node node, String childrenName) throws JSONException {
    assert node != null;
    assert childrenName != null;
    long count = 0;
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling(), count++) {
        if (!childrenName.equals(child.getNodeName())) {
            return null;
        }//w  w  w .j av  a 2  s. c  om
        // so, a child has the name, equal to childrenName
        if (child.getFirstChild() != null) {
            return null;
        }
        // so, a child has no own children
        final NamedNodeMap childAttributes = child.getAttributes();
        if (childAttributes == null || childAttributes.getLength() != 1) {
            return null;
        }
        Node childAttr = childAttributes.item(0);
        String attrName = childAttr.getNodeName();
        String attrValue = childAttr.getNodeValue();
        if (!("value".equals(attrName)) || attrValue == null || attrValue.contains(",")) {
            return null;
        }
        // so, a child has only 1 attribute "value", which is a string, not containing ","
    }
    if (count <= MAX_NON_COMPACTED_SIMILAR_CHILDREN) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    count = 0;
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling(), count++) {
        Node childAttr = child.getAttributes().item(0);
        if (count > 0) {
            sb.append(",");
        }
        sb.append(childAttr.getNodeValue());
    }
    JSONObject result = new JSONObject();
    result.put("childrenName", childrenName);
    result.put("childrenValue", sb.toString());
    return result;
}

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

public static boolean hasApplicationAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (isApplicationAttribute(attr)) {
            return true;
        }//from w  ww  . java2 s . co  m
    }
    return false;
}

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

public static Collection<Attr> listApplicationAttributes(Element element) {
    Collection<Attr> attrs = new ArrayList<Attr>();
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (isApplicationAttribute(attr)) {
            attrs.add(attr);//from  w  w  w .  ja  v  a 2  s  .co  m
        }
    }
    return attrs;
}

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

public static void copyContent(Element source, Element destination) {
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        Attr clone = (Attr) attr.cloneNode(true);
        destination.setAttributeNode(clone);
    }//from  www  . ja va2s .  co m
    NodeList childNodes = source.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        destination.appendChild(item);
    }
}

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

/**
 * Returns map of all namespace declarations from specified element (prefix -> namespace).
 *///from ww  w . j ava  2  s . com
public static Map<String, String> getNamespaceDeclarations(Element element) {
    Map<String, String> nsDeclMap = new HashMap<String, String>();
    NamedNodeMap attributes = element.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);
            nsDeclMap.put(prefix, namespace);
        }
    }
    return nsDeclMap;
}

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

public static String getNamespaceDeclarationForPrefix(Element targetElement, String prefix) {
    NamedNodeMap attributes = targetElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (isNamespaceDefinition(attr)) {
            String thisPrefix = getNamespaceDeclarationPrefix(attr);
            if (comparePrefix(prefix, thisPrefix)) {
                return getNamespaceDeclarationNamespace(attr);
            }/*w ww  .  j a  v  a 2 s. c o  m*/
        }
    }
    return null;
}

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

/**
 * Parses an XML DOM element and loads it into a criteria.
 *
 * @param anElement DOM element./*w w  w .  ja  va2  s.  com*/
 *
 * @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, logicalOperator;

    String className = mDSCriteria.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))
        mDSCriteria.setName(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")))
                continue;
            else
                mDSCriteria.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) {
                logicalOperator = dataField.getFeature("operator");
                if (StringUtils.isEmpty(logicalOperator))
                    logicalOperator = Field.operatorToString(Field.Operator.EQUAL);
                mDSCriteria.add(dataField, Field.stringToOperator(logicalOperator));
            }
        }
    }
}