Example usage for org.dom4j.io OutputFormat setIndentSize

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

Introduction

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

Prototype

public void setIndentSize(int indentSize) 

Source Link

Document

This will set the indent String's size; an indentSize of 4 would result in the indention being equivalent to the String "    " (four space characters).

Usage

From source file:org.nuxeo.ecm.jsf2.migration.impl.MigrationServiceImpl.java

License:Open Source License

/**
 * Create a file containing the migration done in the Document.
 *
 * @param input//from  w ww  . j a  va2s . co m
 * @param filePath
 * @throws Exception
 */
protected void createFile(Document input, String filePath, boolean createNewFile) throws Exception {

    // Create file
    File fileMigrated = new File(filePath);
    if (createNewFile) {
        fileMigrated.createNewFile();
    }
    PrintWriter printWriter = new PrintWriter(fileMigrated);
    OutputFormat format = new OutputFormat();
    format.setIndentSize(2);
    format.setNewlines(true);
    format.setTrimText(true);
    XMLWriter writer = new XMLWriter(printWriter, format);
    writer.write(input);

    printWriter.close();
}

From source file:org.opencms.configuration.CmsConfigurationManager.java

License:Open Source License

/**
 * Writes the XML configuration for the provided configuration instance.<p>
 * /* ww w  .  java 2 s.c  om*/
 * @param clazz the configuration class to write the XML for
 * @throws IOException in case of I/O errors while writing
 * @throws CmsConfigurationException if the given class is not a valid configuration class
 */
public void writeConfiguration(Class<?> clazz) throws IOException, CmsConfigurationException {

    I_CmsXmlConfiguration configuration = getConfiguration(clazz);
    if (configuration == null) {
        throw new CmsConfigurationException(
                Messages.get().container(Messages.ERR_CONFIG_WITH_UNKNOWN_CLASS_1, clazz.getName()));
    }

    // generate the file URL for the XML input
    File file = new File(m_baseFolder, configuration.getXmlFileName());
    if (LOG.isDebugEnabled()) {
        LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITE_CONFIG_XMLFILE_1, file.getAbsolutePath()));
    }

    // generate the XML document 
    Document config = generateXml(configuration);

    // output the document
    XMLWriter writer = null;
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setIndentSize(4);
    format.setTrimText(false);
    format.setEncoding(CmsEncoder.ENCODING_UTF_8);

    try {
        OutputStream out = new FileOutputStream(file);
        writer = new XMLWriter(out, format);
        writer.write(config);
        writer.flush();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

    if (LOG.isInfoEnabled()) {
        LOG.info(Messages.get().getBundle().key(Messages.LOG_WRITE_CONFIG_SUCCESS_2, file.getAbsolutePath(),
                configuration.getClass().getName()));
    }
}

From source file:org.orbeon.oxf.transformer.xupdate.XUpdateDriver.java

License:Open Source License

public static void main(String[] args)
        throws TransformerException, IOException, ParserConfigurationException, SAXException {

    // Check arguments
    if (args.length != 2) {
        System.err.println("Syntax: java " + XUpdateDriver.class.getName() + " input.xml xupdate.xml");
        return;// ww w.  java  2 s  .com
    }

    // Perform transformation
    Templates templates = createTemplates(new FileReader(args[1]));
    final NonLazySAXContentHandler saxContentHandler = new NonLazySAXContentHandler();
    templates.newTransformer().transform(
            new SAXSource(newXMLReader(), new InputSource(new FileReader(args[0]))),
            new SAXResult(saxContentHandler));

    // Output result
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setIndentSize(4);
    XMLWriter xmlWriter = new XMLWriter(System.out, format);
    xmlWriter.write(saxContentHandler.getDocument());
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

/**
 * Convert a dom4j document to a pretty string, for formatting/debugging purposes only.
 *
 * @param document  document to convert//from   ww w. j a  v  a 2 s .  c  o  m
 * @return          resulting string
 */
public static String domToPrettyString(final Document document) {
    final OutputFormat format = new OutputFormat();
    format.setIndentSize(4);
    format.setNewlines(true);
    format.setTrimText(true);
    return domToString(document.getRootElement(), format);
}

From source file:org.projectforge.framework.xstream.XmlHelper.java

License:Open Source License

public static String toString(final Element el, final boolean prettyFormat) {
    if (el == null) {
        return "";
    }/*from   w  ww .  java2  s.  c  om*/
    final StringWriter out = new StringWriter();
    final OutputFormat format = new OutputFormat();
    if (prettyFormat == true) {
        format.setNewlines(true);
        format.setIndentSize(2);
    }
    final XMLWriter writer = new XMLWriter(out, format);
    String result = null;
    try {
        writer.write(el);
        result = out.toString();
        writer.close();
    } catch (final IOException ex) {
        log.error(ex.getMessage(), ex);
    }
    return result;
}

From source file:org.richfaces.cdk.generate.taglib.TaglibWriter.java

License:Open Source License

@Override
public void render(ComponentLibrary library) throws CdkException {
    TaglibGeneratorVisitor visitor = new TaglibGeneratorVisitor();
    library.accept(visitor, library);//from w w w  . j a  va 2s .co m
    if (!visitor.isEmpty()) {
        Document document = visitor.getDocument();
        try {
            OutputFormat format1 = OutputFormat.createPrettyPrint();
            format1.setIndentSize(4);

            XMLWriter writer = new XMLWriter(getOutput(library), format1);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace(); // TODO
        }
    }
}

From source file:org.suren.autotest.platform.util.DomUtils.java

License:Apache License

/**
 * xml?//from w w  w. ja va  2 s.c  o m
 * @param text
 * @return
 */
public static String format(String text) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes());

    SAXReader reader = new SAXReader();
    try {
        Document doc = reader.read(input);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndentSize(4);

        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
    } catch (DocumentException | UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return out.toString();
}

