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:architecture.common.xml.XmlWriter.java

License:Apache License

/**
 * This method is used to write out Nodes that contain text and still allow
 * for xml:space to be handled properly.
 *
 *//*from  ww  w .j a  v a2 s  . c  om*/
protected void writeNodeText(Node node) throws IOException {
    String text = node.getText();
    if (text != null && text.length() > 0) {
        if (escapeText) {
            text = escapeElementEntities(text);
        }

        lastOutputNodeType = Node.TEXT_NODE;
        writer.write(text);
    }
}

From source file:architecture.common.xml.XmlWriter.java

License:Apache License

protected void writeNode(Node node) throws IOException {
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeElement((Element) node);
        break;/*w w  w  .ja v  a 2  s  .c  o m*/
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeNodeText(node);
        // write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        // write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:architecture.ee.plugin.impl.PluginManagerImpl.java

public List loadPlugin(File pluginDir, PluginEntityObject pluginDbBean) throws PluginException {

    if (!ApplicationHelper.isSetupComplete()) {
        return Collections.emptyList();
    }// w ww . ja v a2  s .  c om

    log.debug((new StringBuilder()).append("Loading action from: ").append(pluginDir.getName()).toString());
    Document pluginXML;
    try {
        pluginXML = PluginUtils.getPluginConfiguration(pluginDir);
    } catch (DocumentException e) {
        pluginXML = null;
    }

    if (pluginXML == null) {
        String msg = (new StringBuilder()).append("Plugin ").append(pluginDir.getName())
                .append(" could not be loaded: no plugin.xml file found").toString();
        log.error(msg);
        brokenPlugins.put(pluginDir.getName(), "No plugin.xml found.");
        throw new PluginException(msg);
    }

    ArrayList results = Lists.newArrayList();

    String pluginName;
    PluginClassLoader pluginLoader;

    Node pluginNameNode = pluginXML.selectSingleNode("/plugin/name");

    pluginName = pluginNameNode.getText();

    isValidVersion(pluginName, pluginXML, pluginDir);

    pluginLoader = getPluginClassloader(pluginName, pluginDir);
    if (pluginLoader == null) {
        return Collections.emptyList();
    }
    pluginLoader.initialize();
    log.debug("Plugin classloader urls:" + pluginLoader.getURLS());

    Plugin plugin;
    PluginMetaDataImpl metaData;
    ConfigurationContext context;
    Thread currentThread;
    ClassLoader oldLoader;

    Node classNode = pluginXML.selectSingleNode("/plugin/class");
    if (classNode != null) {
        String className = classNode.getText();
        try {
            log.debug("Plugin class:" + className);
            plugin = (Plugin) pluginLoader.loadClass(className).newInstance();
            log.debug("Plugin object:" + plugin);
            log.debug("******************************** ");
        } catch (Throwable e) {
            brokenPlugins.put(pluginDir.getName(), "Failed to configure class loader.");
            log.debug(e);
            throw new PluginException(e);
        }
    } else {
        plugin = new DummyPlugin(pluginName);
    }
    log.debug("===============================1============");
    metaData = new PluginMetaDataImpl(plugin, pluginLoader, this, pluginXML, pluginDir);
    log.debug("=========================2==================");
    metaData.setPluginDbBean(pluginDbBean);
    log.debug("=======================3====================");
    registerPlugin(plugin, pluginDir);
    log.debug("=======================4====================");
    pluginMeta.put(pluginName, metaData);
    log.debug("======================5=====================");
    pluginMeta.put(plugin, metaData);
    log.debug("====================6=======================");
    context = new ConfigurationContext(metaData);
    log.debug("=======================7====================");
    currentThread = Thread.currentThread();
    oldLoader = currentThread.getContextClassLoader();

    log.debug("===========================================");
    try {
        currentThread.setContextClassLoader(pluginLoader.getClassLoader());

        log.debug("Plugin configures:" + configurators.size());

        for (PluginConfigurator configurator : configurators) {
            log.debug("Plugin configure:" + configurator.getClass().getName());
            configurator.configure(context);
        }
    } catch (Exception e) {
        brokenPlugins.put(pluginDir.getName(), "Failed to configure class loader.");
        throw new PluginException(e);
    } finally {
        if (oldLoader != null)
            currentThread.setContextClassLoader(oldLoader);
    }

    log.debug("===========================================");

    int pluginDbVersion = getPluginDatabaseVersion(metaData);

    boolean init = true;
    if (pluginDbVersion > 0 && metaData.getDatabaseVersion() != pluginDbVersion) {
        brokenPlugins.put(pluginDir.getName(),
                (new StringBuilder()).append("Database version mismatches plugin version. Current: ")
                        .append(pluginDbVersion).append(", Required: ").append(metaData.getDatabaseVersion())
                        .toString());
        init = false;
    }
    if (init) {
        try {
            plugin.init();
            firePluginCreatedEvent(pluginDir.getName(), plugin);
        } catch (IncompatibleClassChangeError e) {
            log.error((new StringBuilder()).append("Unable to initialize plugin, plugin ").append(pluginName)
                    .append(" binds to an old class version needs to be required.").toString());
            results.add(PluginRequiresRebuildResult.getPluginRequiresRebuildResult());
            brokenPlugins.put(pluginDir.getName(), "Failed to initialize.");
        }
        results.addAll(context.getResults());
        ChainingClassLoader.clearCache();
    }
    return results;
}

From source file:architecture.ee.util.xml.XmlWriter.java

License:Apache License

/** Outputs the content of the given element. If whitespace trimming is
 * enabled then all adjacent text nodes are appended together before
 * the whitespace trimming occurs to avoid problems with multiple
 * text nodes being created due to text content that spans parser buffers
 * in a SAX parser.// w  ww  . j  av  a2  s.c o m
 */
protected void writeElementContent(Element element) throws IOException {
    boolean trim = format.isTrimText();
    boolean oldPreserve = preserve;
    if (trim) { //verify we have to before more expensive test
        preserve = isElementSpacePreserved(element);
        trim = !preserve;
    }
    if (trim) {
        // concatenate adjacent text nodes together
        // so that whitespace trimming works properly
        Text lastTextNode = null;
        StringBuilder buffer = null;
        boolean textOnly = true;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                if (lastTextNode == null) {
                    lastTextNode = (Text) node;
                } else {
                    if (buffer == null) {
                        buffer = new StringBuilder(lastTextNode.getText());
                    }
                    buffer.append(node.getText());
                }
            } else {
                if (!textOnly && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }

                textOnly = false;

                if (lastTextNode != null) {
                    if (buffer != null) {
                        writeString(buffer.toString());
                        buffer = null;
                    } else {
                        writeString(lastTextNode.getText());
                    }
                    lastTextNode = null;

                    if (format.isPadText()) {
                        writer.write(PAD_TEXT);
                    }
                }
                writeNode(node);
            }
        }
        if (lastTextNode != null) {
            if (!textOnly && format.isPadText()) {
                writer.write(PAD_TEXT);
            }
            if (buffer != null) {
                writeString(buffer.toString());
                buffer = null;
            } else {
                writeString(lastTextNode.getText());
            }
            lastTextNode = null;
        }
    } else {
        Node lastTextNode = null;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                writeNode(node);
                lastTextNode = node;
            } else {
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                writeNode(node);
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                lastTextNode = null;
            }
        }
    }
    preserve = oldPreserve;
}

