Example usage for org.dom4j.io XMLWriter flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the underlying Writer

Usage

From source file:org.infoglue.cms.applications.contenttool.actions.UpdateContentVersionAction.java

License:Open Source License

public String doStandaloneXML() throws Exception {
    try {/* w w w.jav  a  2 s.co  m*/
        String xmlResult = null;
        getResponse().setContentType("text/xml; charset=UTF-8");
        getResponse().setHeader("Cache-Control", "no-cache");
        getResponse().setHeader("Pragma", "no-cache");
        getResponse().setDateHeader("Expires", 0);
        PrintWriter out = getResponse().getWriter();
        XMLWriter xmlWriter = new XMLWriter(out);
        XStream xStream = new XStream();
        xStream.omitField(contentVersionVO.getClass(), "versionValue");

        /*
        System.out.println("contentVersionId:" + this.contentVersionId);
        System.out.println("contentId:" + this.contentId);
        System.out.println("languageId:" + this.languageId);
        System.out.println("this.contentVersionVO:" + this.contentVersionVO);
        */
        ceb.throwIfNotEmpty();

        if (this.attributeName == null)
            this.attributeName = "";

        if (this.currentEditorId == null)
            this.currentEditorId = 1;

        try {
            this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
            this.contentVersionVO = ContentVersionControllerProxy.getController().acUpdate(
                    this.getInfoGluePrincipal(), this.contentId, this.languageId, this.contentVersionVO);
            this.contentVersionVO = ContentVersionController.getContentVersionController()
                    .getContentVersionVOWithId(this.contentVersionVO.getId());
            xmlResult = xStream.toXML(this.contentVersionVO);
        } catch (ConstraintException ce) {
            super.contentVersionVO = this.contentVersionVO;
            xmlResult = xStream.toXML(ce);
        }

        //System.out.println("xmlResult:" + xmlResult);
        /*
         * Output
         */
        xmlWriter.write(DocumentHelper.parseText(xmlResult));
        xmlWriter.flush();
    } catch (Exception e) {
        logger.warn("Error in UpdateContentVersion.doStandaloneXML: " + e.getMessage());
        if (logger.isInfoEnabled())
            logger.info("Error in UpdateContentVersion.doStandaloneXML: " + e.getMessage(), e);
    }

    return NONE;
}

From source file:org.infoglue.cms.applications.contenttool.actions.UpdateContentVersionAction.java

License:Open Source License

public String doXml() throws IOException, SystemException, Bug, DocumentException {
    try {/*  www.  java  2s. c  om*/
        String xmlResult = null;
        getResponse().setContentType("text/xml; charset=UTF-8");
        getResponse().setHeader("Cache-Control", "no-cache");
        getResponse().setHeader("Pragma", "no-cache");
        getResponse().setDateHeader("Expires", 0);
        PrintWriter out = getResponse().getWriter();
        XMLWriter xmlWriter = new XMLWriter(out);
        XStream xStream = new XStream();
        xStream.omitField(contentVersionVO.getClass(), "versionValue");

        // super.initialize(this.contentVersionId, this.contentId, this.languageId);

        ContentVersionVO currentContentVersionVO = null;
        ContentVersionVO activeContentVersionVO = ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(contentId, languageId);

        //System.out.println("activeContentVersionVO:" + activeContentVersionVO);
        //System.out.println("this.contentVersionVO:" + this.contentVersionVO);
        /*
         * Are we trying to update the active version?
         */
        if (activeContentVersionVO.getContentVersionId().equals(this.contentVersionVO.getContentVersionId())) {
            if (this.contentVersionVO.getId() != null) {
                currentContentVersionVO = ContentVersionController.getContentVersionController()
                        .getContentVersionVOWithId(this.contentVersionVO.getId());
            }

            if (currentContentVersionVO == null
                    || this.oldModifiedDateTime == currentContentVersionVO.getModifiedDateTime().getTime()) {
                this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());

                try {
                    if (activeContentVersionVO.getStateId().equals(ContentVersionVO.WORKING_STATE)) {
                        this.contentVersionVO = ContentVersionControllerProxy.getController().acUpdate(
                                this.getInfoGluePrincipal(), this.contentId, this.languageId,
                                this.contentVersionVO);
                        this.contentVersionVO = ContentVersionController.getContentVersionController()
                                .getContentVersionVOWithId(this.contentVersionVO.getId());
                        this.oldModifiedDateTime = this.contentVersionVO.getModifiedDateTime().getTime();
                        xmlResult = xStream.toXML(contentVersionVO);
                    } else {
                        xmlResult = "<invalidstate/>";
                    }
                } catch (ConstraintException ce) {
                    ce.printStackTrace();
                    xmlResult = xStream.toXML(ce);
                } catch (Exception e) {
                    e.printStackTrace();
                    xmlResult = xStream.toXML(e);
                }
            } else {
                this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
                super.contentVersionVO = this.contentVersionVO;
                concurrentModification = true;
                xmlResult = "<concurrentmodification/>";
            }
        } else {
            /*
             * Not updating active version
             */
            xmlResult = "<invalidversion/>";
        }

        //System.out.println("xmlResult:" + xmlResult);
        /*
         * Output
         */
        xmlWriter.write(DocumentHelper.parseText(xmlResult));
        xmlWriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return NONE;
}

