Example usage for org.dom4j.io XMLWriter XMLWriter

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

Introduction

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

Prototype

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException 

Source Link

Usage

From source file:controllers.FXMLScicumulusController.java

private void saveProject(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException {
    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("SciCumulus");
    root.addAttribute("nameProject", txt_name_workflow.getText().trim());
    Element activities = root.addElement("activities");
    activities.addAttribute("quant", Integer.toString(this.activities.size()));
    for (Activity act : this.activities) {
        Element activity = activities.addElement("activity");
        activity.addAttribute("id", act.getIdObject());
        activity.addAttribute("name", act.getName());
        activity.addAttribute("login", act.getLogin());
        activity.addAttribute("password", act.getPassword());
        activity.addAttribute("tag", act.getTag());
        activity.addAttribute("description", act.getDescription());
        activity.addAttribute("type", act.getType());
        activity.addAttribute("templatedir", act.getTemplatedir());
        activity.addAttribute("activation", act.getActivation());
        activity.addAttribute("input_filename", act.getInput_filename());
        activity.addAttribute("output_filename", act.getOutput_filename());
        activity.addAttribute("timeCommand", act.getTimeCommand().toString());
        //                activity.addAttribute("commands", act.getCommands().toString());
        for (Field field : act.getFields()) {
            Element fields = activity.addElement("fields");
            fields.addAttribute("name", field.getName());
            fields.addAttribute("type", field.getType());
            fields.addAttribute("operation", field.getOperation());
            fields.addAttribute("decimalPlaces", field.getDecimalPlaces());
            fields.addAttribute("input", field.getInput());
            fields.addAttribute("output", field.getOutput());
        }/*from  ww w. ja  va 2 s  .  c o m*/
    }
    Element relations = root.addElement("relations");
    relations.addAttribute("quant", Integer.toString(this.relations.size()));
    for (Relation rel : this.relations) {
        Element relation = relations.addElement("relation");
        relation.addAttribute("id", rel.getIdObject());
        Activity actStart = (Activity) rel.getNodeStart();
        Activity actEnd = (Activity) rel.getNodeEnd();
        relation.addAttribute("name", rel.getName());
        relation.addAttribute("idActStart", actStart.getIdObject());
        relation.addAttribute("nameActStart", actStart.getName());
        relation.addAttribute("idActEnd", actEnd.getIdObject());
        relation.addAttribute("nameActEnd", actEnd.getName());
    }
    //Gravando arquivo
    FileOutputStream fos = new FileOutputStream(file);
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(fos, format);
    writer.write(doc);
    writer.flush();
}

From source file:controllers.ServiceController.java

License:Apache License

private boolean writeBodyToFile(String filename) {
    String xmlFile = filename + ".xml";
    String jsonFile = filename + ".shape.json";
    try {/*from   www . j av a2 s.co  m*/
        BufferedReader br = request.getReader();

        boolean hasJson = false;
        StringBuffer buf = new StringBuffer();
        //Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile),"utf-8"));

        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            if (inputLine.equals("===boundary===")) {
                /*?shapes*/
                //writer.close();
                //writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile),"utf-8"));

                SAXReader reader = new SAXReader();
                reader.setEntityResolver(new DTDEntityResolver());
                Document document = reader.read(new StringReader(buf.toString()));
                OutputFormat format = new OutputFormat(" ", true);
                XMLWriter writer = new XMLWriter(
                        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile), "utf-8")),
                        format);
                writer.write(document);
                writer.flush();
                writer.close();

                hasJson = true;
                buf = new StringBuffer();
                continue;
            }

            buf.append(inputLine).append("\n");

            //writer.write(inputLine);
            //writer.write("\n");
        }
        //writer.close();
        br.close();

        if (!hasJson) {
            Trace.write(Trace.Error, "write osworkflow: no json-shape define!");
            return false;
        }

        String jsonString = buf.toString();
        jsonString = formatJsonStrings(jsonString);
        if (jsonString == null) {
            Trace.write(Trace.Error, "write osworkflow[json]: " + buf.toString());
            return false;
        }

        Writer jsonWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile), "utf-8"));
        jsonWriter.write(jsonString);
        jsonWriter.close();

        return true;
    } catch (DocumentException de) {
        Trace.write(Trace.Error, de, "write osworkflow[xml].");
    } catch (IOException e) {
        Trace.write(Trace.Error, e, "write osworkflow.");
    }

    return false;
}

From source file:cparser.CParser.java

License:GNU General Public License

