Example usage for org.dom4j.io OutputFormat createCompactFormat

List of usage examples for org.dom4j.io OutputFormat createCompactFormat

Introduction

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

Prototype

public static OutputFormat createCompactFormat() 

Source Link

Document

A static helper method to create the default compact format.

Usage

From source file:dom4j.Dom4JExample.java

public void write(Document document) throws IOException {

    // lets write to a file
    XMLWriter writer;//from  w w  w .  jav  a 2 s  . c o m
    //        = new XMLWriter(
    //            new BufferedOutputStream(outputStream));
    //        writer.write( document );
    //        writer.close();

    // Pretty print the document to System.out
    System.out.println("\n\nPretty format");
    OutputFormat format = OutputFormat.createPrettyPrint();
    writer = new XMLWriter(System.out, format);
    writer.write(document);

    // Compact format to System.out
    System.out.println("\n\nCompact format");
    format = OutputFormat.createCompactFormat();
    writer = new XMLWriter(System.out, format);
    writer.write(document);
}

From source file:gov.nih.nci.ispy.web.helper.ReportGeneratorHelper.java

License:BSD License

/**
 * This static method will render a query report of the passed reportXML, in
 * HTML using the XSLT whose name has been passed, to the jsp whose 
 * jspWriter has been passed. The request is used to acquire any filter 
 * params that may have been added to the request and that may be applicable
 * to the XSLT./*from   w ww.jav a2  s.  co m*/
 *  
 * @param request --the request that will contain any parameters you want applied
 * to the XSLT that you specify
 * @param reportXML -- this is the XML that you want transformed to HTML
 * @param xsltFilename --this the XSLT that you want to use
 * @param out --the JSPWriter you want the transformed document to go to...
 */
/*
public static void renderReport(HttpServletRequest request, Document reportXML, String xsltFilename, JspWriter out) {
   File styleSheet = new File(RembrandtContextListener.getContextPath()+"/XSL/"+xsltFilename);
   // load the transformer using JAX
   logger.debug("Applying XSLT "+xsltFilename);
Transformer transformer;
   try {
 transformer = new Transformer(styleSheet, (HashMap)request.getAttribute(RembrandtConstants.FILTER_PARAM_MAP));
   Document transformedDoc = transformer.transform(reportXML);
           
   //*
   // * right now this assumes that we will only have one XSL for CSV
   // * and it checks for that as we do not want to "pretty print" the CSV, we
   // * only want to spit it out as a string, or formatting gets messed up
   // * we will of course want to pretty print the XHTML for the graphical reports
   // * later we can change this to handle mult XSL CSVs
   // * RCL
   // 
   if(!xsltFilename.equals(RembrandtConstants.DEFAULT_XSLT_CSV_FILENAME)){
       OutputFormat format = OutputFormat.createPrettyPrint();
       XMLWriter writer;
       writer = new XMLWriter(out, format );
               
       writer.write( transformedDoc );
       writer.close();
   }
   else   {
       String csv = transformedDoc.getStringValue();
       csv.trim();
       out.println(csv);
   }
   }catch (UnsupportedEncodingException uee) {
 logger.error("UnsupportedEncodingException");
 logger.error(uee);
   }catch (IOException ioe) {
 logger.error("IOException");
 logger.error(ioe);
   }
}
*/

public static String renderReport(Map params, Document reportXML, String xsltFilename) {
    //used only for finding based AJAX
    //Changed XSL to xsl RCL 3/6...this is bombing

    File styleSheet = new File(ISPYContextListener.getContextPath() + "/xsl/" + xsltFilename);
    // load the transformer using JAXP
    logger.debug("Applying XSLT " + xsltFilename);
    Transformer transformer;
    try {
        transformer = new Transformer(styleSheet, params);
        Document transformedDoc = transformer.transform(reportXML);

        /*
         * right now this assumes that we will only have one XSL for CSV
         * and it checks for that as we do not want to "pretty print" the CSV, we
         * only want to spit it out as a string, or formatting gets messed up
         * we will of course want to pretty print the XHTML for the graphical reports
         * later we can change this to handle mult XSL CSVs
         * RCL
         */

        if (!xsltFilename.equals(ispyConstants.DEFAULT_XSLT_CSV_FILENAME)) {

            StringBuffer sb = new StringBuffer();
            OutputFormat format = OutputFormat.createCompactFormat();
            StringWriter sw = new StringWriter(1024);
            HTMLWriter writer = new HTMLWriter(sw, format);
            writer.write(transformedDoc);
            sb = sw.getBuffer();
            return sb.toString();

        } else {
            String csv = transformedDoc.getStringValue();
            csv.trim();
            return csv;
        }
    } catch (UnsupportedEncodingException uee) {
        logger.error("UnsupportedEncodingException");
        logger.error(uee);
    } catch (IOException ioe) {
        logger.error("IOException");
        logger.error(ioe);
    }

    return "Reporting Error";
}

