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.liferay.alloy.tools.xmlbuilder.XMLBuilder.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("taglibs");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));

        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");
            Element nameNode = attributeNode.addElement("name");
            Element inputTypeNode = attributeNode.addElement("inputType");
            Element outputTypeNode = attributeNode.addElement("outputType");
            Element defaultValueNode = attributeNode.addElement("defaultValue");

            Element descriptionNode = attributeNode.addElement("description");

            nameNode.setText(attribute.getName());
            inputTypeNode.setText(attribute.getInputType());
            outputTypeNode.setText(attribute.getOutputType());
            defaultValueNode.setText(attribute.getDefaultValue());
            descriptionNode.addCDATA(_getAttributeDescription(attribute));
        }//  w ww . j a  v a  2 s .  c  om

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element descriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            typeNode.setText(event.getInputType());
            descriptionNode.addCDATA(_getAttributeDescription(event));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(_componentXML);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _componentXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.petra.xml.Dom4jUtil.java

License:Open Source License

public static String toString(Node node, String indent, boolean expandEmptyElements, boolean trimText)
        throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();

    outputFormat.setExpandEmptyElements(expandEmptyElements);
    outputFormat.setIndent(indent);/*  w  w  w  .j a v  a2s . c o m*/
    outputFormat.setLineSeparator(StringPool.NEW_LINE);
    outputFormat.setTrimText(trimText);

    XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat);

    xmlWriter.write(node);

    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.contains(" \n")) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
        content = StringUtil.replaceFirst(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
                "<?xml version=\"1.0\"?>");
    }

    return content;
}

From source file:com.liferay.util.xml.XMLFormatter.java

License:Open Source License

public static String toString(Document doc, String indent) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setIndent(indent);//  www. java 2 s  .com
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(doc);

    String content = baos.toString();

    content = StringUtil.replace(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
            "<?xml version=\"1.0\"?>");

    int x = content.indexOf("<!DOCTYPE");
    if (x != -1) {
        x = content.indexOf(">", x) + 1;
        content = content.substring(0, x) + "\n" + content.substring(x, content.length());
    }

    content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    return content;
}

From source file:com.liferay.util.xml.XMLMergerRunner.java

License:Open Source License

private String _documentToString(Document doc, String docType) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setIndent("\t");
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(doc);/* w ww . ja  va2  s.  c om*/

    String xml = baos.toString();

    int pos = xml.indexOf("<?");

    String header = xml.substring(pos, xml.indexOf("?>", pos) + 2);

    xml = StringUtil.replace(xml, header, "");
    xml = header + "\n" + docType + "\n" + xml;

    return xml;
}

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

License:Open Source License

public static void set(Setting setting) {
    try {//from  ww w.j  av  a  2s .c  o  m
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/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();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        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(shopxxXmlFile);
            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();
    }
}

From source file:com.liveneo.plat.web.action.MakeLicfile.java

public static void createXmlFile(String fileName, Document doc) {
    try {//from  w  w w .  j  a  va  2 s  . co  m
        File testfile = new File("fileName");
        if (testfile.exists()) {
            testfile.delete();
        }
        FileWriter fileWriter = new FileWriter(fileName);
        OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
        xmlFormat.setEncoding("UTF-8");
        xmlFormat.setSuppressDeclaration(false);
        xmlFormat.setExpandEmptyElements(false);
        XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat);
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.love320.templateparser.label.impl.LabelBeanDaoImpl.java

License:Apache License

private boolean XMLWriter() {
    try {/*  w  ww  .j  a  v  a2  s.  c  o m*/
        OutputFormat format = OutputFormat.createPrettyPrint();//?
        format.setEncoding("UTF-8");//?
        XMLWriter output = new XMLWriter(new FileWriter(new File(configPath)), format);//?
        output.write(DOCROOT.getDocument());//
        output.close();//

        return true;
    } catch (IOException e) {
        logger.error("IOException", e);
    }
    return false;
}

From source file:com.magicpwd._util.Jxml.java

License:Open Source License

public static void save(Document document, File file) throws IOException {
    // ?/*from  w  w w. j a  va 2  s .co  m*/
    OutputFormat format = OutputFormat.createPrettyPrint();
    // ??
    // OutputFormat format = OutputFormat.createCompactFormat();
    // XML?
    // format.setEncoding("GBK");
    XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    writer.write(document);
    writer.close();
}

From source file:com.ms.commons.test.treedb.Objects2XmlFileUtil.java

License:Open Source License

private static String prettyPrint(final String xml) {

    if (StringUtils.isBlank(xml)) {
        throw new RuntimeException("xml was null or blank in prettyPrint()");
    }//  www.  ja  v a 2  s  .  c o  m

    final StringWriter sw;

    try {
        final OutputFormat format = OutputFormat.createPrettyPrint();
        final org.dom4j.Document document = DocumentHelper.parseText(xml);
        sw = new StringWriter();
        final XMLWriter writer = new XMLWriter(sw, format);
        writer.write(document);
    } catch (Exception e) {
        throw new RuntimeException("Error pretty printing xml:\n" + xml, e);
    }
    return sw.toString();
}

From source file:com.nokia.ant.Database.java

License:Open Source License

public void createXMLFile(File outputFile) {
    try {/*from  w ww.ja  v a2s. c om*/
        Document outDoc = createDOM();
        OutputStream outStream = System.out;
        if (outputFile != null) {
            outStream = new FileOutputStream(outputFile);
        }
        XMLWriter out = new XMLWriter(outStream, OutputFormat.createPrettyPrint());
        out.write(outDoc);
    } catch (Exception e) {
        throw new BuildException(e.getMessage());
    }
}