Search in sources :

Example 6 with XMLOutputter

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

the class SwitchboardServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        SwitchboardEditor editor = (SwitchboardEditor) getEditor(name);
        Element panel = new Element("panel");
        JFrame frame = editor.getTargetFrame();
        panel.setAttribute("name", name);
        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
        // add more properties
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        panel.setAttribute("hideunconnected", (editor.hideUnconnected()) ? "yes" : "no");
        panel.setAttribute("rangemin", Integer.toString(editor.getPanelMenuRangeMin()));
        panel.setAttribute("rangemax", Integer.toString(editor.getPanelMenuRangeMax()));
        panel.setAttribute("type", editor.getSwitchType());
        panel.setAttribute("connection", editor.getSwitchManu());
        panel.setAttribute("shape", editor.getSwitchShape());
        panel.setAttribute("columns", Integer.toString(editor.getColumns()));
        panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
        log.debug("webserver Switchboard attribs ready");
        Element bgColor = new Element("backgroundColor");
        if (editor.getBackgroundColor() == null) {
            // set to light grey
            bgColor.setAttribute("red", Integer.toString(192));
            bgColor.setAttribute("green", Integer.toString(192));
            bgColor.setAttribute("blue", Integer.toString(192));
        } else {
            bgColor.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
            bgColor.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
            bgColor.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
        }
        panel.addContent(bgColor);
        Element text = new Element("text");
        text.setAttribute("color", editor.getDefaultTextColor());
        text.setAttribute("content", "For now, Switchboards only present buttons in JMRI WebServer.");
        panel.addContent(text);
        // include switches, Bug: how to delete the old ones?
        // call method in SwitchboardEditor
        List<BeanSwitch> _switches = editor.getSwitches();
        log.debug("SwbServlet N switches: {}", _switches.size());
        for (BeanSwitch sub : _switches) {
            if (sub != null) {
                try {
                    Element e = ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        log.debug("element name: {}", e.getName());
                        // }
                        try {
                            e.setAttribute("label", sub.getNameString());
                            e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
                            if (sub.getNamedBean() == null) {
                                e.setAttribute("connected", "false");
                                log.debug("switch {} NOT connected", sub.getNameString());
                            } else {
                                // activate click action via class
                                e.setAttribute("connected", "true");
                            }
                        } catch (NullPointerException ex) {
                            if (sub.getNamedBean() == null) {
                                log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
                            } else {
                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
                            }
                        }
                        // read shared attribs
                        e.setAttribute("textcolor", editor.getDefaultTextColor());
                        e.setAttribute("type", editor.getSwitchType());
                        e.setAttribute("connection", editor.getSwitchManu());
                        e.setAttribute("shape", editor.getSwitchShape());
                        e.setAttribute("columns", Integer.toString(editor.getColumns()));
                        // process and add
                        parsePortableURIs(e);
                        panel.addContent(e);
                    }
                } catch (Exception ex) {
                    log.error("Error reading xml panel element: " + ex, ex);
                }
            }
        }
        Document doc = new Document(panel);
        XMLOutputter out = new XMLOutputter();
        out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return out.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested Switchboard [" + name + "] does not exist.");
        return "ERROR Requested Switchboard [" + name + "] does not exist.";
    }
}
Also used : SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) XMLOutputter(org.jdom2.output.XMLOutputter) JFrame(javax.swing.JFrame) Element(org.jdom2.Element) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch) Document(org.jdom2.Document)

Example 7 with XMLOutputter

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

