Example usage for org.dom4j.io XMLWriter close

List of usage examples for org.dom4j.io XMLWriter close

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying Writer

Usage

From source file:de.awtools.xml.XMLUtils.java

License:Open Source License

/**
 * Schliesst sicher einen <code>XMLWriter</code>.
 *
 * @param writer Ein <code>XMLWriter</code>
 *///from   w  w w  . j a va  2 s .  c  om
public static void close(final XMLWriter writer) {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException ex) {
            // ignored
        }
    }
}

From source file:de.innovationgate.utils.WGUtils.java

License:Apache License

/**
 * Creates a directory link file pointing to a target path
 * @param parentDir The directory to contain the link file
 * @param target The target path that the directory link should point to
 * @throws IOException// ww w  .j  a v  a 2s  . com
 */
public static void createDirLink(File parentDir, String target) throws IOException {
    File link = new File(parentDir, DIRLINK_FILE);
    Document doc = DocumentFactory.getInstance().createDocument();
    Element dirlink = doc.addElement("dirlink");
    Element path = dirlink.addElement("path");
    path.addAttribute("location", target);
    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
    writer.setOutputStream(new FileOutputStream(link));
    writer.write(doc);
    writer.close();
}

From source file:de.innovationgate.wgpublisher.lucene.LuceneIndexConfiguration.java

License:Open Source License

/**
 * writes the current configuration to file
 * @throws IOException/*from  ww w  .  ja v  a  2 s . co m*/
 */
private void writeConfigDoc() throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(_configFile), format);
    writer.write(_configDoc);
    writer.flush();
    writer.close();
}

From source file:de.innovationgate.wgpublisher.WGACore.java

License:Open Source License

public String updateConfigDocument(Document newConfig) throws UnsupportedEncodingException,
        FileNotFoundException, IOException, ParserConfigurationException, DocumentException {
    String newTimestamp = WGACore.DATEFORMAT_GMT.format(new Date());
    newConfig.getRootElement().addAttribute("timestamp", newTimestamp);

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setTrimText(true);//from w  ww . ja v  a2 s. c o m
    outputFormat.setNewlines(true);

    XMLWriter writer = new XMLWriter(new FileOutputStream(getConfigFile()), outputFormat);
    writer.write(newConfig);
    writer.close();
    return newTimestamp;
}

From source file:de.matzefratze123.heavyspleef.persistence.handler.CachingReadWriteHandler.java

License:Open Source License

@Override
public void saveGame(Game game) throws IOException {
    File gameFile = new File(xmlFolder, game.getName() + ".xml");
    if (!gameFile.exists()) {
        gameFile.createNewFile();//from w w  w.  j a v  a2s  .  c  o m
    }

    Document document = DocumentHelper.createDocument();
    Element rootElement = document.addElement("game");

    xmlContext.write(game, rootElement);

    File gameSchematicFolder = new File(schematicFolder, game.getName());
    if (!gameSchematicFolder.exists()) {
        gameSchematicFolder.mkdir();
    }

    XMLWriter writer = null;

    try {
        FileOutputStream out = new FileOutputStream(gameFile);

        writer = new XMLWriter(out, xmlOutputFormat);
        writer.write(document);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

    for (Floor floor : game.getFloors()) {
        File floorFile = new File(gameSchematicFolder, getFloorFileName(floor));
        if (!floorFile.exists()) {
            floorFile.createNewFile();
        }

        schematicContext.write(floorFile, floor);
    }
}

From source file:de.thischwa.pmcms.view.renderer.VelocityUtils.java

License:LGPL

/**
 * Replace the img-tag and a-tag with the equivalent velocity macro. Mainly used before saving a field value to the database.
 * /*from   w ww .  ja  v  a2 s.  co m*/
 * @throws RenderingException
 *             If any exception was thrown while replacing the tags.
 */
@SuppressWarnings("unchecked")
public static String replaceTags(final Site site, final String oldValue) throws RenderingException {
    if (StringUtils.isBlank(oldValue))
        return null;

    // 1. add a root element (to have a proper xml) and replace the ampersand
    String newValue = String.format("<dummytag>\n%s\n</dummytag>",
            StringUtils.replace(oldValue, "&", ampReplacer));
    Map<String, String> replacements = new HashMap<String, String>();

    try {
        Document dom = DocumentHelper.parseText(newValue);
        dom.setXMLEncoding(Constants.STANDARD_ENCODING);

        // 2. Collect the keys, identify the img-tags.
        List<Node> imgs = dom.selectNodes("//img", ".");
        for (Node node : imgs) {
            Element element = (Element) node;
            if (element.attributeValue("src").startsWith("/")) // only internal links have to replaced with a velocity macro
                replacements.put(node.asXML(),
                        generateVelocityImageToolCall(site, element.attributeIterator()));
        }

        // 3. Collect the keys, identify the a-tags
        List<Node> links = dom.selectNodes("//a", ".");
        for (Node node : links) {
            Element element = (Element) node;
            if (element.attributeValue("href").startsWith("/")) // only internal links have to replaced with a velocity macro
                replacements.put(element.asXML(), generateVelocityLinkToolCall(site, element));
        }

        // 4. Replace the tags with the velomacro.
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, sourceFormat);
        writer.write(dom.selectSingleNode("dummytag"));
        writer.close();
        newValue = stringWriter.toString();
        for (String stringToReplace : replacements.keySet())
            newValue = StringUtils.replace(newValue, stringToReplace, replacements.get(stringToReplace));
        newValue = StringUtils.replace(newValue, "<dummytag>", "");
        newValue = StringUtils.replace(newValue, "</dummytag>", "");

    } catch (Exception e) {
        throw new RenderingException("While preprocessing the field value: " + e.getMessage(), e);
    }

    return StringUtils.replace(newValue, ampReplacer, "&");
}

