Search in sources :

Example 16 with XMLOutputter

use of org.jdom2.output.XMLOutputter in project JMRI by JMRI.

the class VariableTableModelTest method testVarTableLoad_2_3.

// Check loading two columns, three rows
public void testVarTableLoad_2_3() {
    String[] args = { "CV", "Name" };
    VariableTableModel t = new VariableTableModel(null, args, new CvTableModel(null, p), null);
    // create a JDOM tree with just some elements
    Element root = new Element("decoder-config");
    Document doc = new Document(root);
    doc.setDocType(new DocType("decoder-config", "decoder-config.dtd"));
    // add some elements
    Element el0, el1;
    root.addContent(// the sites information here lists all relevant
    new Element("decoder").addContent(new Element("variables").addContent(el0 = new Element("variable").setAttribute("CV", "1").setAttribute("label", "one").setAttribute("mask", "VVVVVVVV").setAttribute("item", "really two").setAttribute("readOnly", "no").addContent(new Element("decVal").setAttribute("max", "31").setAttribute("min", "1"))).addContent(el1 = new Element("variable").setAttribute("CV", "4").setAttribute("readOnly", "no").setAttribute("mask", "XXXVVVVX").setAttribute("label", "two").addContent(new Element("decVal").setAttribute("max", "31").setAttribute("min", "1")))));
    // end of adding contents
    // print JDOM tree, to check
    //OutputStream o = System.out;
    //XMLOutputter fmt = new XMLOutputter();
    //fmt.setNewlines(true);   // pretty printing
    //fmt.setIndent(true);
    //try {
    //	 fmt.output(doc, o);
    //} catch (Exception e) { System.out.println("error writing XML: "+e);}
    // and test reading this
    t.setRow(0, el0);
    Assert.assertTrue(t.getValueAt(0, 0).equals("1"));
    Assert.assertTrue(t.getValueAt(0, 1).equals("one"));
    // check that the variable names were set right
    Assert.assertEquals("check loaded label ", "one", t.getLabel(0));
    Assert.assertEquals("check loaded item ", "really two", t.getItem(0));
    t.setRow(1, el1);
    Assert.assertTrue(t.getValueAt(1, 0).equals("4"));
    Assert.assertTrue(t.getValueAt(1, 1).equals("two"));
    Assert.assertTrue(t.getRowCount() == 2);
    // check finding
    Assert.assertEquals("find variable two ", 1, t.findVarIndex("two"));
    Assert.assertEquals("find nonexistant variable ", -1, t.findVarIndex("not there, eh?"));
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) DocType(org.jdom2.DocType)

Example 17 with XMLOutputter

use of org.jdom2.output.XMLOutputter in project JMRI by JMRI.

the class VariableTableModelTest method testVarTableLoadLongAddr.

// Check creating a longaddr type, walk through its programming
public void testVarTableLoadLongAddr() {
    String[] args = { "CV", "Name" };
    VariableTableModel t = new VariableTableModel(null, args, new CvTableModel(null, p), null);
    // create a JDOM tree with just some elements
    Element root = new Element("decoder-config");
    Document doc = new Document(root);
    doc.setDocType(new DocType("decoder-config", "decoder-config.dtd"));
    // add some elements
    Element el0;
    root.addContent(// the sites information here lists all relevant
    new Element("decoder").addContent(new Element("variables").addContent(el0 = new Element("variable").setAttribute("CV", "17").setAttribute("readOnly", "no").setAttribute("mask", "VVVVVVVV").setAttribute("label", "long").addContent(new Element("longAddressVal")))));
    // end of adding contents
    // print JDOM tree, to check
    //OutputStream o = System.out;
    //XMLOutputter fmt = new XMLOutputter();
    //fmt.setNewlines(true);   // pretty printing
    //fmt.setIndent(true);
    //try {
    //	 fmt.output(doc, o);
    //} catch (Exception e) { System.out.println("error writing XML: "+e);}
    // and test reading this
    t.setRow(0, el0);
    Assert.assertTrue(t.getValueAt(0, 0).equals("17"));
    Assert.assertTrue(t.getValueAt(0, 1).equals("long"));
    Assert.assertTrue(t.getRowCount() == 1);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) DocType(org.jdom2.DocType)

Example 18 with XMLOutputter

use of org.jdom2.output.XMLOutputter in project pcgen by PCGen.

the class Initiative method saveToDocument.

/**
	 *  Saves the current combatants out to an XML file
	 *
	 *@param  xml            The File to save to
	 *@exception  Exception  XML and file IO exceptions
	 */
private void saveToDocument(File xml) throws IOException {
    Element party = new Element("Party");
    party.setAttribute("filever", "1.0");
    party.setAttribute("filetype", "initsave");
    /*if(currentInit > -1) {
		 party.setAttribute("current_init", Integer.toString(currentInit));
		 }*/
    initList.forEach((InitHolder anInitList) -> party.addContent(anInitList.getSaveElement()));
    XMLOutputter xmlOut = new XMLOutputter();
    xmlOut.setFormat(Format.getRawFormat().setEncoding("US-ASCII"));
    try (Writer fr = new FileWriter(xml)) {
        Document saveDocument = new Document(party);
        xmlOut.output(saveDocument, fr);
        fr.flush();
        fr.close();
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) FileWriter(java.io.FileWriter) Document(org.jdom2.Document) InitHolder(gmgen.plugin.InitHolder) Writer(java.io.Writer) FileWriter(java.io.FileWriter)

Aggregations

Document (org.jdom2.Document)17 XMLOutputter (org.jdom2.output.XMLOutputter)15 Element (org.jdom2.Element)14 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 FileWriter (java.io.FileWriter)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 JFrame (javax.swing.JFrame)3 Positionable (jmri.jmrit.display.Positionable)3 DocType (org.jdom2.DocType)3 SAXBuilder (org.jdom2.input.SAXBuilder)3 FileInputStream (java.io.FileInputStream)2 Writer (java.io.Writer)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 JDOMException (org.jdom2.JDOMException)2 Format (org.jdom2.output.Format)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1