Example usage for org.w3c.dom Element getNodeName

List of usage examples for org.w3c.dom Element getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Element getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:com.cisco.dvbu.ps.common.adapters.config.AdapterConfig.java

private void parseAdapterCommon(Document doc) throws AdapterException {
    log.debug("Root element :" + doc.getDocumentElement().getNodeName());
    XPath xpath = XPathFactory.newInstance().newXPath();
    try {/*from   www  .  j av  a 2 s  . co  m*/
        Attr a = (Attr) xpath.evaluate(AdapterConstants.XPATH_CONFIG_VERSION, doc, XPathConstants.NODE);
        log.debug(a.getName() + ": " + a.getValue());
        Element e = (Element) xpath.evaluate(AdapterConstants.XPATH_NAME, doc, XPathConstants.NODE);
        name = e.getTextContent();
        log.debug(e.getNodeName() + ": " + e.getTextContent());
        e = (Element) xpath.evaluate(AdapterConstants.XPATH_DESC, doc, XPathConstants.NODE);
        desc = e.getTextContent();
        log.debug(e.getNodeName() + ": " + e.getTextContent());
        e = (Element) xpath.evaluate(AdapterConstants.XPATH_CIS_VERSION, doc, XPathConstants.NODE);
        cisVersion = e.getTextContent();
        log.debug(e.getNodeName() + ": " + e.getTextContent());
        e = (Element) xpath.evaluate(AdapterConstants.XPATH_CONN_RETRYATTMPTS, doc, XPathConstants.NODE);
        retryAttempts = (e != null && e.getTextContent() != null) ? Integer.parseInt(e.getTextContent()) : -1;
        log.debug("retryAttempts: " + retryAttempts);
        e = (Element) xpath.evaluate(AdapterConstants.XPATH_CONN_MAXCLIENTS, doc, XPathConstants.NODE);
        maxClients = (e != null && e.getTextContent() != null) ? Integer.parseInt(e.getTextContent()) : -1;
        log.debug("maxclients: " + maxClients);
        e = (Element) xpath.evaluate(AdapterConstants.XPATH_CONN_MINCLIENTS, doc, XPathConstants.NODE);
        minClients = (e != null && e.getTextContent() != null) ? Integer.parseInt(e.getTextContent()) : -1;
        log.debug("minclients: " + minClients);
    } catch (Exception e) {
        log.error("Configuration File Error! One or more mandatory configuration options are missing");
        throw new AdapterException(302,
                "Configuration File Error! One or more mandatory configuration options are missing.", e);
    }
}

From source file:com.googlecode.npackdweb.RepUploadAction.java

private PackageVersion createPackageVersion(Element e) {
    PackageVersion p = new PackageVersion(e.getAttribute("package"), e.getAttribute("name"));
    p.name = p.package_ + "@" + p.version;
    p.oneFile = e.getAttribute("type").equals("one-file");
    p.url = NWUtils.getSubTagContent(e, "url", "");
    p.sha1 = NWUtils.getSubTagContent(e, "sha1", "");
    p.detectMSI = NWUtils.getSubTagContent(e, "detect-msi", "");

    NodeList children = e.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ch = children.item(i);
        if (ch.getNodeType() == Element.ELEMENT_NODE) {
            Element che = (Element) ch;
            if (che.getNodeName().equals("important-file")) {
                p.importantFilePaths.add(che.getAttribute("path"));
                p.importantFileTitles.add(che.getAttribute("title"));
            } else if (che.getNodeName().equals("file")) {
                p.addFile(che.getAttribute("path"), NWUtils.getTagContent_(che));
            } else if (che.getNodeName().equals("dependency")) {
                p.dependencyPackages.add(che.getAttribute("package"));
                p.dependencyVersionRanges.add(che.getAttribute("versions"));
                p.dependencyEnvVars.add(NWUtils.getSubTagContent(che, "variable", ""));
            }/*from  w w w . j  av  a 2 s.  com*/
        }
    }
    return p;
}

From source file:de.huberlin.wbi.hiway.am.dax.DaxApplicationMaster.java

