Search in sources :

Example 46 with Document

use of org.jdom2.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 47 with Document

use of org.jdom2.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 48 with Document

use of org.jdom2.Document in project JMRI by JMRI.

the class XmlFileTest method testValidationControl.

public void testValidationControl() {
    final String docTypeValid = "<!DOCTYPE decoderIndex-config SYSTEM \"decoderIndex-config.dtd\">";
    final String docTypeInvalid = "<!DOCTYPE layout-config SYSTEM \"layout-config-2-5-4.dtd\">";
    final String contentSchemaValid = "<decoderIndex-config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://jmri.org/xml/schema/decoder.xsd\">";
    final String contentSchemaInvalid = "<decoderIndex-config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://jmri.org/xml/schema/layout-2-9-6.xsd \">";
    final String contentSchemaAbsent = "<decoderIndex-config>";
    final String ending = "<decoderIndex><mfgList nmraListDate=\"\" updated=\"\"><manufacturer mfg=\"\"/></mfgList><familyList><family mfg=\"\" name=\"\" file=\"\"/></familyList></decoderIndex></decoderIndex-config>";
    for (XmlFile.Validate validate : XmlFile.Validate.values()) {
        for (Type theDTD : Type.values()) {
            for (Type theSchema : Type.values()) {
                boolean passes = true;
                boolean checkDTD = (validate == XmlFile.Validate.CheckDtd) || (validate == XmlFile.Validate.CheckDtdThenSchema);
                boolean checkSchema = (validate == XmlFile.Validate.RequireSchema) || (validate == XmlFile.Validate.CheckDtdThenSchema);
                // Cannot find the declaration of element 'decoderIndex-config'.
                if (theSchema == Type.INVALID && checkSchema)
                    passes = false;
                // Cannot find the declaration of element 'decoderIndex-config'.
                if (theSchema == Type.ABSENT && checkSchema)
                    passes = false;
                // Document root element "decoderIndex-config", must match DOCTYPE root "layout-config".
                if (theDTD == Type.INVALID && checkDTD)
                    passes = false;
                // but if you're checking both
                if (checkDTD && checkSchema) {
                    // a pass is a pass
                    if (theDTD == Type.VALID)
                        passes = true;
                    if (theSchema == Type.VALID)
                        passes = true;
                    // but a DTD fail is a fail
                    if (theDTD == Type.INVALID)
                        passes = false;
                }
                // create input
                String content = "";
                if (theDTD == Type.VALID)
                    content += docTypeValid;
                if (theDTD == Type.INVALID)
                    content += docTypeInvalid;
                if (theSchema == Type.VALID)
                    content += contentSchemaValid;
                if (theSchema == Type.INVALID)
                    content += contentSchemaInvalid;
                if (theSchema == Type.ABSENT)
                    content += contentSchemaAbsent;
                content += ending;
                boolean result = false;
                try {
                    XmlFile xf = new XmlFile() {

                        {
                            warned = false;
                        }
                    };
                    // odd syntax is due to XmlFile being abstract
                    xf.setValidate(validate);
                    xf.rootFromInputStream(new java.io.ByteArrayInputStream(content.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
                    result = true;
                } catch (Exception ex) {
                    result = false;
                    log.debug(ex.toString());
                }
                log.debug("DTD: " + theDTD + " SCHEMA: " + theSchema + " (" + validate + ") expects " + passes + " was " + result + (passes != result ? " !!!!!!!!!!!!!!!!!!!!!!!!!" : ""));
                Assert.assertEquals("DTD: " + theDTD + " SCHEMA: " + theSchema + " (" + validate + ")", passes, result);
            }
        }
    }
}
Also used : DocType(org.jdom2.DocType)

Example 49 with Document

use of org.jdom2.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 50 with Document

use of org.jdom2.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)

Aggregations

Document (org.jdom2.Document)86 Element (org.jdom2.Element)76 Test (org.junit.Test)33 File (java.io.File)29 DocType (org.jdom2.DocType)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 IOException (java.io.IOException)16 XMLOutputter (org.jdom2.output.XMLOutputter)15 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 ArrayList (java.util.ArrayList)9 EncodingType (com.google.cloud.language.v1beta2.EncodingType)8 FileOutputStream (java.io.FileOutputStream)8 JLabel (javax.swing.JLabel)7 EncodingType (com.google.cloud.language.v1.EncodingType)6