Search in sources :

Example 76 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.

the class XmlFile method getBuilder.

public static SAXBuilder getBuilder(Validate validate) {
    // should really be a Verify enum
    SAXBuilder builder;
    boolean verifyDTD = (validate == Validate.CheckDtd) || (validate == Validate.CheckDtdThenSchema);
    boolean verifySchema = (validate == Validate.RequireSchema) || (validate == Validate.CheckDtdThenSchema);
    // old style 
    // argument controls DTD validation
    builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", verifyDTD);
    // insert local resolver for includes, schema, DTDs
    builder.setEntityResolver(new JmriLocalEntityResolver());
    // configure XInclude handling
    builder.setFeature("http://apache.org/xml/features/xinclude", true);
    builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
    // only validate if grammar is available, making ABSENT OK
    builder.setFeature("http://apache.org/xml/features/validation/dynamic", verifyDTD && !verifySchema);
    // control Schema validation
    builder.setFeature("http://apache.org/xml/features/validation/schema", verifySchema);
    builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", verifySchema);
    // if not validating DTD, just validate Schema
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", verifyDTD);
    if (!verifyDTD) {
        builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
    }
    // allow Java character encodings
    builder.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
    return builder;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) JmriLocalEntityResolver(jmri.util.JmriLocalEntityResolver)

Example 77 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.

the class XmlFileTest method testProcessPI.

