Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

In this page you can find the example usage for org.jdom2 Element setText.

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

From source file:domainapp.app.services.export.ExportToWordMenu.java

License:Apache License

private static void addTableRow(final Element table, final String[] cells) {
    final Element tr = new Element("tr");
    table.addContent(tr);//w w w .jav a2 s  .c  o  m
    for (final String columnName : cells) {
        final Element td = new Element("td");
        tr.addContent(td);
        td.setText(columnName);
    }
}

From source file:edu.pitt.apollo.runmanagerservice.methods.stage.StageExperimentMethod.java

License:Apache License

@Override
public void runApolloService() {

    XMLSerializer serializer = new XMLSerializer();
    XMLDeserializer deserializer = new XMLDeserializer();

    InfectiousDiseaseScenario baseScenario = idtes.getInfectiousDiseaseScenarioWithoutIntervention();
    // clear all set control strategies in base
    baseScenario.getInfectiousDiseaseControlStrategies().clear();

    List<SoftwareIdentification> modelIds = idtes.getInfectiousDiseaseTransmissionModelIds();
    try {/*  w w w.j  a  v a 2 s.c o  m*/
        DataServiceAccessor dataServiceAccessor = new DataServiceAccessor();

        for (SoftwareIdentification modelId : modelIds) {

            // create a base scenario copy
            String baseXml = serializer.serializeObject(baseScenario);
            InfectiousDiseaseScenario baseScenarioCopy = deserializer.getObjectFromMessage(baseXml,
                    InfectiousDiseaseScenario.class);
            for (InfectiousDiseaseControlStrategy strategy : idtes.getInfectiousDiseaseControlStrategies()) {

                for (InfectiousDiseaseControlMeasure controlMeasure : strategy.getControlMeasures()) {
                    baseScenarioCopy.getInfectiousDiseaseControlStrategies().add(controlMeasure);
                }
            }

            List<SensitivityAnalysisSpecification> sensitivityAnalyses = idtes.getSensitivityAnalyses();
            for (SensitivityAnalysisSpecification sensitivityAnalysis : sensitivityAnalyses) {
                if (sensitivityAnalysis instanceof OneWaySensitivityAnalysisOfContinousVariableSpecification) {
                    OneWaySensitivityAnalysisOfContinousVariableSpecification owsaocv = (OneWaySensitivityAnalysisOfContinousVariableSpecification) sensitivityAnalysis;
                    double min = owsaocv.getMinimumValue();
                    double max = owsaocv.getMaximumValue();
                    double increment = (max - min) / owsaocv.getNumberOfDiscretizations().intValueExact();

                    String scenarioXML = serializer.serializeObject(baseScenarioCopy);

                    double val = min;
                    while (val <= max) {

                        String param = owsaocv.getUniqueApolloLabelOfParameter();

                        Document jdomDocument;
                        SAXBuilder jdomBuilder = new SAXBuilder();
                        try {
                            jdomDocument = jdomBuilder.build(
                                    new ByteArrayInputStream(scenarioXML.getBytes(StandardCharsets.UTF_8)));
                        } catch (JDOMException | IOException ex) {
                            ErrorUtils.reportError(runId,
                                    "Error inserting experiment run. Error was " + ex.getMessage(),
                                    authentication);
                            return;
                        }

                        Element e = jdomDocument.getRootElement();
                        List<Namespace> namespaces = e.getNamespacesInScope();

                        for (Namespace namespace : namespaces) {
                            if (namespace.getURI().contains("http://types.apollo.pitt.edu")) {
                                param = param.replaceAll("/", "/" + namespace.getPrefix() + ":");
                                param = param.replaceAll("\\[", "\\[" + namespace.getPrefix() + ":");
                                break;
                            }
                        }

                        XPathFactory xpf = XPathFactory.instance();
                        XPathExpression<Element> expr;
                        expr = xpf.compile(param, Filters.element(), null, namespaces);
                        List<Element> elements = expr.evaluate(jdomDocument);

                        for (Element element : elements) {
                            element.setText(Double.toString(val));
                        }

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        XMLOutputter xmlOutputter = new XMLOutputter();
                        xmlOutputter.output(jdomDocument, baos);

                        InfectiousDiseaseScenario newScenario = deserializer.getObjectFromMessage(
                                new String(baos.toByteArray()), InfectiousDiseaseScenario.class);

                        // new scenario is ready to be staged
                        RunSimulationMessage runSimulationMessage = new RunSimulationMessage();
                        runSimulationMessage.setAuthentication(authentication);
                        runSimulationMessage
                                .setSimulatorTimeSpecification(message.getSimulatorTimeSpecification());
                        runSimulationMessage.setSoftwareIdentification(modelId);
                        runSimulationMessage.setInfectiousDiseaseScenario(newScenario);

                        StageMethod stageMethod = new StageMethod(runSimulationMessage, runId);
                        InsertRunResult result = stageMethod.stage();
                        BigInteger newRunId = result.getRunId();

                        MethodCallStatus status = dataServiceAccessor.getRunStatus(newRunId, authentication);
                        if (status.getStatus().equals(MethodCallStatusEnum.FAILED)) {
                            ErrorUtils.reportError(runId,
                                    "Error inserting run in experiment with run ID " + runId + ""
                                            + ". Error was for inserting ID " + newRunId + " with message "
                                            + status.getMessage(),
                                    authentication);
                            return;
                        }

                        val += increment;
                    }
                }
            }
        }

        dataServiceAccessor.updateStatusOfRun(runId, MethodCallStatusEnum.TRANSLATION_COMPLETED,
                "All runs for this experiment have been translated", authentication);
    } catch (DeserializationException | IOException | SerializationException | RunManagementException ex) {
        ErrorUtils.reportError(runId, "Error inserting experiment run. Error was " + ex.getMessage(),
                authentication);
        return;
    }
}

