Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

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

Prototype

public Document(List<? extends Content> content) 

Source Link

Document

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Usage

From source file:tuupertunut.hintahaku.suodattimet.Suodattimet.java

License:Open Source License

public Document kirjoitaXML() {
    Element juuri = new Element("suodattimet");
    juuri.setAttribute("info", "Hintahaku-suodattimet");

    for (Kauppa kauppa : kaupat.values()) {
        Element kauppaElem = new Element("kauppa");
        kauppaElem.addContent(new Element("nimi").setText(kauppa.getKaupanNimi()));
        kauppaElem.addContent(new Element("voiNoutaa").setText(Boolean.toString(kauppa.isVoiNoutaa())));
        kauppaElem.addContent(new Element("suodataPois").setText(Boolean.toString(kauppa.isSuodataPois())));

        juuri.addContent(kauppaElem);//from www.  j  a v  a 2s. c  o m
    }

    return new Document(juuri);
}

From source file:uno.gpt.generator.XMLWriter.java

License:Open Source License

/**
 * Write to the XML file /*from   w  w  w  . j a v  a 2s . c om*/
 * @param input The set of goal-plan tree
 * @param ph The path
 */
public void CreateXML(ArrayList<GoalNode> input, String ph) {
    try {
        Element forest = new Element("Forest");
        Document document = new Document(forest);
        // write each top-level goals
        for (int i = 0; i < input.size(); i++) {
            GoalNode gl = input.get(i);
            writeGoal(gl, forest);
        }

        XMLOutputter xmlOutputer = new XMLOutputter();
        xmlOutputer.setFormat(Format.getPrettyFormat());
        xmlOutputer.output(document, new FileWriter(ph));
        System.out.println("XML File was created successfully!");
    } catch (IOException ex) {

    }
}

From source file:utils.RandomTopo.java

License:Open Source License

/** Generates an XCSP representation of a graph coloring problem
 * @param publicInteragentConstraints    whether inter-agent constraints should be public
 * @param soft                      whether the output should be a Max-DisCSP
 * @param intensional                whether the output should be intensional
 * @return An XCSP-formatted Document/*from w ww .ja va 2  s . co  m*/
 */
