Example usage for org.dom4j.io OutputFormat createPrettyPrint

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

Introduction

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

Prototype

public static OutputFormat createPrettyPrint() 

Source Link

Document

A static helper method to create the default pretty printing format.

Usage

From source file:org.alfresco.repo.transfer.manifest.XMLTransferManifestWriter.java

License:Open Source License

/**
 * Start the transfer manifest// w  w  w.  j a v  a 2s  .c o m
 */
public void startTransferManifest(Writer writer) throws SAXException {
    format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding("UTF-8");

    this.writer = new XMLWriter(writer, format);
    this.writer.startDocument();

    this.writer.startPrefixMapping(PREFIX, TransferModel.TRANSFER_MODEL_1_0_URI);
    this.writer.startPrefixMapping("cm", NamespaceService.CONTENT_MODEL_1_0_URI);

    // Start Transfer Manifest // uri, name, prefix
    this.writer.startElement(TransferModel.TRANSFER_MODEL_1_0_URI, ManifestModel.LOCALNAME_TRANSFER_MAINIFEST,
            PREFIX + ":" + ManifestModel.LOCALNAME_TRANSFER_MAINIFEST, EMPTY_ATTRIBUTES);
}

From source file:org.alfresco.repo.transfer.report.XMLTransferReportWriter.java

License:Open Source License

/**
 * Start the transfer report//from w w  w  . j  av  a2  s . c  o  m
 */
public void startTransferReport(String encoding, Writer writer) throws SAXException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding(encoding);

    this.writer = new XMLWriter(writer, format);
    this.writer.startDocument();

    this.writer.startPrefixMapping(PREFIX, TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI);

    // Start Transfer Manifest  // uri, name, prefix
    this.writer.startElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI,
            TransferReportModel.LOCALNAME_TRANSFER_REPORT,
            PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_REPORT, EMPTY_ATTRIBUTES);
}

From source file:org.alfresco.repo.transfer.reportd.XMLTransferDestinationReportWriter.java

License:Open Source License

/**
 * Start the transfer report/*  w w w  .  j  av a 2s.  co m*/
 */
public void startTransferReport(String encoding, Writer writer) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding(encoding);

    try {

        this.writer = new XMLWriter(writer, format);
        this.writer.startDocument();

        this.writer.startPrefixMapping(PREFIX, TransferDestinationReportModel.TRANSFER_REPORT_MODEL_1_0_URI);

        // Start Transfer Manifest  // uri, name, prefix
        this.writer.startElement(TransferDestinationReportModel.TRANSFER_REPORT_MODEL_1_0_URI,
                TransferDestinationReportModel.LOCALNAME_TRANSFER_DEST_REPORT,
                PREFIX + ":" + TransferDestinationReportModel.LOCALNAME_TRANSFER_DEST_REPORT, EMPTY_ATTRIBUTES);

    } catch (SAXException se) {
        se.printStackTrace();
    }
}

From source file:org.alfresco.repo.transfer.requisite.XMLTransferRequsiteWriter.java

License:Open Source License

public XMLTransferRequsiteWriter(Writer out) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);/*from ww w. ja  va2 s . com*/
    format.setEncoding("UTF-8");

    this.writer = new XMLWriter(out, format);
}

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);//from  w  w w.ja v  a 2s  .  co m
    format.setEncoding(encoding);
    output = outputStream;
    this.dom4jWriter = new org.dom4j.io.XMLWriter(outputStream, format);
}

From source file:org.alfresco.web.bean.dashboard.PageConfig.java

License:Open Source License

/**
 * Convert this config to an XML definition which can be serialized.
 * Example://from w  ww.  ja v a 2  s . com
 * <code>
 * <?xml version="1.0"?>
 * <dashboard>
 *    <page id="main" layout-id="narrow-left-2column">
 *       <column>
 *          <dashlet idref="clock" />
 *          <dashlet idref="random-joke" />
 *       </column>
 *       <column>
 *          <dashlet idref="getting-started" />
 *          <dashlet idref="task-list" />
 *          <dashlet idref="my-checkedout-docs" />
 *          <dashlet idref="my-documents" />
 *       </column>
 *    </page>
 * </dashboard>
 * </code>
 * 
 * @return XML for this config
 */
