/* * JAVA PRESS Vol.5 * "Scripting with ECMAScipt" example (List 3 / English Version) * Written by Summery Sardine */ import FESI.jslib.*; import java.io.*; import java.util.*; /** * Simple pre-processor using FESI ECMAScript interpreter. */ class jpre { String source, result; Vector props; final char startingChar = '#'; /** * Construct a new preprocessor. * * @param source source file * @param result output file * @param props variables */ jpre(String source, String result, Vector props) { this.source = source; this.result = result; this.props = props; } // main procedure void process() throws IOException, JSException { BufferedReader reader = null; BufferedWriter writer = null; try { // prepare File temp = new File("temp.es"); reader = new BufferedReader(new FileReader(source)); writer = new BufferedWriter(new FileWriter(temp)); // write to temporary file String s, t; while ((s = reader.readLine()) != null) { t = s.replace('\t', ' ').trim(); if (t.equals("")) { writer.write("put(\"" + s + "\");"); } else if (t.charAt(0) == startingChar) { int p = s.indexOf(startingChar); writer.write(s.substring(0, p) + s.substring(p + 1)); } else { writer.write("put(\"" + escape(s) + "\");"); } writer.newLine(); } reader.close(); writer.close(); // construct FESI interpreter JSGlobalObject global = null; try { global = JSUtil.makeEvaluator(); } catch (JSException e) { System.err.println("Cannot initialize FESI."); System.exit(1); } writer = new BufferedWriter(new FileWriter(result)); global.setMember("put", new PutFunction(writer)); // define variables Enumeration e = props.elements(); while (e.hasMoreElements()) { global.eval((String)e.nextElement()); } // execute temporary script file global.eval(new BufferedReader(new FileReader(temp)), "Temporary file"); } finally { if (reader != null) reader.close(); if (writer != null) writer.close(); } } /** * Replace doublequotes and backslashes with their escape sequences. * * @param target string * @return result */ String escape(String s) { StringBuffer buf = new StringBuffer(); char[] chars = s.toCharArray(); int n = chars.length; for (int i = 0; i < n; i++) { switch (chars[i]) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; default: buf.append(chars[i]); } } return buf.toString(); } /** * An object defining the behavior of "put" function. */ class PutFunction extends JSFunctionAdapter { BufferedWriter writer = null; PutFunction(BufferedWriter writer) { this.writer = writer; } public Object doCall(JSObject thisObject, Object[] args) throws JSException { try { writer.write(args[0].toString()); writer.newLine(); } catch (IOException e) { throw new JSException("Output failed.", e); } return null; } } public static void main(String[] args) { if (args.length >= 2) { try { Vector props = new Vector(); int n = args.length;; for (int i = 2; i < n; i++) { if ((args[i].indexOf('=')) > -1) { props.addElement(args[i] + ";"); } else { props.addElement(args[i] + " = true;"); } } jpre jp = new jpre(args[0], args[1], props); jp.process(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } else { System.err.println("usage: jpre source outfile [property...]"); System.err.println(" property: name[=value]"); } } }