001: /*
002: * readDynamicPage.java
003: *
004: * Copyright (c) 2003-2006 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 pnuts.compiler.Compiler;
015: import java.io.*;
016: import java.net.*;
017: import org.pnuts.servlet.protocol.pea.Handler;
018:
019: public class readDynamicPage extends PnutsFunction {
020:
021: private final static String SERVLET_COMPILER = "pnuts.servlet.compiler"
022: .intern();
023:
024: public readDynamicPage() {
025: super ("readDynamicPage");
026: }
027:
028: public boolean defined(int nargs) {
029: return nargs == 1 || nargs == 2 || nargs == 3;
030: }
031:
032: static URL getDynamicPageURL(URL url, String encoding,
033: Context context) {
034: try {
035: String specURL;
036: if (encoding == null) {
037: specURL = "pea:" + url.toExternalForm();
038: } else {
039: specURL = "pea:" + url.toExternalForm() + "!charset="
040: + encoding;
041: }
042: return new URL(null, specURL, new Handler(context, null));
043: } catch (MalformedURLException mue) {
044: mue.printStackTrace();
045: return null;
046: }
047: }
048:
049: static Reader getReader(InputStream in, String enc, Context context)
050: throws UnsupportedEncodingException {
051: if (enc != null) {
052: return new InputStreamReader(in, enc);
053: } else {
054: return Runtime.getScriptReader(in, context);
055: }
056: }
057:
058: protected Object exec(Object[] args, Context context) {
059: int nargs = args.length;
060: if (nargs == 0 || nargs > 3) {
061: undefined(args, context);
062: }
063: Object arg = args[0];
064: String encoding;
065: if (nargs >= 2) {
066: encoding = (String) args[1];
067: if (encoding == null) {
068: encoding = context.getScriptEncoding();
069: }
070: } else {
071: encoding = context.getScriptEncoding();
072: }
073:
074: boolean reloadUpdatedScripts = true;
075: if (nargs >= 3) {
076: reloadUpdatedScripts = ((Boolean) args[2]).booleanValue();
077: }
078: Pnuts parsed = null;
079: URL url = null;
080: Reader reader = null;
081:
082: final Compiler compiler = (Compiler) context
083: .get(SERVLET_COMPILER);
084: URL baseloc = null;
085: try {
086: if (arg instanceof InputStream) {
087: reader = getReader((InputStream) arg, encoding, context);
088: } else if (arg instanceof Reader) {
089: reader = (Reader) arg;
090: } else if (arg instanceof URL) {
091: url = (URL) arg;
092: baseloc = url;
093: } else if (arg instanceof File) {
094: File file = (File) arg;
095: url = file.toURL();
096: baseloc = file.getAbsoluteFile().getParentFile()
097: .toURL();
098: } else if (arg instanceof String) {
099: url = Pnuts.getResource((String) arg, context);
100: baseloc = url;
101: } else {
102: throw new IllegalArgumentException(String.valueOf(arg));
103: }
104: if (url != null) {
105: return new DynamicPage(url, encoding, context,
106: reloadUpdatedScripts) {
107: protected Compiler getCompiler() {
108: return compiler;
109: }
110: };
111: } else {
112: StringWriter sw = new StringWriter();
113: DynamicPage.convert(reader, sw, baseloc, encoding,
114: context, null);
115: parsed = Pnuts.parse(new StringReader(sw.toString()),
116: null, context);
117: if (compiler != null) {
118: parsed = compiler.compile(parsed, context);
119: }
120: return parsed;
121: }
122: } catch (Throwable t) {
123: throw new PnutsException(t, context);
124: }
125: }
126:
127: public String toString() {
128: return "function readDynamicPage((String|InputStream|Reader|File|URL) input {, encoding {, reload_updated_scripts}})";
129: }
130: }
|