Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static void parseBrachyTherapies(List<ClinicalEvent> clinicalEvents, Node patientNode, String patientId,
        int cancerStudyId) {
    List<Node> treatmentNodes = patientNode.selectNodes("BrachyTherapies/BrachyTherapy");

    for (Node treatmentNode : treatmentNodes) {
        Patient patient = DaoPatient.getPatientByCancerStudyAndPatientId(cancerStudyId, patientId);
        ClinicalEvent clinicalEvent = new ClinicalEvent();
        clinicalEvent.setPatientId(patient.getInternalId());
        clinicalEvent.setEventType("TREATMENT");
        clinicalEvent.addEventDatum("TREATMENT_TYPE", "Brachytherapy");

        Node node = treatmentNode.selectSingleNode("BrachyDate");
        if (node == null) {
            System.err.println("no date");
            continue;
        }//from ww  w  . j a  va 2  s  .com
        clinicalEvent.setStartDate(Long.parseLong(node.getText()));

        node = treatmentNode.selectSingleNode("BrachyIsotope");
        if (node != null) {
            clinicalEvent.addEventDatum("BRACHY_ISOTOPE", node.getText());
        }

        node = treatmentNode.selectSingleNode("BrachyPrescribedDose");
        if (node != null) {
            clinicalEvent.addEventDatum("DOSE", node.getText());
        }

        node = treatmentNode.selectSingleNode("BrachyDoseNotes");
        if (node != null) {
            clinicalEvent.addEventDatum("DOSE_NOTES", node.getText());
        }

        clinicalEvents.add(clinicalEvent);
    }
}

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static void parseDiagnostics(List<ClinicalEvent> clinicalEvents, Node patientNode, String patientId,
        int cancerStudyId) {
    List<Node> diagnosticNodes = patientNode.selectNodes("Diagnostics/Diagnostic");
    for (Node diagnosticNode : diagnosticNodes) {
        Patient patient = DaoPatient.getPatientByCancerStudyAndPatientId(cancerStudyId, patientId);
        ClinicalEvent clinicalEvent = new ClinicalEvent();
        clinicalEvent.setPatientId(patient.getInternalId());
        clinicalEvent.setEventType("DIAGNOSTIC");

        Node node = diagnosticNode.selectSingleNode("DxDate");
        if (node == null) {
            System.err.println("no date");
            continue;
        }/*from   w  w w  . j  ava 2s . c  om*/
        clinicalEvent.setStartDate(Long.parseLong(node.getText()));

        node = diagnosticNode.selectSingleNode("DxType");
        if (node != null) {
            clinicalEvent.addEventDatum("DIAGNOSTIC_TYPE", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxTarget");
        if (node != null) {
            clinicalEvent.addEventDatum("TARGET", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxResult");
        if (node != null) {
            clinicalEvent.addEventDatum("RESULT", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxNotes");
        if (node != null) {
            clinicalEvent.addEventDatum("NOTES", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxSide");
        if (node != null) {
            clinicalEvent.addEventDatum("SIDE", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxStatus");
        if (node != null) {
            clinicalEvent.addEventDatum("STATUS", node.getText());
        }

        node = diagnosticNode.selectSingleNode("ImgBaseline");
        if (node != null) {
            clinicalEvent.addEventDatum("BASELINE", node.getText());
        }

        node = diagnosticNode.selectSingleNode("DxNumNewTumors");
        if (node != null) {
            clinicalEvent.addEventDatum("NUM_NEW_TUMORS", node.getText());
        }

        clinicalEvents.add(clinicalEvent);
    }
}

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static void parseLabTests(List<ClinicalEvent> clinicalEvents, Node patientNode, String patientId,
        int cancerStudyId) {
    List<Node> labTestNodes = patientNode.selectNodes("LabTests/LabTest");
    for (Node labTestNode : labTestNodes) {
        Patient patient = DaoPatient.getPatientByCancerStudyAndPatientId(cancerStudyId, patientId);
        ClinicalEvent clinicalEvent = new ClinicalEvent();
        clinicalEvent.setPatientId(patient.getInternalId());
        clinicalEvent.setEventType("LAB_TEST");

        Node node = labTestNode.selectSingleNode("LabDate");
        if (node == null) {
            System.err.println("no date");
            continue;
        }//w  w  w  . ja  v a 2 s .com
        clinicalEvent.setStartDate(Long.parseLong(node.getText()));

        node = labTestNode.selectSingleNode("LabTest");
        if (node == null) {
            System.err.println("no lab test name");
            continue;
        }
        clinicalEvent.addEventDatum("TEST", node.getText());

        node = labTestNode.selectSingleNode("LabResult");
        if (node == null) {
            System.err.println("no lab result");
            continue;
        }
        clinicalEvent.addEventDatum("RESULT", node.getText());

        node = labTestNode.selectSingleNode("LabUnits");
        if (node != null) {
            clinicalEvent.addEventDatum("UNIT", node.getText());
        }

        node = labTestNode.selectSingleNode("LabNormalRange");
        if (node != null) {
            clinicalEvent.addEventDatum("NORMAL_RANGE", node.getText());
        }

        node = labTestNode.selectSingleNode("LabNotes");
        if (node != null) {
            clinicalEvent.addEventDatum("NOTES", node.getText());
        }

        clinicalEvents.add(clinicalEvent);
    }
}

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static void parseClinicalEventsFromSpecimen(List<ClinicalEvent> clinicalEvents, Node patientNode,
        String patientId, int cancerStudyId) {
    List<Node> specimenAccessionNodes = patientNode.selectNodes("SpecimenAccessions/SpecimenAccession");
    for (Node specimenAccessionNode : specimenAccessionNodes) {
        Node node = specimenAccessionNode.selectSingleNode("AccessionDate");
        if (node == null) {
            System.err.println("no date");
            continue;
        }/* ww  w  .  ja  v a  2s.c  o m*/
        long date = Long.parseLong(node.getText());

        String site = null, type = null, instrument = null;
        node = specimenAccessionNode.selectSingleNode("AccessionAnatomicSite");
        if (node != null) {
            site = node.getText();
        }
        node = specimenAccessionNode.selectSingleNode("AccessionVisitType");
        if (node != null) {
            type = node.getText();
        }
        node = specimenAccessionNode.selectSingleNode("AccessionProcInstrument");
        if (node != null) {
            instrument = node.getText();
        }

        List<Node> specimenNodes = specimenAccessionNode.selectNodes("Specimens/Specimen");
        for (Node specimenNode : specimenNodes) {
            Patient patient = DaoPatient.getPatientByCancerStudyAndPatientId(cancerStudyId, patientId);

            ClinicalEvent clinicalEvent = new ClinicalEvent();
            clinicalEvent.setPatientId(patient.getInternalId());
            clinicalEvent.setEventType("SPECIMEN");
            clinicalEvent.setStartDate(date);
            if (site != null) {
                clinicalEvent.addEventDatum("SPECIMEN_SITE", site);
            }
            if (type != null) {
                clinicalEvent.addEventDatum("ANATOMIC_SITE", type);
            }
            if (instrument != null) {
                clinicalEvent.addEventDatum("PROC_INSTRUMENT", instrument);
            }

            addAllDataUnderNode(clinicalEvent, Element.class.cast(specimenNode));

            clinicalEvents.add(clinicalEvent);
        }
    }
}

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static List<ClinicalData> parseClinicalDataFromSpecimen(Node patientNode, int cancerStudyId,
        Map<String, String> mapSu2cSampleIdSampleId) {
    List<ClinicalData> clinicalData = new ArrayList<ClinicalData>();
    List<Node> specimenAccessionNodes = patientNode.selectNodes("SpecimenAccessions/SpecimenAccession");
    for (Node specimenAccessionNode : specimenAccessionNodes) {
        String site = null, instrument = null;
        Node node = specimenAccessionNode.selectSingleNode("AccessionAnatomicSite");
        if (node != null) {
            site = node.getText();/*from ww  w  . j a v a2  s . c o m*/
        }
        node = specimenAccessionNode.selectSingleNode("AccessionProcInstrument");
        if (node != null) {
            instrument = node.getText();
        }

        List<Node> specimenNodes = specimenAccessionNode.selectNodes("Specimens/Specimen");
        for (Node specimenNode : specimenNodes) {
            node = specimenNode.selectSingleNode("SpecimenReferenceNumber");
            if (node == null) {
                continue;
            }
            String su2cSampleId = node.getText();
            String sampleId = mapSu2cSampleIdSampleId.get(su2cSampleId);
            if (sampleId == null) {
                continue;
            }

            if (site != null) {
                ClinicalData clinicalDatum = new ClinicalData(cancerStudyId, sampleId, "TUMOR_SITE", site);
                clinicalData.add(clinicalDatum);
            }

            if (instrument != null) {
                ClinicalData clinicalDatum = new ClinicalData(cancerStudyId, sampleId, "PROC_INSTRUMENT",
                        instrument);
                clinicalData.add(clinicalDatum);
            }
        }
    }
    return clinicalData;
}

From source file:org.mskcc.cbio.portal.scripts.ImportCaisesClinicalXML.java

License:Open Source License

private static long parseStatusesAndReturnDiagnosisDate(List<ClinicalEvent> clinicalEvents, Node patientNode,
        String patientId, int cancerStudyId) {
    List<Node> statusNodes = patientNode.selectNodes("Statuses/Status");
    long diagnosisDate = 0;
    for (Node statusNode : statusNodes) {
        Patient patient = DaoPatient.getPatientByCancerStudyAndPatientId(cancerStudyId, patientId);
        ClinicalEvent clinicalEvent = new ClinicalEvent();
        clinicalEvent.setPatientId(patient.getInternalId());
        clinicalEvent.setEventType("STATUS");

        Node node = statusNode.selectSingleNode("StatusDate");
        if (node == null) {
            System.err.println("no date");
            continue;
        }/*w ww .ja  va  2  s  .c o  m*/
        long statusDate = Long.parseLong(node.getText());
        clinicalEvent.setStartDate(statusDate);

        node = statusNode.selectSingleNode("Status");
        if (node == null) {
            System.err.println("no status");
            continue;
        }
        clinicalEvent.addEventDatum("STATUS", node.getText());
        if (node.getText().equalsIgnoreCase("Diagnosis Date")) {
            diagnosisDate = statusDate;
        }

        clinicalEvents.add(clinicalEvent);
    }
    return diagnosisDate;
}

From source file:org.onecmdb.core.utils.transform.xml.XPathAttributeSelector.java

License:Open Source License

public IAttributeValue getAttribute(IInstance row) {
    if (row instanceof XMLRow) {
        Node node = ((XMLRow) row).getNode();
        Node selectedNode = null;
        try {//from  www.jav a2s  . co  m
            selectedNode = node.selectSingleNode(getXpath());
        } catch (InvalidXPathException e) {
            throw new IllegalArgumentException(
                    "Invalid XPath '" + getXpath() + "' in attribute '" + getName() + "'", e);
        }
        if (selectedNode == null) {
            return (new EmptyAttributeValue(this));
            //throw new IllegalArgumentException("Column '" + getXPath() + "' not found in row '" + node.getPath() +"'");
        }
        TextAttributeValue col = new TextAttributeValue(this, selectedNode.getText());
        return (col);
    }
    return (null);
}

From source file:org.opencms.setup.xml.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Sets the given value in all nodes identified by the given xpath of the given xml file.<p>
 * /* w w w  .ja v  a  2s .  c o  m*/
 * If value is <code>null</code>, all nodes identified by the given xpath will be deleted.<p>
 * 
 * If the node identified by the given xpath does not exists, the missing nodes will be created
 * (if <code>value</code> not <code>null</code>).<p>
 * 
 * @param document the xml document
 * @param xPath the xpath to set
 * @param value the value to set (can be <code>null</code> for deletion)
 * @param nodeToInsert optional, if given it will be inserted after xPath with the given value
 * 
 * @return the number of successful changed or deleted nodes
 */
public static int setValue(Document document, String xPath, String value, String nodeToInsert) {

    int changes = 0;
    // be naive and try to find the node
    Iterator<Node> itNodes = CmsCollectionsGenericWrapper.<Node>list(document.selectNodes(xPath)).iterator();

    // if not found
    if (!itNodes.hasNext()) {
        if (value == null) {
            // if no node found for deletion
            return 0;
        }
        // find the node creating missing nodes in the way
        Iterator<String> it = CmsStringUtil.splitAsList(xPath, "/", false).iterator();
        Node currentNode = document;
        while (it.hasNext()) {
            String nodeName = it.next();
            // if a string condition contains '/'
            while ((nodeName.indexOf("='") > 0) && (nodeName.indexOf("']") < 0)) {
                nodeName += "/" + it.next();
            }
            Node node = currentNode.selectSingleNode(nodeName);
            if (node != null) {
                // node found
                currentNode = node;
                if (!it.hasNext()) {
                    currentNode.setText(value);
                }
            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) currentNode;
                if (!nodeName.startsWith("@")) {
                    elem = handleNode(elem, nodeName);
                    if (!it.hasNext() && CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
                        elem.setText(value);
                    }
                } else {
                    // if node is attribute create it with given value
                    elem.addAttribute(nodeName.substring(1), value);
                }
                currentNode = elem;
            } else {
                // should never happen
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.ERR_XML_SET_VALUE_2, xPath, value));
                }
                break;
            }
        }
        if (nodeToInsert == null) {
            // if not inserting we are done
            return 1;
        }
        // if inserting, we just created the insertion point, so continue
        itNodes = CmsCollectionsGenericWrapper.<Node>list(document.selectNodes(xPath)).iterator();
    }

    // if found 
    while (itNodes.hasNext()) {
        Node node = itNodes.next();
        if (nodeToInsert == null) {
            // if not inserting
            if (value != null) {
                // if found, change the value
                node.setText(value);
            } else {
                // if node for deletion is found
                node.getParent().remove(node);
            }
        } else {
            // first create the node to insert
            Element parent = node.getParent();
            Element elem = handleNode(parent, nodeToInsert);
            if (value != null) {
                elem.setText(value);
            }
            // get the parent element list
            List<Node> list = CmsCollectionsGenericWrapper.<Node>list(parent.content());
            // remove the just created element
            list.remove(list.size() - 1);
            // insert it back to the right position
            int pos = list.indexOf(node);
            list.add(pos + 1, elem); // insert after
        }
        changes++;
    }
    return changes;
}

From source file:org.opencms.util.ant.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Sets the given value in all nodes identified by the given xpath of the given xml file.<p>
 * /*from  ww w . j ava  2s  . c om*/
 * If value is <code>null</code>, all nodes identified by the given xpath will be deleted.<p>
 * 
 * If the node identified by the given xpath does not exists, the missing nodes will be created
 * (if <code>value</code> not <code>null</code>).<p>
 * 
 * @param document the xml document
 * @param xPath the xpath to set
 * @param value the value to set (can be <code>null</code> for deletion)
 * @param nodeToInsert optional, if given it will be inserted after xPath with the given value
 * 
 * @return the number of successful changed or deleted nodes
 */
@SuppressWarnings("unchecked")
public static int setValue(Document document, String xPath, String value, String nodeToInsert) {

    int changes = 0;
    // be naive and try to find the node
    Iterator<Node> itNodes = document.selectNodes(xPath).iterator();

    // if not found
    if (!itNodes.hasNext()) {
        if (value == null) {
            // if no node found for deletion
            return 0;
        }
        // find the node creating missing nodes in the way
        Iterator<String> it = CmsStringUtil.splitAsList(xPath, "/", false).iterator();
        Node currentNode = document;
        while (it.hasNext()) {
            String nodeName = it.next();
            // if a string condition contains '/'
            while ((nodeName.indexOf("='") > 0) && (nodeName.indexOf("']") < 0)) {
                nodeName += "/" + it.next();
            }
            Node node = currentNode.selectSingleNode(nodeName);
            if (node != null) {
                // node found
                currentNode = node;
                if (!it.hasNext()) {
                    currentNode.setText(value);
                }
            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) currentNode;
                if (!nodeName.startsWith("@")) {
                    elem = handleNode(elem, nodeName);
                    if (!it.hasNext() && !CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
                        elem.setText(value);
                    }
                } else {
                    // if node is attribute create it with given value
                    elem.addAttribute(nodeName.substring(1), value);
                }
                currentNode = elem;
            } else {
                // should never happen
                break;
            }
        }
        if (nodeToInsert == null) {
            // if not inserting we are done
            return 1;
        }
        // if inserting, we just created the insertion point, so continue
        itNodes = document.selectNodes(xPath).iterator();
    }

    // if found 
    while (itNodes.hasNext()) {
        Node node = itNodes.next();
        if (nodeToInsert == null) {
            // if not inserting
            if (value != null) {
                // if found, change the value
                node.setText(value);
            } else {
                // if node for deletion is found
                node.getParent().remove(node);
            }
        } else {
            // first create the node to insert
            Element parent = node.getParent();
            Element elem = handleNode(parent, nodeToInsert);
            if (value != null) {
                elem.setText(value);
            }
            // get the parent element list
            List<Node> list = parent.content();
            // remove the just created element
            list.remove(list.size() - 1);
            // insert it back to the right position
            int pos = list.indexOf(node);
            list.add(pos + 1, elem); // insert after
        }
        changes++;
    }
    return changes;
}

From source file:org.pdfsam.console.business.pdf.handlers.ConcatCmdExecutor.java

License:Open Source License

/**
 * given a fileset node returns the PdfFile objects
 * //www .  ja v  a  2s.  c  o  m
 * @param fileSetNode
 * @param parentDir
 * @return a list of PdfFile objects
 * @throws Exception
 */
private List getFileNodesFromFileset(Node fileSetNode, String parentDir) throws Exception {
    String parentPath = null;
    Node useCurrentDir = fileSetNode.selectSingleNode("@usecurrentdir");
    Node dir = fileSetNode.selectSingleNode("@dir");
    if (dir != null && dir.getText().trim().length() > 0) {
        parentPath = dir.getText();
    } else {
        if (useCurrentDir != null && Boolean.valueOf(useCurrentDir.getText()).booleanValue()) {
            parentPath = parentDir;
        }
    }
    return getPdfFileListFromNode(fileSetNode.selectNodes("file"), parentPath);
}