@Override
public void parseWorkflow() {
    Map<Object, TaskInstance> tasks = new HashMap<>();
    System.out.println("Parsing Pegasus DAX " + getWorkflowFile());

    try {//from w w  w .j  ava2 s . c o m
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new File(getWorkflowFile().getLocalPath().toString()));
        NodeList jobNds = doc.getElementsByTagName("job");

        for (int i = 0; i < jobNds.getLength(); i++) {
            Element jobEl = (Element) jobNds.item(i);
            String id = jobEl.getAttribute("id");
            String taskName = jobEl.getAttribute("name");
            DaxTaskInstance task = new DaxTaskInstance(getRunId(), taskName);
            task.setRuntime(
                    jobEl.hasAttribute("runtime") ? Double.parseDouble(jobEl.getAttribute("runtime")) : 0d);
            tasks.put(id, task);

            StringBuilder arguments = new StringBuilder();
            NodeList argumentNds = jobEl.getElementsByTagName("argument");
            for (int j = 0; j < argumentNds.getLength(); j++) {
                Element argumentEl = (Element) argumentNds.item(j);

                NodeList argumentChildNds = argumentEl.getChildNodes();
                for (int k = 0; k < argumentChildNds.getLength(); k++) {
                    Node argumentChildNd = argumentChildNds.item(k);
                    String argument = "";

                    switch (argumentChildNd.getNodeType()) {
                    case Node.ELEMENT_NODE:
                        Element argumentChildEl = (Element) argumentChildNd;
                        if (argumentChildEl.getNodeName().equals("file")) {
                            if (argumentChildEl.hasAttribute("name")) {
                                argument = argumentChildEl.getAttribute("name");
                            }
                        } else if (argumentChildEl.getNodeName().equals("filename")) {
                            if (argumentChildEl.hasAttribute("file")) {
                                argument = argumentChildEl.getAttribute("file");
                            }
                        }
                        break;
                    case Node.TEXT_NODE:
                        argument = argumentChildNd.getNodeValue().replaceAll("\\s+", " ").trim();
                        break;
                    default:
                    }

                    if (argument.length() > 0) {
                        arguments.append(" ").append(argument);
                    }
                }
            }

            NodeList usesNds = jobEl.getElementsByTagName("uses");
            for (int j = 0; j < usesNds.getLength(); j++) {
                Element usesEl = (Element) usesNds.item(j);
                String link = usesEl.getAttribute("link");
                String fileName = usesEl.getAttribute("file");
                long size = usesEl.hasAttribute("size") ? Long.parseLong(usesEl.getAttribute("size")) : 0l;
                List<String> outputs = new LinkedList<>();

                switch (link) {
                case "input":
                    if (!getFiles().containsKey(fileName)) {
                        Data data = new Data(fileName);
                        data.setInput(true);
                        getFiles().put(fileName, data);
                    }
                    Data data = getFiles().get(fileName);
                    task.addInputData(data, size);
                    break;
                case "output":
                    if (!getFiles().containsKey(fileName))
                        getFiles().put(fileName, new Data(fileName));
                    data = getFiles().get(fileName);
                    task.addOutputData(data, size);
                    data.setInput(false);
                    outputs.add(fileName);
                    break;
                default:
                }

                task.getReport()
                        .add(new JsonReportEntry(task.getWorkflowId(), task.getTaskId(), task.getTaskName(),
                                task.getLanguageLabel(), Long.valueOf(task.getId()), null,
                                JsonReportEntry.KEY_INVOC_OUTPUT, new JSONObject().put("output", outputs)));
            }

            task.setCommand(taskName + arguments.toString());
            System.out.println(
                    "Adding task " + task + ": " + task.getInputData() + " -> " + task.getOutputData());
        }

        NodeList childNds = doc.getElementsByTagName("child");
        for (int i = 0; i < childNds.getLength(); i++) {
            Element childEl = (Element) childNds.item(i);
            String childId = childEl.getAttribute("ref");
            TaskInstance child = tasks.get(childId);

            NodeList parentNds = childEl.getElementsByTagName("parent");
            for (int j = 0; j < parentNds.getLength(); j++) {
                Element parentEl = (Element) parentNds.item(j);
                String parentId = parentEl.getAttribute("ref");
                TaskInstance parent = tasks.get(parentId);

                child.addParentTask(parent);
                parent.addChildTask(child);
            }
        }

        for (TaskInstance task : tasks.values()) {
            if (task.getChildTasks().size() == 0) {
                for (Data data : task.getOutputData()) {
                    data.setOutput(true);
                }
            }

            task.getReport()
                    .add(new JsonReportEntry(task.getWorkflowId(), task.getTaskId(), task.getTaskName(),
                            task.getLanguageLabel(), Long.valueOf(task.getId()), null,
                            JsonReportEntry.KEY_INVOC_SCRIPT, task.getCommand()));
        }

    } catch (WorkflowStructureUnknownException | IOException | JSONException | ParserConfigurationException
            | SAXException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    getScheduler().addTasks(tasks.values());
}