public String toXML() {
    try {
        Document doc = DocumentHelper.createDocument();

        Element root = doc.addElement(ELEMENT_DASHBOARD);
        for (Page page : pages) {
            Element pageElement = root.addElement(ELEMENT_PAGE);
            pageElement.addAttribute(ATTR_ID, page.getId());
            pageElement.addAttribute(ATTR_LAYOUTID, page.getLayoutDefinition().Id);
            for (Column column : page.getColumns()) {
                Element columnElement = pageElement.addElement(ELEMENT_COLUMN);
                for (DashletDefinition dashletDef : column.getDashlets()) {
                    columnElement.addElement(ELEMENT_DASHLET).addAttribute(ATTR_REFID, dashletDef.Id);
                }
            }
        }

        StringWriter out = new StringWriter(512);
        XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
        writer.setWriter(out);
        writer.write(doc);

        return out.toString();
    } catch (Throwable err) {
        throw new AlfrescoRuntimeException(
                "Unable to serialize Dashboard PageConfig to XML: " + err.getMessage(), err);
    }
}

From source file:org.alfresco.web.bean.search.SearchContext.java

License:Open Source License

/**
 * @return this SearchContext as XML/*from  w w  w.  ja va 2s.  c o m*/
 * 
 * Example:
 * <code>
 * <?xml version="1.0" encoding="UTF-8"?>
 * <search>
 *    <text>CDATA</text>
 *    <mode>int</mode>
 *    <location>XPath</location>
 *    <categories>
 *       <category>XPath</category>
 *    </categories>
 *    <content-type>String</content-type>
 *    <folder-type>String</folder-type>
 *    <mimetype>String</mimetype>
 *    <attributes>
 *       <attribute name="String">String</attribute>
 *    </attributes>
 *    <ranges>
 *       <range name="String">
 *          <lower>String</lower>
 *          <upper>String</upper>
 *          <inclusive>boolean</inclusive>
 *       </range>
 *    </ranges>
 *    <fixed-values>
 *       <value name="String">String</value>
 *    </fixed-values>
 *    <query>CDATA</query>
 * </search>
 * </code>
 */
public String toXML() {
    try {
        NamespaceService ns = Repository.getServiceRegistry(FacesContext.getCurrentInstance())
                .getNamespaceService();

        Document doc = DocumentHelper.createDocument();

        Element root = doc.addElement(ELEMENT_SEARCH);

        root.addElement(ELEMENT_TEXT).addCDATA(this.text);
        root.addElement(ELEMENT_MODE).addText(Integer.toString(this.mode));
        if (this.location != null) {
            root.addElement(ELEMENT_LOCATION).addText(this.location);
        }

        Element categories = root.addElement(ELEMENT_CATEGORIES);
        for (String path : this.categories) {
            categories.addElement(ELEMENT_CATEGORY).addText(path);
        }

        if (this.contentType != null) {
            root.addElement(ELEMENT_CONTENT_TYPE).addText(this.contentType);
        }
        if (this.folderType != null) {
            root.addElement(ELEMENT_FOLDER_TYPE).addText(this.folderType);
        }
        if (this.mimeType != null && this.mimeType.length() != 0) {
            root.addElement(ELEMENT_MIMETYPE).addText(this.mimeType);
        }

        Element attributes = root.addElement(ELEMENT_ATTRIBUTES);
        for (QName attrName : this.queryAttributes.keySet()) {
            attributes.addElement(ELEMENT_ATTRIBUTE).addAttribute(ELEMENT_NAME, attrName.toPrefixString(ns))
                    .addCDATA(this.queryAttributes.get(attrName));
        }

        Element ranges = root.addElement(ELEMENT_RANGES);
        for (QName rangeName : this.rangeAttributes.keySet()) {
            RangeProperties rangeProps = this.rangeAttributes.get(rangeName);
            Element range = ranges.addElement(ELEMENT_RANGE);
            range.addAttribute(ELEMENT_NAME, rangeName.toPrefixString(ns));
            range.addElement(ELEMENT_LOWER).addText(rangeProps.lower);
            range.addElement(ELEMENT_UPPER).addText(rangeProps.upper);
            range.addElement(ELEMENT_INCLUSIVE).addText(Boolean.toString(rangeProps.inclusive));
        }

        Element values = root.addElement(ELEMENT_FIXED_VALUES);
        for (QName valueName : this.queryFixedValues.keySet()) {
            values.addElement(ELEMENT_VALUE).addAttribute(ELEMENT_NAME, valueName.toPrefixString(ns))
                    .addCDATA(this.queryFixedValues.get(valueName));
        }

        // outputing the full lucene query may be useful for some situations
        Element query = root.addElement(ELEMENT_QUERY);
        String queryString = buildQuery(0);
        if (queryString != null) {
            query.addCDATA(queryString);
        }

        StringWriter out = new StringWriter(1024);
        XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
        writer.setWriter(out);
        writer.write(doc);

        return out.toString();
    } catch (Throwable err) {
        throw new AlfrescoRuntimeException("Failed to export SearchContext to XML.", err);
    }
}

