Search in sources :

Example 11 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class XmlUtil method serialize.

/**
 * Return a pretty String version of the Element.
 *
 * @param element the Element to serialize
 * @return the pretty String representation of the Element
 */
public static String serialize(Element element) {
    Writer sw = new StringBuilderWriter();
    serialize(new DOMSource(element), sw);
    return sw.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) DOMSource(javax.xml.transform.dom.DOMSource) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer)

Example 12 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class XmlUtil method asString.

private static String asString(Node node) {
    Writer sw = new StringBuilderWriter();
    PrintWriter pw = new PrintWriter(sw);
    XmlNodePrinter nodePrinter = new XmlNodePrinter(pw);
    nodePrinter.setPreserveWhitespace(true);
    nodePrinter.print(node);
    return sw.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 13 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class Groovyc method runCompiler.

private void runCompiler(String[] commandLine) {
    // hand crank it so we can add our own compiler configuration
    try {
        FileSystemCompiler.CompilationOptions options = new FileSystemCompiler.CompilationOptions();
        CommandLine parser = FileSystemCompiler.configureParser(options);
        parser.parseArgs(commandLine);
        configuration = options.toCompilerConfiguration();
        configuration.setScriptExtensions(getScriptExtensions());
        String tmpExtension = getScriptExtension();
        if (tmpExtension.startsWith("*."))
            tmpExtension = tmpExtension.substring(1);
        configuration.setDefaultScriptExtension(tmpExtension);
        if (targetBytecode != null) {
            configuration.setTargetBytecode(targetBytecode);
        }
        // Load the file name list
        String[] fileNames = options.generateFileNames();
        boolean fileNameErrors = (fileNames == null || !FileSystemCompiler.validateFiles(fileNames));
        if (!fileNameErrors) {
            try (GroovyClassLoader loader = buildClassLoaderFor()) {
                FileSystemCompiler.doCompilation(configuration, makeCompileUnit(loader), fileNames, forceLookupUnnamedFiles);
            }
        }
    } catch (Exception e) {
        Throwable t = e;
        if (e.getClass() == RuntimeException.class && e.getCause() != null) {
            // unwrap to the real exception
            t = e.getCause();
        }
        Writer writer = new StringBuilderWriter();
        new ErrorReporter(t, false).write(new PrintWriter(writer));
        String message = writer.toString();
        taskSuccess = false;
        if (errorProperty != null) {
            getProject().setNewProperty(errorProperty, "true");
        }
        if (failOnError) {
            log.error(message);
            throw new BuildException("Compilation Failed", t, getLocation());
        } else {
            log.error(message);
        }
    }
}
Also used : FileSystemCompiler(org.codehaus.groovy.tools.FileSystemCompiler) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) URISyntaxException(java.net.URISyntaxException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) GroovyClassLoader(groovy.lang.GroovyClassLoader) CommandLine(picocli.CommandLine) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) BuildException(org.apache.tools.ant.BuildException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 14 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class Groovy method processError.

private void processError(Exception e) {
    Writer writer = new StringBuilderWriter();
    new ErrorReporter(e, false).write(new PrintWriter(writer));
    String message = writer.toString();
    throw new BuildException("Script Failed: " + message, e, getLocation());
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) BuildException(org.apache.tools.ant.BuildException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 15 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class EncodingGroovyMethods method encodeBase64.

private static Writable encodeBase64(final byte[] data, final boolean chunked, final boolean urlSafe, final boolean pad) {
    return new Writable() {

        @Override
        public Writer writeTo(final Writer writer) throws IOException {
            int charCount = 0;
            final int dLimit = (data.length / 3) * 3;
            final char[] table = urlSafe ? T_TABLE_URLSAFE : T_TABLE;
            for (int dIndex = 0; dIndex != dLimit; dIndex += 3) {
                int d = ((data[dIndex] & 0XFF) << 16) | ((data[dIndex + 1] & 0XFF) << 8) | (data[dIndex + 2] & 0XFF);
                writer.write(table[d >> 18]);
                writer.write(table[(d >> 12) & 0X3F]);
                writer.write(table[(d >> 6) & 0X3F]);
                writer.write(table[d & 0X3F]);
                if (chunked && ++charCount == 19) {
                    writer.write(CHUNK_SEPARATOR);
                    charCount = 0;
                }
            }
            if (dLimit != data.length) {
                int d = (data[dLimit] & 0XFF) << 16;
                if (dLimit + 1 != data.length) {
                    d |= (data[dLimit + 1] & 0XFF) << 8;
                }
                writer.write(table[d >> 18]);
                writer.write(table[(d >> 12) & 0X3F]);
                if (pad) {
                    writer.write((dLimit + 1 < data.length) ? table[(d >> 6) & 0X3F] : '=');
                    writer.write('=');
                } else {
                    if (dLimit + 1 < data.length) {
                        writer.write(table[(d >> 6) & 0X3F]);
                    }
                }
                if (chunked && charCount != 0) {
                    writer.write(CHUNK_SEPARATOR);
                }
            }
            return writer;
        }

        @Override
        public String toString() {
            Writer buffer = new StringBuilderWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) StringWriterIOException(groovy.lang.StringWriterIOException) Writable(groovy.lang.Writable) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer)

Aggregations

StringBuilderWriter (org.apache.groovy.io.StringBuilderWriter)25 Writer (java.io.Writer)22 PrintWriter (java.io.PrintWriter)15 IOException (java.io.IOException)11 OutputStreamWriter (java.io.OutputStreamWriter)6 BuildException (org.apache.tools.ant.BuildException)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 Writable (groovy.lang.Writable)4 ErrorReporter (org.codehaus.groovy.tools.ErrorReporter)4 StringWriterIOException (groovy.lang.StringWriterIOException)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 BufferedWriter (java.io.BufferedWriter)2 URISyntaxException (java.net.URISyntaxException)2 AntBuilder (groovy.ant.AntBuilder)1 GroovyPrintWriter (groovy.io.GroovyPrintWriter)1 Binding (groovy.lang.Binding)1 GString (groovy.lang.GString)1 GroovyShell (groovy.lang.GroovyShell)1 MissingMethodException (groovy.lang.MissingMethodException)1 Script (groovy.lang.Script)1