001: /* Interpreter.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Sep 5 11:12:47 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.servlet.dsp;
020:
021: import java.io.Writer;
022: import java.io.IOException;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.zkoss.lang.D;
031: import org.zkoss.util.media.ContentTypes;
032: import org.zkoss.util.resource.Locator;
033: import org.zkoss.util.logging.Log;
034: import org.zkoss.xel.XelContext;
035: import org.zkoss.xel.XelException;
036:
037: import org.zkoss.web.servlet.dsp.impl.Parser;
038:
039: /**
040: * The interpreter of the DSP file.
041: *
042: * <p>Note: we recognize only <%, <\%, %>, %\>, ${ and $\{.
043: * Unlike JSP, we don't recognize \${ or \$\{.
044: *
045: * @author tomyeh
046: */
047: public class Interpreter {
048: // private static final Log log = Log.lookup(Interpreter.class);
049:
050: /** Returns the content type by specifying a path, or null
051: * if no content type is available or path is null.
052: *
053: * <p>It determines the content type by looking the extension.
054: * Note: it considers the extension of "a.css.dsp" as "css".
055: */
056: public static final String getContentType(String path) {
057: if (path == null)
058: return null;
059:
060: int j = path.lastIndexOf('.');
061: if (j < 0 || path.indexOf('/', j + 1) >= 0)
062: return null;
063:
064: int k = path.indexOf(';', j + 1); //it might contain session-id
065: String ext = (k >= 0 ? path.substring(j + 1, k) : path
066: .substring(j + 1)).toLowerCase();
067: if ("dsp".equals(ext)) {
068: if (j == 0)
069: return null; //unknown
070: k = path.lastIndexOf('.', j - 1);
071: if (k < 0)
072: return null; //unknown
073: ext = path.substring(k + 1, j);
074: if (ext.indexOf('/') >= 0)
075: return null; //unknown
076: }
077: return ContentTypes.getContentType(ext);
078: }
079:
080: /** Constructor.
081: */
082: public Interpreter() {
083: }
084:
085: /** Parses a content to a meta format called {@link Interpretation}.
086: *
087: * @param xelc the context formation for evaluating ZUL exxpressions.
088: * It can be null, in which case no additional functions
089: * and variable resolvers are initialized at the beginning.
090: * @param ctype the content type. Optional. It is used only if
091: * no page action at all. If it is not specified and not page
092: * action, "text/html" is assumed.
093: * @since 3.0.0
094: */
095: public final Interpretation parse(String content, String ctype,
096: XelContext xelc, Locator loc)
097: throws javax.servlet.ServletException, IOException,
098: XelException {
099: return new Parser().parse(content, ctype, xelc, loc);
100: }
101:
102: /** Interprets the specified content and generates the result to
103: * the output specified in {@link DspContext}.
104: *
105: * @param dc the interpreter context; never null.
106: * @param content the content of DSP to interpret
107: * @param ctype the content type. Optional. It is used only if
108: * no page action at all. If it is not specified and not page
109: * action, "text/html" is assumed.
110: * @since 3.0.0
111: */
112: public final void interpret(DspContext dc, String content,
113: String ctype, XelContext xelc)
114: throws javax.servlet.ServletException, IOException,
115: XelException {
116: parse(content, ctype, xelc, dc.getLocator()).interpret(dc);
117: }
118:
119: /** Interprets the specified content based on the HTTP request.
120: * It actually wraps the HTTP request into {@link DspContext}
121: * and then invoke {@link #interpret(DspContext, String, String, XelContext)}.
122: *
123: * @param locator used to locate resources, such as taglib.
124: * If null is specified, the locator for the specified servlet context is
125: * used. (In other words, we use {@link org.zkoss.web.util.resource.ServletContextLocator}
126: * if locator is null).
127: * @param ctype the content type. Optional. It is used only if
128: * no page action at all. If it is not specified and not page
129: * action, "text/html" is assumed.
130: */
131: public final void interpret(ServletContext ctx,
132: HttpServletRequest request, HttpServletResponse response,
133: String content, String ctype, Locator locator)
134: throws javax.servlet.ServletException, IOException,
135: XelException {
136: interpret(
137: new ServletDspContext(ctx, request, response, locator),
138: content, ctype, null);
139: }
140:
141: /** Interprets the specified content based on the HTTP request.
142: * It actually wraps the HTTP request into {@link DspContext}
143: * and then invoke {@link #interpret(DspContext, String, String, XelContext)}.
144: *
145: * @param locator used to locate resources, such as taglib.
146: * If null is specified, the locator for the specified servlet context is
147: * used. (In other words, we use {@link org.zkoss.web.util.resource.ServletContextLocator}
148: * if locator is null).
149: * @param ctype the content type. Optional. It is used only if
150: * no page action at all. If it is not specified and not page
151: * action, "text/html" is assumed.
152: * @param out the output to generate the result.
153: * If null, it is the same as {@link #interpret(ServletContext,HttpServletRequest,HttpServletResponse,String,String,Locator)}
154: * In other words, response.getWriter() is used.
155: * @since 2.4.1
156: */
157: public final void interpret(ServletContext ctx,
158: HttpServletRequest request, HttpServletResponse response,
159: Writer out, String content, String ctype, Locator locator)
160: throws javax.servlet.ServletException, IOException,
161: XelException {
162: interpret(new ServletDspContext(ctx, request, response, out,
163: locator), content, ctype, null);
164: }
165: }
|