Search in sources :

Example 21 with StringBuilderWriter

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

the class CompileTaskSupport method handleException.

protected void handleException(final Exception e) throws BuildException {
    assert e != null;
    Writer writer = new StringBuilderWriter();
    new ErrorReporter(e, false).write(new PrintWriter(writer));
    String message = writer.toString();
    if (failOnError) {
        throw new BuildException(message, e, getLocation());
    } else {
        log.error(message);
    }
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 22 with StringBuilderWriter

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

the class StringGroovyMethods method denormalize.

/**
 * Return a CharSequence with lines (separated by LF, CR/LF, or CR)
 * terminated by the platform specific line separator.
 *
 * @param self a CharSequence object
 * @return the denormalized toString() of this CharSequence
 *
 * @since 1.8.2
 */
public static String denormalize(final CharSequence self) {
    // TODO: Put this lineSeparator property somewhere everyone can use it.
    if (lineSeparator == null) {
        final Writer sw = new StringBuilderWriter(2);
        // the security manager rigamarole to deal with the possible exception
        try (final BufferedWriter bw = new BufferedWriter(sw)) {
            bw.newLine();
            bw.flush();
            lineSeparator = sw.toString();
        } catch (IOException ioe) {
            // This shouldn't happen, but this is the same default used by
            // BufferedWriter on a security exception.
            lineSeparator = "\n";
        }
    }
    final int len = self.length();
    if (len < 1) {
        return self.toString();
    }
    final StringBuilder sb = new StringBuilder((110 * len) / 100);
    int i = 0;
    // GROOVY-7873: GString calls toString() on each invocation of CharSequence methods such
    // as charAt which is very expensive for large GStrings.
    CharSequence cs = (self instanceof GString) ? self.toString() : self;
    while (i < len) {
        final char ch = cs.charAt(i++);
        switch(ch) {
            case '\r':
                sb.append(lineSeparator);
                // Eat the following LF if any.
                if ((i < len) && (cs.charAt(i) == '\n')) {
                    ++i;
                }
                break;
            case '\n':
                sb.append(lineSeparator);
                break;
            default:
                sb.append(ch);
                break;
        }
    }
    return sb.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) IOException(java.io.IOException) GString(groovy.lang.GString) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 23 with StringBuilderWriter

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

the class FormatHelper method append.

/**
 * Appends an object to an Appendable using Groovy's default representation for the object.
 */
public static void append(Appendable out, Object object) throws IOException {
    if (object instanceof String) {
        out.append((String) object);
    } else if (object instanceof Object[]) {
        out.append(toArrayString((Object[]) object));
    } else if (object instanceof Map) {
        out.append(toMapString((Map) object));
    } else if (object instanceof Collection) {
        out.append(toListString((Collection) object));
    } else if (object instanceof Writable) {
        Writable writable = (Writable) object;
        Writer stringWriter = new StringBuilderWriter();
        writable.writeTo(stringWriter);
        out.append(stringWriter.toString());
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        try (Reader reader = object instanceof InputStream ? new InputStreamReader((InputStream) object) : (Reader) object) {
            char[] chars = new char[8192];
            for (int i; (i = reader.read(chars)) != -1; ) {
                for (int j = 0; j < i; j++) {
                    out.append(chars[j]);
                }
            }
        }
    } else {
        out.append(toString(object));
    }
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Map(java.util.Map) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer)

Example 24 with StringBuilderWriter

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

the class IOGroovyMethods method filterLine.

/**
 * Filter the lines from this Reader, and return a Writable which can be
 * used to stream the filtered lines to a destination.  The closure should
 * return <code>true</code> if the line should be passed to the writer.
 *
 * @param reader  this reader
 * @param closure a closure used for filtering
 * @return a Writable which will use the closure to filter each line
 *         from the reader when the Writable#writeTo(Writer) is called.
 * @since 1.0
 */
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
    final BufferedReader br = new BufferedReader(reader);
    return new Writable() {

        @Override
        public Writer writeTo(Writer out) throws IOException {
            BufferedWriter bw = new BufferedWriter(out);
            String line;
            BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
            while ((line = br.readLine()) != null) {
                if (bcw.call(line)) {
                    bw.write(line);
                    bw.newLine();
                }
            }
            bw.flush();
            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 : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) StringWriterIOException(groovy.lang.StringWriterIOException) BufferedReader(java.io.BufferedReader) Writable(groovy.lang.Writable) FromString(groovy.transform.stc.FromString) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) GroovyPrintWriter(groovy.io.GroovyPrintWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 25 with StringBuilderWriter

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

the class StringEscapeUtils method escapeJavaStyleString.

/**
 * Worker method for the {@link #escapeJavaScript(String)} method.
 *
 * @param str String to escape values in, may be null
 * @param escapeSingleQuotes escapes single quotes if <code>true</code>
 * @param escapeForwardSlash TODO
 * @return the escaped string
 */
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
    if (str == null) {
        return null;
    }
    try {
        Writer writer = new StringBuilderWriter(str.length() * 2);
        escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
        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)

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