From source file:gov.nih.nci.rembrandt.web.helper.ReportGeneratorHelper.java

License:BSD License

public static String renderReport(Map params, Document reportXML, String xsltFilename) {
    //used only for finding based AJAX

    File styleSheet = new File(RembrandtContextListener.getContextPath() + "/XSL/" + xsltFilename);
    // load the transformer using JAXP
    logger.debug("Applying XSLT " + xsltFilename);
    Transformer transformer;/*w  w w.j  a v a 2  s .  c o m*/
    try {
        transformer = new Transformer(styleSheet, params);
        Document transformedDoc = transformer.transform(reportXML);

        /*
         * right now this assumes that we will only have one XSL for CSV
         * and it checks for that as we do not want to "pretty print" the CSV, we
         * only want to spit it out as a string, or formatting gets messed up
         * we will of course want to pretty print the XHTML for the graphical reports
         * later we can change this to handle mult XSL CSVs
         * RCL
         */

        if (!xsltFilename.equals(RembrandtConstants.DEFAULT_XSLT_CSV_FILENAME)) {

            StringBuffer sb = new StringBuffer();
            OutputFormat format = OutputFormat.createCompactFormat();
            StringWriter sw = new StringWriter(1024);
            HTMLWriter writer = new HTMLWriter(sw, format);
            writer.write(transformedDoc);
            sb = sw.getBuffer();
            return sb.toString();

        } else {
            String csv = transformedDoc.getStringValue();
            csv.trim();
            return csv;
        }
    } catch (UnsupportedEncodingException uee) {
        logger.error("UnsupportedEncodingException");
        logger.error(uee);
    } catch (IOException ioe) {
        logger.error("IOException");
        logger.error(ioe);
    }

    return "Reporting Error";
}

From source file:jp.aegif.alfresco.online_webdav.WebDAVMethod.java

License:Open Source License

/**
 * Returns the format required for an XML response. This may vary per method.
 *//*from   w  w  w  .  ja v a2 s.co m*/
protected OutputFormat getXMLOutputFormat() {
    // Check if debug output or XML pretty printing is enabled
    return (XMLPrettyPrint || logger.isDebugEnabled()) ? OutputFormat.createPrettyPrint()
            : OutputFormat.createCompactFormat();
}

From source file:net.contextfw.web.application.internal.WebResponder.java

License:Apache License

@SuppressWarnings("unchecked")
@Inject// www .ja va 2  s.  c  o  m
public WebResponder(Configuration configuration, Injector injector) {
    rootResourcePaths.add("net.contextfw.web.application");
    transformers = new Transformers();
    resourcePaths.addAll(configuration.get(Configuration.RESOURCE_PATH));
    namespaces.addAll(configuration.get(Configuration.NAMESPACE));

    htmlFormat = OutputFormat.createCompactFormat();
    htmlFormat.setXHTML(true);
    htmlFormat.setTrimText(false);
    htmlFormat.setPadText(true);
    htmlFormat.setNewlines(false);
    htmlFormat.setExpandEmptyElements(true);

    if (configuration.get(Configuration.XSL_POST_PROCESSOR) != null) {
        xslPostProcessor = Utils.toInstance(configuration.get(Configuration.XSL_POST_PROCESSOR), injector);
    } else {
        xslPostProcessor = null;
    }
    if (configuration.get(Configuration.LOG_XML)) {
        Object obj = configuration.get(Configuration.XML_RESPONSE_LOGGER);
        if (obj instanceof XMLResponseLogger) {
            responseLogger = (XMLResponseLogger) obj;
        } else if (obj instanceof Class && XMLResponseLogger.class.isAssignableFrom((Class<?>) obj)) {
            responseLogger = injector.getInstance((Class<XMLResponseLogger>) obj);
        } else {
            responseLogger = null;
        }
    } else {
        responseLogger = null;
    }
}