From source file:architecture.ee.util.xml.XmlWriter.java

License:Apache License

protected void writeNode(Node node) throws IOException {
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeElement((Element) node);
        break;/*from  w  w  w.  j  av a 2 s. co  m*/
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeNodeText(node);
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected PCAssay populateAssayFromXMLNode(Node topNode) throws PubChemException {
    // String assayDescPath =
    // "PC-AssaySubmit_assay/PC-AssaySubmit_assay_descr/PC-AssayDescription";
    Node assayDescNode = null;/* w ww  . jav  a 2 s. c o  m*/
    if (topNode.getName().equals("PC-AssayDescription"))
        assayDescNode = topNode;
    else {
        assayDescNode = topNode.selectSingleNode(".//PC-AssayDescription");
    }
    if (assayDescNode == null)
        throw new PubChemException(
                String.format("Cannot find PC-AssayDescription node in provided node %s", topNode.getPath()));

    Node node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_id");
    Integer aid = new Integer(node.getText());

    try {
        PCAssay assay = new PCAssay();
        if (aid > 0)
            assay.setAID(aid);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_version");
        Integer version = new Integer(node.getText());
        assay.setVersion(version);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_revision");
        Integer revision = new Integer(node.getText());
        assay.setRevision(revision);

        Node trackingNode = assayDescNode
                .selectSingleNode("PC-AssayDescription_aid-source/PC-Source/PC-Source_db/PC-DBTracking");

        node = trackingNode.selectSingleNode("PC-DBTracking_name");
        assay.setSourceName(node.getText());

        node = trackingNode.selectSingleNode("PC-DBTracking_source-id/Object-id/Object-id_str");
        assay.setExtRegId(node.getText());

        // hold until date
        node = trackingNode.selectSingleNode("PC-DBTracking_date");
        if (node != null) {
            String year = node.selectSingleNode("Date/Date_std/Date-std/Date-std_year").getText();
            String month = node.selectSingleNode("Date/Date_std/Date-std/Date-std_month").getText();
            String day = node.selectSingleNode("Date/Date_std/Date-std/Date-std_day").getText();
            if (DEBUGGING)
                log.info("year: " + year + " month: " + month + " day: " + day);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
            assay.setHoldUntilDate(calendar.getTime());
            if (DEBUGGING)
                log.info(calendar.getTime().toString());
        }

        node = assayDescNode.selectSingleNode("PC-AssayDescription_name");
        assay.setName(node.getText());

        List<Node> nodes = assayDescNode
                .selectNodes("PC-AssayDescription_description/PC-AssayDescription_description_E");
        assay.setDescription(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_protocol/PC-AssayDescription_protocol_E");
        assay.setProtocol(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_comment/PC-AssayDescription_comment_E");
        assay.setComment(join(nodes, separator));

        node = assayDescNode.selectSingleNode("PC-AssayDescription_activity-outcome-method");
        if (node != null)
            assay.setActivityOutcomeMethod(node.valueOf("@value"));

        handlePlots(assay, assayDescNode);

        node = assayDescNode
                .selectSingleNode("PC-AssayDescription_grant-number/PC-AssayDescription_grant-number_E");
        if (node != null)
            assay.setGrantNumber(node.getText());

        node = assayDescNode.selectSingleNode("PC-AssayDescription_project-category");
        if (node != null)
            assay.setProjectCategory(node.valueOf("@value"));

        assay.getAssayXRefs().removeAll(assay.getAssayXRefs());

        nodes = assayDescNode.selectNodes("PC-AssayDescription_xref/PC-AnnotatedXRef");
        handleXRefs(assay, null, nodes);

        nodes = assayDescNode.selectNodes("PC-AssayDescription_target/PC-AssayTargetInfo");
        handleTargetXRefs(assay, null, nodes);

        handlePanels(assay, assayDescNode);

        handleColumns(assay, assayDescNode);

        handleComments(assay, assayDescNode);

        return assay;
    } catch (Exception ex) {
        throw new RuntimeException("Problem with AID " + aid, ex);
    }
}

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected void handlePanels(PCAssay assay, Node assayDescNode) {
    Node node = assayDescNode.selectSingleNode("PC-AssayDescription_is-panel");
    if (node == null)
        assay.setPanel(false);/*from  ww w.ja  v  a2  s. c o  m*/
    else
        assay.setPanel("true".equals(node.valueOf("@value")));

    if (assay.isPanel()) {
        node = assayDescNode
                .selectSingleNode("PC-AssayDescription_panel-info/PC-AssayPanel/PC-AssayPanel_name");
        assay.setPanelName(node.valueOf("text()"));
        node = assayDescNode
                .selectSingleNode("PC-AssayDescription_panel-info/PC-AssayPanel/PC-AssayPanel_descr");
        assay.setPanelDescription(node == null ? "" : node.valueOf("text()"));
    }

    List<Node> nodes = assayDescNode.selectNodes(
            "PC-AssayDescription_panel-info/PC-AssayPanel/PC-AssayPanel_member/PC-AssayPanelMember");
    for (Node n : nodes) {
        String mid = n.selectSingleNode("PC-AssayPanelMember_mid").getText();
        Node node2 = n.selectSingleNode("PC-AssayPanelMember_name");
        String name = node2 == null ? "" : node2.getText();

        PCAssayPanel panel = new PCAssayPanel();
        panel.setAssay(assay);
        assay.getPanels().add(panel);
        panel.setPanelNumber(Integer.parseInt(mid));
        panel.setName(name);

        node2 = n.selectSingleNode("PC-AssayPanelMember_description");
        panel.setDescription(node2 == null ? "" : node2.getText());

        List<Node> nodes2 = n.selectNodes("PC-AssayPanelMember_protocol/PC-AssayPanelMember_protocol_E");
        panel.setProtocol(join(nodes2, separator));

        nodes2 = n.selectNodes("PC-AssayPanelMember_comment/PC-AssayPanelMember_comment_E");
        panel.setComment(join(nodes2, separator));

        List<Node> xrefNodes = n.selectNodes("PC-AssayPanelMember_xref/PC-AnnotatedXRef");
        handleXRefs(assay, panel, xrefNodes);

        List<Node> targetNodes = n.selectNodes("PC-AssayPanelMember_target/PC-AssayTargetInfo");
        handleTargetXRefs(assay, panel, targetNodes);
    }
}

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected void handleColumns(PCAssay assay, Node assayDescNode) {
    Map<Integer, PCAssayColumn> map = new HashMap<Integer, PCAssayColumn>();
    for (PCAssayColumn col : assay.getColumns())
        map.put(col.getTID(), col);/*from  ww w .ja  v  a  2 s  .  co  m*/

    ensureColumn(assay, -1, "Outcome", "string");
    ensureColumn(assay, 0, "Score", "float");

    Map<Integer, PCAssayPanel> mapPanels = new HashMap<Integer, PCAssayPanel>();
    for (PCAssayPanel panel : assay.getPanels())
        mapPanels.put(panel.getPanelNumber(), panel);

    List<Node> nodes = assayDescNode.selectNodes("PC-AssayDescription_results/PC-ResultType");
    for (Node n : nodes) {
        String tid = n.selectSingleNode("PC-ResultType_tid").getText();
        String name = n.selectSingleNode("PC-ResultType_name").getText();
        String type = n.selectSingleNode("PC-ResultType_type").valueOf("@value");
        PCAssayColumn column = ensureColumn(assay, Integer.parseInt(tid), name, type);

        Node node = n.selectSingleNode("PC-ResultType_unit");
        if (node != null)
            column.setUnit(node.valueOf("@value"));

        List<Node> descNodes = n.selectNodes("PC-ResultType_description/PC-ResultType_description_E");
        column.setDescription(join(descNodes, separator));
        if (DEBUGGING)
            log.info("Column description: " + join(descNodes, separator));

        node = n.selectSingleNode("PC-ResultType_ac");
        if (node != null)
            column.setActiveConcentration("true".equals(node.valueOf("@value")));

        node = n.selectSingleNode("PC-ResultType_tc");
        if (node != null) {
            Node node2 = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_dr-id");
            if (node2 != null) {
                column.setCurvePlotLabel(Integer.parseInt(node2.getText()));
            }
            String testedConc = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_concentration")
                    .getText();
            column.setTestedConcentration(Double.parseDouble(testedConc));
            String testedUnit = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_unit")
                    .valueOf("@value");
            column.setTestedConcentrationUnit(testedUnit);
        }

        node = n.selectSingleNode("PC-ResultType_panel-info/PC-AssayPanelTestResult");
        if (node != null) {
            String panelId = node.selectSingleNode("PC-AssayPanelTestResult_mid").getText();
            PCAssayPanel panel = mapPanels.get(Integer.parseInt(panelId));
            column.setPanel(panel);
            String panelColumnType = node.selectSingleNode("PC-AssayPanelTestResult_readout-annot")
                    .valueOf("@value");
            column.setPanelReadoutType(panelColumnType);
        }
    }
}

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected void handleXRefs(PCAssay assay, PCAssayPanel panel, List<Node> nodes) {
    for (Node n : nodes) {
        Node node = n.selectSingleNode("PC-AnnotatedXRef_comment");
        String comment = node == null ? "" : node.getText();

        node = n.selectSingleNode("PC-AnnotatedXRef_xref/PC-XRefData/*");

        String type = node.getName();
        type = type.substring(type.lastIndexOf("_") + 1, type.length());
        String database = targetType.containsKey(type) ? targetType.get(type) : type;
        String id = node.getText();

        XRef xref = new XRef();
        xref.setXRefId(id);// w ww.  ja  v  a  2  s.  com
        xref.setDatabase(database);
        xref.setType(type);

        PCAssayXRef aXref = new PCAssayXRef();
        aXref.setPanel(panel);
        aXref.setAssay(assay);
        aXref.setComment(comment);
        aXref.setXRef(xref);
        aXref.setTarget(false);
        assay.getAssayXRefs().add(aXref);
    }
}

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected void handleTargetXRefs(PCAssay assay, PCAssayPanel panel, List<Node> nodes) {
    for (Node n : nodes) {
        String name = n.selectSingleNode("PC-AssayTargetInfo_name").getText();
        String id = n.selectSingleNode("PC-AssayTargetInfo_mol-id").getText();
        String type = n.selectSingleNode("PC-AssayTargetInfo_molecule-type").valueOf("@value");
        String database = targetType.containsKey(type) ? targetType.get(type) : type;
        Node taxonNode = n.selectSingleNode("PC-AssayTargetInfo_organism/BioSource/BioSource_org/Org-ref");
        String taxonName = "", taxonCommon = "", taxon = "";
        if (taxonNode != null) {
            taxonName = nullSafeGet(taxonNode, "Org-ref_taxname", "text()");
            taxonCommon = nullSafeGet(taxonNode, "Org-ref_common", "text()");
            String db = nullSafeGet(taxonNode, "Org-ref_db/Dbtag/Dbtag_db", "text()");
            if (!"taxon".equals(db))
                throw new RuntimeException("Non taxon BioSource Org-ref_db (was " + db + ")");
            taxonNode = taxonNode.selectSingleNode("Org-ref_db/Dbtag/Dbtag_tag/Object-id/Object-id_id");
            taxon = taxonNode.getText();
        }//from   ww w.j av  a 2  s .  c  om

        XRef xref = new XRef();
        xref.setXRefId(id);
        xref.setDatabase(database);
        xref.setType(type);
        xref.setName(name);

        PCAssayXRef aXref = new PCAssayXRef();
        aXref.setTarget(true);
        aXref.setPanel(panel);
        aXref.setAssay(assay);
        aXref.setXRef(xref);
        if (!taxon.equals("")) {
            aXref.setTaxon(Long.parseLong(taxon));
            aXref.setTaxonName(taxonName);
            aXref.setTaxonCommon(taxonCommon);
        }
        assay.getAssayXRefs().add(aXref);
    }
}