public void testProcessPI() throws org.jdom2.JDOMException, java.io.IOException {
    // Document from test file
    Document doc;
    Element e;
    FileInputStream fs = new FileInputStream(new File("java/test/jmri/jmrit/XmlFileTest_PI.xml"));
    try {
        // argument controls validation
        SAXBuilder builder = XmlFile.getBuilder(XmlFile.Validate.None);
        doc = builder.build(new BufferedInputStream(fs));
        Assert.assertNotNull("Original Document found", doc);
        e = doc.getRootElement();
        Assert.assertNotNull("Original root element found", e);
        XmlFile x = new XmlFile() {
        };
        Document d = x.processInstructions(doc);
        Assert.assertNotNull(d);
        // test transform changes <contains> element to <content>
        e = d.getRootElement();
        Assert.assertNotNull("Transformed root element found", e);
        Assert.assertTrue("Transformed root element is right type", e.getName().equals("top"));
        Assert.assertTrue("Old element gone", e.getChild("contains") == null);
        Assert.assertTrue("New element there", e.getChild("content") != null);
        Assert.assertTrue("New element has content", e.getChild("content").getChildren().size() == 2);
    } catch (java.io.IOException ex) {
        throw ex;
    } catch (org.jdom2.JDOMException ex) {
        throw ex;
    } finally {
        fs.close();
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 78 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.

the class ProfileManager method export.

/**
     * Export the {@link jmri.profile.Profile} to a zip file.
     *
     * @param profile                 The profile to export
     * @param target                  The file to export the profile into
     * @param exportExternalUserFiles If the User Files are not within the
     *                                profile directory, should they be
     *                                included?
     * @param exportExternalRoster    It the roster is not within the profile
     *                                directory, should it be included?
     * @throws java.io.IOException     if unable to write a file during the
     *                                 export
     * @throws org.jdom2.JDOMException if unable to create a new profile
     *                                 configuration file in the exported
     *                                 Profile
     */
public void export(Profile profile, File target, boolean exportExternalUserFiles, boolean exportExternalRoster) throws IOException, JDOMException {
    if (!target.exists() && !target.createNewFile()) {
        throw new IOException("Unable to create file " + target);
    }
    // NOI18N
    String tempDirPath = System.getProperty("java.io.tmpdir") + File.separator + "JMRI" + System.currentTimeMillis();
    FileUtil.createDirectory(tempDirPath);
    File tempDir = new File(tempDirPath);
    File tempProfilePath = new File(tempDir, profile.getPath().getName());
    FileUtil.copy(profile.getPath(), tempProfilePath);
    // NOI18N
    File config = new File(tempProfilePath, "ProfileConfig.xml");
    Document doc = (new SAXBuilder()).build(config);
    if (exportExternalUserFiles) {
        FileUtil.copy(new File(FileUtil.getUserFilesPath()), tempProfilePath);
        // NOI18N
        Element fileLocations = doc.getRootElement().getChild("fileLocations");
        for (Element fl : fileLocations.getChildren()) {
            if (fl.getAttribute("defaultUserLocation") != null) {
                // NOI18N
                // NOI18N
                fl.setAttribute("defaultUserLocation", "profile:");
            }
        }
    }
    if (exportExternalRoster) {
        // NOI18N
        FileUtil.copy(new File(Roster.getDefault().getRosterIndexPath()), new File(tempProfilePath, "roster.xml"));
        // NOI18N
        FileUtil.copy(new File(Roster.getDefault().getRosterLocation(), "roster"), new File(tempProfilePath, "roster"));
        // NOI18N
        Element roster = doc.getRootElement().getChild("roster");
        // NOI18N
        roster.removeAttribute("directory");
    }
    if (exportExternalUserFiles || exportExternalRoster) {
        try (FileWriter fw = new FileWriter(config)) {
            XMLOutputter fmt = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
            fmt.output(doc, fw);
        }
    }
    try (FileOutputStream out = new FileOutputStream(target);
        ZipOutputStream zip = new ZipOutputStream(out)) {
        this.exportDirectory(zip, tempProfilePath, tempProfilePath.getPath());
    }
    FileUtil.delete(tempDir);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) ZipOutputStream(java.util.zip.ZipOutputStream) Element(org.jdom2.Element) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Document(org.jdom2.Document) File(java.io.File)

Example 79 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.

the class DiceBagModel method loadFromFile.

/**
	 * <p>Loads the data from the specified file.  The document must be a valid
	 * xml document with the following form:</p>
	 * <p>
	 * <code>
	 * <dice-bag name="[Some name]">
	 *    <dice-roll>[a dice expression]</dice-roll>
	 * </dice-bag>
	 * </code>
	 * </p>
	 *
	 * @param file The file to open from.
	 */
private void loadFromFile(File file) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(file);
        m_filePath = file.getPath();
        loadFromDocument(doc);
        m_changed = false;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(GMGenSystem.inst, "File load error: " + file.getName());
        Logging.errorPrint("File Load Error" + file.getName());
        Logging.errorPrint(e.getMessage(), e);
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Document(org.jdom2.Document)

Example 80 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.

the class TravelMethodFactory method load.

// ### Factory methods ###
public static Vector<TravelMethod> load(File datadir) {
    //Create a new list for the travel methods
    Vector<TravelMethod> tms = new Vector<>();
    File path = new File(datadir, DIR_TRAVELMETHODS);
    if (path.isDirectory()) {
        File[] dataFiles = path.listFiles(new XMLFilter());
        SAXBuilder builder = new SAXBuilder();
        for (int i = 0; i < dataFiles.length; i++) {
            try {
                Document methodSet = builder.build(dataFiles[i]);
                DocType dt = methodSet.getDocType();
                if (dt.getElementName().equals(XML_ELEMENT_TRAVEL)) {
                    //Do work here
                    TravelMethod tm = TravelMethodFactory.create(methodSet);
                    tms.add(tm);
                }
            } catch (Exception e) {
                Logging.errorPrint(e.getMessage(), e);
            }
        }
    } else {
        //$NON-NLS-1$
        Logging.errorPrintLocalised("in_plugin_overland_noDatafile", path.getPath());
    }
    return tms;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) XMLFilter(plugin.overland.gui.XMLFilter) Document(org.jdom2.Document) Vector(java.util.Vector) File(java.io.File) DocType(org.jdom2.DocType) ParseException(java.text.ParseException)

Aggregations

SAXBuilder (org.jdom2.input.SAXBuilder)134 Document (org.jdom2.Document)94 Element (org.jdom2.Element)55 IOException (java.io.IOException)33 JDOMException (org.jdom2.JDOMException)27 File (java.io.File)20 StringReader (java.io.StringReader)20 Test (org.junit.jupiter.api.Test)18 InputStream (java.io.InputStream)14 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)11 URL (java.net.URL)9 XMLOutputter (org.jdom2.output.XMLOutputter)8 Modification (com.thoughtworks.go.domain.materials.Modification)7 List (java.util.List)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputSource (org.xml.sax.InputSource)6 ParseException (java.text.ParseException)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 BufferedInputStream (java.io.BufferedInputStream)4