From source file:org.alfresco.module.org_alfresco_module_wcmquickstart.util.AssetSerializerXmlImpl.java

License:Open Source License

public void start(Writer underlyingWriter) throws AssetSerializationException {
    try {// w  w w  .  j  a v a 2  s  .co m
        OutputFormat format = OutputFormat.createCompactFormat();
        format.setEncoding("UTF-8");

        writer = new XMLWriter(underlyingWriter, format);
        writer.startDocument();
        startElement("assets", EMPTY_ATTRIBUTES);
    } catch (Exception ex) {
        throw new AssetSerializationException(ex);
    }
}

From source file:org.alfresco.repo.transfer.XMLWriter.java

License:Open Source License

public XMLWriter(OutputStream outputStream, boolean prettyPrint, String encoding)
        throws UnsupportedEncodingException {
    OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);//  w  w w  .j av  a 2 s .  co m
    format.setEncoding(encoding);
    output = outputStream;
    this.dom4jWriter = new org.dom4j.io.XMLWriter(outputStream, format);
}

From source file:org.alfresco.repo.webdav.PropFindMethod.java

License:Open Source License

@Override
protected OutputFormat getXMLOutputFormat() {
    String userAgent = m_request.getHeader("User-Agent");
    return ((null != userAgent) && userAgent.toLowerCase().startsWith("microsoft-webdav-miniredir/5.1."))
            ? OutputFormat.createCompactFormat()
            : super.getXMLOutputFormat();

}

From source file:org.compass.core.xml.dom4j.converter.AbstractXmlWriterXmlContentConverter.java

License:Apache License

/**
 * Converts the {@link XmlObject} (assumes it is a {@link org.compass.core.xml.dom4j.Dom4jXmlObject}) into
 * an xml string. Uses dom4j <code>XmlWriter</code> and <code>OutputFormat</code>
 * (in a compact mode) to perform it.//from www.  ja va  2 s  . c  o m
 *
 * @param xmlObject The xml object to convert into an xml string (must be a {@link org.compass.core.xml.dom4j.Dom4jXmlObject} implementation).
 * @return An xml string representation of the xml object
 * @throws ConversionException Should not really happne...
 */
public String toXml(XmlObject xmlObject) throws ConversionException {
    Dom4jXmlObject dom4jXmlObject = (Dom4jXmlObject) xmlObject;
    StringBuilderWriter stringWriter = StringBuilderWriter.Cached.cached();
    OutputFormat outputFormat = null;
    if (compact) {
        outputFormat = OutputFormat.createCompactFormat();
    }
    XMLWriter xmlWriter;
    if (outputFormat != null) {
        xmlWriter = new XMLWriter(stringWriter, outputFormat);
    } else {
        xmlWriter = new XMLWriter(stringWriter);
    }
    try {
        xmlWriter.write(dom4jXmlObject.getNode());
        xmlWriter.close();
    } catch (IOException e) {
        throw new ConversionException("This should not happen", e);
    }
    return stringWriter.toString();
}

From source file:org.craftercms.core.util.xml.marshalling.xstream.CrafterXStreamMarshaller.java

License:Open Source License

/**
 * Just as super(), but instead of a {@link com.thoughtworks.xstream.io.xml.CompactWriter} creates a {@link
 * EscapingCompactWriter}.//from   w  w  w . j a  va 2 s.  c o  m
 * Also if the object graph is a Dom4j document, the document is written directly instead of using XStream.
 */
@Override
public void marshalWriter(Object graph, Writer writer, DataHolder dataHolder)
        throws XmlMappingException, IOException {
    if (graph instanceof Document) {
        OutputFormat outputFormat = OutputFormat.createCompactFormat();
        outputFormat.setSuppressDeclaration(suppressXmlDeclaration);

        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        try {
            xmlWriter.write((Document) graph);
        } finally {
            try {
                xmlWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush XMLWriter", ex);
            }
        }
    } else {
        if (!suppressXmlDeclaration) {
            writer.write(XML_DECLARATION);
        }

        HierarchicalStreamWriter streamWriter = new EscapingCompactWriter(writer);
        try {
            getXStream().marshal(graph, streamWriter, dataHolder);
        } catch (Exception ex) {
            throw convertXStreamException(ex, true);
        } finally {
            try {
                streamWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush HierarchicalStreamWriter", ex);
            }
        }
    }
}