Example usage for org.dom4j Node getText

List of usage examples for org.dom4j Node getText

Introduction

In this page you can find the example usage for org.dom4j Node getText.

Prototype

String getText();

Source Link

Document

Returns the text of this node.

Usage

From source file:edu.scripps.fl.pubchem.web.pug.PowerUserGateway.java

License:Apache License

private String makeRequest() throws Exception {
    InputStream is = fetchResponse(getRequest());
    Document doc = getDocument(is);
    setResponse(doc);/*ww  w . j av a  2s  .  c  o  m*/
    Node node = doc.selectSingleNode("//PCT-Status[@value='server-error']");
    if (node != null) {
        String msg = node.getParent().getParent().getText();
        throw new Exception(String.format("PUG Error: %s", msg));
    }
    node = doc.selectSingleNode("//*/PCT-Waiting_reqid");
    String requestId = null;
    if (node != null) {
        requestId = node.getText();
        log.debug("PCT-Waiting_reqid: " + requestId);
    }
    return requestId;
}

From source file:edu.scripps.fl.pubchem.xml.extract.CategorizedCommentExtractor.java

License:Apache License

public List<CategorizedComment> getCategorizedCommentsFromXML(Document doc) throws Exception {
    List<CategorizedComment> comments = new ArrayList<CategorizedComment>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_categorized-comment/PC-CategorizedComment");
    PubChemXMLUtils utils = new PubChemXMLUtils();
    for (Node nn : nodes) {
        CategorizedComment comment = new CategorizedComment();
        comment.setCommentTag(nn.selectSingleNode("PC-CategorizedComment_title").getText());
        List<Node> valueNodes = nn.selectNodes("PC-CategorizedComment_comment/PC-CategorizedComment_comment_E");
        String text = "";
        for (Node valeuNode : valueNodes) {
            if (null != valeuNode.getText())
                text = text + valeuNode.getText() + "\n";
        }// w w  w .jav a2  s.c  o m
        comment.setCommentValue(text);
        comments.add(comment);
    }
    return comments;
}

From source file:edu.scripps.fl.pubchem.xml.extract.PanelExtractor.java

License:Apache License

private List<Integer> getXrefValues(String nodeName, Node parent) {
    List<Node> nodes = parent.selectNodes(nodeName);
    List<Integer> list = new ArrayList<Integer>();
    if (nodes != null) {
        for (Node node : nodes)
            list.add(Integer.parseInt(node.getText()));
    }//  ww  w  .j a v  a  2  s . c o m
    return list;
}

From source file:edu.scripps.fl.pubchem.xml.extract.ResultTidExtractor.java

License:Apache License

