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.dentaku.gentaku.tools.cgen.xmi.XMIGenTask.java

License:Apache License

private void writeFile(Branch document, File file) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(System.getProperty("file.encoding"));
    format.setSuppressDeclaration(false);
    format.setExpandEmptyElements(false);

    Writer out = new FileWriter(file);
    final XMLWriter xmlWriter = new XMLWriter(out, format);
    xmlWriter.setEscapeText(false);//from  ww  w  .  j ava2s  .c  o m

    xmlWriter.write(document);
    xmlWriter.flush();
    xmlWriter.close();
}

From source file:org.directwebremoting.convert.DOM4JConverter.java

License:Apache License

public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws ConversionException {
    try {/*www .ja v  a  2  s  .  c  o  m*/
        // Using XSLT to convert to a stream. Setup the source
        if (!(data instanceof Node)) {
            throw new ConversionException(data.getClass());
        }

        Node node = (Node) data;

        OutputFormat outformat = OutputFormat.createCompactFormat();
        outformat.setEncoding("UTF-8");

        // Setup the destination
        StringWriter xml = new StringWriter();

        XMLWriter writer = new XMLWriter(xml, outformat);
        writer.write(node);
        writer.flush();
        xml.flush();

        String script;
        if (data instanceof Element) {
            script = EnginePrivate.xmlStringToJavascriptDomElement(xml.toString());
        } else {
            script = EnginePrivate.xmlStringToJavascriptDomDocument(xml.toString());
        }

        OutboundVariable ov = new NonNestedOutboundVariable(script);
        outctx.put(data, ov);

        return ov;
    } catch (ConversionException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ConversionException(data.getClass(), ex);
    }
}

From source file:org.efaps.webdav4vfs.handler.PropFindHandler.java

License:Apache License

