Example usage for org.w3c.dom Node getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node findFirstDescendantHasAttr(Node parent, String attrName, String attrValue) {
    if (parent == null)
        return null;
    Node n = parent.getFirstChild();
    while (n != null) {
        NamedNodeMap attrs = n.getAttributes();
        if (attrs != null) {
            Node attr = attrs.getNamedItem(attrName);
            if (attr != null) {
                if (attr.getNodeValue().equals(attrValue)) {
                    return n;
                }//ww  w .j a  va  2  s .co m
            }
        }
        Node d = findFirstDescendantHasAttr(n, attrName, attrValue);
        if (d != null) {
            return d;
        }
        n = n.getNextSibling();
    }
    return n;
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;//from   w w w .j a  va 2  s  . c  o m

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:Main.java

/**
 * Returns the value of a named node attribute in the form of a boolean
 * @param node The node to retrieve the attribute from
 * @param name The name of the attribute
 * @param defaultValue The default value if the attribute cannot be located or converted.
 * @return true or false/*w  w w.ja  va 2 s.  c  om*/
 */
public static boolean getAttributeByName(Node node, String name, boolean defaultValue) {
    if (node == null || name == null)
        return defaultValue;
    try {
        return getAttributeBooleanByName(node.getAttributes(), name);
    } catch (Exception e) {
        return defaultValue;
    }
}

From source file:edu.emory.library.tast.database.tabscommon.VisibleAttribute.java

private static VisibleAttribute fromXML(Node node) {

    String name = node.getAttributes().getNamedItem("id").getNodeValue();
    String category = node.getAttributes().getNamedItem("userCategory").getNodeValue();
    String userLabel = node.getAttributes().getNamedItem("userLabel").getNodeValue();
    String format = null;// ww w  . j  a v a  2  s .  c  o m
    if (node.getAttributes().getNamedItem("format") != null) {
        format = node.getAttributes().getNamedItem("format").getNodeValue();
    }

    boolean date = false;
    if (node.getAttributes().getNamedItem("date") != null) {
        date = "true".equals(node.getAttributes().getNamedItem("date").getNodeValue());
    }

    List attributesList = new ArrayList();
    NodeList attrsList = node.getChildNodes();
    for (int j = 0; j < attrsList.getLength(); j++) {
        Node attrsListChild = attrsList.item(j);
        if (attrsListChild != null) {
            NodeList attrs = attrsListChild.getChildNodes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                if (attr.getNodeType() == Node.ELEMENT_NODE) {
                    String attrName = attr.getAttributes().getNamedItem("name").getNodeValue();
                    attributesList.add(Voyage.getAttribute(attrName));
                }
            }
        }
    }
    VisibleAttribute attr = new VisibleAttribute(name,
            (Attribute[]) attributesList.toArray(new Attribute[] {}));
    String[] cats = category.split(",");
    UserCategories cat = new UserCategories();
    for (int i = 0; i < cats.length; i++) {
        cat.addTo(UserCategory.parse(cats[i]));
    }
    attr.setUserCategories(cat);
    attr.setUserLabel(userLabel);
    attr.setDate(date);
    if (!StringUtils.isNullOrEmpty(format)) {
        attr.setFormat(format);
    }
    return attr;
}

From source file:com.ephesoft.dcma.util.OCREngineUtil.java

/**
 * To format HOCR for Tesseract.//from  w  w w  . j  a  v  a2  s .c  om
 * @param outputFilePath {@link String}
 * @param actualFolderLocation  {@link String}
 * @param pageId {@link String}
 * @throws XPathExpressionException if error occurs
 * @throws TransformerException if error occurs
 * @throws IOException if error occurs
 */
