Update of /cvsroot/webmacro/webmacro/src/org/webmacro/util
In directory usw-pr-cvs1:/tmp/cvs-serv2153/webmacro/src/org/webmacro/util
Modified Files:
WMEval.java
Log Message:
updates for 1.1
Index: WMEval.java
===================================================================
RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/util/WMEval.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** WMEval.java 11 Nov 2002 19:22:39 -0000 1.9
--- WMEval.java 15 Nov 2002 00:24:13 -0000 1.10
***************
*** 24,32 ****
import java.io.*;
!
import org.webmacro.*;
import org.webmacro.engine.StreamTemplate;
/**
* WMEval encapsulates an instance of WebMacro for reuse in any java application.
--- 24,34 ----
import java.io.*;
! import javax.servlet.http.*;
! import javax.servlet.ServletException;
import org.webmacro.*;
import org.webmacro.engine.StreamTemplate;
+
/**
* WMEval encapsulates an instance of WebMacro for reuse in any java application.
***************
*** 65,68 ****
--- 67,71 ----
private OutputStream out = System.out;
private Context context;
+ private boolean useWebContext = false;
/**
* If an output file is not specified as an argument, it
***************
*** 75,88 ****
* The constructor which creates the environment for evaluating a rule.
*/
! public WMEval() {
// Build a web macro environment for rule execution.
try {
! wm = new WM();
context = wm.getContext();
}
catch (Exception e) {
! e.printStackTrace();
}
}
--- 78,96 ----
* The constructor which creates the environment for evaluating a rule.
*/
! public WMEval(HttpServlet servlet) {
// Build a web macro environment for rule execution.
try {
! wm = new WM(servlet);
context = wm.getContext();
}
catch (Exception e) {
! e.printStackTrace(System.err);
! throw new IllegalStateException(e.toString());
}
}
+
+ public WMEval() {
+ this(null);
+ }
***************
*** 110,113 ****
--- 118,125 ----
return rule;
}
+
+ public void error(String msg, Exception e) {
+ wm.getLog("ERROR").error(msg, e);
+ }
/**
***************
*** 212,215 ****
--- 224,279 ----
rule.write(w, context);
w.flush();
+ }
+
+ /**
+ * Evaluates the string template against the current context
+ * and returns the value. If an output file is specified, the value
+ * is written out as well.
+ * @param templateName The name of the template.
+ * @param output An optional output.
+ * @return The output from the evaluated template
+ */
+ public String eval(String templateName, OutputStream out) throws Exception {
+ Template t = wm.getTemplate(templateName);
+ String val = t.evaluate(context).toString();
+ if (out != null) {
+ out.write(val.getBytes());
+ }
+ return val;
+ }
+
+ /**
+ * Evaluates the string template against a new context and writes
+ * it to the http Response output stream using the proper encoding.
+ * <p>
+ * This is an exceptionally useful method for a servlet to use to
+ * write out a template.
+ * <p>
+ * <b>Note:</b> This method places "FastWriter" in the context
+ * so you can write out to the stream.
+ * @param templateName The name of the template.
+ * @param response The servlet response object and its output stream
+ * @return The output from the evaluated template
+ */
+ public void eval(String templateName, HttpServletRequest req,
+ HttpServletResponse resp) throws ServletException {
+ try {
+ context = wm.getWebContext(req, resp);
+ String encoding = (String) wm.getConfig(WMConstants.TEMPLATE_OUTPUT_ENCODING);
+ if (encoding == null) {
+ encoding = resp.getCharacterEncoding();
+ }
+ FastWriter w = context.getBroker().getFastWriter(out, encoding);
+ Template t = wm.getTemplate(templateName);
+ context.put("FastWriter", w); // allow template writers to access the output stream!
+ t.write(w, context);
+ resp.setContentLength(w.size());
+ w.writeTo(resp.getOutputStream());
+ w.flush();
+ }
+ catch (Exception e) {
+ e.printStackTrace(System.err);
+ throw new ServletException(e.toString());
+ }
}
|