From source file:de.tud.kom.p2psim.impl.network.gnp.topology.HostMap.java

License:Open Source License

/**
 * saves host postion location, GNP position, groups, GNP Space, PingER
 * Lookup table in an xml file used within the simulation
 * //w w  w  .  j  a va2 s  .  co m
 * @param file
 */
public void exportToXml(File file) {
    log.debug("Export Hosts to an XML File");
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(getDocument());
        writer.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:de.tud.kom.p2psim.impl.vis.util.Config.java

License:Open Source License

/**
 * Schreibt die bestehende XML-Struktur in die Config-Datei
 *///from w ww  .  j av  a  2s.c  om
public static void writeXMLFile() {

    if (config != null) { // Schreibe Datei nur, wenn XML-Baum im Speicher
        // vorhanden
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter(configFile), format);
            writer.write(Config.config);
            writer.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:de.xaniox.heavyspleef.migration.GameMigrator.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w ww.j  a  va 2 s  .c o  m
public void migrate(Configuration inputSource, File outputFolder, Object cookie) throws MigrationException {
    if (cookie == null || !(cookie instanceof List<?>)) {
        throw new MigrationException("Cookie must be a game of lists");
    }

    countMigrated = 0;

    List<Game> gameList = (List<Game>) cookie;
    Set<String> gameNames = inputSource.getKeys(false);

    for (String name : gameNames) {
        ConfigurationSection section = inputSource.getConfigurationSection(name);

        File xmlFile = new File(outputFolder, name + FILE_EXTENSION);
        if (xmlFile.exists()) {
            //Rename this game as there is already a file
            xmlFile = new File(outputFolder, name + "_1" + FILE_EXTENSION);
        }

        XMLWriter writer = null;

        try {
            xmlFile.createNewFile();

            GameAccessor accessor = new GameAccessor(heavySpleef);
            Document document = DocumentHelper.createDocument();
            Element rootElement = document.addElement("game");

            Game game = migrateGame(section, rootElement);
            if (game == null) {
                continue;
            }

            accessor.write(game, rootElement);
            gameList.add(game);

            OutputStream out = new FileOutputStream(xmlFile);
            writer = new XMLWriter(out, outputFormat);
            writer.write(document);
            ++countMigrated;
        } catch (IOException e) {
            throw new MigrationException(e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:de.xaniox.heavyspleef.persistence.handler.CachingReadWriteHandler.java

License:Open Source License

@Override
public void saveGame(Game game) throws IOException {
    File gameFile = new File(xmlFolder, game.getName() + ".xml");
    if (!gameFile.exists()) {
        gameFile.createNewFile();/*from w w  w. j ava 2 s .  co  m*/
    }

    Document document = DocumentHelper.createDocument();
    Element rootElement = document.addElement("game");

    xmlContext.write(game, rootElement);

    File gameSchematicFolder = new File(schematicFolder, game.getName());
    if (!gameSchematicFolder.exists()) {
        gameSchematicFolder.mkdir();
    }

    XMLWriter writer = null;

    try {
        FileOutputStream out = new FileOutputStream(gameFile);

        writer = new XMLWriter(out, xmlOutputFormat);
        writer.write(document);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

    for (File file : gameSchematicFolder.listFiles(FLOOR_SCHEMATIC_FILTER)) {
        String floorName = file.getName().substring(2, file.getName().length() - 6);
        if (game.isFloorPresent(floorName)) {
            continue;
        }

        file.delete();
    }

    for (Floor floor : game.getFloors()) {
        File floorFile = new File(gameSchematicFolder, getFloorFileName(floor));
        if (!floorFile.exists()) {
            floorFile.createNewFile();
        }

        schematicContext.write(floorFile, floor);
    }
}