001package org.javasimon.console.json;
002
003import java.io.IOException;
004import java.io.StringWriter;
005import java.io.Writer;
006
007/**
008 * Base class for all JavaScript things.
009 *
010 * @author gquintana
011 */
012public abstract class AnyJS {
013
014        /** Renders the response. */
015        public abstract void write(Writer writer) throws IOException;
016
017        final void writeString(Writer writer, String string) throws IOException {
018                writer.write("\"");
019                writer.write(string);
020                writer.write("\"");
021        }
022
023        /** Renders the response in a String. */
024        @Override
025        public String toString() {
026                try {
027                        StringWriter writer = new StringWriter();
028                        write(writer);
029                        return writer.toString();
030                } catch (IOException iOException) {
031                        return iOException.getMessage();
032                }
033        }
034}