Example usage for org.apache.commons.io.output XmlStreamWriter XmlStreamWriter

List of usage examples for org.apache.commons.io.output XmlStreamWriter XmlStreamWriter

Introduction

In this page you can find the example usage for org.apache.commons.io.output XmlStreamWriter XmlStreamWriter.

Prototype

public XmlStreamWriter(File file, String defaultEncoding) throws FileNotFoundException 

Source Link

Document

Construct an new XML stream writer for the specified file with the specified default encoding.

Usage

From source file:com.c4om.autoconf.ulysses.configanalyzer.guilauncher.TemporaryChangesetFilesSavingGUILauncher.java

/**
 * This method generates the catalog file and stores it into the temporary folder
 * @param temporaryFolder the temporary folder
 * @param changesetPaths URIs poiting to changeset files
 * @throws CatalogGenerationException if there is any problem at catalog generation
 *///from   ww w  . j  av a 2s . c om
private void generateCatalog(File temporaryFolder, List<URI> changesetPaths) throws CatalogGenerationException {
    try {
        Map<QName, List<URI>> variables = ImmutableMap.of(new QName(FILES_PARAMETER_NAME), changesetPaths);
        String catalogFileString = saxonQuery(generateCatalogQuery, variables);
        FileOutputStream fileOutputStream = new FileOutputStream(
                new File(temporaryFolder, CATALOG_XML_FILE_NAME));
        Writer writer = new XmlStreamWriter(fileOutputStream, "UTF-8");
        writer.write(catalogFileString);
        writer.close();
    } catch (IOException | SaxonApiException e) {
        throw new CatalogGenerationException(e);
    }
}

From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java

/**
 * Method that prints a W3C {@link Document} object to a provided {@link OutputStream}. 
 * Encoding should be correctly specified.
 * @param document the document to convert
 * @param os the output stream to print to
 * @param indent whether lines must be indented or not
 * @param omitDeclaration whether the XML declaration should be omitted or not. 
 * @param encoding the encoding placed at the XML declaration and used to encode the file.
 * @return the {@link String} representation of the document
 * @throws TransformerException if there are problems during the conversion
 *///from ww w.  j ava2 s .  c om
public static void printW3CDocumentToOutputStream(Document document, OutputStream os, boolean indent,
        boolean omitDeclaration, String encoding) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    XmlStreamWriter writer;
    if (encoding != null) {
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        writer = new XmlStreamWriter(os, encoding);
    } else {
        writer = new XmlStreamWriter(os);
    }
    StreamResult streamResult = new StreamResult(writer);
    transformer.transform(new DOMSource(document), streamResult);
}

From source file:com.c4om.jschematronvalidator.JSchematronValidatorMain.java

/**
 * Prints a {@link org.w3c.dom.Document} to an {@link OutputStream}.
 * @param outputDocument The output document
 * @param outputStream The output stream
 * @param charset the charset.//  w w  w.j av a2s . c  o m
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
private static void printW3CDocumentToOutputStream(Document outputDocument, OutputStream outputStream,
        String charset)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    LOGGER.info("Printing W3C Document to an output stream with encoding '" + charset + "'");
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, charset);
    LOGGER.debug("XML output properties: " + transformer.getOutputProperties().toString());

    DOMSource source = new DOMSource(outputDocument);
    Writer outputWriter = new XmlStreamWriter(outputStream, charset);

    StreamResult result = new StreamResult(outputWriter);

    transformer.transform(source, result);
    LOGGER.info("Document printed");
}

From source file:org.codehaus.mojo.jspc.CompilationMojoSupport.java

private void writeWebXml() throws MojoExecutionException {
    if (!inputWebXml.exists()) {
        throw new MojoExecutionException("web.xml does not exist at: " + inputWebXml);
    }//from  ww w .  java  2 s  .  com
    if (!webFragmentFile.exists()) {
        throw new MojoExecutionException("web-fragment.xml does not exist at: " + webFragmentFile);
    }

    final String webXml = readXmlToString(inputWebXml);
    if (webXml.indexOf(injectString) == -1) {
        throw new MojoExecutionException(
                "web.xml does not contain inject string '" + injectString + "' - " + webFragmentFile);
    }

    getLog().debug("Injecting " + webFragmentFile + " into " + inputWebXml + " and copying to " + outputWebXml);

    final String fragmentXml = readXmlToString(webFragmentFile);

    String output = StringUtils.replace(webXml, injectString, fragmentXml);

    // If using the default, then tack on the end of the document
    if (DEFAULT_INJECT_STRING.equals(injectString)) {
        output += DEFAULT_INJECT_STRING;
    }

    // Write the jsp web.xml file
    final File workingWebXml = new File(workingDirectory, "jspweb.xml");
    XmlStreamWriter xmlStreamWriter = null;
    try {
        xmlStreamWriter = new XmlStreamWriter(workingWebXml, this.javaEncoding);
        IOUtils.write(output, xmlStreamWriter);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to write '" + outputWebXml
                + "' as XML file with default encoding: " + this.javaEncoding, e);
    } finally {
        IOUtils.closeQuietly(xmlStreamWriter);
    }

    // Make sure parent dirs exist
    outputWebXml.getParentFile().mkdirs();

    // Copy the file into place filtering it on the way
    final MavenFileFilterRequest request = new MavenFileFilterRequest();
    request.setEncoding(this.javaEncoding);
    request.setMavenSession(this.session);
    request.setMavenProject(this.project);
    request.setFiltering(this.filtering);

    request.setFrom(workingWebXml);
    request.setTo(outputWebXml);

    try {
        this.mavenFileFilter.copyFile(request);
    } catch (MavenFilteringException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}