public static void formatHOCRForTesseract(final String outputFilePath, final String actualFolderLocation,
        final String pageId) throws XPathExpressionException, TransformerException, IOException {
    LOGGER.info("Entering format HOCR for tessearct . outputfilepath : " + outputFilePath);
    InputStream inputStream = new FileInputStream(outputFilePath);
    XPathFactory xFactory = new org.apache.xpath.jaxp.XPathFactoryImpl();
    XPath xpath = xFactory.newXPath();
    XPathExpression pageExpr = xpath.compile("//div[@class=\"ocr_page\"]");
    XPathExpression wordExpr = xpath.compile("//span[@class=\"ocr_word\"]");

    // Output format supported by Tesseract 3.00
    XPathExpression xOcrWordExpr = xpath.compile("//span[@class=\"xocr_word\"]");

    // Output format supported by Tesseract 3.01
    XPathExpression ocrXWordExpr = xpath.compile("//span[@class=\"ocrx_word\"]");

    org.w3c.dom.Document doc2 = null;
    try {
        doc2 = XMLUtil.createDocumentFrom(inputStream);
    } catch (Exception e) {
        LOGGER.info("Premature end of file for " + outputFilePath + e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    if (doc2 != null) {
        LOGGER.info("document is not null.");
        NodeList wordList = (NodeList) wordExpr.evaluate(doc2, XPathConstants.NODESET);
        for (int wordNodeIndex = 0; wordNodeIndex < wordList.getLength(); wordNodeIndex++) {
            setWordNodeTextContent(xOcrWordExpr, ocrXWordExpr, wordList, wordNodeIndex);
        }

        NodeList pageList = (NodeList) pageExpr.evaluate(doc2, XPathConstants.NODESET);
        for (int pageNodeIndex = 0; pageNodeIndex < pageList.getLength(); pageNodeIndex++) {
            Node pageNode = pageList.item(pageNodeIndex);
            if (pageNode != null
                    && ((Node) pageNode.getAttributes().getNamedItem(UtilConstants.ID_ATTR)) != null) {
                String pageID = ((Node) pageNode.getAttributes().getNamedItem(UtilConstants.ID_ATTR))
                        .getTextContent();
                wordExpr = xpath.compile("//div[@id='" + pageID + "']//span[@class='ocr_word']");
                NodeList wordInPageList = (NodeList) wordExpr.evaluate(pageNode, XPathConstants.NODESET);

                Node pageNodeClone = pageNode.cloneNode(false);
                for (int i = 0; i < wordInPageList.getLength(); i++) {
                    pageNodeClone.appendChild(wordInPageList.item(i));
                }
                pageNode.getParentNode().appendChild(pageNodeClone);
                pageNode.getParentNode().removeChild(pageNode);
            }
        }

        XMLUtil.flushDocumentToFile(doc2.getDocumentElement().getOwnerDocument(), outputFilePath);
        File tempFile = new File(actualFolderLocation + File.separator + pageId + "_tempFile_hocr.html");
        FileUtils.copyFile(new File(outputFilePath), tempFile);

        XMLUtil.htmlOutputStream(tempFile.getAbsolutePath(), outputFilePath);
        boolean isTempFileDeleted = tempFile.delete();
        if (!isTempFileDeleted) {
            tempFile.delete();
        }
    }
    LOGGER.info("Exiting format HOCR for tessearct . outputfilepath : " + outputFilePath);
}

From source file:it.polimi.diceH2020.plugin.control.JSonReader.java

/**
 * set the attribute named as "name" in the node "n" with a given value
 * //from  ww w  .  j  ava2  s.c  o m
 * @param n
 *            the node in which you want to set the attribute
 * @param name
 *            the name of the attribute
 * @param value
 *            the value of the attribute
 */
private static void setAttribute(Node n, String name, String value) {
    NamedNodeMap attributes = n.getAttributes();
    if (!JSonReader.findAttribute(attributes, name)) {
        Element el = (Element) n;
        el.setAttribute(name, value);
    } else {
        Node nodeAttrType1 = attributes.getNamedItem(name);
        nodeAttrType1.setTextContent(value);
    }
}

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }/* w  ww . j  a v  a  2s  .  c om*/
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:org.ensembl.gti.seqstore.database.cramstore.EnaCramSubmitter.java

protected static boolean isSuccess(Document doc) {
    try {//from w ww . j av a2 s.  c  o  m
        XPathExpression success = xpath.compile("/RECEIPT[@success]");
        Node nl = (Node) success.evaluate(doc, XPathConstants.NODE);
        return "true".equals(nl.getAttributes().getNamedItem("success").getTextContent());
    } catch (XPathExpressionException e) {
        throw new EnaSubmissionException("Could not parse submission receipt", e);
    }
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();/*from w w  w . j  a va 2  s .  co m*/
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:com.tascape.qa.th.android.model.UIA.java

public static UIANode parseNode(Node node) {
    if (!node.getNodeName().equals(UIANode.TAG_NAME)) {
        return null;
    }/*from w  w  w  . j  a  v  a  2 s .  c  o  m*/

    NamedNodeMap map = node.getAttributes();
    String klass = map.getNamedItem("class").getNodeValue();
    UIANode uiNode = newNode(klass);

    for (int i = 0, j = map.getLength(); i < j; i++) {
        Node attr = map.item(i);
        uiNode.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        UIANode n = parseNode(nl.item(i));
        if (n == null) {
            continue;
        }
        uiNode.addNode(n);
    }

    return uiNode;
}