From source file:com.tomdignan.android.opencnam.library.teststub.test.OpenCNAMRequestTestCase.java

public void testXMLRequest() {
    mOpenCNAMRequest.setSerializationFormat(OpenCNAMRequest.FORMAT_XML);
    // TEXT is default.
    String response = null;/* www. j a  v a2s  .c o m*/

    try {
        response = (String) mOpenCNAMRequest.execute();
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "ClientProtocolException: " + e.getMessage());
    }

    assertNotNull(response);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(response.getBytes());
    Document document = null;

    try {
        document = mDocumentBuilder.parse(inputStream);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    assertNotNull(document);

    Element root = document.getDocumentElement();

    String rootName = root.getNodeName();
    assertNotNull(rootName);
    assertEquals(rootName, "object");

    NodeList children = root.getChildNodes();
    assertNotNull(children);

    int size = children.getLength();
    assertEquals(size, 2);

    Node cnamNode = children.item(0);
    assertNotNull(cnamNode);

    String cnamValue = cnamNode.getTextContent().trim();
    assertEquals(cnamValue, CNAM);

    String cnamNodeName = cnamNode.getNodeName();
    assertEquals(cnamNodeName, "cnam");

    Node numberNode = children.item(1);
    assertNotNull(numberNode);

    String numberName = numberNode.getNodeName().trim();

    assertNotNull(numberName);
    assertEquals(numberName, "number");

    String numberValue = numberNode.getTextContent().trim();

    assertNotNull(numberValue);
    assertEquals(numberValue, NUMBER);
}

From source file:com.google.publicalerts.cap.CapJsonBuilder.java

private void toJsonObjectInner(Element element, JSONObject object, Map<String, Set<String>> repeatedFieldsMap)
        throws JSONException {
    NodeList nl = element.getChildNodes();
    Set<String> repeatedFields = repeatedFieldsMap.get(element.getNodeName());
    for (int i = 0; i < nl.getLength(); i++) {
        Element child = (Element) nl.item(i);
        int gcLength = child.getChildNodes().getLength();
        if (gcLength == 0) {
            continue;
        }//from w  ww  . ja  v a2 s  .c o m
        String nodeName = child.getNodeName();
        if (repeatedFields != null && repeatedFields.contains(nodeName)) {
            if (gcLength == 1 && child.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) {
                object.append(nodeName, child.getTextContent());
            } else {
                JSONObject childObj = new JSONObject();
                toJsonObjectInner(child, childObj, repeatedFieldsMap);
                object.append(nodeName, childObj);
            }
        } else {
            if (gcLength == 1 && child.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) {
                object.put(child.getNodeName(), child.getTextContent());
            } else {
                JSONObject childObj = new JSONObject();
                toJsonObjectInner(child, childObj, repeatedFieldsMap);
                object.put(child.getNodeName(), childObj);
            }
        }
    }
}

From source file:com.bluexml.side.portal.alfresco.reverse.reverser.EclipseReverser.java

protected void readAnyElements(Map<String, String> props, List<Object> any) {
    for (Object object : any) {
        String nodeName = null;//  w w w  .ja  va2s  .  c  o  m
        String nodeValue = null;
        if (object instanceof Element) {
            System.out.println(" any Element (w3c) ?" + object);
            Element el = (Element) object;
            nodeName = el.getNodeName();
            nodeValue = el.getTextContent();
            props.put(nodeName, nodeValue);
        } else if (object instanceof JAXBElement) {
            JAXBElement<String> jaxbE = (JAXBElement<String>) object;
            QName name = jaxbE.getName();
            nodeName = name.getLocalPart();
            nodeValue = jaxbE.getValue();
        }

        props.put(nodeName, nodeValue);
    }
}