public static void main(final String argv[]) {
    System.loadLibrary("cparser");
    init();//from w  ww.  j  av a 2 s.  co  m

    if (argv.length < 1)
        System.exit(1);

    final CParser cp = new CParser(new File(argv[0]).getAbsolutePath());

    System.out.println("Parsing");
    int ret = cp.parse();

    if (ret != 0)
        System.err.println("Parsing failed with error " + ret);

    final Node n = cp.getRoot();
    System.out.println("Computing");
    n.compute();
    System.out.println("Creating CFG");
    n.createCFG();
    System.out.println("Generating XML");
    final String xml = n.toXML();
    System.out.println("Writing XML text");
    try {
        final FileWriter fw = new FileWriter("xml.text");
        fw.write(xml);
        fw.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    System.err.println("Node:\n\tcls=" + n.getClass().getName());
    System.err.println("\tstr=" + n.toString());
    System.err.println("\tXML:");
    try {
        System.out.println("Generating XML tree");
        Document doc = DocumentHelper.parseText(xml);
        System.out.println("Writing XML");
        OutputFormat format = OutputFormat.createPrettyPrint();
        OutputStream os = new FileOutputStream("out.xml");
        try {
            XMLWriter writer = new XMLWriter(os, format);
            writer.write(doc);
            writer.close();
            os.close();
            /*            writer = new XMLWriter(System.err, format);
                        writer.write(doc);
                        writer.close();*/
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    fini();
}

From source file:cz.muni.stanse.utils.xmlpatterns.XMLAlgo.java

License:GNU General Public License

/**
 * Pretty-print XML node to a stream/*from w ww  .  j ava  2s . co  m*/
 *
 * @param n node to dump
 * @param o stream to dump to
 */
public static void outputXML(Node n, OutputStream o) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    try {
        XMLWriter writer = new XMLWriter(o, format);
        writer.write(n);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:cz.mzk.editor.server.fedora.utils.DocumentSaver.java

License:Open Source License

public static boolean writeDocument(Document document, OutputStream out, PrintType printType) {
    OutputFormat format = chooseOutputFormat(printType);
    try {//from   w w w.ja v a 2s . c o m
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();
        return true;
    } catch (UnsupportedEncodingException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:DAO.LocationDAO.java

public boolean addNewLocation(String webAppPath, String location) {
    try {// w  w w  .  j a  v a2s.  co m
        SAXReader reader = new SAXReader();
        Document document = reader.read(webAppPath + "/xml/Locations.xml");
        Element root = document.getRootElement();
        int max = 0;
        for (Iterator i = root.elementIterator("Location"); i.hasNext();) {
            Element elt = (Element) i.next();
            int cur = Integer.parseInt(elt.element("LocationId").getText());
            if (cur > max) {
                max = cur;
            }
        }

        Integer newLocationId = max + 1;
        Element newLocation = root.addElement("Location");
        newLocation.addElement("LocationId").setText(newLocationId.toString());
        newLocation.addElement("Name").setText(location);
        root.appendAttributes(newLocation);
        try {
            FileOutputStream fos = new FileOutputStream(webAppPath + "/xml/Locations.xml");
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlwriter = new XMLWriter(fos, format);
            xmlwriter.write(document);
            System.out.println("dead");
            xmlwriter.close();
        } catch (Exception ex) {
            System.out.println("Failed!");
            ex.printStackTrace();
        }
        //Node node = document.selectSingleNode("/Locations/Location[not(../Location/LocationId > LocationId)]");
        //String id = node.valueOf("LocationId");
        //System.out.println(id);
        return true;
    } catch (DocumentException ex) {
        System.out.println("Add New Location Failed!");
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:datasource.XMLTools.java

License:Open Source License

public static long writeToXML(Document doc, String Filename) {
    long errorcode = 0;

    try {/*from  w ww.j av  a2 s  .  c  o m*/
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-8");
        FileOutputStream out = new FileOutputStream(Filename);
        XMLWriter writer = new XMLWriter(out, outformat);
        writer.write(doc);
        writer.flush();
    } catch (Exception x) {
        System.out.println(x.getMessage());
        errorcode = x.hashCode();
    }
    return errorcode;
}

From source file:de.ailis.wlandsuite.game.blocks.GameBlock.java

License:Open Source License

/**
 * Writes the block to a stream as XML/* w  w w. j av a  2s  . c o m*/
 *
 * @param stream
 *            The output stream
 * @throws IOException
 *             When file operation fails.
 */

public void writeXml(final OutputStream stream) throws IOException {
    XMLWriter writer;
    Document document;
    OutputFormat format;

    format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);

    writer = new XMLWriter(stream, format);
    try {
        final Element rootElement = toXml();
        document = DocumentHelper.createDocument(rootElement);
        writer.write(document);
    } finally {
        writer.close();
    }
}

From source file:de.ailis.xadrian.utils.XmlUtils.java

License:Open Source License

/**
 * Writes the specified document to the specified file.
 *
 * @param document//w  ww  .j a  va  2  s  . c  om
 *            The document to write
 * @param file
 *            The file to write to
 * @throws IOException
 *             If file could not be written
 */
public static void write(final Document document, final File file) throws IOException {
    final OutputFormat format = OutputFormat.createPrettyPrint();
    final XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format);
    try {
        writer.write(document);
    } finally {
        writer.close();
    }
}

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

License:Open Source License

/**
 * Schreibt ein dom4j Dokument in eine Datei.
 *
 * @param document Ein dom4j Dokument.//from ww  w .j a  v a 2s .c o  m
 * @param file Die zu schreibende Datei.
 * @param encoding Das Encoding.
 * @throws UnhandledException Da ging was schief.
 */
public static void toFile(final Document document, final File file, final String encoding)
        throws UnhandledException {

    XMLWriter writer = null;
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        writer = new XMLWriter(new FileWriter(file), format);
        writer.write(document);
    } catch (IOException ex) {
        log.debug("Fehler: ", ex);
        throw new UnhandledException(ex);
    } finally {
        XMLUtils.close(writer);
    }
}