Search in sources :

Example 31 with JDOMException

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

the class NceConsistRoster method readFile.

/**
     * Read the contents of a roster XML file into this object. Note that this
     * does not clear any existing entries.
     * @param name file name for consist roster
     * @throws org.jdom2.JDOMException other errors
     * @throws java.io.IOException error accessing file
     */
void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // find root
    Element root = rootFromName(name);
    if (root == null) {
        log.debug("ConsistRoster file could not be read");
        return;
    }
    // decode type, invoke proper processing routine if a decoder file
    if (root.getChild("roster") != null) {
        List<Element> l = root.getChild("roster").getChildren("consist");
        if (log.isDebugEnabled()) {
            log.debug("readFile sees " + l.size() + " children");
        }
        for (int i = 0; i < l.size(); i++) {
            addEntry(new NceConsistRosterEntry(l.get(i)));
        }
        //any <?p?> processor directives and change them to back \n characters
        for (int i = 0; i < numEntries(); i++) {
            //Get a RosterEntry object for this index
            NceConsistRosterEntry r = _list.get(i);
            //Extract the Comment field and create a new string for output
            String tempComment = r.getComment();
            StringBuffer buf = new StringBuffer();
            //characters in tempComment.
            for (int k = 0; k < tempComment.length(); k++) {
                if (tempComment.startsWith("<?p?>", k)) {
                    buf.append("\n");
                    k = k + 4;
                } else {
                    buf.append(tempComment.substring(k, k + 1));
                }
            }
            r.setComment(buf.toString());
        }
    } else {
        log.error("Unrecognized ConsistRoster file contents in file: " + name);
    }
}
Also used : Element(org.jdom2.Element)

Example 32 with JDOMException

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

the class XmlFile method processOneInstruction.

Document processOneInstruction(ProcessingInstruction p, Document doc) throws org.jdom2.transform.XSLTransformException, org.jdom2.JDOMException, java.io.IOException {
    log.trace("handling ", p);
    // check target
    String target = p.getTarget();
    if (!target.equals("transform-xslt")) {
        return doc;
    }
    String href = p.getPseudoAttributeValue("href");
    // we expect this to start with http://jmri.org/ and refer to the JMRI file tree
    if (!href.startsWith("http://jmri.org/")) {
        return doc;
    }
    href = href.substring(16);
    // if starts with 'xml/' we remove that; findFile will put it back
    if (href.startsWith("xml/")) {
        href = href.substring(4);
    }
    // read the XSLT transform into a Document to get XInclude done
    SAXBuilder builder = getBuilder(Validate.None);
    Document xdoc = builder.build(new BufferedInputStream(new FileInputStream(findFile(href))));
    org.jdom2.transform.XSLTransformer transformer = new org.jdom2.transform.XSLTransformer(xdoc);
    return transformer.transform(doc);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) Document(org.jdom2.Document) FileInputStream(java.io.FileInputStream)

Example 33 with JDOMException

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

the class XmlFile method getRoot.

/**
     * Get the root element from an XML document in a stream.
     *
     * @param stream input containing the XML document
     * @return the root element of the XML document
     * @throws org.jdom2.JDOMException if the XML document is invalid
     * @throws java.io.IOException     if the input cannot be read
     */
protected Element getRoot(InputStream stream) throws JDOMException, IOException {
    log.trace("getRoot from stream");
    SAXBuilder builder = getBuilder(getValidate());
    Document doc = builder.build(new BufferedInputStream(stream));
    // handle any process instructions
    doc = processInstructions(doc);
    // find root
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) Document(org.jdom2.Document)

Example 34 with JDOMException

use of org.jdom2.JDOMException 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 35 with JDOMException

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

the class XmlFile method getRoot.

/**
     * Get the root element from an XML document in a Reader.
     *
     * Runs through a BufferedReader for increased performance.
     *
     *
     * @param verifySchema true if the XML document should be validated against
     *                     its schema
     * @param verifyDTD    true if the XML document should be validated against
     *                     its DTD
     * @param reader       input containing the XML document
     * @return the root element of the XML document
     * @throws org.jdom2.JDOMException if the XML document is invalid
     * @throws java.io.IOException     if the input cannot be read
     * @since 3.1.5
     * @deprecated 4.7.2 use setVerifySchema, setVerifyDTD methods
     */
@Deprecated
protected Element getRoot(boolean verifySchema, boolean verifyDTD, InputStreamReader reader) throws JDOMException, IOException {
    warnDeprecated();
    log.trace("getRoot from reader with encoding {}", reader.getEncoding());
    // argument controls validation
    SAXBuilder builder = getBuilder(getValidate());
    Document doc = builder.build(new BufferedReader(reader));
    // handle any process instructions
    doc = processInstructions(doc);
    // find root
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedReader(java.io.BufferedReader) Document(org.jdom2.Document)

Aggregations

Element (org.jdom2.Element)154 Document (org.jdom2.Document)113 JDOMException (org.jdom2.JDOMException)89 IOException (java.io.IOException)75 SAXBuilder (org.jdom2.input.SAXBuilder)67 Test (org.junit.Test)36 File (java.io.File)32 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 Attribute (org.jdom2.Attribute)16 StringReader (java.io.StringReader)15 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 SAXException (org.xml.sax.SAXException)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10