Search in sources :

Example 6 with StringBuilderWriter

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

the class StringEscapeUtils method unescapeJava.

/**
 * Unescapes any Java literals found in the <code>String</code>.
 * For example, it will turn a sequence of <code>'\'</code> and
 * <code>'n'</code> into a newline character, unless the <code>'\'</code>
 * is preceded by another <code>'\'</code>.
 *
 * @param str  the <code>String</code> to unescape, may be null
 * @return a new unescaped <code>String</code>, <code>null</code> if null string input
 */
public static String unescapeJava(String str) {
    if (str == null) {
        return null;
    }
    try {
        Writer writer = new StringBuilderWriter(str.length());
        unescapeJava(writer, str);
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringBuilderWriter
        throw new RuntimeException(ioe);
    }
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) IOException(java.io.IOException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer)

Example 7 with StringBuilderWriter

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

the class EncodingGroovyMethods method encodeHex.

/**
 * Produces a Writable that writes the hex encoding of the byte[]. Calling
 * toString() on this Writable returns the hex encoding as a String. The hex
 * encoding includes two characters for each byte and all letters are lower case.
 *
 * @param data byte array to be encoded
 * @return object which will write the hex encoding of the byte array
 * @see Integer#toHexString(int)
 */
public static Writable encodeHex(final byte[] data) {
    return new Writable() {

        @Override
        public Writer writeTo(Writer out) throws IOException {
            for (byte datum : data) {
                // convert byte into unsigned hex string
                String hexString = Integer.toHexString(datum & 0xFF);
                // add leading zero if the length of the string is one
                if (hexString.length() < 2) {
                    out.write("0");
                }
                // write hex string to writer
                out.write(hexString);
            }
            return out;
        }

        @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)

Example 8 with StringBuilderWriter

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

the class JavacJavaCompiler method compile.

@Override
public void compile(List<String> files, CompilationUnit cu) {
    List<String> javacParameters = makeParameters(cu.getClassLoader());
    StringBuilderWriter javacOutput = new StringBuilderWriter();
    int javacReturnValue = 0;
    try {
        try {
            boolean successful = doCompileWithSystemJavaCompiler(cu, files, javacParameters, javacOutput);
            if (!successful) {
                javacReturnValue = 1;
            }
        } catch (IllegalArgumentException e) {
            // any of the options are invalid
            javacReturnValue = 2;
            cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
        } catch (IOException e) {
            javacReturnValue = 1;
            cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
        }
    } catch (Exception e) {
        cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
    }
    if (javacReturnValue != 0) {
        switch(javacReturnValue) {
            case 1:
                addJavacError("Compile error during compilation with javac.", cu, javacOutput);
                break;
            case 2:
                addJavacError("Invalid commandline usage for javac.", cu, javacOutput);
                break;
            default:
                addJavacError("unexpected return value by javac.", cu, javacOutput);
                break;
        }
    } else {
        // print warnings if any
        System.out.print(javacOutput);
    }
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 9 with StringBuilderWriter

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

the class XmlTemplateEngine method createTemplate.

@Override
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringBuilderWriter writer = new StringBuilderWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter.incrementAndGet() + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 10 with StringBuilderWriter

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

the class BaseTemplate method stringOf.

public String stringOf(Closure cl) throws IOException {
    Writer old = out;
    Writer stringWriter = new StringBuilderWriter(32);
    out = stringWriter;
    Object result = cl.call();
    if (result != null && result != this) {
        stringWriter.append(result.toString());
    }
    out = old;
    return stringWriter.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) NullWriter(org.codehaus.groovy.control.io.NullWriter) 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