Example usage for org.dom4j.io XMLWriter setOutputStream

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

Introduction

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

Prototype

public void setOutputStream(OutputStream out) throws UnsupportedEncodingException 

Source Link

Usage

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/* w ww  . ja v  a 2 s. 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:fr.gouv.culture.vitam.digest.DigestCompute.java

License:Open Source License

public static int createDigest(File src, File dst, File ftar, File fglobal, boolean oneDigestPerFile,
        List<File> filesToScan) {
    try {/*from  w  w  w  .j  a v a 2s  .c o  m*/
        Element global = null;
        Document globalDoc = null;
        if (fglobal != null) {
            global = XmlDom.factory.createElement("digests");
            global.addAttribute("source", src.getAbsolutePath());
            globalDoc = XmlDom.factory.createDocument(global);
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(StaticValues.CURRENT_OUTPUT_ENCODING);
        XMLWriter writer = new XMLWriter(format);
        int error = 0;
        int currank = 0;
        for (File file : filesToScan) {
            currank++;
            Element result = DigestCompute.checkDigest(StaticValues.config, src, file);
            if (result.selectSingleNode(".[@status='ok']") == null) {
                System.err.println(StaticValues.LBL.error_error.get()
                        + StaticValues.LBL.error_computedigest.get() + StaticValues.getSubPath(file, src));
                error++;
            } else if (oneDigestPerFile) {
                Element rootElement = XmlDom.factory.createElement("digest");
                Document unique = XmlDom.factory.createDocument(rootElement);
                rootElement.add(result);
                FileOutputStream out = null;
                String shortname = StaticValues.getSubPath(file, src);
                String shortnameWithoutFilename = shortname.substring(0, shortname.lastIndexOf(file.getName()));
                File fdirout = new File(dst, shortnameWithoutFilename);
                fdirout.mkdirs();
                File fout = new File(fdirout, file.getName() + "_digest.xml");
                try {
                    out = new FileOutputStream(fout);
                    writer.setOutputStream(out);
                    writer.write(unique);
                    writer.close();
                } catch (FileNotFoundException e) {
                    System.err.println(
                            StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString());
                    error++;
                } catch (IOException e) {
                    System.err.println(
                            StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString());
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e1) {
                        }
                    }
                    error++;
                }
                result.detach();
            }
            if (fglobal != null) {
                global.add(result);
            }
        }
        if (ftar != null) {
            currank++;
            Element result = DigestCompute.checkDigest(StaticValues.config, src, ftar);
            if (result.selectSingleNode(".[@status='ok']") == null) {
                System.err.println(StaticValues.LBL.error_error.get()
                        + StaticValues.LBL.error_computedigest.get() + ftar.getAbsolutePath());
                error++;
            } else if (oneDigestPerFile) {
                Element rootElement = XmlDom.factory.createElement("digest");
                Document unique = XmlDom.factory.createDocument(rootElement);
                rootElement.add(result);
                FileOutputStream out = null;
                File fout = new File(dst, ftar.getName() + "_tar_digest.xml");
                try {
                    out = new FileOutputStream(fout);
                    writer.setOutputStream(out);
                    writer.write(unique);
                    writer.close();
                } catch (FileNotFoundException e) {
                    System.err.println(
                            StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString());
                    error++;
                } catch (IOException e) {
                    System.err.println(
                            StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString());
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e1) {
                        }
                    }
                    error++;
                }
                result.detach();
            }
            if (fglobal != null) {
                global.add(result);
            }
        }
        if (fglobal != null) {
            if (error > 0) {
                global.addAttribute("status", "error");
            } else {
                global.addAttribute("status", "ok");
            }
            XmlDom.addDate(VitamArgument.ONEXML, StaticValues.config, global);
            FileOutputStream out;
            try {
                out = new FileOutputStream(fglobal);
                writer.setOutputStream(out);
                writer.write(globalDoc);
                writer.close();
            } catch (FileNotFoundException e) {
                System.err.println(
                        StaticValues.LBL.error_error.get() + fglobal.getAbsolutePath() + " " + e.toString());
                error++;
            } catch (IOException e) {
                System.err.println(
                        StaticValues.LBL.error_error.get() + fglobal.getAbsolutePath() + " " + e.toString());
                error++;
            }
        }
        if (error > 0) {
            System.err.println(StaticValues.LBL.error_error.get() + " Digest" + " [ " + currank
                    + (error > 0 ? " (" + StaticValues.LBL.error_error.get() + error + " ) " : "") + " ]");
            return -error;
        }
        return currank;
    } catch (UnsupportedEncodingException e) {
        System.err.println(StaticValues.LBL.error_error.get() + " " + e.toString());
        return -1;
    }

}

From source file:org.dom4j.samples.RoundTripDemo.java

License:Open Source License

/** Outputs the document to a buffer, parse it back again then output it */
protected void process(Document document) throws Exception {

    System.out.println("about to output: " + document);

    // output the document to a buffer
    StringWriter out = new StringWriter();
    XMLWriter writer = new XMLWriter(out);
    writer.write(document);//from   www.ja v  a 2  s .  c  om
    writer.close();

    // parse back again
    StringReader in = new StringReader(out.toString());
    SAXReader reader = new SAXReader();
    Document doc2 = reader.read(in);

    System.out.println("parsed back again: " + doc2);

    // now lets output it again
    writer.setOutputStream(System.out);
    writer.write(doc2);
}

From source file:org.dom4j.samples.rule.SongFilter.java

License:Open Source License

public static void main(String[] args) throws Exception {
    SongFilter filter = new SongFilter();
    URL source = filter.getClass().getResource("/org/dom4j/samples/rule/Songs.xml");
    Document result = filter.filtering(new SAXReader().read(source));

    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
    writer.setOutputStream(System.out);
    writer.write(result);//from  w  w  w  . jav a 2 s . c  o m

}