Search in sources :

Example 6 with Writable

use of groovy.lang.Writable in project grails-core by grails.

the class JSONObject method writeQuoted.

static void writeQuoted(Writer writer, Object value) throws IOException {
    if (useStreamingJavascriptEncoder) {
        writer.write("\"");
        if (value.getClass() == String.class || value.getClass() == StringBuilder.class || value.getClass() == StringBuffer.class) {
            encodeToWriter((CharSequence) value, writer);
        } else if (value instanceof StreamingEncoderWritable) {
            ((StreamingEncoderWritable) value).encodeTo(writer, javascriptEncoderStateless);
        } else if (value instanceof Writable) {
            ((Writable) value).writeTo(new StreamingEncoderWriter(writer, javascriptEncoder, null));
        } else {
            encodeToWriter(value.toString(), writer);
        }
        writer.write("\"");
    } else {
        writer.write(valueToString(value));
    }
}
Also used : StreamingEncoderWriter(org.grails.encoder.StreamingEncoderWriter) StreamingEncoderWritable(org.grails.encoder.StreamingEncoderWritable) Writable(groovy.lang.Writable) StreamingEncoderWritable(org.grails.encoder.StreamingEncoderWritable)

Example 7 with Writable

use of groovy.lang.Writable in project groovy-core by groovy.

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() {

        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;
        }

        public String toString() {
            StringWriter buffer = new StringWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) StringWriter(java.io.StringWriter) 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) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 8 with Writable

use of groovy.lang.Writable in project groovy-core by groovy.

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() {

        public Writer writeTo(Writer out) throws IOException {
            for (int i = 0; i < data.length; i++) {
                // convert byte into unsigned hex string
                String hexString = Integer.toHexString(data[i] & 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;
        }

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

Example 9 with Writable

use of groovy.lang.Writable in project grails-core by grails.

the class Base64ByteArrayMarshaller method marshalObject.

public void marshalObject(Object object, XML xml) throws ConverterException {
    xml.attribute("encoding", "BASE-64");
    xml.chars("");
    Writable w;
    if (object instanceof byte[]) {
        w = EncodingGroovyMethods.encodeBase64((byte[]) object);
    } else {
        w = EncodingGroovyMethods.encodeBase64((Byte[]) object);
    }
    try {
        w.writeTo(xml.getStream());
    } catch (IOException e) {
        throw new ConverterException(e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) Writable(groovy.lang.Writable) IOException(java.io.IOException)

Example 10 with Writable

use of groovy.lang.Writable in project groovy-core by groovy.

the class XmlUtil method asString.

private static String asString(GPathResult node) {
    // little bit of hackery to avoid Groovy dependency in this file
    try {
        Object builder = ((Class) Class.forName("groovy.xml.StreamingMarkupBuilder")).newInstance();
        InvokerHelper.setProperty(builder, "encoding", "UTF-8");
        Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
    } catch (Exception e) {
        return "Couldn't convert node to string because: " + e.getMessage();
    }
}
Also used : Writable(groovy.lang.Writable) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

Writable (groovy.lang.Writable)16 IOException (java.io.IOException)10 StringWriter (java.io.StringWriter)9 Writer (java.io.Writer)7 StringWriterIOException (groovy.lang.StringWriterIOException)6 Template (groovy.text.Template)3 OutputStreamWriter (java.io.OutputStreamWriter)3 GroovyPrintWriter (groovy.io.GroovyPrintWriter)2 GString (groovy.lang.GString)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 SpreadMap (groovy.lang.SpreadMap)2 FromString (groovy.transform.stc.FromString)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintWriter (java.io.PrintWriter)2 Reader (java.io.Reader)2 Collection (java.util.Collection)2