From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java

License:Apache License

/**
 * xml//from   w ww .jav  a 2  s  .c o  m
 * @see #write(Document, OutputFormat, String)
 * @param doc
 * @param resPath
 */
private void write(final Document doc, final String resPath) {
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setIndentSize(4);

    write(doc, outputFormat, resPath);
}

From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java

License:Apache License

/**
 * xml/* w w  w .  j a v a2s  . c o m*/
 * @param doc 
 * @param format ?
 * @param resPath 
 */
private void write(final Document doc, final OutputFormat format, final String resPath) {
    ClassLoader clsLoader = this.getClass().getClassLoader();
    URL url = clsLoader.getResource(resPath);

    String outputFileName = null;
    if (url != null) {
        outputFileName = new File(url.getFile()).getName();
    } else {
        outputFileName = new File(resPath).getName();
    }

    File outputDirFile = new File(outputDir);
    if (!outputDirFile.isDirectory()) {
        outputDirFile.mkdirs();
    }

    File outputFile = new File(outputDirFile, outputFileName);
    try (OutputStream dsOutput = new FileOutputStream(outputFile)) {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setIndentSize(4);
        XMLWriter xmlWriter = new XMLWriter(dsOutput, outputFormat);

        xmlWriter.write(doc);

        if (callback != null) {
            callback.callback(outputFile);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.suren.autotest.web.framework.code.DefaultXmlGenerator.java

License:Apache License

@Override
public void generate(String srcCoding, final String outputDir) {
    //???Page//from  w w  w  . j a v a2 s  .c  om
    Generator generator = new DefaultXmlCodeGenerator() {

        private String springConfigPath = "applicationContext.xml";
        private Set<String> allPackage = new HashSet<String>();

        @Override
        protected void create(AutoPage autoPage) {
            super.create(autoPage);

            allPackage.add(autoPage.getPackageName());
        }

        @Override
        protected void done() {
            super.done();

            if (allPackage.size() == 0) {
                return;
            }

            Document springConfigDoc = null;

            try (InputStream inputStream = getClassLoader().getResourceAsStream(springConfigPath)) {
                if (inputStream != null) {
                    springConfigDoc = new SAXReader().read(inputStream);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }

            if (springConfigDoc == null) {
                springConfigDoc = DocumentHelper.createDocument();

                String prefix = "p";
                Element root = springConfigDoc.addElement(prefix + ":beans");

                root.addNamespace(prefix, "http://www.springframework.org/schema/beans");
                root.addNamespace("context", "http://www.springframework.org/schema/context");
                root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.addAttribute("xsi:schemaLocation",
                        "http://www.springframework.org/schema/beans "
                                + "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "
                                + "http://www.springframework.org/schema/context "
                                + "http://www.springframework.org/schema/context/spring-context-3.0.xsd  ");
            }

            SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
            simpleNamespaceContext.addNamespace("p", "http://www.springframework.org/schema/beans");
            simpleNamespaceContext.addNamespace("context", "http://www.springframework.org/schema/context");

            XPath xpath = new DefaultXPath("/p:beans/context:component-scan");
            xpath.setNamespaceContext(simpleNamespaceContext);

            //?
            Element componentScanEle = (Element) xpath.selectSingleNode(springConfigDoc);
            if (componentScanEle == null) {
                componentScanEle = springConfigDoc.getRootElement().addElement("context:component-scan");
            }

            //pagepackage?
            String basePackage = componentScanEle.attributeValue("base-package", "");
            basePackage = basePackage.trim();
            if (!"".equals(basePackage) && !basePackage.endsWith(",")) {
                basePackage += ",";
            }

            basePackage.replaceAll(",\\s*", ",");
            for (String newPackage : allPackage) {
                if (!basePackage.contains(newPackage + ",")) {
                    basePackage += (newPackage + ",");
                }
            }

            componentScanEle.addAttribute("base-package", basePackage);

            //???
            File outputDirFile = new File(outputDir);
            if (!outputDirFile.isDirectory()) {
                outputDirFile.mkdirs();
            }

            try (OutputStream dsOutput = new FileOutputStream(new File(outputDirFile, springConfigPath))) {
                OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                outputFormat.setIndentSize(4);
                XMLWriter xmlWriter = new XMLWriter(dsOutput, outputFormat);

                xmlWriter.write(springConfigDoc);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    generator.generate(srcCoding, outputDir); //?

    generator = new DefaultXmlDataSourceGenerator();
    generator.generate(srcCoding, outputDir); //?

    generator = new DefaultXmlSuiteRunnerGenerator();
    generator.generate(srcCoding, outputDir); //?}
}