the class ControlPanelServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        ControlPanelEditor editor = (ControlPanelEditor) getEditor(name);
        Element panel = new Element("panel");
        JFrame frame = editor.getTargetFrame();
        panel.setAttribute("name", name);
        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        if (editor.getBackgroundColor() != null) {
            Element color = new Element("backgroundColor");
            color.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
            color.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
            color.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
            panel.addContent(color);
        }
        // include contents
        List<Positionable> contents = editor.getContents();
        log.debug("N elements: {}", contents.size());
        for (Positionable sub : contents) {
            if (sub != null) {
                try {
                    Element e = ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        if ("signalmasticon".equals(e.getName())) {
                            //insert icon details into signalmast
                            e.addContent(getSignalMastIconsElement(e.getAttributeValue("signalmast")));
                        }
                        try {
                            e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
                        } catch (NullPointerException ex) {
                            if (sub.getNamedBean() == null) {
                                log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
                            } else {
                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
                            }
                        }
                        parsePortableURIs(e);
                        panel.addContent(e);
                    }
                } catch (Exception ex) {
                    log.error("Error storing panel element: " + ex, ex);
                }
            }
        }
        Document doc = new Document(panel);
        XMLOutputter out = new XMLOutputter();
        out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return out.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested ControlPanel [" + name + "] does not exist.");
        return "ERROR Requested panel [" + name + "] does not exist.";
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) ControlPanelEditor(jmri.jmrit.display.controlPanelEditor.ControlPanelEditor) JFrame(javax.swing.JFrame) Element(org.jdom2.Element) Positionable(jmri.jmrit.display.Positionable) Document(org.jdom2.Document)

Example 8 with XMLOutputter

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

the class VariableTableModelTest method testVarTableLoadBogus.

// Check creating bogus XML (unknown variable type)
public void testVarTableLoadBogus() {
    String[] args = { "CV", "Name" };
    VariableTableModel t = new VariableTableModel(null, args, new CvTableModel(null, p), null) {

        @Override
        void reportBogus() {
        }
    };
    // 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("mask", "VVVVVVVV").setAttribute("readOnly", "no").setAttribute("label", "long").addContent(new Element("bogusVal")))));
    // 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.getRowCount() == 0);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) DocType(org.jdom2.DocType)

Example 9 with XMLOutputter

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

the class XmlFile method writeXML.

/**
     * Write a File as XML.
     *
     * @throws FileNotFoundException if file not found
     * @param file File to be created.
     * @param doc  Document to be written out. This should never be null.
     */
public void writeXML(File file, Document doc) throws IOException, FileNotFoundException {
    // ensure parent directory exists
    if (file.getParent() != null) {
        FileUtil.createDirectory(file.getParent());
    }
    // write the result to selected file
    try (FileOutputStream o = new FileOutputStream(file)) {
        XMLOutputter fmt = new XMLOutputter();
        fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM_FULL_WHITE));
        fmt.output(doc, o);
        o.flush();
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) FileOutputStream(java.io.FileOutputStream)

Example 10 with XMLOutputter

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

the class StoreXmlThrottlesLayoutAction method saveThrottlesLayout.

public void saveThrottlesLayout(java.io.File f) {
    try {
        Element root = new Element("throttle-layout-config");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttle-layout-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle-layout-config.xsl"?>
        /*TODO   java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation + "throttle-layout-config.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0, p); */
        java.util.ArrayList<Element> children = new java.util.ArrayList<Element>(5);
        // throttle list window
        children.add(ThrottleFrameManager.instance().getThrottlesListPanel().getXml());
        // throttle windows
        for (Iterator<ThrottleWindow> i = ThrottleFrameManager.instance().getThrottleWindows(); i.hasNext(); ) {
            ThrottleWindow tw = i.next();
            Element throttleElement = tw.getXml();
            children.add(throttleElement);
        }
        root.setContent(children);
        FileOutputStream o = new java.io.FileOutputStream(f);
        try {
            XMLOutputter fmt = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
            fmt.output(doc, o);
        } catch (IOException ex) {
            log.warn("Exception in storing throttle xml: " + ex);
        } finally {
            o.close();
        }
    } catch (FileNotFoundException ex) {
        log.warn("Exception in storing throttle xml: " + ex);
    } catch (IOException ex) {
        log.warn("Exception in storing throttle xml: " + ex);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.jdom2.Document)

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