Author: sylvain Date: Sat Mar 12 05:02:44 2005 New Revision: 157251 URL: http://svn.apache.org/viewcvs?view=rev&rev=157251 Log: Add support for flowscript file-specific encoding using a first-line comment
Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js cocoon/branches/BRANCH_2_1_X/status.xml Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java?view=diff&r1=157250&r2=157251 ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java Sat Mar 12 05:02:44 2005 @@ -42,6 +42,9 @@ import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceResolver; import org.apache.excalibur.source.SourceValidity; +import org.apache.regexp.RE; +import org.apache.regexp.RECompiler; +import org.apache.regexp.REProgram; import org.mozilla.javascript.BaseFunction; import org.mozilla.javascript.Context; import org.mozilla.javascript.EcmaError; @@ -65,9 +68,11 @@ import java.awt.Dimension; import java.awt.Toolkit; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.PushbackInputStream; import java.io.Reader; import java.util.ArrayList; import java.util.HashMap; @@ -644,14 +649,42 @@ protected Script compileScript(Context cx, Scriptable scope, Source src) throws Exception { - InputStream is = src.getInputStream(); + PushbackInputStream is = new PushbackInputStream(src.getInputStream(), ENCODING_BUF_SIZE); try { - Reader reader = new BufferedReader(new InputStreamReader(is)); + String encoding = findEncoding(is); + Reader reader = encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding); + reader = new BufferedReader(reader); Script compiledScript = cx.compileReader(scope, reader, src.getURI(), 1, null); return compiledScript; } finally { is.close(); + } + } + + // A charset name can be up to 40 characters taken from the printable characters of US-ASCII + // (see http://www.iana.org/assignments/character-sets). So reading 100 bytes should be more than enough. + private final static int ENCODING_BUF_SIZE = 100; + // Match 'encoding = xxxx' on the first line + REProgram encodingRE = new RECompiler().compile("^.*encoding\\s*=\\s*([^\\s]*)"); + + /** + * Find the encoding of the stream, or null if not specified + */ + String findEncoding(PushbackInputStream is) throws IOException { + // Read some bytes + byte[] buffer = new byte[ENCODING_BUF_SIZE]; + int len = is.read(buffer, 0, buffer.length); + // and push them back + is.unread(buffer, 0, len); + + // Interpret them as an ASCII string + String str = new String(buffer, 0, len, "ASCII"); + RE re = new RE(encodingRE); + if (re.match(str)) { + return re.getParen(1); + } else { + return null; } } Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js?view=diff&r1=157250&r2=157251 ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js (original) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/flow/javascript/fom/fom_system.js Sat Mar 12 05:02:44 2005 @@ -38,5 +38,12 @@ return wk; } - +/** + * Halt the current script. Useful when we want to send a page and never allow going + * back, and exiting the current function isn't possible because it goes back to the + * caller. + */ +FOM_Cocoon.prototype.suicide() { + FOM_Cocoon.suicide(); +} Modified: cocoon/branches/BRANCH_2_1_X/status.xml URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?view=diff&r1=157250&r2=157251 ============================================================================== --- cocoon/branches/BRANCH_2_1_X/status.xml (original) +++ cocoon/branches/BRANCH_2_1_X/status.xml Sat Mar 12 05:02:44 2005 @@ -202,6 +202,11 @@ <changes> <release version="@version@" date="@date@"> + <action dev="SW" type="add"> + A flowscript's file encoding can now be specified by a comment on the script's + very first line, in the form "<code>// encoding = xxxx</code>". Otherwise, the + default platform encoding is used. + </action> <action dev="JQ" type="add"> Added QueryBean as a standalone block, copied over from 2.2.0. Removed the original from the Lucene Block. Stores Favourites using OJB in HSQLDB. </action>