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:com.pureinfo.dolphin.export.impl.XmlExporterImpl.java

License:Open Source License

/**
 * @see com.pureinfo.dolphin.export.IExporter#export(OutputStream,
 *      DolphinExportGoods)/*w ww . ja va 2  s  .co  m*/
 */
public void export(OutputStream _os, IExportGoods _goods) throws PureException {
    try {
        //1. to create XML element
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement(_goods.getName());

        //2. to prepare column names
        if (!_goods.hasHeader()) {
            throw new PureRuntimeException(PureException.SETTING_MISSING, "headers required in goods");
        }
        String[] colNames = _goods.getHeaders();

        //3. to export data
        Object[] values;
        Iterator iter = _goods.iterator();
        while (iter.hasNext()) {
            values = _goods.unpackGoods(iter.next());
            exportRow(root, values, colNames);
        }

        //4. to output to stream
        new XMLWriter(_os, OutputFormat.createPrettyPrint()).write(doc);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}

From source file:com.pureinfo.srm.config.affirm.domain.AffirmMgrImpl.java

License:Open Source License

public void store() throws PureException {
    if (!isLoaded())
        return;/*from ww w .j a  v  a  2s  .com*/
    synchronized (this) {
        Element root = DocumentHelper.createElement(ELEMENT_AFFIRMS);
        m_affirm.toXMLElement(root);
        String sFileName = ClassResourceUtil.mapFullPath(DATA_FILENAME, true);
        try {
            new XMLWriter(new FileOutputStream(sFileName), OutputFormat.createPrettyPrint())
                    .write(DocumentHelper.createDocument(root));
        } catch (Exception ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        }
    }
}

From source file:com.pureinfo.srm.config.notice.domain.NoticeMgrImpl.java

License:Open Source License

public void store() throws PureException {
    if (!isLoaded())
        return;//  w w  w  .ja  v a2 s.  com
    synchronized (this) {
        Element root = DocumentHelper.createElement(ELEMENT_NOTICES);
        for (Iterator iter = m_notices.values().iterator(); iter.hasNext();) {
            Notice notice = (Notice) iter.next();
            notice.toXMLElement(root.addElement(ELEMENT_NOTICE));
        }
        String sFileName = ClassResourceUtil.mapFullPath(DATA_FILENAME, true);
        try {
            new XMLWriter(new FileOutputStream(sFileName), OutputFormat.createPrettyPrint())
                    .write(DocumentHelper.createDocument(root));
        } catch (Exception ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        }
    }
}

From source file:com.qagen.osfe.programData.readerWriter.ReaderWriter.java

License:Apache License

protected void saveDocument() {
    try {/*from w  w w. ja v  a2s .  c om*/
        final OutputFormat format = OutputFormat.createPrettyPrint();
        final FileWriter writer = new FileWriter(fileName);
        final XMLWriter xmlWriter = new XMLWriter(writer, format);

        xmlWriter.write(document);
        xmlWriter.close();
    } catch (IOException e) {
        throw new ProgramDataException(e);
    }
}

From source file:com.rockagen.commons.util.XmlUtil.java

License:Apache License

/**
 * Format xml {@link OutputFormat#createPrettyPrint()}
 *
 * @param xmlStr                xml String
 * @param enc                   encoding
 * @param isSuppressDeclaration set supperss declaration
 * @return pretty String/*from   ww  w  .  j a v  a2  s .com*/
 */
public static String formatPretty(String xmlStr, String enc, boolean isSuppressDeclaration) {

    if (CommUtil.isBlank(xmlStr))
        return xmlStr;

    if (enc == null)
        enc = ENCODING;

    OutputFormat formater = OutputFormat.createPrettyPrint();
    formater.setEncoding(enc);
    formater.setSuppressDeclaration(isSuppressDeclaration);
    return format(xmlStr, formater);
}

From source file:com.rowtheboat.gui.OptionsSingleton.java

License:Open Source License

/**
 * This method saves the options to the options.xml file
 *///from w  ww .  j a v  a  2s.  c o  m
public void saveOptions() throws SAXException, IOException {

    /* Start the document */
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(optionsFile), format);
    writer.startDocument();

    /* Add the main options section */
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("Options");

    /* Standard options */
    DefaultElement standardElement = new DefaultElement("Standard");
    standardElement.addElement("FullStrokeData").addText(getFullStrokeData() + "");
    standardElement.addElement("BoatSmoothing").addText(getBoatSmoothing() + "");
    root.add(standardElement);

    /* Input options */
    DefaultElement inputElement = new DefaultElement("Input");
    inputElement.addElement("SerialPort").addText(getSerialPort());
    root.add(inputElement);

    /* Race options */
    DefaultElement raceElement = new DefaultElement("Race");
    raceElement.addElement("Countdown").addText(getDelay() + "");
    root.add(raceElement);

    /* End the document */
    writer.write(root);
    writer.endDocument();
    writer.close();
}

From source file:com.safi.workshop.sqlexplorer.history.SQLHistory.java

License:Open Source License

/**
 * Save all the used queries into a file, so that we can reuse them next time.
 *//* w w w.j  ava 2 s .c  om*/
public void save() {

    try {
        File file = new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V350);
        Element root = new DefaultElement(HISTORY);
        for (SQLHistoryElement elem : _history)
            root.add(elem.describeAsXml());
        XMLWriter xmlWriter = new XMLWriter(new FileWriter(file), OutputFormat.createPrettyPrint());
        xmlWriter.write(root);
        xmlWriter.flush();
        xmlWriter.close();

        // Get rid of old versions
        new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V300).delete();

    } catch (IOException e) {
        SQLExplorerPlugin.error("Couldn't save sql history.", e);
    }

}

From source file:com.sammyun.util.SettingUtils.java

License:Open Source License

/**
 * //w  w  w  .  j av  a2 s. co  m
 * 
 * @param setting 
 */
public static void set(Setting setting) {
    try {
        File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
        Document document = new SAXReader().read(preschoolEduXmlFile);
        List<Element> elements = document.selectNodes("/preschoolEdu/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(preschoolEduXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
    }
}

From source file:com.sap.data.db.dao.StructureUtil.java

private void save(Document document, String filepath) throws IOException {
    document.addDocType(this.HIBERNATE_MAPPING, this.HIBERNATE_MAPPING_EN, this.HIBERNATE_MAPPING_DTD);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    XMLWriter writer = new XMLWriter(new FileWriter(new File(filepath)), format);
    writer.write(document);//from ww w  .j av  a2 s  . com
    writer.close();
}

From source file:com.servoy.extension.install.LibActivationHandler.java

License:Open Source License

protected void writeBackXML(Document doc, File f, String encoding)
        throws IOException, TransformerFactoryConfigurationError, TransformerException, DocumentException {
    BufferedOutputStream os = null;
    try {// ww w . j av a2 s. co m
        os = new BufferedOutputStream(new FileOutputStream(f));
        doc.normalize();
        StringWriter sw = new StringWriter(1024);
        DOMSource source = new DOMSource(doc);
        Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
        newTransformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        newTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        newTransformer.transform(source, new StreamResult(sw));

        // normally the transformer code above should have been enough for producing pretty formatted XML (needed so that repeated remove/restore of tags
        // doesn't produce endless newlines or other bad looking XML); but it seems that with some versions of the JDK that doesn't do it's job so we use dom4j
        final OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        final XMLWriter writer = new XMLWriter(os, format);
        writer.write(DocumentHelper.parseText(sw.toString()));
    } finally {
        Utils.closeOutputStream(os);
    }

}