From source file:edu.ucla.loni.server.ServerUtils.java

License:Open Source License

/**
 * Updates a Document (XML file) with all the attributes from a Pipefile
 * @throws Exception // www  .  j  a v a 2  s. c  o  m
 */
public static Document updateXML(Document doc, Pipefile pipe) throws Exception {
    Element main = getMainElement(doc);

    // Update name (attribute)
    main.setAttribute("name", pipe.name);
    // Update package (attribute)
    main.setAttribute("package", pipe.packageName);
    // Update description (attribute)
    main.setAttribute("description", pipe.description);

    // Update the tags (children)
    main.removeChildren("tag"); // Remove all old tags
    String tags = pipe.tags;
    if (tags != null && tags.length() > 0) {
        String[] tagArray = tags.split(",");
        for (String tag : tagArray) {
            Element child = new Element("tag");
            child.setText(tag);
            main.addContent(child);
        }
    }

    if (pipe.type.equals("Data")) {
        // Update values (values child => children)
        Element valuesElement = main.getChild("values");
        if (valuesElement == null) {
            valuesElement = new Element("values");
            main.addContent(valuesElement);
        }

        valuesElement.removeChildren("value"); // Remove all old values

        String values = pipe.values;
        if (values != null && values.length() > 0) {
            String[] valueArray = values.split("\n");
            for (String value : valueArray) {
                Element valueElement = new Element("value");
                valueElement.setText(value);
                valuesElement.addContent(valueElement);
            }
        }

        // Update formatType (output child => format child => attribute)
        Element output = main.getChild("output");
        if (output == null) {
            output = new Element("output");
            main.addContent(output);
        }

        Element format = output.getChild("format");
        if (format == null) {
            format = new Element("format");
            main.addContent(format);
        }

        format.setAttribute("type", pipe.formatType);
    }

    if (pipe.type.equals("Modules")) {
        // Update location (attribute)
        main.setAttribute("location", pipe.location);
    }

    if (pipe.type.equals("Modules") || pipe.type.equals("Groups")) {
        // Update uri (child)
        Element uri = main.getChild("uri");

        // If child not present, create
        if (uri == null) {
            uri = new Element("uri");
            main.addContent(uri);
        }

        uri.setText(pipe.uri);
    }

    return doc;
}

From source file:edu.unc.lib.dl.xml.DepartmentOntologyUtil.java

License:Apache License

/**
 * Add the given department to the parent element as an affiliation if it is not already present
 *
 * @param dept//from  ww w .  j a  v  a 2s  .c o m
 * @param parentEl
 * @param affilIndex
 * @param affiliationSet
 * @return True if an affiliation was added
 */
private boolean addAffiliation(String dept, Element parentEl, int affilIndex, Set<String> affiliationSet) {

    // Prevent duplicate departments from being added
    if (dept != null && !affiliationSet.contains(dept)) {
        Element newAffilEl = new Element("affiliation", parentEl.getNamespace());
        newAffilEl.setText(dept);
        // Insert the new element near where the original was
        try {
            parentEl.addContent(affilIndex, newAffilEl);
        } catch (IndexOutOfBoundsException e) {
            parentEl.addContent(newAffilEl);
        }
        affiliationSet.add(dept);

        return true;
    }

    return false;
}

From source file:egovframework.rte.fdl.xml.AbstractXMLUtility.java

License:Apache License

/**
 * TextNode Update//from w ww  .  j a v  a 2s .  c o  m
 * 
 * @param element - ? Element
 * @param list - update TextNode 
 */
public void updTextNode(Element element, List<?> list) {
    List<?> childList = element.getChildren();

    if (childList.size() != 0) {
        for (int yy = 0; yy < childList.size(); yy++) {
            Element tmp = (Element) childList.get(yy);
            for (int tt = 0; tt < list.size(); tt++) {
                SharedObject sobj = (SharedObject) list.get(tt);
                if (tmp.getValue().equals((String) sobj.getKey())) {
                    tmp.setText((String) sobj.getValue());
                    break;
                }
            }
            updTextNode(tmp, list);
        }
    } else {
        for (int tt = 0; tt < list.size(); tt++) {
            SharedObject sobj = (SharedObject) list.get(tt);
            if (element.getValue().equals((String) sobj.getKey())) {
                element.setText((String) sobj.getValue());
                break;
            }
        }
    }
}

