Search in sources :

Example 26 with Document

use of de.micromata.opengis.kml.v_2_2_0.Document in project JMRI by JMRI.

the class OperationsSetupXml method writeFile.

@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
    log.debug("writeFile {}", name);
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
        file = new File(name);
    }
    // create root element
    // NOI18N
    Element root = new Element("operations-config");
    // NOI18N
    Document doc = newDocument(root, dtdLocation + "operations-config.dtd");
    // add XSLT processing instruction
    java.util.Map<String, String> m = new java.util.HashMap<String, String>();
    // NOI18N
    m.put("type", "text/xsl");
    // NOI18N
    m.put("href", xsltLocation + "operations-config.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // add top-level elements         
    root.addContent(Setup.store());
    // add manifest header text strings
    root.addContent(TrainManifestHeaderText.store());
    // add manifest text strings
    root.addContent(TrainManifestText.store());
    // add switch list text strings
    root.addContent(TrainSwitchListText.store());
    // add control elements
    root.addContent(Control.store());
    writeXML(file, doc);
    // done, so can't be dirty
    setDirty(false);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 27 with Document

use of de.micromata.opengis.kml.v_2_2_0.Document in project JMRI by JMRI.

the class TrainManagerXml method writeFile.

@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
    log.debug("writeFile {}", name);
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
        file = new File(name);
    }
    // create root element
    // NOI18N
    Element root = new Element("operations-config");
    // NOI18N
    Document doc = newDocument(root, dtdLocation + "operations-trains.dtd");
    // add XSLT processing instruction
    java.util.Map<String, String> m = new java.util.HashMap<String, String>();
    // NOI18N
    m.put("type", "text/xsl");
    // NOI18N
    m.put("href", xsltLocation + "operations-trains.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    TrainManager.instance().store(root);
    TrainScheduleManager.instance().store(root);
    AutomationManager.instance().store(root);
    writeXML(file, doc);
    // done - train file now stored, so can't be dirty
    setDirty(false);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 28 with Document

use of de.micromata.opengis.kml.v_2_2_0.Document in project JMRI by JMRI.

the class VSDecoderPreferences method save.

public void save() {
    if (prefFile == null) {
        return;
    }
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(prefFile);
    File file = new File(prefFile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory, check result
            !parentDir.mkdir()) {
                log.error("failed to make parent directory");
            }
        }
        if (// create file, check result
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the new VSDecoder preferences file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("vsdecoder-preferences");
        //Document doc = XmlFile.newDocument(root, XmlFile.dtdLocation+"vsdecoder-preferences.dtd");
        Document doc = XmlFile.newDocument(root);
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.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+"throttles-preferences.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        root.setContent(store());
        xf.writeXML(file, doc);
    } catch (Exception ex) {
        // TODO fix null value for Attribute
        log.warn("Exception in storing vsdecoder preferences xml: " + ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 29 with Document

use of de.micromata.opengis.kml.v_2_2_0.Document in project JMRI by JMRI.

the class DefaultCatalogTreeManagerXml method writeCatalogTrees.

/*
     *  Writes out tree values to a file in the user's preferences directory
     */
public void writeCatalogTrees() throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("entered writeCatalogTreeValues");
    }
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    List<String> trees = manager.getSystemNameList();
    boolean found = false;
    Iterator<String> iter = manager.getSystemNameList().iterator();
    while (iter.hasNext()) {
        String sname = iter.next();
        CatalogTree tree = manager.getBySystemName(sname);
        if (log.isDebugEnabled()) {
            log.debug("Tree: sysName= " + sname + ", userName= " + tree.getUserName());
            CatalogTreeNode root = tree.getRoot();
            log.debug("enumerateTree called for root= " + root.toString() + ", has " + root.getChildCount() + " children");
            // root.depthFirstEnumeration isn't fully typed in JDOM2
            @SuppressWarnings("unchecked") Enumeration<CatalogTreeNode> e = root.depthFirstEnumeration();
            while (e.hasMoreElements()) {
                CatalogTreeNode n = e.nextElement();
                log.debug("nodeName= " + n.getUserObject() + " has " + n.getLeaves().size() + " leaves and " + n.getChildCount() + " subnodes.");
            }
        }
        if (sname != null && sname.charAt(1) == CatalogTree.XML) {
            found = true;
            break;
        }
    }
    if (found) {
        // there are trees defined, create root element
        Element root = new Element("catalogTrees");
        Document doc = newDocument(root, dtdLocation + "catalogTree.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/tree-values.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<>();
        m.put("type", "text/xsl");
        m.put("href", xsltLocation + "panelfile.xsl");
        org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        store(root, trees);
        try {
            if (!checkFile(DEFAULT_FILE_NAME)) {
                // file does not exist, create it
                File file = new File(DEFAULT_FILE_NAME);
                if (!file.createNewFile()) {
                    log.error("createNewFile failed");
                }
            }
            // write content to file
            writeXML(findFile(DEFAULT_FILE_NAME), doc);
            // memory consistent with file
            jmri.jmrit.catalog.ImageIndexEditor.indexChanged(false);
        } catch (java.io.IOException ioe) {
            log.error("IO Exception " + ioe);
            throw (ioe);
        }
    }
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode) CatalogTree(jmri.CatalogTree) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 30 with Document

use of de.micromata.opengis.kml.v_2_2_0.Document in project JMRI by JMRI.

the class DecoderIndexFile method writeFile.

public void writeFile(String name, DecoderIndexFile oldIndex, String[] files) throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("writeFile " + name);
    }
    // This is taken in large part from "Java and XML" page 368
    File file = new File(FileUtil.getUserFilesPath() + name);
    // create root element and document
    Element root = new Element("decoderIndex-config");
    root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/decoder.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    Document doc = newDocument(root);
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/DecoderID.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "DecoderID.xsl");
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // add top-level elements
    Element index;
    root.addContent(index = new Element("decoderIndex"));
    index.setAttribute("version", Integer.toString(fileVersion));
    log.debug("version written to file as " + fileVersion);
    // add mfg list from existing DecoderIndexFile item
    Element mfgList = new Element("mfgList");
    // copy dates from original mfgList element
    if (oldIndex.nmraListDate != null) {
        mfgList.setAttribute("nmraListDate", oldIndex.nmraListDate);
    }
    if (oldIndex.updated != null) {
        mfgList.setAttribute("updated", oldIndex.updated);
    }
    if (oldIndex.lastAdd != null) {
        mfgList.setAttribute("lastadd", oldIndex.lastAdd);
    }
    // We treat "NMRA" special...
    Element mfg = new Element("manufacturer");
    mfg.setAttribute("mfg", "NMRA");
    mfg.setAttribute("mfgID", "999");
    mfgList.addContent(mfg);
    // start working on the rest of the entries
    List<String> keys = new ArrayList<>(oldIndex._mfgIdFromNameHash.keySet());
    Collections.sort(keys);
    for (Object item : keys) {
        String mfgName = (String) item;
        if (!mfgName.equals("NMRA")) {
            mfg = new Element("manufacturer");
            mfg.setAttribute("mfg", mfgName);
            mfg.setAttribute("mfgID", oldIndex._mfgIdFromNameHash.get(mfgName));
            mfgList.addContent(mfg);
        }
    }
    // add family list by scanning files
    Element familyList = new Element("familyList");
    for (String fileName : files) {
        DecoderFile d = new DecoderFile();
        try {
            Element droot = d.rootFromName(DecoderFile.fileLocation + fileName);
            Element family = droot.getChild("decoder").getChild("family").clone();
            family.setAttribute("file", fileName);
            familyList.addContent(family);
        } catch (org.jdom2.JDOMException exj) {
            log.error("could not parse " + fileName + ": " + exj.getMessage());
        } catch (java.io.FileNotFoundException exj) {
            log.error("could not read " + fileName + ": " + exj.getMessage());
        } catch (IOException exj) {
            log.error("other exception while dealing with " + fileName + ": " + exj.getMessage());
        }
    }
    index.addContent(mfgList);
    index.addContent(familyList);
    writeXML(file, doc);
    // force a read of the new file next time
    resetInstance();
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Aggregations

Document (org.jdom2.Document)86 Element (org.jdom2.Element)57 File (java.io.File)29 Test (org.junit.Test)24 DocType (org.jdom2.DocType)23 SAXBuilder (org.jdom2.input.SAXBuilder)20 IOException (java.io.IOException)16 XMLOutputter (org.jdom2.output.XMLOutputter)14 ProcessingInstruction (org.jdom2.ProcessingInstruction)13 XmlFile (jmri.jmrit.XmlFile)11 Document (com.google.cloud.language.v1beta2.Document)10 ApiException (com.google.api.gax.grpc.ApiException)9 Document (com.google.cloud.language.v1.Document)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 EncodingType (com.google.cloud.language.v1beta2.EncodingType)8 FileOutputStream (java.io.FileOutputStream)7 EncodingType (com.google.cloud.language.v1.EncodingType)6 FileNotFoundException (java.io.FileNotFoundException)6 JDOMException (org.jdom2.JDOMException)6