Search in sources :

Example 51 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project JMRI by JMRI.

the class SpeedometerFrame method doStore.

private void doStore() {
    log.debug("Check if there's anything to store");
    int verify = verifyInputs(false);
    if (verify == 0) {
        if (JOptionPane.showConfirmDialog(this, Bundle.getMessage("QuestionNothingToStore"), Bundle.getMessage("TitleStoreQuestion"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
            return;
        }
    }
    log.debug("Start storing speedometer settings...");
    SpeedometerXml x = new SpeedometerXml();
    x.makeBackupFile(SpeedometerXml.getDefaultFileName());
    File file = x.getFile(true);
    // Create root element
    Element root = new Element("speedometer-config");
    root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/speedometer-3-9-3.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    Document doc = new Document(root);
    // add XSLT processing instruction
    java.util.Map<String, String> m = new java.util.HashMap<String, String>();
    m.put("type", "text/xsl");
    m.put("href", SpeedometerXml.xsltLocation + "speedometer.xsl");
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    Element values;
    // Store configuration
    root.addContent(values = new Element("configuration"));
    values.addContent(new Element("useMetric").addContent(dim ? "yes" : "no"));
    // Store values
    if (verify > 0 || startSensor.getText().length() > 0) {
        // Create sensors element
        root.addContent(values = new Element("sensors"));
        // Store start sensor
        Element e = new Element("sensor");
        e.addContent(new Element("sensorName").addContent(startSensor.getText()));
        e.addContent(new Element("type").addContent("StartSensor"));
        e.addContent(new Element("trigger").addContent(startOnEntry.isSelected() ? "entry" : "exit"));
        values.addContent(e);
        // If valid, store stop sensor 1
        if (verify > 0) {
            e = new Element("sensor");
            e.addContent(new Element("sensorName").addContent(stopSensor1.getText()));
            e.addContent(new Element("type").addContent("StopSensor1"));
            e.addContent(new Element("trigger").addContent(stopOnEntry1.isSelected() ? "entry" : "exit"));
            try {
                e.addContent(new Element("distance").addContent(String.valueOf(IntlUtilities.floatValue(distance1.getText()))));
            } catch (java.text.ParseException ex) {
                log.error("Distance isn't a valid floating number: " + distance1.getText());
            }
            values.addContent(e);
        }
        // If valid, store stop sensor 2
        if (verify > 1) {
            e = new Element("sensor");
            e.addContent(new Element("sensorName").addContent(stopSensor2.getText()));
            e.addContent(new Element("type").addContent("StopSensor2"));
            e.addContent(new Element("trigger").addContent(stopOnEntry2.isSelected() ? "entry" : "exit"));
            try {
                e.addContent(new Element("distance").addContent(String.valueOf(IntlUtilities.floatValue(distance2.getText()))));
            } catch (java.text.ParseException ex) {
                log.error("Distance isn't a valid floating number: " + distance2.getText());
            }
            values.addContent(e);
        }
    }
    try {
        x.writeXML(file, doc);
    } catch (FileNotFoundException ex) {
        log.error("File not found when writing: " + ex);
    } catch (IOException ex) {
        log.error("IO Exception when writing: " + ex);
    }
    log.debug("...done");
}
Also used : Element(org.jdom2.Element) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 52 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document 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 53 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project JMRI by JMRI.

the class ProfileManager method readProfiles.

private void readProfiles() throws JDOMException, IOException {
    try {
        boolean reWrite = false;
        if (!catalog.exists()) {
            this.writeProfiles();
        }
        if (!catalog.canRead()) {
            return;
        }
        this.readingProfiles = true;
        Document doc = (new SAXBuilder()).build(catalog);
        profiles.clear();
        for (Element e : doc.getRootElement().getChild(PROFILES).getChildren()) {
            File pp = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
            try {
                Profile p = new Profile(pp);
                this.addProfile(p);
            } catch (FileNotFoundException ex) {
                log.info("Cataloged profile \"{}\" not in expected location\nSearching for it in {}", e.getAttributeValue(Profile.ID), pp.getParentFile());
                this.findProfiles(pp.getParentFile());
                reWrite = true;
            }
        }
        searchPaths.clear();
        for (Element e : doc.getRootElement().getChild(SEARCH_PATHS).getChildren()) {
            File path = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
            if (!searchPaths.contains(path)) {
                this.addSearchPath(path);
            }
            if (Boolean.parseBoolean(e.getAttributeValue(DEFAULT))) {
                this.defaultSearchPath = path;
            }
        }
        if (searchPaths.isEmpty()) {
            this.addSearchPath(FileUtil.getFile(FileUtil.getPreferencesPath()));
        }
        this.readingProfiles = false;
        if (reWrite) {
            this.writeProfiles();
        }
        this.profiles.sort(null);
    } catch (JDOMException | IOException ex) {
        this.readingProfiles = false;
        throw ex;
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File)

Example 54 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project JMRI by JMRI.

the class NodeIdentity method init.

private synchronized void init() {
    File identityFile = this.identityFile();
    if (identityFile.exists()) {
        try {
            Document doc = (new SAXBuilder()).build(identityFile);
            String id = doc.getRootElement().getChild(NODE_IDENTITY).getAttributeValue(NODE_IDENTITY);
            this.formerIdentities.clear();
            doc.getRootElement().getChild(FORMER_IDENTITIES).getChildren().stream().forEach((e) -> {
                this.formerIdentities.add(e.getAttributeValue(NODE_IDENTITY));
            });
            if (!this.validateIdentity(id)) {
                log.debug("Node identity {} is invalid. Generating new node identity.", id);
                this.formerIdentities.add(id);
                this.getIdentity(true);
            } else {
                this.getIdentity(true);
            }
        } catch (JDOMException | IOException ex) {
            log.error("Unable to read node identities: {}", ex.getLocalizedMessage());
            this.getIdentity(true);
        }
    } else {
        this.getIdentity(true);
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File)

Example 55 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project JMRI by JMRI.

the class DecoderFileTest method setupDecoder.

// provide a test document in the above static variables
public void setupDecoder() {
    // create a JDOM tree with just some elements
    root = new Element("decoder-config");
    doc = new Document(root);
    doc.setDocType(new DocType("decoder-config", "decoder-config.dtd"));
    // add some elements
    root.addContent(decoder = new Element("decoder").addContent(new Element("family").setAttribute("family", "DH142 etc").setAttribute("mfg", "Digitrax").setAttribute("defnVersion", "242").setAttribute("comment", "DH142 decoder: FX, transponding")).addContent(new Element("programming").setAttribute("direct", "byteOnly").setAttribute("paged", "yes").setAttribute("register", "yes").setAttribute("ops", "yes")).addContent(new Element("variables").addContent(new Element("variable").setAttribute("label", "Address").setAttribute("CV", "1").setAttribute("minFn", "4").setAttribute("mask", "VVVVVVVV").setAttribute("readOnly", "no").addContent(new Element("decVal").setAttribute("max", "127"))).addContent(new Element("variable").setAttribute("label", "Acceleration rate").setAttribute("CV", "3").setAttribute("minOut", "2").setAttribute("mask", "VVVVVVVV").setAttribute("readOnly", "no").addContent(new Element("decVal").setAttribute("max", "127"))).addContent(new Element("variable").setAttribute("label", "Normal direction of motion").setAttribute("CV", "29").setAttribute("minFn", "2").setAttribute("minOut", "5").setAttribute("mask", "XXXXXXXV").setAttribute("readOnly", "no").addContent(new Element("enumVal").addContent(new Element("enumChoice").setAttribute("choice", "forward")).addContent(new Element("enumChoice").setAttribute("choice", "reverse"))))));
    return;
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) DocType(org.jdom2.DocType)

Aggregations

Document (org.jdom2.Document)391 Element (org.jdom2.Element)243 Test (org.junit.Test)104 SAXBuilder (org.jdom2.input.SAXBuilder)84 IOException (java.io.IOException)72 File (java.io.File)56 XMLOutputter (org.jdom2.output.XMLOutputter)56 JDOMException (org.jdom2.JDOMException)44 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)34 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)25 DocType (org.jdom2.DocType)24 ArrayList (java.util.ArrayList)22 MCRContent (org.mycore.common.content.MCRContent)22 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)22 MCRException (org.mycore.common.MCRException)21 Document (com.google.cloud.language.v1.Document)20 HashMap (java.util.HashMap)19 Attribute (org.jdom2.Attribute)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)19 URL (java.net.URL)18