Search in sources :

Example 1 with FileHostObject

use of org.jaggeryjs.hostobjects.file.FileHostObject in project jaggery by wso2.

the class FeedHostObject method jsFunction_writeTo.

public static Scriptable jsFunction_writeTo(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
    FeedHostObject feedObject = (FeedHostObject) thisObj;
    FileHostObject fileHostObject;
    OutputStreamWriter outputStreamWriter = null;
    try {
        if (arguments[0] instanceof String) {
            fileHostObject = (FileHostObject) cx.newObject(feedObject, "File", arguments);
            outputStreamWriter = new OutputStreamWriter(fileHostObject.getOutputStream());
            feedObject.feed.writeTo(outputStreamWriter);
            outputStreamWriter.flush();
        } else if (arguments[0] instanceof FileHostObject) {
            fileHostObject = (FileHostObject) arguments[0];
            outputStreamWriter = new OutputStreamWriter(fileHostObject.getOutputStream());
            feedObject.feed.writeTo(outputStreamWriter);
            outputStreamWriter.flush();
        } else {
            throw new ScriptException("Invalid parameter");
        }
        return feedObject;
    } catch (IOException e) {
        throw new ScriptException(e);
    } finally {
        if (outputStreamWriter != null) {
            try {
                outputStreamWriter.close();
            } catch (IOException e) {
                log.warn("Error closing the stream", e);
            }
        }
    }
}
Also used : FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1