Search in sources :

Example 1 with XMLOutputter

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

the class GoControlLog method writeLogFile.

protected void writeLogFile(File file, Element element) throws IOException {
    // Write the log file out, let jdom care about the encoding by using
    // an OutputStream instead of a Writer.
    OutputStream logStream = null;
    try {
        Format format = Format.getPrettyFormat();
        XMLOutputter outputter = new XMLOutputter(format);
        IO.mkdirFor(file);
        file.setWritable(true);
        logStream = new BufferedOutputStream(new FileOutputStream(file));
        outputter.output(new Document(element), logStream);
    } finally {
        IO.close(logStream);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Format(org.jdom2.output.Format) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with XMLOutputter

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

the class DiceBagModel method saveToFile.

/**
	 * <p>Saves the dicebag to the specified file as a UTF-8 xml file, with the format
	 * specified above in {@code loadFromFile()}</p>
	 *
	 * @param file File to save to.
	 */
void saveToFile(File file) {
    try {
        Document doc = new Document();
        saveToDocument(doc);
        XMLOutputter xmlOut = new XMLOutputter();
        xmlOut.setFormat(Format.getPrettyFormat());
        FileWriter fr = new FileWriter(file);
        xmlOut.output(doc, fr);
        fr.flush();
        fr.close();
        m_filePath = file.getPath();
        m_changed = false;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(GMGenSystem.inst, "File load error: " + file.getName());
        Logging.errorPrint("File Load Error" + file.getName());
        Logging.errorPrint(e.getMessage(), e);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) FileWriter(java.io.FileWriter) Document(org.jdom2.Document)

Example 3 with XMLOutputter

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

the class XmlFileValidateAction method processFile.

protected void processFile(File file) {
    if (log.isDebugEnabled()) {
        log.debug("located file " + file + " for XML processing");
    }
    // handle the file (later should be outside this thread?)
    try {
        xmlfile.setValidate(XmlFile.Validate.CheckDtdThenSchema);
        readFile(file);
    } catch (Exception ex) {
        // because of XInclude, we're doing this
        // again to validate the entire file
        // without losing the error message
        Document doc;
        try {
            InputStream stream = new BufferedInputStream(new FileInputStream(file));
            SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", false);
            builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
            builder.setFeature("http://apache.org/xml/features/xinclude", true);
            builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
            builder.setFeature("http://apache.org/xml/features/validation/schema", false);
            builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
            builder.setFeature("http://xml.org/sax/features/namespaces", true);
            doc = builder.build(new BufferedInputStream(stream));
        } catch (JDOMException | IOException ex2) {
            showFailResults(_who, "Err(1): " + ex2);
            return;
        }
        XMLOutputter outputter = new XMLOutputter();
        outputter.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
        StringWriter out = new StringWriter();
        try {
            outputter.output(doc, out);
        } catch (IOException ex2) {
            showFailResults(_who, "Err(4): " + ex2);
            return;
        }
        StringReader input = new StringReader(new String(out.getBuffer()));
        SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
        builder.setEntityResolver(new jmri.util.JmriLocalEntityResolver());
        builder.setFeature("http://apache.org/xml/features/xinclude", true);
        builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
        builder.setFeature("http://apache.org/xml/features/validation/schema", true);
        builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        builder.setFeature("http://xml.org/sax/features/namespaces", true);
        try {
            builder.build(input).getRootElement();
        } catch (JDOMException | IOException ex2) {
            showFailResults(_who, "Err(2): " + ex2);
            return;
        }
        showFailResults(_who, "Err(3): " + ex);
        return;
    }
    showOkResults(_who, "OK");
    if (log.isDebugEnabled()) {
        log.debug("parsing complete");
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.jdom2.Document) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) FileInputStream(java.io.FileInputStream) StringWriter(java.io.StringWriter) BufferedInputStream(java.io.BufferedInputStream) StringReader(java.io.StringReader)

Example 4 with XMLOutputter

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

the class SymbolicProgFrame method writeFile.

// dead class doesn't need this fixed right now
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION")
void writeFile() {
    log.warn("SymbolicProgFrame writeFile invoked - is this still right, or should the LocoFile method be used?");
    log.warn("Note use of VersionID attribute...");
    try {
        // get the file
        int retVal = fco.showSaveDialog(this);
        // handle selection or cancel
        if (retVal != JFileChooser.APPROVE_OPTION) {
            // leave early
            return;
        }
        File file = fco.getSelectedFile();
        // This is taken in large part from "Java and XML" page 368
        // create root element
        Element root = new Element("locomotive-config");
        Document doc = jmri.jmrit.XmlFile.newDocument(root, jmri.jmrit.XmlFile.getDefaultDtdLocation() + "locomotive-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/locomotive.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
        m.put("type", "text/xsl");
        m.put("href", jmri.jmrit.XmlFile.xsltLocation + "locomotive.xsl");
        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        // add top-level elements
        Element values;
        root.addContent(// locomotive values are first item
        new Element("locomotive").setAttribute("roadNumber", locoRoadNumber.getText()).setAttribute("roadName", locoRoadName.getText()).setAttribute("mfg", locoMfg.getText()).setAttribute("model", locoModel.getText()).addContent(new Element("decoder").setAttribute("model", decoderModel.getText()).setAttribute("mfg", decoderMfg.getText()).setAttribute("versionID", "").setAttribute("mfgID", "")).addContent(values = new Element("values")));
        // Append a decoderDef element to values
        Element decoderDef;
        values.addContent(decoderDef = new Element("decoderDef"));
        // add the variable values to the decoderDef Element
        for (int i = 0; i < variableModel.getRowCount(); i++) {
            decoderDef.addContent(new Element("varValue").setAttribute("item", variableModel.getLabel(i)).setAttribute("value", variableModel.getValString(i)));
        }
        // add the CV values to the values Element
        for (int i = 0; i < cvModel.getRowCount(); i++) {
            values.addContent(new Element("CVvalue").setAttribute("name", cvModel.getName(i)).setAttribute("value", cvModel.getValString(i)));
        }
        // write the result to selected file
        java.io.FileOutputStream o = new java.io.FileOutputStream(file);
        try {
            XMLOutputter fmt = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
            fmt.output(doc, o);
        } finally {
            o.close();
        }
        // mark file as OK
        variableModel.setFileDirty(false);
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) Document(org.jdom2.Document) DecoderFile(jmri.jmrit.decoderdefn.DecoderFile) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with XMLOutputter

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

the class StoreXmlVSDecoderAction method saveVSDecoderProfile.

public void saveVSDecoderProfile(java.io.File f) {
    try {
        Element root = new Element("VSDecoderConfig");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "vsdecoder-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);
        for (java.util.Iterator<VSDecoder> i = VSDecoderManager.instance().getVSDecoderList().iterator(); i.hasNext(); ) {
            VSDecoder vsd = i.next();
            children.add(vsd.getXml());
        }
        // Throttle-specific stuff below.  Kept for reference
        /*
             // 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);
             }
             */
        // End Throttle-specific stuff.
        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 VSDecoder xml: " + ex);
        } finally {
            o.close();
        }
    } catch (FileNotFoundException ex) {
        log.warn("Exception in storing VSDecoder xml: " + ex);
    } catch (IOException ex) {
        log.warn("Exception in storing VSDecoder 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