/**
 * Handle a PROPFIND request.//from ww  w  . j  a  v  a  2  s . co  m
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error that cannot be handled normally
 */
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SAXReader saxReader = new SAXReader();
    try {
        Document propDoc = saxReader.read(request.getInputStream());
        logXml(propDoc);

        Element propFindEl = propDoc.getRootElement();
        for (Object propElObject : propFindEl.elements()) {
            Element propEl = (Element) propElObject;
            if (VALID_PROPFIND_TAGS.contains(propEl.getName())) {
                FileObject object = VFSBackend.resolveFile(request.getPathInfo());
                if (object.exists()) {
                    // respond as XML encoded multi status
                    response.setContentType("text/xml");
                    response.setCharacterEncoding("UTF-8");
                    response.setStatus(SC_MULTI_STATUS);

                    Document multiStatusResponse = getMultiStatusResponse(object, propEl, getBaseUrl(request),
                            getDepth(request));
                    logXml(multiStatusResponse);

                    // write the actual response
                    XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
                    writer.write(multiStatusResponse);
                    writer.flush();
                    writer.close();

                } else {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }
                break;
            }
        }
    } catch (DocumentException e) {
        LOG.error("invalid request: " + e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:org.efaps.webdav4vfs.handler.PropPatchHandler.java

License:Apache License

/**
 * Handle a PROPPATCH request./* w w  w  .  j  a  v a2 s  .c  om*/
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error that cannot be handled normally
 */
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        SAXReader saxReader = new SAXReader();
        try {
            Document propDoc = saxReader.read(request.getInputStream());
            logXml(propDoc);

            Element propUpdateEl = propDoc.getRootElement();
            List<Element> requestedProperties = new ArrayList<Element>();
            for (Object elObject : propUpdateEl.elements()) {
                Element el = (Element) elObject;
                String command = el.getName();
                if (AbstractDavResource.TAG_PROP_SET.equals(command)
                        || AbstractDavResource.TAG_PROP_REMOVE.equals(command)) {
                    for (Object propElObject : el.elements()) {
                        for (Object propNameElObject : ((Element) propElObject).elements()) {
                            Element propNameEl = (Element) propNameElObject;
                            requestedProperties.add(propNameEl);
                        }
                    }
                }
            }

            // respond as XML encoded multi status
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(SC_MULTI_STATUS);

            Document multiStatusResponse = getMultiStatusResponse(object, requestedProperties,
                    getBaseUrl(request));

            logXml(multiStatusResponse);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(multiStatusResponse);
            writer.flush();
            writer.close();

        } catch (DocumentException e) {
            LOG.error("invalid request: " + e.getMessage());
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        LOG.error(object.getName().getPath() + " NOT FOUND");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.etudes.api.app.melete.util.XMLHelper.java

License:Apache License

/**
 * creates xml file for the give path// w w  w .  ja  va2  s  . c  o  m
 * @param url - path to create xml file
 * @throws IOException
 */
static public void generateXMLFile(String url, Document document) throws Exception {
    XMLWriter writer = null;
    FileOutputStream out = null;
    try {
        if (document == null)
            document = DocumentHelper.createDocument();

        out = new FileOutputStream(new File(url));
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        //outformat.setEncoding(aEncodingScheme);
        writer = new XMLWriter(out, outformat);
        writer.write(document);
        writer.flush();
    } catch (IOException e1) {
        throw e1;
    } catch (Exception e) {
        throw e;
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e2) {
        }

        try {
            if (out != null)
                out.close();
        } catch (IOException e3) {
        }
    }
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * @see org.gbif.portal.util.mhf.Message#getRawData()
 *///from   w ww .  j a v a  2  s . c o  m
public String getRawData() throws MessageAccessException {
    if (rawXml != null)
        return rawXml.toString();
    else {
        // build a short string
        StringWriter writer = new StringWriter();
        OutputFormat outformat = OutputFormat.createCompactFormat();
        outformat.setSuppressDeclaration(true);

        XMLWriter xmlWriter = new XMLWriter(writer, outformat);
        try {
            xmlWriter.write(getDocument());
            xmlWriter.flush();
            String rawXml = writer.toString();
            setRawData(rawXml);
            return rawXml;
        } catch (IOException e) {
            throw new MessageAccessException("Error writing the XML Document", e);
        } finally {
            try {
                xmlWriter.close();
            } catch (IOException e) {
            }
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * @see org.gbif.portal.util.mhf.message.impl.xml.BeautifiedData#getBeautifiedData()
 *//*from w w w  .  j  a v  a 2 s  .c  o  m*/
public String getLoggableData() throws MessageAccessException {
    // build a beautiful string
    StringWriter writer = new StringWriter();
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setSuppressDeclaration(true);
    XMLWriter xmlWriter = new XMLWriter(writer, outformat);
    try {
        xmlWriter.write(getDocument());
        xmlWriter.flush();
        String rawXml = writer.toString();
        setRawData(rawXml);
        return rawXml.trim();
    } catch (IOException e) {
        throw new MessageAccessException("Error writing the XML Document", e);
    } finally {
        try {
            xmlWriter.close();
        } catch (IOException e) {
        }
        try {
            writer.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.hibernate.console.ConfigurationXMLFactory.java

License:Open Source License

public static void dump(OutputStream os, Element element) {
    // try to "pretty print" it
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    try {/*  www.  j  a  v a 2s  . co  m*/
        XMLWriter writer = new XMLWriter(os, outformat);
        writer.write(element);
        writer.flush();
    } catch (IOException e1) {
        // otherwise, just dump it
        try {
            os.write(element.asXML().getBytes());
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:org.hibernate.internal.util.xml.XMLHelper.java

License:LGPL

public static void dump(Element element) {
    try {//from  w w  w .  j a v a2s.  c o m
        // try to "pretty print" it
        OutputFormat outFormat = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(System.out, outFormat);
        writer.write(element);
        writer.flush();
        System.out.println("");
    } catch (Throwable t) {
        // otherwise, just dump it
        System.out.println(element.asXML());
    }

}

From source file:org.hightides.annotations.util.SpringXMLUtil.java

License:Apache License

/**
 * Private helper to save XML document.//from  w w w . ja v  a2s.c o m
 * @param filename
 * @param doc
 * @param backup
 * @return
 */
private static boolean saveXMLDocument(String filename, Document doc, boolean backup) {
    // copy the target to backup folder
    if (backup)
        FileUtil.backupFile(filename);

    // overwrite the target
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setExpandEmptyElements(false);
    format.setIndentSize(4);
    format.setNewLineAfterDeclaration(true);
    XMLWriter out;
    try {
        out = new XMLWriter(new FileWriter(filename), format);
        out.write(doc);
        out.flush();
        out.close();
        return true;
    } catch (IOException e) {
        _log.info("Failed to write to output filename [" + filename + "]", e);
        return false;
    }
}