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:com.ikon.util.FormUtils.java

/**
 * Parse form.xml definitions/*from  ww  w  . ja  v  a2s  .c o m*/
 * 
 * @return A Map with all the forms and its form elements.
 */
public static Map<String, List<FormElement>> parseWorkflowForms(InputStream is) throws ParseException {
    log.debug("parseWorkflowForms({})", is);
    Map<String, List<FormElement>> forms = new HashMap<String, List<FormElement>>();

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        ErrorHandler handler = new ErrorHandler();
        // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.setEntityResolver(resolver);

        if (is != null) {
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nlForm = doc.getElementsByTagName("workflow-form");

            for (int i = 0; i < nlForm.getLength(); i++) {
                Node nForm = nlForm.item(i);

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    String taskName = nForm.getAttributes().getNamedItem("task").getNodeValue();
                    NodeList nlField = nForm.getChildNodes();
                    List<FormElement> fe = parseField(nlField);
                    forms.put(taskName, fe);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ParseException(e.getMessage(), e);
    }

    log.debug("parseWorkflowForms: {}", forms);
    return forms;
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *//*from   ww w  .j a  va  2  s.  c om*/
public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if (target == null || node.getOwnerDocument() == target)
        // same Document
        return node.cloneNode(deep);
    else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null)
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep)
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
                newNode.appendChild(cloneNode(child, target, true));

        return newNode;
    }
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static List<TechnicalFragment> parseRassetXML(String path, TechnicalFragment tf) {

    List<TechnicalFragment> dependencies = new ArrayList<TechnicalFragment>();

    Document doc = getDocument(path);
    if (doc != null) {
        if (doc != null) {
            Node type = doc.getElementsByTagName("type").item(0);
            Node origin = doc.getElementsByTagName("origin").item(0);
            Node objective = doc.getElementsByTagName("objective").item(0);
            Node input = doc.getElementsByTagName("input").item(0);
            Node output = doc.getElementsByTagName("output").item(0);

            tf.setType(type.getAttributes().getNamedItem("value").getNodeValue());
            tf.setOrigin(origin.getAttributes().getNamedItem("value").getNodeValue());
            tf.setObjective(objective.getAttributes().getNamedItem("value").getNodeValue());
            tf.setInput(input.getAttributes().getNamedItem("value").getNodeValue());
            tf.setOutput(output.getAttributes().getNamedItem("value").getNodeValue());

            NodeList deps = doc.getElementsByTagName("dependency");
            int length = deps.getLength();
            for (int i = 0; i < length; i++) {
                Node depNode = deps.item(i);
                String depName = depNode.getAttributes().getNamedItem("value").getNodeValue();
                String extension = ".ras.zip";
                String assetName = depName.substring(0, depName.length() - extension.length());
                TechnicalFragment asset = new TechnicalFragment(assetName, "", "", "", "", "");
                dependencies.add(asset);
            }//from  ww w.  j ava  2s .  c o m
        }
    }

    return dependencies;
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;//from ww  w  . j  a  v  a2 s .  co  m
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:org.ambraproject.article.service.ArticleDocumentServiceTest.java

@Test
public void testGetFullXml() throws Exception {
    Article article = new Article("info:doi/10.1371/journal.pgen.1000096");
    dummyDataStore.store(article);/*from   w w  w. jav a2  s . co m*/

    Document xml = articleDocumentService.getFullDocument(article.getDoi());
    assertNotNull(xml, "returned null xml document");

    //check the doi from the xml
    NodeList articleIdNodes = xml.getElementsByTagName("article-id");
    for (int i = 0; i < articleIdNodes.getLength(); i++) {
        Node node = articleIdNodes.item(i);
        if (node.getAttributes().getNamedItem("pub-id-type").getNodeValue().equals("doi")) {
            assertEquals(node.getChildNodes().item(0).getNodeValue(),
                    article.getDoi().replaceFirst("info:doi/", ""), "returned article xml with incorrect doi");
            break;
        }
    }
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *//*from  w w w.java  2  s.  c  o m*/
public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if ((target == null) || (node.getOwnerDocument() == target)) {
        // same Document
        return node.cloneNode(deep);
    } else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null) {
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }
            }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                newNode.appendChild(cloneNode(child, target, true));
            }
        }

        return newNode;
    }
}

From source file:DOMTreeFull.java

public static String toString(Node node) {
    StringBuffer sb = new StringBuffer();

    // is there anything to do?
    if (node == null) {
        return "";
    }/* w w  w.  jav  a2s .  c om*/

    int type = node.getNodeType();
    sb.append(whatArray[type]);
    sb.append(" : ");
    sb.append(node.getNodeName());
    String value = node.getNodeValue();
    if (value != null) {
        sb.append(" Value: \"");
        sb.append(value);
        sb.append("\"");
    }

    switch (type) {

    // document
    case Node.DOCUMENT_NODE: {
        break;
    }

    // element with attributes
    case Node.ELEMENT_NODE: {
        Attr attrs[] = sortAttributes(node.getAttributes());
        if (attrs.length > 0)
            sb.append(" ATTRS:");
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];

            sb.append(' ');
            sb.append(attr.getNodeName());
            sb.append("=\"");
            sb.append(normalize(attr.getNodeValue()));
            sb.append('"');
        }
        sb.append('>');
        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        break;
    }

    // cdata sections
    case Node.CDATA_SECTION_NODE: {
        break;
    }

    // text
    case Node.TEXT_NODE: {
        break;
    }

    // processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        break;
    }

    // comment node
    case Node.COMMENT_NODE: {
        break;
    }
    // DOCTYPE node
    case Node.DOCUMENT_TYPE_NODE: {
        break;
    }
    // Notation node
    case Node.NOTATION_NODE: {
        sb.append("public:");
        String id = ((Notation) node).getPublicId();
        if (id == null) {
            sb.append("PUBLIC ");
            sb.append(id);
            sb.append(" ");
        }
        id = ((Notation) node).getSystemId();
        if (id == null) {
            sb.append("system: ");
            sb.append(id);
            sb.append(" ");
        }
        break;
    }
    }
    return sb.toString();
}

From source file:com.intel.podm.redfish.resources.MetadataResourceProvider.java

private void updateUris(Document xmlDocument) {
    NodeList list = xmlDocument.getElementsByTagName("edmx:Reference");
    for (int i = 0; i < list.getLength(); i++) {
        Node item = list.item(i);
        Node uri = item.getAttributes().getNamedItem("Uri");
        uri.setTextContent("/redfish/v1/metadata/" + uri.getTextContent());
    }//  ww  w . j  av a2s  .co  m
}

From source file:com.rackspace.api.clients.veracode.responses.AppListResponse.java

private Map<String, String> buildAppMap() {
    Map<String, String> applications = new HashMap<String, String>();

    NodeList applicationNodes = null;

    try {//  w ww . ja  v  a2s.  c  o  m
        XPathExpression expression = xpath.compile(XPATH_EXPRESSION);

        applicationNodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }

    if (applicationNodes != null) {
        for (int i = 0; i < applicationNodes.getLength(); i++) {
            Node currentNode = applicationNodes.item(i);

            applications.put(currentNode.getAttributes().getNamedItem("app_name").getNodeValue(),
                    currentNode.getAttributes().getNamedItem("app_id").getNodeValue());
        }
    }

    return applications;
}