001: /*
002: * @(#)convertDynamicPage.java 1.2 04/12/06
003: *
004: * Copyright (c) 2003,2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.servlet;
010:
011: import pnuts.servlet.*;
012: import pnuts.lang.*;
013: import pnuts.lang.Runtime;
014: import java.io.*;
015:
016: public class convertDynamicPage extends PnutsFunction {
017:
018: static final String CWD = "cwd".intern();
019: PnutsFunction pipe;
020: PnutsFunction getFile;
021:
022: public convertDynamicPage() {
023: super ("convertDynamicPage");
024: }
025:
026: public boolean defined(int nargs) {
027: return nargs == 1 || nargs == 2;
028: }
029:
030: protected Object exec(Object[] args, Context context) {
031: File ifile = null;
032: File ofile = null;
033: Reader reader = null;
034: Writer writer = null;
035: int nargs = args.length;
036: if (nargs > 2 || nargs == 0) {
037: undefined(args, context);
038: return null;
039: }
040: try {
041: if (nargs >= 1) {
042: Object arg0 = args[0];
043: if (arg0 instanceof String) {
044: if (getFile == null) {
045: getFile = (PnutsFunction) context
046: .resolveSymbol("getFile");
047: }
048: if (getFile == null) {
049: throw new PnutsException(
050: "getFile is not defined", context);
051: }
052: ifile = (File) getFile.call(new Object[] { arg0 },
053: context);
054: reader = Runtime.getScriptReader(
055: new FileInputStream(ifile), context);
056: } else if (arg0 instanceof File) {
057: ifile = (File) arg0;
058: reader = Runtime.getScriptReader(
059: new FileInputStream(ifile), context);
060: } else if (arg0 instanceof Reader) {
061: reader = (Reader) arg0;
062: } else if (arg0 instanceof InputStream) {
063: reader = Runtime.getScriptReader(
064: (InputStream) arg0, context);
065: } else {
066: throw new IllegalArgumentException(String
067: .valueOf(arg0));
068: }
069: }
070: if (nargs >= 2) {
071: Object arg1 = args[1];
072: if (arg1 instanceof String) {
073: if (getFile == null) {
074: getFile = (PnutsFunction) context
075: .resolveSymbol("getFile");
076: }
077: if (getFile == null) {
078: throw new PnutsException(
079: "getFile is not defined", context);
080: }
081: ofile = (File) getFile.call(new Object[] { arg1 },
082: context);
083: writer = new FileWriter(ofile);
084: } else if (arg1 instanceof File) {
085: ofile = (File) arg1;
086: writer = new FileWriter(ofile);
087: } else if (arg1 instanceof Writer) {
088: writer = (Writer) arg1;
089: } else if (arg1 instanceof OutputStream) {
090: String enc = context.getScriptEncoding();
091: if (enc != null) {
092: writer = new OutputStreamWriter(
093: (OutputStream) arg1, enc);
094: } else {
095: writer = new OutputStreamWriter(
096: (OutputStream) arg1);
097: }
098: } else {
099: throw new IllegalArgumentException(String
100: .valueOf(arg1));
101: }
102: }
103: File dir;
104: if (ifile != null) {
105: dir = ifile.getAbsoluteFile().getParentFile();
106: } else {
107: String cwd = (String) context.get(CWD);
108: if (cwd == null) {
109: context.set(CWD, cwd = System
110: .getProperty("user.dir"));
111: }
112: dir = new File(cwd);
113: }
114: if (writer != null) {
115: try {
116: try {
117: DynamicPage.convert(reader, writer,
118: dir.toURL(), context
119: .getScriptEncoding(), context);
120: } finally {
121: if (ifile != null) {
122: reader.close();
123: }
124: }
125: } finally {
126: if (ofile != null) {
127: writer.close();
128: }
129: }
130: return null;
131: } else {
132: if (pipe == null) {
133: pipe = (PnutsFunction) context
134: .resolveSymbol("pipe");
135: }
136: if (pipe == null) {
137: throw new PnutsException("pipe is not defined",
138: context);
139: }
140: return pipe
141: .call(new Object[] { this , reader }, context);
142: }
143: } catch (IOException e) {
144: throw new PnutsException(e, context);
145: }
146: }
147:
148: public String toString() {
149: return "function convertDynamicPage(input {, output } )";
150: }
151: }
|