From source file:org.apache.archiva.xml.XMLWriter.java

License:Apache License

/**
 * Write the Document to the provided Writer, with an option to close the writer upon completion.
 * //  w  w  w. ja  v  a  2s.  com
 * @param doc the document to write.
 * @param writer the writer to write to.
 * @param close true to close the writer on completion.
 * @throws XMLException if there was a problem writing the xml to the writer.
 */
public static void write(Document doc, Writer writer, boolean close) throws XMLException {
    org.dom4j.io.XMLWriter xmlwriter = null;

    try {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        xmlwriter = new org.dom4j.io.XMLWriter(writer, outputFormat);
        xmlwriter.write(doc);
        xmlwriter.flush();
    } catch (IOException e) {
        throw new XMLException("Unable to write xml contents to writer: " + e.getMessage(), e);
    } finally {
        if (close && (xmlwriter != null)) {
            try {
                xmlwriter.close();
            } catch (IOException e) {
                /* quietly ignore */
            }
        }
    }
}

From source file:org.apache.commons.jelly.tags.core.FileTag.java

License:Apache License

/**
 * A Factory method to create a new XMLOutput from the given Writer.
 *///w w  w  .jav  a2s.c  om
protected XMLOutput createXMLOutput(Writer writer) {

    OutputFormat format = null;
    if (prettyPrint) {
        format = OutputFormat.createPrettyPrint();
    } else {
        format = new OutputFormat();
    }
    if (encoding != null) {
        format.setEncoding(encoding);
    }
    if (omitXmlDeclaration) {
        format.setSuppressDeclaration(true);
    }

    boolean isHtml = outputMode != null && outputMode.equalsIgnoreCase("html");
    final XMLWriter xmlWriter = (isHtml) ? new HTMLWriter(writer, format) : new XMLWriter(writer, format);

    xmlWriter.setEscapeText(isEscapeText());

    XMLOutput answer = new XMLOutput() {
        public void close() throws IOException {
            xmlWriter.close();
        }
    };
    answer.setContentHandler(xmlWriter);
    answer.setLexicalHandler(xmlWriter);
    return answer;
}

From source file:org.apache.ddlutils.task.DumpMetadataTask.java

License:Apache License

/**
 * {@inheritDoc}//from  w w w .  j  a v a  2s .  c o m
 */
public void execute() throws BuildException {
    if (_dataSource == null) {
        log("No data source specified, so there is nothing to do.", Project.MSG_INFO);
        return;
    }

    Connection connection = null;
    try {
        Document document = DocumentFactory.getInstance().createDocument();
        Element root = document.addElement("metadata");

        root.addAttribute("driverClassName", _dataSource.getDriverClassName());

        connection = _dataSource.getConnection();

        dumpMetaData(root, connection.getMetaData());

        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = null;

        outputFormat.setEncoding(_outputEncoding);
        if (_outputFile == null) {
            xmlWriter = new XMLWriter(System.out, outputFormat);
        } else {
            xmlWriter = new XMLWriter(new FileOutputStream(_outputFile), outputFormat);
        }
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (Exception ex) {
        throw new BuildException(ex);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }
}