Search in sources :

Example 61 with Document

use of iso.std.iso._20022.tech.xsd.pain_001_001.Document 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 62 with Document

use of iso.std.iso._20022.tech.xsd.pain_001_001.Document 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)

Example 63 with Document

use of iso.std.iso._20022.tech.xsd.pain_001_001.Document in project JMRI by JMRI.

the class EngineManagerXml 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-engines.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-engines.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    EngineModels.instance().store(root);
    EngineTypes.instance().store(root);
    EngineLengths.instance().store(root);
    EngineManager.instance().store(root);
    writeXML(file, doc);
    // done - engine 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 64 with Document

use of iso.std.iso._20022.tech.xsd.pain_001_001.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 65 with Document

use of iso.std.iso._20022.tech.xsd.pain_001_001.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)

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