Example usage for org.apache.commons.io IOUtils writeLines

List of usage examples for org.apache.commons.io IOUtils writeLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils writeLines.

Prototype

public static void writeLines(Collection lines, String lineEnding, Writer writer) throws IOException 

Source Link

Document

Writes the toString() value of each item in a collection to a Writer line by line, using the specified line ending.

Usage

From source file:org.toobsframework.mojo.XsdRelease.java

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
    int count = 0;
    int of = 0;/*from   w w  w .j  a  v a 2  s.  c o  m*/

    try {
        if (verbose) {
            getLog().info("release-xsd will attempt copy versions of xsds from directory " + source.getPath()
                    + " to directory " + target.getPath() + " with version " + version);
        }

        if (!source.exists()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + source.getPath() + " since the directory does not exists");
        }
        if (!source.isDirectory()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + source.getPath() + " since it is not a directory");
        }

        if (verbose) {
            getLog().info(source.getPath() + " directory was located");
        }

        if (!target.exists()) {
            if (!target.mkdirs()) {
                throw new MojoExecutionException("Cannot create the targetdirectory " + target.getPath());
            }
            if (verbose) {
                getLog().info(target.getPath() + " directory was created");
            }
        } else if (!target.isDirectory()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + target.getPath() + " since it is not a directory");
        } else {
            if (verbose) {
                getLog().info(target.getPath() + " directory was located");
            }
        }

        File[] sourceFiles = source.listFiles((FileFilter) new SuffixFileFilter(".xsd"));
        for (File inputFile : sourceFiles) {
            of++;

            String fileName = replace(inputFile.getName(), ".xsd", "-" + version + ".xsd");
            File outputFile = new File(target, fileName);

            if (outputFile.exists() && FileUtils.isFileNewer(outputFile, inputFile)) {
                if (verbose) {
                    getLog().info("File " + inputFile.getPath() + " is newer than " + outputFile.getPath()
                            + ".  Skipped.");
                }
                continue;
            }

            if (verbose) {
                getLog().info("copying file " + inputFile.getPath() + " to " + outputFile.getPath());
            }

            InputStream inputStream = new FileInputStream(inputFile);
            List<String> inputLines;
            try {
                inputLines = IOUtils.readLines(inputStream);
            } finally {
                inputStream.close();
            }

            List<String> outputLines = new ArrayList<String>(inputLines.size());
            for (String inputLine : inputLines) {
                String line = fixupLine(inputLine);
                outputLines.add(line);
                /*getLog().info(">>" + line);*/
            }

            OutputStream outputStream = new FileOutputStream(outputFile, false);
            try {
                IOUtils.writeLines(outputLines, "\n", outputStream);
            } finally {
                outputStream.close();
            }
            count++;
        }

        if (count == 0) {
            getLog().info("No xsds released - " + of + " are up to date");
        } else {
            getLog().info(count + " xsd(s) out of " + of + " were sucessfully updated");
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error occurred when trying to release xsds: " + e.getMessage(), e);
    }
}

From source file:org.xwiki.extension.repository.internal.DefaultExtensionSerializer.java

private void addLicenses(Document document, Element parentElement, Extension extension) {
    if (extension.getLicenses() != null && !extension.getLicenses().isEmpty()) {
        Element licensesElement = document.createElement(ELEMENT_LICENSES);
        parentElement.appendChild(licensesElement);

        for (ExtensionLicense license : extension.getLicenses()) {
            Element licenseElement = document.createElement(ELEMENT_LLICENSE);
            licensesElement.appendChild(licenseElement);

            addElement(document, licenseElement, ELEMENT_LLNAME, license.getName());
            if (this.licenseManager.getLicense(license.getName()) == null && license.getContent() != null) {
                // Only store content if it's a custom license (license content is pretty big generally)
                StringWriter content = new StringWriter();
                try {
                    IOUtils.writeLines(license.getContent(), IOUtils.LINE_SEPARATOR_UNIX, content);
                } catch (IOException e) {
                    // That should never happen
                }//from  ww  w  .  ja  v  a2  s  .c o m
                addElement(document, licenseElement, ELEMENT_LLCONTENT, content.toString());
            }
        }
    }
}

From source file:pt.webdetails.cdc.hazelcast.CfgTestApp.java

protected void initCommands() {

    commands = new HashMap<String, Command>();

    this.new Command("m.binKeys", "shows serialized map keys", "") {
        public void run() {
            for (Object key : getMap().keySet()) {
                println(key);//ww  w  .j ava  2  s  .co  m
                byte[] bytes = serializeObject(key);
                println(toHexString(bytes));
                println("#######################");
            }
        }
    };

    this.new Command("m.dumpKeys", "prints serialized map keys to a file", "") {
        public void run() {
            String fileName = getMap().getName() + "_" + System.currentTimeMillis() + ".txt";
            File dumpFile = new File(fileName);
            FileOutputStream fos = null;
            try {
                dumpFile.createNewFile();
                fos = new FileOutputStream(dumpFile);
                ArrayList<String> lines = new ArrayList<String>();
                for (Object key : getMap().keySet()) {
                    lines.add(key.toString());
                }
                IOUtils.writeLines(lines, System.getProperty("line.separator"), fos);
                println("keys dumped to " + dumpFile.getName());
            } catch (FileNotFoundException e) {
                println("");
            } catch (IOException e) {
                println("error creating file: " + e.getMessage());
            } finally {
                IOUtils.closeQuietly(fos);
            }
        }
    };

    this.new Command("m.config", "shows map configuration", "") {
        public void run() {
            String mapName = getMap().getName();
            MapConfig mapConfig = hazelcast.getConfig().getMapConfig(mapName);
            println(formatConfig(mapConfig.toString()));
        }
    };

    this.new Command("config", "show full configuration", "") {
        public void run() {
            Config cfg = hazelcast.getConfig();
            println(formatConfig(cfg.toString()));
        }
    };
}