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:AL_gui.java

License:Apache License

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    try {/*from   ww w  .j  av  a 2 s.c o m*/
        String nnetName = JOptionPane.showInputDialog(jButton3, "Enter a filename, excluding extention.",
                "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
        if (nnetName == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }

        Element nnet = new Element("NNETWORK");
        nnet.setAttribute(new Attribute("noNamespaceSchemaLocation", "ANNeML.xsd",
                Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")));
        nnet.setAttribute(new Attribute("NNET_NAME", nnetName));
        Document doc = new Document(nnet);

        String subnnets = JOptionPane.showInputDialog(jButton3, "How many SUBNET(s)?", "ANNeML Wizard",
                JOptionPane.QUESTION_MESSAGE);
        if (subnnets == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }
        int numSubs = java.lang.Integer.parseInt(subnnets);
        int i = 0;
        do {
            Element subnet = new Element("SUBNET");
            String learningRate = JOptionPane.showInputDialog(jButton3, "SUBNET learning rate(eta)?",
                    "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
            if (learningRate == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            subnet.setAttribute(new Attribute("NNET_V2", learningRate));
            subnet.setAttribute(new Attribute("SNET_NAME", nnetName + "-subnet" + String.valueOf(i + 1)));
            subnet.setAttribute(new Attribute("ADJUST_LOCK", "0"));

            String input_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<INPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (input_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numInLayers = java.lang.Integer.parseInt(input_layers);
            int x = 0;
            do {
                Element inLayer = new Element("LAYER");
                inLayer.setAttribute(new Attribute("LAYER_NAME", "INPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                inLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String inNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<INPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (inNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numInNodes = java.lang.Integer.parseInt(inNodes);
                int y = 0;
                do {
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "IN" + String.valueOf(x + 1) + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", "0.0"));
                    node.setAttribute(new Attribute("CNAME", "Input node#" + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    Element inSynapse = new Element("SYNAPSE");
                    inSynapse.setAttribute(new Attribute("WEIGHT", "1.00"));
                    inSynapse.setAttribute(new Attribute("ORG_NEURODE", "INPUT"));
                    node.addContent(inSynapse);
                    inLayer.addContent(node);
                    y++;
                } while (y < numInNodes);
                subnet.addContent(inLayer);
                x++;
            } while (x < numInLayers);

            String hidden_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<HIDDEN>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numHLayers = java.lang.Integer.parseInt(hidden_layers);
            int z = 0;
            do {
                Element hLayer = new Element("LAYER");
                hLayer.setAttribute(new Attribute("LAYER_NAME", "HIDDEN"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                hLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String hNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<HIDDEN>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (hNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numhNodes = java.lang.Integer.parseInt(hNodes);
                int a = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "N" + String.valueOf(z + 1) + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Hidden node#" + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    hLayer.addContent(node);
                    a++;
                } while (a < numhNodes);
                subnet.addContent(hLayer);
                z++;
            } while (z < numHLayers);

            String output_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<OUTPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numOLayers = java.lang.Integer.parseInt(output_layers);
            int b = 0;
            do {
                Element oLayer = new Element("LAYER");
                oLayer.setAttribute(new Attribute("LAYER_NAME", "OUTPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                oLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String oNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<OUTPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (oNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numoNodes = java.lang.Integer.parseInt(oNodes);
                int d = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "ON" + String.valueOf(b + 1) + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Output node#" + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    oLayer.addContent(node);
                    d++;
                } while (d < numoNodes);
                subnet.addContent(oLayer);
                b++;
            } while (b < numOLayers);

            doc.getRootElement().addContent(subnet);
            i++;
        } while (i < numSubs);

        //generate fully interconnected SYNAPSE(s) for all NEURODE(s) within each SUBNET

        java.util.List subnets = XPath.newInstance("//SUBNET").selectNodes(doc);
        Iterator itSubslist = subnets.iterator();
        do {
            Element currentSnet = (Element) itSubslist.next();
            String snetName = currentSnet.getAttributeValue("SNET_NAME");
            //System.out.println(snetName);

            java.util.List Hnodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                    .selectNodes(doc);
            Iterator itHNodelist = Hnodes.iterator();
            do {
                Element node = (Element) itHNodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List Inodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='INPUT']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = Inodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itHNodelist.hasNext());

            java.util.List Onodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='OUTPUT']/NEURODE")
                    .selectNodes(doc);
            Iterator itONodelist = Onodes.iterator();
            do {
                Element node = (Element) itONodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List hnodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = hnodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itONodelist.hasNext());

        } while (itSubslist.hasNext());

        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, System.out);
        xmlOutput.output(doc, new FileWriter(nnetName + ".xml"));

        System.out.println("File Saved!");

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:app.simulation.Exporter.java

License:MIT License

public void exportXML() throws Exception {
    Element root = new Element("simulation");
    Document document = new Document(root);

    if (configuration != null) {
        Element tagConfiguration = new Element("configuration");

        Element delay = new Element("delay");
        delay.setText(String.format("%s", configuration.getDelay()));

        Element iterations = new Element("iterations");
        iterations.setText(String.format("%s", configuration.getIterations()));

        Element agents = new Element("agents");
        agents.setText(String.format("%s", configuration.getAgents()));

        Element latticeSize = new Element("latticeSize");
        latticeSize.setText(String.format("%s", configuration.getLatticeSize()));

        Element description = new Element("description");
        description.setText(String.format("%s", configuration.getDescription()));

        tagConfiguration.addContent(delay);
        tagConfiguration.addContent(iterations);
        tagConfiguration.addContent(agents);
        tagConfiguration.addContent(latticeSize);
        tagConfiguration.addContent(description);

        root.addContent(tagConfiguration);
    }//  w ww .j  av  a  2 s  .  c  o m

    if (initialCell != null) {
        Element tagInitialCell = new Element("initialCell");

        Element x = new Element("x");
        x.setText(String.format("%s", initialCell.getX()));

        Element y = new Element("y");
        y.setText(String.format("%s", initialCell.getY()));

        Element z = new Element("z");
        z.setText(String.format("%s", initialCell.getZ()));

        Element state = new Element("state");
        state.setText(String.format("%s", initialCell.getState()));

        Element color = new Element("color");

        Element r = new Element("r");
        r.setText(String.format("%s", initialCell.getColor().getRed()));

        Element g = new Element("g");
        g.setText(String.format("%s", initialCell.getColor().getGreen()));

        Element b = new Element("b");
        b.setText(String.format("%s", initialCell.getColor().getBlue()));

        color.addContent(r);
        color.addContent(g);
        color.addContent(b);

        tagInitialCell.addContent(x);
        tagInitialCell.addContent(y);
        tagInitialCell.addContent(z);
        tagInitialCell.addContent(state);
        tagInitialCell.addContent(color);

        root.addContent(tagInitialCell);
    }

    if (rules != null) {
        Element tagRules = new Element("rules");

        for (Rule rule : rules) {
            Element tagRule = new Element("rule");

            Element neighbourhood = new Element("neighbourhood");
            neighbourhood.setText(rule.getNeighbourhood());

            Element state = new Element("state");
            state.setText(String.format("%s", rule.getState()));

            Element color = new Element("color");

            Element r = new Element("r");
            r.setText(String.format("%s", rule.getColor().getRed()));

            Element g = new Element("g");
            g.setText(String.format("%s", rule.getColor().getGreen()));

            Element b = new Element("b");
            b.setText(String.format("%s", rule.getColor().getBlue()));

            color.addContent(r);
            color.addContent(g);
            color.addContent(b);

            tagRule.addContent(neighbourhood);
            tagRule.addContent(state);
            tagRule.addContent(color);

            tagRules.addContent(tagRule);
        }

        root.addContent(tagRules);
    }

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    outputter.output(document, new FileWriter(path));
}

From source file:backtesting.BackTesterNinety.java

private static void SaveSettings(BTSettings settings) {
    BufferedWriter output = null;
    try {/*from  w  w  w.ja  v  a  2  s.c  o  m*/
        Element rootElement = new Element("Settings");
        Document doc = new Document(rootElement);
        rootElement.setAttribute("start", settings.startDate.toString());
        rootElement.setAttribute("end", settings.endDate.toString());

        rootElement.setAttribute("capital", Double.toString(settings.capital));
        rootElement.setAttribute("leverage", Double.toString(settings.leverage));

        rootElement.setAttribute("reinvest", Boolean.toString(settings.reinvest));

        XMLOutputter xmlOutput = new XMLOutputter();

        File fileSettings = new File("backtest/cache/_settings.xml");
        fileSettings.createNewFile();
        FileOutputStream oFile = new FileOutputStream(fileSettings, false);

        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, oFile);

    } catch (IOException ex) {
        Logger.getLogger(BackTesterNinety.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:br.com.nfe.util.Chave.java

public void criarChave(String uf, String tpEmis, String nNfe, String cNf, String serie, String AnoMes,
        String cnpj) {/*from   w  w w.  ja va2  s. c om*/
    this.cNf = cNf;
    Element gerarChave = new Element("gerarChave");
    Document documento = new Document(gerarChave);
    Element UF = new Element("UF");
    UF.setText(uf);

    Element NNFE = new Element("nNF");
    NNFE.setText(nNfe);

    Element CNF = new Element("cNF");
    CNF.setText(cNf);

    Element TPEMIS = new Element("tpEmis");
    TPEMIS.setText(tpEmis);

    Element SERIE = new Element("serie");
    SERIE.setText(serie);

    Element ANOMES = new Element("AAMM");
    ANOMES.setText(AnoMes);

    Element CNPJ = new Element("CNPJ");
    CNPJ.setText(cnpj);

    gerarChave.addContent(UF);
    gerarChave.addContent(TPEMIS);
    gerarChave.addContent(NNFE);
    //gerarChave.addContent(CNF);
    gerarChave.addContent(SERIE);
    gerarChave.addContent(ANOMES);
    gerarChave.addContent(CNPJ);
    XMLOutputter xout = new XMLOutputter();
    try {

        FileWriter arquivo = new FileWriter(
                new File("c:/unimake/uninfe/" + pasta + "/envio/" + cNf + "-gerar-chave.xml"));

        xout.output(documento, arquivo);

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:broadwick.graph.writer.GraphMl.java

License:Apache License

/**
 * Get the string representation of the network.
 * @param network  the network object to be written.
 * @param directed a boolean flag, true if the network is directed.
 * @return a string representing a document.
 *//*from  ww  w  . ja  va  2s  .  co  m*/
public static String toString(final Graph<? extends Vertex, ? extends Edge<?>> network,
        final boolean directed) {
    // graphml document header
    final Element graphml = new Element("graphml", "http://graphml.graphdrawing.org/xmlns");
    final Document document = new Document(graphml);
    final Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    final Namespace schemLocation = Namespace.getNamespace("schemLocation",
            "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0rc/graphml.xsd");

    // add Namespace
    graphml.addNamespaceDeclaration(xsi);
    graphml.addNamespaceDeclaration(schemLocation);

    // keys for graphic representation
    for (VertexAttribute attr : network.getVertexAttributes()) {
        final Element element = new Element("key");
        element.setAttribute("id", attr.getName());
        element.setAttribute("for", "node");
        element.setAttribute("attr.name", attr.getName());
        element.setAttribute("attr.type", attr.getType().getName());
        if (attr.getDefaultValue() != null) {
            final Element defaultValueElement = new Element("default");
            defaultValueElement.addContent(attr.getDefaultValue().toString());
            element.addContent(defaultValueElement);
        }
        graphml.addContent(element);
    }

    for (EdgeAttribute attr : network.getEdgeAttributes()) {
        final Element element = new Element("key");
        element.setAttribute("id", attr.getName());
        element.setAttribute("for", "edge");
        element.setAttribute("attr.name", attr.getName());
        element.setAttribute("attr.type", attr.getType().getName());
        if (attr.getDefaultValue() != null) {
            final Element defaultValueElement = new Element("default");
            defaultValueElement.addContent(attr.getDefaultValue());
            element.addContent(defaultValueElement);
        }
        graphml.addContent(element);
    }

    final Element graph = new Element("graph");
    graph.setAttribute("id", "G");
    if (directed) {
        graph.setAttribute("edgedefault", "directed");
    } else {
        graph.setAttribute("edgedefault", "undirected");
    }
    graphml.addContent(graph);

    final ImmutableList<Vertex> vertices = ImmutableList.copyOf(network.getVertices());
    final Iterator<Vertex> vertexIterator = vertices.iterator();
    while (vertexIterator.hasNext()) {
        final Vertex vertex = vertexIterator.next();
        addNode(vertex, graph);
    }

    final ImmutableList<Edge<? extends Vertex>> edges;
    edges = (ImmutableList<Edge<? extends Vertex>>) ImmutableList.copyOf(network.getEdges());
    final UnmodifiableIterator<Edge<? extends Vertex>> edgeIterator = edges.iterator();
    while (edgeIterator.hasNext()) {
        addEdge(edgeIterator.next(), graph);
    }

    final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    return outputter.outputString(document).replaceAll("xmlns=\"\" ", "");
}

From source file:by.epam.lw05.xml.ListToXml.java

private static Document listToDocument(List<Gun> guns) {
    Element root = new Element("arsenal", "tns", "http://www.example.com/Tarifes");
    root.addNamespaceDeclaration(Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    Attribute attr = new Attribute("schemaLocation", "http://www.example.com/Tarifes myschema.xsd",
            Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    root.setAttribute(attr);//from  w  w w . j a  v a  2s .  c om
    for (Gun gun : guns) {
        Element combatUnit = new Element("combatunit");

        combatUnit.setAttribute("serial", String.valueOf(gun.getSerial()));

        Element model = new Element("model");
        model.setText(gun.getModel());
        combatUnit.addContent(model);

        Element handy = new Element("handy");
        handy.setText(gun.getHandy());
        combatUnit.addContent(handy);

        Element origin = new Element("origin");
        origin.setText(String.valueOf(gun.getOrigin()));
        combatUnit.addContent(origin);

        Element ttx = new Element("ttx");

        Element distance = new Element("distance");
        distance.setText(String.valueOf(gun.getDistance()));
        ttx.addContent(distance);

        Element optics = new Element("optics");
        optics.setText(String.valueOf(gun.isOptics()));
        ttx.addContent(optics);
        combatUnit.addContent(ttx);

        root.addContent(combatUnit);
    }
    return new Document(root);
}

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

/**
 * Write to root Element to a writer./*from w ww.  j av  a 2 s .c o  m*/
 *
 * @param root Root Element to write.
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(new Document(root), writer);
}

From source file:ca.nrc.cadc.caom2.xml.ArtifactAccessWriter.java

License:Open Source License

public void write(ArtifactAccess aa, Writer writer) throws IOException {
    Element root = new Element(ArtifactAccessReader.ENAMES.artifactAccess.name());

    Element ae = new Element(ArtifactAccessReader.ENAMES.artifact.name());
    root.addContent(ae);//from  w  w w . j av  a  2 s . c om

    addChild(ae, ArtifactAccessReader.ENAMES.uri.name(), aa.getArtifact().getURI().toASCIIString());
    addChild(ae, ArtifactAccessReader.ENAMES.productType.name(), aa.getArtifact().getProductType().getValue());
    addChild(ae, ArtifactAccessReader.ENAMES.releaseType.name(), aa.getArtifact().getReleaseType().getValue());
    if (aa.getArtifact().contentChecksum != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentChecksum.name(),
                aa.getArtifact().contentChecksum.toASCIIString());
    }
    if (aa.getArtifact().contentLength != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentLength.name(),
                aa.getArtifact().contentLength.toString());
    }
    if (aa.getArtifact().contentType != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentType.name(), aa.getArtifact().contentType);
    }

    Element pub = new Element(ArtifactAccessReader.ENAMES.isPublic.name());
    pub.setText(Boolean.toString(aa.isPublic));
    root.addContent(pub);

    Element rg = new Element(ArtifactAccessReader.ENAMES.readGroups.name());
    if (!aa.getReadGroups().isEmpty() || writeEmptyCollections) {
        root.addContent(rg);
    }
    addGroups(aa.getReadGroups(), rg);

    Document doc = new Document(root);
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(doc, writer);
}

From source file:ca.nrc.cadc.caom2.xml.JsonWriter.java

License:Open Source License

/**
 * Write the root Element to a writer./*from w  w  w  . j a  v  a2s. c o  m*/
 *
 * @param root
 *            Root Element to write.
 * @param writer
 *            Writer to write to.
 * @throws IOException
 *             if the writer fails to write.
 */
@Override
protected void write(Element root, Writer writer) throws IOException {
    JsonOutputter outputter = new JsonOutputter();
    outputter.getListElementNames().add("planes");
    outputter.getListElementNames().add("artifacts");
    outputter.getListElementNames().add("parts");
    outputter.getListElementNames().add("vertices");
    outputter.getListElementNames().add("points");
    outputter.getListElementNames().add("inputs");
    outputter.getListElementNames().add("states");
    outputter.getListElementNames().add("samples");
    outputter.getListElementNames().add("members");
    if (docVersion >= 23) {
        outputter.getListElementNames().add("keywords");
    }

    outputter.getStringElementNames().add("observationID");
    outputter.getStringElementNames().add("productID");
    outputter.getStringElementNames().add("sequenceNumber");
    outputter.getStringElementNames().add("name"); // anything with a name

    Format fmt = null;
    if (prettyPrint) {
        fmt = Format.getPrettyFormat();
        fmt.setIndent("  "); // 2 spaces
    }
    outputter.setFormat(fmt);
    Document document = new Document(root);
    outputter.output(document, writer);
}

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Write the root Element to a writer./*from ww  w.ja  va  2 s.c o m*/
 *
 * @param root
 *            Root Element to write.
 * @param writer
 *            Writer to write to.
 * @throws IOException
 *             if the writer fails to write.
 */
@SuppressWarnings("unchecked")
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    if (stylesheetURL != null) {
        Map<String, String> instructionMap = new HashMap<String, String>(2);
        instructionMap.put("type", "text/xsl");
        instructionMap.put("href", stylesheetURL);
        ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", instructionMap);
        document.getContent().add(0, pi);
    }
    outputter.output(document, writer);
}