From source file:egovframework.rte.fdl.xml.AbstractXMLUtility.java

License:Apache License

/**
 *  Node //from   w  w  w  .j a va 2 s. com
 * 
 * @param element - ? Element
 * @param addNDName - ?
 * @param list - add TextNode 
 */
public void addNode(Element element, String addNDName, List<?> list) {
    List<?> childList = element.getChildren();

    if (childList.size() != 0) {
        for (int yy = 0; yy < childList.size(); yy++) {
            Element tmp = (Element) childList.get(yy);
            if (tmp.getName().equals(addNDName)) {
                for (int tt = 0; tt < list.size(); tt++) {
                    SharedObject sobj = (SharedObject) list.get(tt);
                    Element enew = new Element((String) sobj.getKey());
                    enew.setText((String) sobj.getValue());
                    tmp.addContent(enew);
                }
            }
            addNode(tmp, addNDName, list);
        }
    } else {
        if (element.getName().equals(addNDName)) {
            for (int tt = 0; tt < list.size(); tt++) {
                SharedObject sobj = (SharedObject) list.get(tt);
                Element enew = new Element((String) sobj.getKey());
                enew.setText((String) sobj.getValue());
                element.addContent(enew);
            }
        }
    }
}

From source file:egovframework.rte.fdl.xml.AbstractXMLUtility.java

License:Apache License

/**
 * XML ?//from w w  w.  j  a  v a2s .c o  m
 * 
 * @param root - Root Element
 * @param list - Element 
 */
public void createXML(Element root, List<?> list) {
    for (int i = 0; i < list.size(); i++) {
        SharedObject sobj = (SharedObject) list.get(i);
        Element elm = new Element(sobj.getKey());
        elm.setText((String) sobj.getValue());
        root.addContent(elm);
    }
}

From source file:es.ucm.fdi.ac.Annotation.java

License:Open Source License

public Element saveToXML() throws IOException {
    Element element = new Element("annotation");

    // Add labels attribute
    StringBuilder labelsString = new StringBuilder();
    for (Label l : labels) {
        labelsString.append(l.toString());
        labelsString.append(" ");
    }//from   w ww  .  ja  va2s.co m
    element.setAttribute("labels", labelsString.toString().trim());

    // Add comment
    if (commentary != null) {
        element.setText(commentary.trim());
    }

    // Add optional attributes
    if (author != null) {
        element.setAttribute("author", author.trim());
    }
    if (date != null) {
        element.setAttribute("date", dateFormat.format(date));
    }
    if (target != null) {
        element.setAttribute("target", target);
    }
    if (localFile != null) {
        element.setAttribute("localFile", localFile);
    }
    if (targetFile != null) {
        element.setAttribute("targetFile", targetFile);
    }

    return element;
}

From source file:es.ucm.fdi.ac.extract.PatternFilter.java

License:Open Source License

@Override
public void saveInner(Element e) throws IOException {
    e.setAttribute("class", this.getClass().getName());
    e.setText(getPattern().trim());
}

From source file:es.upm.dit.xsdinferencer.generation.generatorimpl.statisticsgeneration.StatisticResultsDocGeneratorImpl.java

License:Apache License

/**
 * Method that generates the general statistics of width and depth
 * @param statistics statistics Statistics object
 * @return the generalStatistics element of the statistics document, as a JDOM2 {@link Element} object.
 *//*from  w ww  . j a va2 s.c  o  m*/
protected Element generateGeneralStatistics(Statistics statistics) {
    Element generalStatisticsElement = new Element("generalStatistics", STATISTICS_NAMESPACE);

    Element depthElement = new Element("depth", STATISTICS_NAMESPACE);
    Element depthMaxElement = new Element("max", STATISTICS_NAMESPACE);
    depthMaxElement.setText(Long.toString(statistics.getMaxDepth()));
    depthElement.addContent(depthMaxElement);
    Element depthAvgElement = new Element("avg", STATISTICS_NAMESPACE);
    depthAvgElement.setText(roundingFormat.format(statistics.getAvgDepth()));
    depthElement.addContent(depthAvgElement);
    generalStatisticsElement.addContent(depthElement);

    Element widthElement = new Element("width", STATISTICS_NAMESPACE);
    Element widthMaxElement = new Element("max", STATISTICS_NAMESPACE);
    widthMaxElement.setText(Long.toString(statistics.getMaxWidth()));
    widthElement.addContent(widthMaxElement);
    Element widthAvgElement = new Element("avg", STATISTICS_NAMESPACE);
    widthAvgElement.setText(roundingFormat.format(statistics.getAvgWidth()));
    widthElement.addContent(widthAvgElement);

    generalStatisticsElement.addContent(widthElement);
    return generalStatisticsElement;
}