From source file:com.msopentech.odatajclient.engine.data.impl.v3.AtomDeserializer.java

public AtomEntry entry(final Element input) {
    if (!ODataConstants.ATOM_ELEM_ENTRY.equals(input.getNodeName())) {
        return null;
    }/*  w w w  .  ja  va  2  s.com*/

    final AtomEntry entry = new AtomEntry();

    common(input, entry);

    final String etag = input.getAttribute(ODataConstants.ATOM_ATTR_ETAG);
    if (StringUtils.isNotBlank(etag)) {
        entry.setETag(etag);
    }

    final List<Element> categories = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CATEGORY);
    if (!categories.isEmpty()) {
        entry.setType(categories.get(0).getAttribute(ODataConstants.ATOM_ATTR_TERM));
    }

    final List<Element> links = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_LINK);
    for (Element linkElem : links) {
        final AtomLink link = new AtomLink();
        link.setRel(linkElem.getAttribute(ODataConstants.ATTR_REL));
        link.setTitle(linkElem.getAttribute(ODataConstants.ATTR_TITLE));
        link.setHref(linkElem.getAttribute(ODataConstants.ATTR_HREF));

        if (ODataConstants.SELF_LINK_REL.equals(link.getRel())) {
            entry.setSelfLink(link);
        } else if (ODataConstants.EDIT_LINK_REL.equals(link.getRel())) {
            entry.setEditLink(link);
        } else if (link.getRel().startsWith(
                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NAVIGATION_LINK_REL))) {

            link.setType(linkElem.getAttribute(ODataConstants.ATTR_TYPE));
            entry.addNavigationLink(link);

            final List<Element> inlines = XMLUtils.getChildElements(linkElem, ODataConstants.ATOM_ELEM_INLINE);
            if (!inlines.isEmpty()) {
                final List<Element> entries = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_ENTRY);
                if (!entries.isEmpty()) {
                    link.setInlineEntry(entry(entries.get(0)));
                }

                final List<Element> feeds = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_FEED);
                if (!feeds.isEmpty()) {
                    link.setInlineFeed(feed(feeds.get(0)));
                }
            }
        } else if (link.getRel().startsWith(
                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.ASSOCIATION_LINK_REL))) {

            entry.addAssociationLink(link);
        } else if (link.getRel().startsWith(
                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.MEDIA_EDIT_LINK_REL))) {

            entry.addMediaEditLink(link);
        }
    }

    final List<Element> authors = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_AUTHOR);
    if (!authors.isEmpty()) {
        final AtomEntry.Author author = new AtomEntry.Author();
        for (Node child : XMLUtils.getChildNodes(input, Node.ELEMENT_NODE)) {
            if (ODataConstants.ATOM_ELEM_AUTHOR_NAME.equals(XMLUtils.getSimpleName(child))) {
                author.setName(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_URI.equals(XMLUtils.getSimpleName(child))) {
                author.setUri(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_EMAIL.equals(XMLUtils.getSimpleName(child))) {
                author.setEmail(child.getTextContent());
            }
        }
        if (!author.isEmpty()) {
            entry.setAuthor(author);
        }
    }

    final List<Element> actions = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_ACTION);
    for (Element action : actions) {
        final ODataOperation operation = new ODataOperation();
        operation.setMetadataAnchor(action.getAttribute(ODataConstants.ATTR_METADATA));
        operation.setTitle(action.getAttribute(ODataConstants.ATTR_TITLE));
        operation.setTarget(URI.create(action.getAttribute(ODataConstants.ATTR_TARGET)));

        entry.addOperation(operation);
    }

    final List<Element> contents = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CONTENT);
    if (!contents.isEmpty()) {
        final Element content = contents.get(0);

        List<Element> props = XMLUtils.getChildElements(content, ODataConstants.ELEM_PROPERTIES);
        if (props.isEmpty()) {
            entry.setMediaContentSource(content.getAttribute(ODataConstants.ATOM_ATTR_SRC));
            entry.setMediaContentType(content.getAttribute(ODataConstants.ATTR_TYPE));

            props = XMLUtils.getChildElements(input, ODataConstants.ELEM_PROPERTIES);
            if (!props.isEmpty()) {
                entry.setMediaEntryProperties(props.get(0));
            }
        } else {
            entry.setContent(props.get(0));
        }
    }

    return entry;
}