From source file:org.infoglue.cms.util.dom.DOMBuilder.java

License:Open Source License

/**
 * This method gets the xml as a string with the correct encoding.
 *///from   w w  w . j  av  a2 s .c om

private String getEncodedString(Element element) throws Exception {
    OutputFormat outFormat = OutputFormat.createCompactFormat();
    outFormat.setEncoding("UTF-8");
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    XMLWriter out = new XMLWriter(bao, outFormat);
    out.write(element);
    out.flush();
    String s = bao.toString();
    logger.info("OUT: " + s);
    return s;
}

From source file:org.itracker.web.util.ImportExportUtilities.java

License:Open Source License

/**
 * export the issues to an XML and write it to the response.
 * @param issues//from  w w  w. java2  s.  c o  m
 * @param config
 * @param request
 * @param response
 * @return  if <code>true</code> the export was sucessful.
 * @throws ServletException
 * @throws IOException
 */
public static boolean exportIssues(List<Issue> issues, SystemConfiguration config, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/xml; charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=\"issue_export.xml\"");

    XMLWriter writer = new XMLWriter(response.getOutputStream(), OutputFormat.createCompactFormat());

    try {
        // TODO instead to have a string returned, it should directly serialize the
        // export to the response-writer.
        ImportExportUtilities.exportIssues(writer, issues, config);

    } catch (ImportExportException iee) {
        logger.error("Error exporting issue data. Message: " + iee.getMessage(), iee);
        return false;
    } finally {
        if (null != writer) {
            writer.flush();
            writer.close();
        }
    }

    return true;
}

From source file:org.jboss.ide.eclipse.as.core.extensions.descriptors.XMLDocumentRepository.java

License:Open Source License

public static void saveDocument(Document doc, String fullPath) {
    Exception ex = null;//w  ww.  j a v a 2  s. c  o  m
    try {
        File outFile = new File(fullPath);
        FileOutputStream os = new FileOutputStream(outFile);
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(os, outformat);
        writer.write(doc);
        writer.flush();
    } catch (MalformedURLException e) {
        ex = e;
    } catch (FileNotFoundException e) {
        ex = e;
    } catch (UnsupportedEncodingException e) {
        ex = e;
    } catch (IOException e) {
        ex = e;
    }
    if (ex != null) {
        JBossServerCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR,
                JBossServerCorePlugin.PLUGIN_ID, NLS.bind(Messages.saveXMLDocumentFailed, fullPath), ex));
    }
}

From source file:org.jboss.tools.jbpm.convert.bpmnto.util.DomXmlWriter.java

License:Open Source License