public Document toXCSP(final boolean publicInteragentConstraints, final boolean soft,
        final boolean intensional) {

    // Create the root element
    Element probElement = new Element("instance");
    probElement.setAttribute("noNamespaceSchemaLocation",
            "src/frodo2/algorithms/XCSPschema" + (soft || !intensional ? "" : "JaCoP") + ".xsd",
            Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));

    // Create the "presentation" element
    Element presElmt = new Element("presentation");
    probElement.addContent(presElmt);
    presElmt.setAttribute("name", this.instanceName);
    presElmt.setAttribute("maxConstraintArity", "2");
    presElmt.setAttribute("maximize", "false");
    presElmt.setAttribute("format", "XCSP 2.1_FRODO");

    // Create the "agents" element
    Element elmt = new Element("agents");
    probElement.addContent(elmt);
    final int nbrNodes = graph.nodes.size();
    elmt.setAttribute("nbAgents", Integer.toString(nbrNodes - this.stochNodes.size()));
    for (String varID : this.graph.nodes) {

        // Skip the stoch nodes
        if (this.stochNodes.contains(varID))
            continue;

        Element subElmt = new Element("agent");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "a" + varID);
    }

    // Create the "domains" element
    elmt = new Element("domains");
    probElement.addContent(elmt);
    elmt.setAttribute("nbDomains", "1");
    Element subElmt = new Element("domain");
    elmt.addContent(subElmt);
    subElmt.setAttribute("name", "colors");
    subElmt.setAttribute("nbValues", Integer.toString(nbrColors));
    subElmt.addContent("1.." + Integer.toString(nbrColors));

    // Create the "variables" element
    elmt = new Element("variables");
    probElement.addContent(elmt);
    elmt.setAttribute("nbVariables", Integer.toString(graph.nodes.size()));
    for (String varID : this.graph.nodes) {
        subElmt = new Element("variable");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "n" + varID);
        subElmt.setAttribute("domain", "colors");
        if (this.stochNodes.contains(varID))
            subElmt.setAttribute("type", "random");
        else
            subElmt.setAttribute("agent", "a" + varID);
    }

    // Create the "relations" element
    Element relElmt = new Element("relations");
    if (soft || !intensional || !this.unaryCons.isEmpty()) {
        probElement.addContent(relElmt);
    }

    if (soft || !intensional) { // extensional

        subElmt = new Element("relation");
        relElmt.addContent(subElmt);
        subElmt.setAttribute("name", "neq");
        subElmt.setAttribute("semantics", "soft");
        subElmt.setAttribute("arity", "2");
        subElmt.setAttribute("defaultCost", "0");
        subElmt.setAttribute("nbTuples", Integer.toString(nbrColors));

        StringBuilder builder = new StringBuilder(soft ? "1: " : "infinity: ");
        for (int i = 1; i < nbrColors; i++)
            builder.append(Integer.toString(i) + " " + i + " | ");
        builder.append(Integer.toString(nbrColors) + " " + nbrColors);
        subElmt.setText(builder.toString());

    } else { // pure satisfaction, intensional

        // Create the "predicates" element
        elmt = new Element("predicates");
        probElement.addContent(elmt);
        elmt.setAttribute("nbPredicates", "1");

        subElmt = new Element("predicate");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "neq");

        elmt = new Element("parameters");
        subElmt.addContent(elmt);
        elmt.setText("int X int Y");

        elmt = new Element("expression");
        subElmt.addContent(elmt);

        subElmt = new Element("functional");
        elmt.addContent(subElmt);
        subElmt.setText("ne(X, Y)");
    }

    if (!this.stochNodes.isEmpty()) {

        // Create the "probabilities" element
        elmt = new Element("probabilities");
        probElement.addContent(elmt);
        elmt.setAttribute("nbProbabilities", Integer.toString(this.stochNodes.size()));

        for (String varID : this.stochNodes) {

            subElmt = new Element("probability");
            elmt.addContent(subElmt);
            subElmt.setAttribute("name", "n" + varID + "proba");
            subElmt.setAttribute("semantics", "soft");
            subElmt.setAttribute("arity", "1");
            subElmt.setAttribute("nbTuples", Integer.toString(nbrColors));

            // Choose a random probability distribution
            StringBuilder builder = new StringBuilder();
            double[] probas = new double[nbrColors];
            double sum = 0.0;
            for (int i = 0; i < nbrColors; i++) {
                probas[i] = Math.random();
                sum += probas[i];
            }
            for (int i = 0; i < nbrColors - 1; i++)
                builder.append(Double.toString(probas[i] / sum) + ": " + (i + 1) + " | ");
            builder.append(Double.toString(probas[nbrColors - 1] / sum) + ": " + nbrColors);
            subElmt.setText(builder.toString());
        }
    }

    // Create the "constraints" element
    Element conElmt = new Element("constraints");
    probElement.addContent(conElmt);

    // Create the unary constraints
    double tightness = 0.0;
    final double weight = 1.0 / (this.nbrColors * (this.graph.nodes.size() - this.stochNodes.size()));
    for (Map.Entry<String, ArrayList<Integer>> entry : this.unaryCons.entrySet()) {
        String n = entry.getKey();
        ArrayList<Integer> colors = entry.getValue();
        tightness += colors.size() * weight;

        // Create the relation
        subElmt = new Element("relation");
        relElmt.addContent(subElmt);
        subElmt.setAttribute("name", "unaryRel_" + n);
        subElmt.setAttribute("semantics", soft || !intensional ? "soft" : "conflicts");
        subElmt.setAttribute("arity", "1");
        if (soft || !intensional)
            subElmt.setAttribute("defaultCost", "0");
        subElmt.setAttribute("nbTuples", Integer.toString(colors.size()));

        StringBuilder builder = new StringBuilder(soft ? "1: " : !intensional ? "infinity: " : "");
        for (int i = colors.size() - 1; i > 0; i--)
            builder.append(colors.get(i)).append("|");
        builder.append(colors.get(0));
        subElmt.setText(builder.toString());

        // Create the constraint
        elmt = new Element("constraint");
        conElmt.addContent(elmt);
        elmt.setAttribute("name", "unaryCons_" + n);
        elmt.setAttribute("scope", "n" + n);
        elmt.setAttribute("arity", "1");
        elmt.setAttribute("reference", "unaryRel_" + n);
        elmt.setAttribute("agent", "a" + n);
        //random volatility
        int vol;
        do {
            vol = (int) Math.round(gaussian ? ((new Random().nextGaussian() + 50)) : ((Math.random() * 100)));
        } while (vol < 0 || vol > 100);
        elmt.setAttribute("volatility", String.valueOf(vol));
    }

    // Go through all edges in the graph
    for (Edge edge : graph.edges) {

        // Skip this constraint if it involves two random variables
        if (this.stochNodes.contains(edge.source) && this.stochNodes.contains(edge.dest))
            continue;

        elmt = new Element("constraint");
        conElmt.addContent(elmt);

        final String n1 = "n" + edge.source;
        final String n2 = "n" + edge.dest;

        elmt.setAttribute("name", n1 + "_neq_" + n2);
        elmt.setAttribute("scope", n1 + " " + n2);
        elmt.setAttribute("arity", "2");
        elmt.setAttribute("reference", "neq");
        if (publicInteragentConstraints)
            elmt.setAttribute("agent", "PUBLIC");

        if (!soft && intensional) {
            subElmt = new Element("parameters");
            elmt.addContent(subElmt);
            subElmt.setText(n1 + " " + n2);
        }
        //random volatility
        int vol;
        do {
            vol = (int) Math
                    .round(gaussian ? ((new Random().nextGaussian() * 100 + 50)) : ((Math.random() * 100)));
        } while (vol < 0 || vol > 100);
        elmt.setAttribute("volatility", String.valueOf(vol));
    }

    // Add the probability distributions
    for (String varID : this.stochNodes) {
        final String varName = "n" + varID;

        elmt = new Element("constraint");
        conElmt.addContent(elmt);
        elmt.setAttribute("name", varName + "dist");
        elmt.setAttribute("scope", varName);
        elmt.setAttribute("arity", "1");
        elmt.setAttribute("reference", varName + "proba");
    }

    relElmt.setAttribute("nbRelations", Integer.toString(relElmt.getContentSize()));
    conElmt.setAttribute("nbConstraints", Integer.toString(conElmt.getContentSize()));

    // Write the stats
    presElmt.addContent(createStats("number of nodes", Integer.toString(this.graph.nodes.size())));
    presElmt.addContent(createStats("target density", Double.toString(this.targetDensity)));
    presElmt.addContent(createStats("true average density", Double.toString(this.graph.computeDensity())));
    presElmt.addContent(createStats("target unary tightness", Double.toString(this.targetTightness)));
    presElmt.addContent(createStats("true average unary tightness", Double.toString(tightness)));
    presElmt.addContent(createStats("number of colors", Integer.toString(this.nbrColors)));
    presElmt.addContent(
            createStats("number of uncontrollable nodes", Integer.toString(this.stochNodes.size())));
    presElmt.addContent(
            createStats("number of disconnected components", Integer.toString(graph.components.size())));
    presElmt.addContent(createStats("max degree", Integer.toString(graph.computeMaxDeg())));

    return new Document(probElement);
}