From source file:importer.handler.post.stages.Splitter.java

/**
 * Locate clusters of siblings and their children
 * @param elem the element to start from, already marked
 * @param pos the registry of the cluster
 *//*  ww w . j  a v a 2s .  c  o m*/
private void prepare(Element elem, Cluster pos) {
    if (discriminator.isSibling(elem)) {
        String eName = elem.getNodeName();
        Element next = discriminator.nextTrueSibling(elem);
        if (next != null) {
            pos.inc(eName, elem);
            String pName = pos.getName(eName);
            discriminator.addVersion(elem, pName);
        }
        // else there is no corresponding sibling - ignore it
    }
    // descend depth-first
    Element child = Discriminator.firstChild(elem);
    if (child != null) {
        if (pos.size() > 0)
            pos.descend();
        // recurse down
        prepare(child, pos);
    }
    // try to go sideways
    Element sibling = Discriminator.nextSibling(elem, true);
    if (sibling != null) {
        Element next = discriminator.nextTrueSibling(elem);
        if (next != null) {
            prepare(next, pos);
        } else if (pos.ripe()) {
            pos.percolateUp(this);
            prepare(sibling, new Cluster(discriminator));
        } else
            prepare(sibling, pos);
    }
    // there may be no more true siblings here also
    else if (pos.ripe())
        pos.percolateUp(this);
    else
        pos.ascend();
}

From source file:com.nexmo.client.verify.endpoints.SearchEndpoint.java

protected SearchResult[] parseSearchResponse(String response) throws NexmoResponseParseException {
    Document doc = xmlParser.parseXml(response);

    Element root = doc.getDocumentElement();
    if ("verify_response".equals(root.getNodeName())) {
        // error response
        VerifyResult result = SharedParsers.parseVerifyResponseXmlNode(root);
        return new SearchResult[] {
                new SearchResult(result.getStatus(), result.getRequestId(), null, null, null, 0, null, null,
                        null, null, null, null, null, result.getErrorText(), result.isTemporaryError()) };
    } else if (("verify_request").equals(root.getNodeName())) {
        return new SearchResult[] { parseVerifyRequestXmlNode(root) };
    } else if ("verification_requests".equals(root.getNodeName())) {
        List<SearchResult> results = new ArrayList<>();

        NodeList fields = root.getChildNodes();
        for (int i = 0; i < fields.getLength(); i++) {
            Node node = fields.item(i);
            if (node.getNodeType() != Node.ELEMENT_NODE)
                continue;

            if ("verify_request".equals(node.getNodeName()))
                results.add(parseVerifyRequestXmlNode((Element) node));
        }/*from  w  w  w.j av  a2  s  . c  o  m*/

        return results.toArray(new SearchResult[results.size()]);
    } else {
        throw new NexmoResponseParseException("No valid response found [ " + response + "] ");
    }
}

From source file:com.datatorrent.stram.client.DTConfiguration.java

public void loadFile(File file, Scope defaultScope)
        throws IOException, ParserConfigurationException, SAXException, ConfigException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
    Element documentElement = doc.getDocumentElement();
    if (!documentElement.getNodeName().equals("configuration")) {
        throw new ConfigException("Root element needs to be \"configuration\"");
    }//from w w w  .  j av  a  2 s .c o m
    if (doc.hasChildNodes()) {
        NodeList propertyNodes = documentElement.getChildNodes();
        for (int i = 0; i < propertyNodes.getLength(); i++) {
            Node propertyNode = propertyNodes.item(i);
            if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
                if (propertyNode.getNodeName().equals("property")) {
                    processPropertyNode((Element) propertyNode, defaultScope);
                } else {
                    LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName());
                }
            }
        }
    }
}