Search in sources :

Example 41 with Document

use of io.atlasmap.v2.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 42 with Document

use of io.atlasmap.v2.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 43 with Document

use of io.atlasmap.v2.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)

Example 44 with Document

use of io.atlasmap.v2.Document in project JMRI by JMRI.

the class DecoderIndexFileTest method setupDoc.

// provide a test document in the above static variables
void setupDoc() {
    // create a JDOM tree with just some elements
    root = new Element("decoderIndex-config");
    doc = new Document(root);
    doc.setDocType(new DocType("decoderIndex-config", "decoderIndex-config.dtd"));
    // add some elements
    root.addContent(decoderIndexElement = new Element("decoderIndex").addContent(new Element("mfgList").addContent(new Element("manufacturer").setAttribute("mfg", "NMRA")).addContent(new Element("manufacturer").setAttribute("mfg", "Digitrax").setAttribute("mfgID", "129"))).addContent(new Element("familyList").addContent(family1 = new Element("family").setAttribute("mfg", "NMRA").setAttribute("name", "NMRA S&RP definitions").setAttribute("file", "NMRA.xml").addContent(new Element("model").setAttribute("model", "full set").setAttribute("comment", "all CVs in RP 9.2.1")).addContent(new Element("model").setAttribute("model", "required set").setAttribute("comment", "required CVs in RP 9.2.1"))).addContent(family2 = new Element("family").setAttribute("mfg", "Digitrax").setAttribute("name", "FX2 family").setAttribute("file", "DH142.xml").addContent(new Element("model").setAttribute("model", "DH142").setAttribute("numFns", "4").setAttribute("numOuts", "2").setAttribute("lowVersionID", "21")).addContent(new Element("model").setAttribute("model", "DN142").setAttribute("numFns", "5").setAttribute("numOuts", "1").addContent(new Element("versionCV").setAttribute("lowVersionID", "22").setAttribute("highVersionID", "24"))))));
    return;
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) DocType(org.jdom2.DocType)

Example 45 with Document

use of io.atlasmap.v2.Document in project JMRI by JMRI.

the class RouteManagerXml 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-routes.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-routes.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    RouteManager.instance().store(root);
    writeXML(file, doc);
    // done - route 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)

Aggregations

Document (org.jdom2.Document)403 Element (org.jdom2.Element)248 Test (org.junit.Test)111 SAXBuilder (org.jdom2.input.SAXBuilder)95 IOException (java.io.IOException)74 File (java.io.File)58 XMLOutputter (org.jdom2.output.XMLOutputter)55 JDOMException (org.jdom2.JDOMException)44 Field (io.atlasmap.v2.Field)40 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)34 ArrayList (java.util.ArrayList)30 Test (org.junit.jupiter.api.Test)27 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)25 DocType (org.jdom2.DocType)24 InputStream (java.io.InputStream)23 JsonField (io.atlasmap.json.v2.JsonField)22 MCRContent (org.mycore.common.content.MCRContent)22 Document (com.google.cloud.language.v1.Document)21 HashMap (java.util.HashMap)21 MCRException (org.mycore.common.MCRException)21