From source file:wasr.Report.java

License:Apache License

private Report(File reportFolder, ReportTemplate copyFromTemplate)
        throws ReportException, IOException, JDOMException {
    if (!reportFolder.exists()) {
        //noinspection ResultOfMethodCallIgnored
        reportFolder.mkdirs();/*from www  .  j  ava  2s. c o  m*/
    } else if (reportFolder.listFiles().length > 0) {
        throw new ReportException("NewReport.Exception.FolderNotEmpty");
    }

    this.reportFolder = reportFolder;

    this.reportTemplate = TemplateManager.copyTemplate(copyFromTemplate, this);
    LOG.info("Using template: " + copyFromTemplate);

    loadTemplateDOM();

    Element templateRoot = templateDOM.getRootElement();
    String rootName = templateRoot.getAttributeValue("id");
    Element rootElement = new Element(rootName);
    reportDOM = new Document(rootElement);
    treeRoot = new DocNode(templateRoot, rootElement);
    treeRoot.initChildrenFromTemplate();
}

From source file:XMLGenerator.GeneratorXML.java

public GeneratorXML(String Root, String xmlns) {
    root = new Element(Root, xmlns);
    doc = new Document(root);
    head = new ArrayList<Element>();
}

From source file:xmlwriter.XmlWriter.java

/**
 * Crea un documento y comienza el algoritmo recursivo addElements que construye el documento de nodo en nodo.
 * @param root Elemento que ser usado como raiz del documento.
 * @return Document documento xml formado despus de la interaccion con el usuario.
 *//*from  w  w  w  .j a va 2 s  .  co  m*/
private static Document buildDocument(Element root) {
    Document doc = new Document(addElementsTo(root));
    return doc;
}