Search in sources :

Example 1 with Writable

use of groovy.lang.Writable in project groovy by apache.

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)

Example 2 with Writable

use of groovy.lang.Writable 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() {

        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 3 with Writable

use of groovy.lang.Writable in project groovy by apache.

the class InvokerHelper method write.

/**
     * Writes an object to a Writer using Groovy's default representation for the object.
     */
public static void write(Writer out, Object object) throws IOException {
    if (object instanceof String) {
        out.write((String) object);
    } else if (object instanceof Object[]) {
        out.write(toArrayString((Object[]) object));
    } else if (object instanceof Map) {
        out.write(toMapString((Map) object));
    } else if (object instanceof Collection) {
        out.write(toListString((Collection) object));
    } else if (object instanceof Writable) {
        Writable writable = (Writable) object;
        writable.writeTo(out);
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        Reader reader;
        if (object instanceof InputStream) {
            reader = new InputStreamReader((InputStream) object);
        } else {
            reader = (Reader) object;
        }
        char[] chars = new char[8192];
        int i;
        while ((i = reader.read(chars)) != -1) {
            out.write(chars, 0, i);
        }
        reader.close();
    } else {
        out.write(toString(object));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 4 with Writable

use of groovy.lang.Writable in project groovy by apache.

the class InvokerHelper 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;
        StringWriter stringWriter = new StringWriter();
        writable.writeTo(stringWriter);
        out.append(stringWriter.toString());
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        Reader reader;
        if (object instanceof InputStream) {
            reader = new InputStreamReader((InputStream) object);
        } else {
            reader = (Reader) object;
        }
        char[] chars = new char[8192];
        int i;
        while ((i = reader.read(chars)) != -1) {
            for (int j = 0; j < i; j++) {
                out.append(chars[j]);
            }
        }
        reader.close();
    } else {
        out.append(toString(object));
    }
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 5 with Writable

use of groovy.lang.Writable in project ratpack by ratpack.

the class MarkupTemplateRenderer method render.

@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
    String contentType = template.getContentType();
    contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
    try {
        Template compiledTemplate = engine.createTemplateByPath(template.getName());
        Writable boundTemplate = compiledTemplate.make(template.getModel());
        ByteBuf byteBuf = byteBufAllocator.directBuffer();
        try {
            OutputStream outputStream = new ByteBufOutputStream(byteBuf);
            Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
            boundTemplate.writeTo(writer);
        } catch (Exception e) {
            byteBuf.release();
            throw e;
        }
        ctx.getResponse().send(contentType, byteBuf);
    } catch (IOException e) {
        ctx.error(e);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Writable(groovy.lang.Writable) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MimeTypes(ratpack.file.MimeTypes) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MarkupTemplate(ratpack.groovy.template.MarkupTemplate) Template(groovy.text.Template)

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