Search in sources :

Example 41 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 42 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 43 with SAXBuilder

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

the class Initiative method loadINIT.

/**
	 * Perform initial loading
	 * @param initFile
	 * @param comp
	 */
public void loadINIT(File initFile, PCGenMessageHandler comp) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document character = builder.build(initFile);
        loadFromDocument(character, comp);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), "File load error: " + initFile.getName());
        Logging.errorPrint("File Load Error" + initFile.getName());
        Logging.errorPrint(e.getMessage(), e);
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Document(org.jdom2.Document) IOException(java.io.IOException)

Example 44 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 45 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)43 Document (org.jdom2.Document)21 Test (org.junit.Test)19 File (java.io.File)9 IOException (java.io.IOException)8 Element (org.jdom2.Element)8 Modification (com.thoughtworks.go.domain.materials.Modification)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 BufferedInputStream (java.io.BufferedInputStream)5 ParseException (java.text.ParseException)5 FileInputStream (java.io.FileInputStream)4 StringReader (java.io.StringReader)4 ModifiedFile (com.thoughtworks.go.domain.materials.ModifiedFile)3 InputStream (java.io.InputStream)3 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 DocType (org.jdom2.DocType)3 JDOMException (org.jdom2.JDOMException)3 XmlUtils.buildXmlDocument (com.thoughtworks.go.util.XmlUtils.buildXmlDocument)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2