public static String toString(Document document) throws IOException {
    OutputFormat outputFormat = new OutputFormat("  ", true);
    Writer writer = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
    xmlWriter.write(document);/*  w w w .j  av  a2s . co  m*/
    xmlWriter.flush();
    writer.flush();
    return writer.toString();
}

From source file:org.jbpm.instantiation.Delegation.java

License:Open Source License

public void read(Element delegateElement, JpdlXmlReader jpdlReader) {
    processDefinition = jpdlReader.getProcessDefinition();
    className = delegateElement.attributeValue("class");
    if (className == null) {
        jpdlReader.addWarning("no class specified in " + delegateElement.asXML());
    }/*from w  w  w.j a va2 s. co  m*/

    configType = delegateElement.attributeValue("config-type");
    if (delegateElement.hasContent()) {
        try {
            StringWriter stringWriter = new StringWriter();
            // when parsing, it could be to store the config in the database, so we want to make the configuration compact
            XMLWriter xmlWriter = new XMLWriter(stringWriter, OutputFormat.createCompactFormat());
            Iterator iter = delegateElement.content().iterator();
            while (iter.hasNext()) {
                Object node = iter.next();
                xmlWriter.write(node);
            }
            xmlWriter.flush();
            configuration = stringWriter.toString();
        } catch (IOException e) {
            jpdlReader.addWarning("io problem while parsing the configuration of " + delegateElement.asXML());
        }
    }
}

From source file:org.jbpm.jpdl.convert.Converter.java

License:Open Source License

public void serializetoXML(OutputStream out, Document document) throws Exception {

    OutputFormat outformat = OutputFormat.createPrettyPrint();
    //outformat.setEncoding(aEncodingScheme);
    XMLWriter writer = new XMLWriter(out, outformat);
    writer.write(document);//from  w  w w . ja  v  a  2s.  c  o m
    writer.flush();
}

From source file:org.jbpm.jpdl.xml.JpdlXmlWriter.java

License:Open Source License

public void write(ProcessDefinition processDefinition) {
    problems = new ArrayList();
    if (processDefinition == null)
        throw new JbpmException("processDefinition is null");
    try {//from  w ww  . j a v  a  2s  .  co m
        // collect the actions of the process definition
        // we will remove each named event action and the remaining ones will be written 
        // on the process definition.
        // create a dom4j dom-tree for the process definition
        Document document = createDomTree(processDefinition);

        // write the dom-tree to the given writer
        OutputFormat outputFormat = new OutputFormat("  ", true);
        // OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        xmlWriter.write(document);
        xmlWriter.flush();
        writer.flush();
    } catch (IOException e) {
        addProblem("couldn't write process definition xml: " + e.getMessage());
    }

    if (problems.size() > 0) {
        throw new JpdlException(problems);
    }
}

From source file:org.jivesoftware.openfire.archive.ArchiveIndexer.java

License:Open Source License

/**
 * Loads a property manager for search properties if it isn't already
 * loaded. If an XML file for the search properties isn't already
 * created, it will attempt to make a file with default values.
 */// ww w  . j  av a  2  s . c  o  m
private void loadPropertiesFile(File searchDir) throws IOException {
    File indexPropertiesFile = new File(searchDir, "indexprops.xml");

    // Make sure the file actually exists. If it doesn't, a new file
    // will be created.
    // If it doesn't exists we have to create it.
    if (!indexPropertiesFile.exists()) {
        org.dom4j.Document doc = DocumentFactory.getInstance()
                .createDocument(DocumentFactory.getInstance().createElement("search"));
        // Now, write out to the file.
        Writer out = null;
        try {
            // Use JDOM's XMLOutputter to do the writing and formatting.
            out = new FileWriter(indexPropertiesFile);
            XMLWriter outputter = new XMLWriter(out, OutputFormat.createPrettyPrint());
            outputter.write(doc);
            outputter.flush();
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                // Ignore.
            }
        }
    }
    indexProperties = new XMLProperties(indexPropertiesFile);
}