public List<ResultTid> getTidValuesFromXML(Document doc)
        throws IllegalAccessException, InvocationTargetException {

    List<ResultTid> tidValues = new ArrayList<ResultTid>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_results/PC-ResultType");
    for (Node nn : nodes) {
        ResultTid tidValue = new ResultTid();
        tidValue.setTidName(nn.selectSingleNode("PC-ResultType_name").getText());
        tidValue.setTidType(nn.selectSingleNode("PC-ResultType_type").valueOf("@value"));
        Node node = nn.selectSingleNode("PC-ResultType_unit");
        if (node != null)
            tidValue.setTidUnit(node.valueOf("@value"));
        node = nn.selectSingleNode("PC-ResultType_description/PC-ResultType_description_E");
        if (node != null)
            tidValue.setTidDescription(node.getText());
        node = nn.selectSingleNode("PC-ResultType_ac");
        if (node != null)
            tidValue.setIsActiveConcentration(true);
        node = nn.selectSingleNode("PC-ResultType_tc");
        if (node != null) {
            Node node2 = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_dr-id");
            if (node2 != null) {
                BeanUtils.setProperty(tidValue, "tidPlot", node2.getText());
            }/* w  w  w .  j a v  a2 s.  c om*/
            BeanUtils.setProperty(tidValue, "tidConcentration",
                    node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_concentration").getText());
        }
        node = nn.selectSingleNode("PC-ResultType_panel-info/PC-AssayPanelTestResult");
        if (node != null) {
            BeanUtils.setProperty(tidValue, "tidPanelNum",
                    node.selectSingleNode("PC-AssayPanelTestResult_mid").getText());
            tidValue.setTidPanelReadout(
                    node.selectSingleNode("PC-AssayPanelTestResult_readout-annot").valueOf("@value"));
        }
        tidValues.add(tidValue);
    }
    return tidValues;
}

From source file:edu.scripps.fl.pubchem.xml.extract.XrefExtractor.java

License:Apache License

public List<Xref> getXrefValuesFromXML(Document doc, List<Panel> panel) throws Exception {
    List<Xref> xrefs = new ArrayList<Xref>();
    List<Node> nodes = doc.selectNodes("//PC-AssayDescription_xref/PC-AnnotatedXRef");
    for (Node nn : nodes) {
        Xref xref = new Xref();
        Node node = nn.selectSingleNode("PC-AnnotatedXRef_xref/PC-XRefData");
        selectXref(xref, node, "PC-XRefData_aid", "AID");
        selectXref(xref, node, "PC-XRefData_protein-gi", "Protein");
        selectXref(xref, node, "PC-XRefData_gene", "Gene");
        selectXref(xref, node, "PC-XRefData_taxonomy", "Taxonomy");
        selectXref(xref, node, "PC-XRefData_mim", "OMIM");
        selectXref(xref, node, "PC-XRefData_dburl", "Source Database Homepage");
        selectXref(xref, node, "PC-XRefData_mesh", "MESH");
        selectXref(xref, node, "PC-XRefData_sburl", "Substance Homepage");
        selectXref(xref, node, "PC-XRefData_asurl", "Assay Homepage");
        selectXref(xref, node, "PC-XRefData_rn", "Substance Registry #");
        selectXref(xref, node, "PC-XRefData_pmid", "PMID");
        selectXref(xref, node, "PC-XRefData_sid", "SID");
        selectXref(xref, node, "PC-XRefData_cid", "CID");
        selectXref(xref, node, "PC-XRefData_nucleotide-gi", "Nucleotide");
        selectXref(xref, node, "PC-XRefData_gi", "GenBank");
        selectXref(xref, node, "PC-XRefData_mmdb", "MMDB");
        selectXref(xref, node, "PC-XRefData_biosystem", "Biosystems Id");
        if (nn.selectSingleNode("PC-AnnotatedXRef_comment") != null)
            xref.setXrefComment(nn.selectSingleNode("PC-AnnotatedXRef_comment").getText());
        Node citationNode = nn.selectSingleNode("PC-AnnotatedXRef_type");
        if (citationNode != null) {
            if (citationNode.getText().equals("1"))
                xref.setIsPrimaryCitation(true);
        }// www  .  jav a  2  s.  c  o m
        xrefs.add(xref);
    }
    nodes = doc.selectNodes("//PC-AssayDescription_target/PC-AssayTargetInfo");
    for (Node nn : nodes) {
        Xref xref = new Xref();
        BeanUtils.setProperty(xref, "xrefValue", nn.selectSingleNode("PC-AssayTargetInfo_mol-id").getText());
        BeanUtils.setProperty(xref, "xrefTargetValue",
                nn.selectSingleNode("PC-AssayTargetInfo_molecule-type").getText());
        BeanUtils.setProperty(xref, "isTarget", true);
        xrefs.add(xref);
    }
    if (panel != null)
        checkXrefAgainstPanel(xrefs, panel);
    return xrefs;
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java

/**
 * Procedure to populate the RDF metadata and ingest the files
 *//*from   w  ww.j  a  va2 s. c  om*/
public boolean execute() throws Exception {
    if (filesPaths != null) {
        File file = null;
        // List the source files
        for (int i = 0; i < filesPaths.length; i++) {
            file = new File(filesPaths[i]);
            if (file.exists()) {
                listFile(filesMap, file);
            }
        }
    }

    String message = "";
    Document doc = null;
    DamsURI damsURI = null;

    String oid = null;
    int fLen = rdfFiles.length;
    String currFile = null;
    SAXReader saxReader = new SAXReader();
    for (int i = 0; i < fLen && !interrupted; i++) {
        currFile = rdfFiles[i].getName();
        setStatus(
                "Processing external import for file " + currFile + " (" + (i + 1) + " of " + fLen + ") ... ");
        try {
            doc = saxReader.read(rdfFiles[i]);
            List<Node> nodes = doc.selectNodes("//@rdf:about");
            for (int j = 0; j < nodes.size(); j++) {
                Node nUri = nodes.get(j);
                String iUri = nUri.getStringValue();
                Node parentNode = nUri.getParent();
                String nName = parentNode.getName();
                if (iUri.endsWith("/COL") || !(iUri.startsWith("http") && iUri.indexOf("/ark:/") > 0)) {
                    // Assign ARK
                    if (nName.endsWith("Object") || nName.endsWith("Component") || nName.endsWith("File")
                            || (((Element) parentNode).isRootElement()
                                    || (parentNode.getParent().isRootElement()
                                            && parentNode.getParent().getName().equals("RDF")))) {
                        String objId = iUri;

                        if (nName.endsWith("Component") || nName.endsWith("File")) {
                            damsURI = DamsURI.toParts(iUri, null);
                            objId = damsURI.getObject();
                        }
                        String srcObjKey = objId + "::" + rdfFiles[i].getAbsolutePath();
                        oid = idsMap.get(srcObjKey);

                        // Assign new ARK
                        if (oid == null) {
                            oid = getNewId();
                            idsMap.put(srcObjKey, oid);
                        }

                        if (nName.endsWith("Object")) {
                            objId = oid;
                            objRecords.put(objId, currFile);
                        } else if (nName.endsWith("Component") || nName.endsWith("File")) {
                            damsURI.setObject(oid);
                            // XXX
                            // Assign cid and fid for Component and FIle if required
                            objId = damsURI.toString();
                        } else
                            objId = oid;
                        nUri.setText(objId);
                        updateReference(doc, iUri, objId);
                    } else {
                        String field = null;
                        Node tNode = null;
                        String xPath = null;
                        Map<String, String> props = new TreeMap<String, String>();
                        String elemXPath = parentNode.getPath();
                        if (nName.endsWith("Collection") || nName.endsWith("CollectionPart")) {
                            // Retrieve the Collection record
                            field = "title_tesim";
                            xPath = "dams:title/mads:Title/mads:authoritativeLabel";
                            tNode = parentNode.selectSingleNode(xPath);
                            if (tNode == null) {
                                // Loop through to locate the rdfs:label if not selected by xPath.
                                Node n = parentNode.selectSingleNode("dams:title");
                                for (Iterator<Element> it = ((Element) n).elementIterator(); it.hasNext();) {
                                    Element elem = it.next();
                                    if (elem.getNamespacePrefix().equals("mads")
                                            && elem.getName().equals("Title"))
                                        tNode = elem.selectSingleNode("mads:authoritativeLabel");
                                }
                            }
                        } /* else if (nName.endsWith("Language") || nName.endsWith("Authority") || nName.endsWith("Subject") || nName.endsWith("Name") || nName.endsWith("Topic") || nName.endsWith("GenreForm") || nName.endsWith("Temporal") || nName.endsWith("Geographic")){
                            // Subject, Authority records use mads:authoritativeLabel
                            field = "name_tesim";
                            xPath = "mads:authoritativeLabel";
                            tNode = parentNode.selectSingleNode(xPath);
                          } */else if (nName.endsWith(COPYRIGHT)) {
                            // Copyright records use dams:copyrightStatus, plus other properties in the next step.
                            field = "status_tesim";
                            xPath = "dams:copyrightStatus";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = copyrightProperties(parentNode);
                        } else if (nName.endsWith(LICENSE)) {
                            // License records use dams:LicenseNote, plus other properties in the next step.
                            field = "note_tesim";
                            xPath = "dams:licenseNote";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = licenseProperties(parentNode);
                        } else if (nName.endsWith(OTHERRIGHTS)) {
                            // Copyright records use dams:copyrightStatus, plus other properties in the next step.
                            field = "otherRightsBasis_tesim";
                            xPath = "dams:otherRightsBasis";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = licenseProperties(parentNode);
                        } else if (nName.endsWith(RELATEDRESOURCE)) {
                            // RelatedResource records use dams:description, plus other properties in the next step.
                            field = "description_tesim";
                            xPath = "dams:description";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = relatedResourceProperties(parentNode);
                        } else if (nName.endsWith(SOURCECAPTURE)) {
                            // SourceCapture records use dams:sourceType, plus other properties in the next step.
                            field = "sourceType_tesim";
                            xPath = "dams:sourceType";
                            tNode = parentNode.selectSingleNode(xPath);
                            props = sourceCaptureProperties(parentNode);
                        } else if (elemXPath.indexOf("mads", elemXPath.lastIndexOf('/') + 1) >= 0) {
                            // MADSScheme and Language
                            if (nName.endsWith(MADSSCHEME) || nName.equals(LANGUAGE)) {
                                field = "code_tesim";
                                xPath = "mads:code";
                                tNode = parentNode.selectSingleNode(xPath);
                                if (tNode == null) {
                                    field = "name_tesim";
                                    xPath = "rdfs:label";
                                    tNode = parentNode.selectSingleNode(xPath);
                                    if (tNode == null) {
                                        // Loop through to locate the rdfs:label if not selected by xPath.
                                        for (Iterator<Element> it = ((Element) parentNode).elementIterator(); it
                                                .hasNext();) {
                                            Element elem = it.next();
                                            if (elem.getNamespacePrefix().equals("rdfs")
                                                    && elem.getName().equals("label"))
                                                tNode = elem;
                                        }
                                    }
                                }
                            } else {
                                // Subject, Authority records use mads:authoritativeLabel
                                field = "name_tesim";
                                xPath = "mads:authoritativeLabel";
                                tNode = parentNode.selectSingleNode(xPath);
                                if (tNode == null) {
                                    // Try to use the mads:code for mapping when mads:authoritativeLabel is not available
                                    field = "code_tesim";
                                    xPath = "mads:code";
                                    tNode = parentNode.selectSingleNode(xPath);
                                }
                                // Mapping for mads:isMemberOfMADSScheme
                                String madsScheme = null;
                                Node madsSchemeNode = parentNode.selectSingleNode("mads:isMemberOfMADSScheme");
                                if (madsSchemeNode != null) {
                                    Node msValueNode = madsSchemeNode.selectSingleNode("@rdf:resource");
                                    if (msValueNode != null) {
                                        madsScheme = madsSchemeNode.getStringValue();
                                        props.put("scheme_tesim", madsScheme);
                                    } else if ((madsSchemeNode = madsSchemeNode
                                            .selectSingleNode("mads:MADSScheme")) != null
                                            && madsSchemeNode.hasContent()) {
                                        if ((msValueNode = madsSchemeNode
                                                .selectSingleNode("mads:code")) != null) {
                                            madsScheme = msValueNode.getText();
                                            props.put("scheme_code_tesim", madsScheme);
                                        } else if ((msValueNode = madsSchemeNode
                                                .selectSingleNode("rdfs:label")) != null) {
                                            madsScheme = msValueNode.getText();
                                            props.put("scheme_name_tesim", madsScheme);
                                        }
                                    } else {
                                        props.put("scheme_tesim", "");
                                    }
                                } else {
                                    props.put("scheme_tesim", null);
                                }
                            }

                        } else {
                            // XXX Other Rights records like Statute, License, Other Rights etc. 
                            field = "value_tesim";
                            xPath = "rdf:value";
                            tNode = parentNode.selectSingleNode(xPath);
                            field = "code_tesim";
                            if (tNode == null) {
                                xPath = "dams:code";
                                tNode = parentNode.selectSingleNode(xPath);
                            }
                        }
                        if (tNode == null) {
                            throw new Exception("Element " + xPath + " is missing from the " + nName
                                    + " record " + iUri + " in file " + currFile + ".");
                        }
                        updateDocument(doc, parentNode, field, tNode.getText(), props);
                    }
                } else if (nName.endsWith("Object")) {
                    objRecords.put(iUri, currFile);
                }
            }

            String dams4Rdf = doc.asXML();
            logData("dams4_" + rdfFiles[i].getName(), dams4Rdf);

            // Ingest the records
            String subjectId = null;
            DamsURI objURI = null;
            List<DamsURI> objURIs = null;
            RDFStore graph = null;

            rdfStore = new RDFStore();
            Model rdf = rdfStore.loadRDFXML(dams4Rdf);
            initHandler();

            Model iRdf = null;
            int jLen = items.size();

            for (int j = 0; j < jLen && !interrupted; j++) {
                graph = new RDFStore();
                recordsCount++;
                // Add subject
                subjectId = items.get(j);
                try {
                    setStatus("Processing metadata for record " + subjectId + " (" + (j + 1) + " of " + jLen
                            + ") in file " + currFile + " ... ");
                    boolean succeeded = false;
                    objURIs = objects.get(subjectId);

                    for (int k = 0; k < objURIs.size(); k++) {
                        objURI = objURIs.get(k);
                        iRdf = rdfStore.querySubject(objURI.toString());
                        graph.merge(iRdf);
                    }

                    // Update object
                    //log.info(j + " ingesting record " + subjectId + ":\n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT) + "\n\n");

                    succeeded = damsClient.updateObject(subjectId, graph.export(RDFStore.RDFXML_ABBREV_FORMAT),
                            Constants.IMPORT_MODE_ADD);

                    if (!succeeded) {
                        if (metadataFailed.indexOf(currFile) < 0)
                            failedCount++;
                        metadataFailed.append(subjectId + " (" + currFile + "), \n");
                        message = "Metadata import for record " + subjectId + " failed (" + (j + 1) + " of "
                                + jLen + ") in file " + currFile + ".";
                        setStatus(message);
                        logError(message + "\n Error RDF: \n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT));
                    } else {
                        recordsIngested.add(subjectId);
                        message = "Metadata import for record " + subjectId + " succeeded (" + (j + 1) + " of "
                                + jLen + ") in file " + currFile + ". ";
                        setStatus(message);
                        logMessage(message);
                        log.info(message);

                        // Update SOLR fre records ingested.
                        updateSOLR(subjectId);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (metadataFailed.indexOf(currFile) < 0)
                        failedCount++;
                    metadataFailed.append(subjectId + " (" + currFile + "), \n");
                    message = "Metadata import failed: " + e.getMessage();
                    setStatus(message + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + ".");
                    logError(message);
                }

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    interrupted = true;
                    failedCount++;
                    metadataFailed.append(subjectId + " (" + currFile + ") \n");
                    message = "Metadata import interrupted for subject " + subjectId + ". \n Error: "
                            + e.getMessage() + "\n";
                    setStatus("Canceled");
                    clearSession();
                    logError(message);
                }
            }

            // Ingest the source file
            if (importOption.equalsIgnoreCase("metadataAndFiles")) {
                uploadFiles(rdf, currFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            failedCount++;
            message = "Import failed for " + currFile + ": " + e.getMessage();
            setStatus(message + " (" + (i + 1) + " of " + fLen + ").");
            logError(message);
        } finally {
            // Update SOLR for files uploaded
            int iLen = objWithFiles.size();
            for (int j = 0; j < iLen && !interrupted; j++) {
                updateSOLR(objWithFiles.get(j));
            }
        }

        setProgressPercentage(((i + 1) * 100) / fLen);

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
            interrupted = true;
            failedCount++;
            message = "Import interrupted for oject in " + currFile + ". \n Error: " + e.getMessage() + "\n";
            setStatus("Canceled");
            clearSession();
            logError(message);
        }
    }
    return exeResult;
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java

private Map<String, String> getProperties(Node record, Map<String, String> propNames) {
    Map<String, String> props = new TreeMap<String, String>();
    String key = null;//from w  ww.  j  av a  2 s . co m
    String solrName = null;
    String propValue = null;
    for (Iterator<String> it = propNames.keySet().iterator(); it.hasNext();) {
        propValue = null;
        key = it.next();
        solrName = propNames.get(key);
        Node tNode = record.selectSingleNode(key);
        if (tNode != null)
            propValue = tNode.getText().trim();

        props.put(solrName, propValue);
    }
    return props;
}

From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportTsHandler.java

private String encodeLiteralValue(Node node) {
    String value = null;/*from w  ww .j a v a 2 s. c o  m*/
    if (node != null) {
        value = "\"" + StringEscapeUtils.escapeJava(Normalizer.normalize(node.getText(), Normalizer.Form.NFC))
                .replace("'", "\\'").replace("\"", "\\\"") + "\"";
        Node langAttr = node.selectSingleNode("@xml:lang");
        if (langAttr != null)
            value += "@" + langAttr.getStringValue();
    }
    return value;
}

From source file:edu.ucsd.library.xdre.web.CollectionOperationController.java

private static String getTitle(Document doc) {
    String title = "";
    Node node = doc.selectSingleNode("//dams:Object/dams:title/mads:Title/mads:authoritativeLabel");
    if (node != null) {
        title = node.getText();
    }//w w  w.  ja  va  2  s.  co m
    return title;
}

From source file:edu.umd.cs.buildServer.inspection.PMDRunner.java

License:Apache License

private void readPMDTestOutcomes(XMLDocumentBuilder stdoutMonitor) {
    Document document = stdoutMonitor.getDocument();
    if (document == null)
        return;//ww  w . j a  v  a 2  s. c  o  m

    int count = TestOutcome.FIRST_TEST_NUMBER;
    Iterator<?> fileNodeIter = document.selectNodes("//pmd/file").iterator();
    while (fileNodeIter.hasNext()) {
        Node fileElement = (Node) fileNodeIter.next();
        String fileName = fileElement.valueOf("@name");
        Iterator<?> violationIter = fileElement.selectNodes("./violation").iterator();
        while (violationIter.hasNext()) {
            Node violationElement = (Node) violationIter.next();
            String line = violationElement.valueOf("@line");
            String rule = violationElement.valueOf("@rule");
            String description = violationElement.getText();
            String priority = violationElement.valueOf("@priority");

            // Turn the warning into a TestOutcome
            TestOutcome testOutcome = new TestOutcome();
            testOutcome.setTestType(TestOutcome.TestType.PMD);
            testOutcome.setTestName(rule);
            testOutcome.setOutcome(TestOutcome.STATIC_ANALYSIS);
            testOutcome.setShortTestResult(fileName + ":" + line);
            testOutcome.setLongTestResultCompressIfNeeded(description);
            testOutcome.setTestNumber(Integer.toString(count++));
            testOutcome.setExceptionClassName(priority); // XXX: HACK!

            testOutcomeCollection.add(testOutcome);
        }
    }

    projectSubmission.getLog().info("Recorded " + count + " PMD warnings as test outcomes");
}