http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory.java b/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory.java deleted file mode 100644 index 0b9245b..0000000 --- a/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import javax.servlet.Servlet; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.jsp.JspEngineInfo; -import javax.servlet.jsp.JspFactory; -import javax.servlet.jsp.PageContext; - -/** - */ -abstract class FreeMarkerJspFactory extends JspFactory { - protected abstract String getSpecificationVersion(); - - @Override - public JspEngineInfo getEngineInfo() { - return new JspEngineInfo() { - @Override - public String getSpecificationVersion() { - return FreeMarkerJspFactory.this.getSpecificationVersion(); - } - }; - } - - @Override - public PageContext getPageContext(Servlet servlet, ServletRequest request, - ServletResponse response, String errorPageURL, - boolean needsSession, int bufferSize, boolean autoFlush) { - // This is never meant to be called. JSP pages compiled to Java - // bytecode use this API, but in FreeMarker, we're running templates, - // and not JSP pages precompiled to bytecode, therefore we have no use - // for this API. - throw new UnsupportedOperationException(); - } - - @Override - public void releasePageContext(PageContext ctx) { - // This is never meant to be called. JSP pages compiled to Java - // bytecode use this API, but in FreeMarker, we're running templates, - // and not JSP pages precompiled to bytecode, therefore we have no use - // for this API. - throw new UnsupportedOperationException(); - } -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory21.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory21.java b/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory21.java deleted file mode 100644 index a8def22..0000000 --- a/src/main/java/freemarker/ext/jsp/FreeMarkerJspFactory21.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import javax.servlet.ServletContext; -import javax.servlet.jsp.JspApplicationContext; - -/** - */ -class FreeMarkerJspFactory21 extends FreeMarkerJspFactory { - private static final String JSPCTX_KEY = - FreeMarkerJspFactory21.class.getName() + "#jspAppContext"; - - @Override - protected String getSpecificationVersion() { - return "2.1"; - } - - @Override - public JspApplicationContext getJspApplicationContext(ServletContext ctx) { - JspApplicationContext jspctx = (JspApplicationContext) ctx.getAttribute( - JSPCTX_KEY); - if (jspctx == null) { - synchronized (ctx) { - jspctx = (JspApplicationContext) ctx.getAttribute(JSPCTX_KEY); - if (jspctx == null) { - jspctx = new FreeMarkerJspApplicationContext(); - ctx.setAttribute(JSPCTX_KEY, jspctx); - } - } - } - return jspctx; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/FreeMarkerPageContext.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/FreeMarkerPageContext.java b/src/main/java/freemarker/ext/jsp/FreeMarkerPageContext.java deleted file mode 100644 index d8c3bda..0000000 --- a/src/main/java/freemarker/ext/jsp/FreeMarkerPageContext.java +++ /dev/null @@ -1,492 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -package freemarker.ext.jsp; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.ListIterator; - -import javax.servlet.GenericServlet; -import javax.servlet.Servlet; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletOutputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; -import javax.servlet.http.HttpSession; -import javax.servlet.jsp.JspWriter; -import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.tagext.BodyContent; - -import freemarker.core.Environment; -import freemarker.ext.servlet.FreemarkerServlet; -import freemarker.ext.servlet.HttpRequestHashModel; -import freemarker.ext.servlet.ServletContextHashModel; -import freemarker.ext.util.WrapperTemplateModel; -import freemarker.template.AdapterTemplateModel; -import freemarker.template.ObjectWrapper; -import freemarker.template.ObjectWrapperAndUnwrapper; -import freemarker.template.TemplateBooleanModel; -import freemarker.template.TemplateDateModel; -import freemarker.template.TemplateHashModelEx; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateModelIterator; -import freemarker.template.TemplateNumberModel; -import freemarker.template.TemplateScalarModel; -import freemarker.template._TemplateAPI; -import freemarker.template.utility.UndeclaredThrowableException; - -/** - */ -abstract class FreeMarkerPageContext extends PageContext implements TemplateModel { - private static final Class OBJECT_CLASS = Object.class; - - private final Environment environment; - private final int incompatibleImprovements; - private List tags = new ArrayList(); - private List outs = new ArrayList(); - private final GenericServlet servlet; - private HttpSession session; - private final HttpServletRequest request; - private final HttpServletResponse response; - private final ObjectWrapper wrapper; - private final ObjectWrapperAndUnwrapper unwrapper; - private JspWriter jspOut; - - protected FreeMarkerPageContext() throws TemplateModelException { - environment = Environment.getCurrentEnvironment(); - incompatibleImprovements = environment.getConfiguration().getIncompatibleImprovements().intValue(); - - TemplateModel appModel = environment.getGlobalVariable( - FreemarkerServlet.KEY_APPLICATION_PRIVATE); - if (!(appModel instanceof ServletContextHashModel)) { - appModel = environment.getGlobalVariable( - FreemarkerServlet.KEY_APPLICATION); - } - if (appModel instanceof ServletContextHashModel) { - this.servlet = ((ServletContextHashModel) appModel).getServlet(); - } else { - throw new TemplateModelException("Could not find an instance of " + - ServletContextHashModel.class.getName() + - " in the data model under either the name " + - FreemarkerServlet.KEY_APPLICATION_PRIVATE + " or " + - FreemarkerServlet.KEY_APPLICATION); - } - - TemplateModel requestModel = - environment.getGlobalVariable(FreemarkerServlet.KEY_REQUEST_PRIVATE); - if (!(requestModel instanceof HttpRequestHashModel)) { - requestModel = environment.getGlobalVariable( - FreemarkerServlet.KEY_REQUEST); - } - if (requestModel instanceof HttpRequestHashModel) { - HttpRequestHashModel reqHash = (HttpRequestHashModel) requestModel; - request = reqHash.getRequest(); - session = request.getSession(false); - response = reqHash.getResponse(); - wrapper = reqHash.getObjectWrapper(); - unwrapper = this.wrapper instanceof ObjectWrapperAndUnwrapper - ? (ObjectWrapperAndUnwrapper) this.wrapper : null; - } else { - throw new TemplateModelException("Could not find an instance of " + - HttpRequestHashModel.class.getName() + - " in the data model under either the name " + - FreemarkerServlet.KEY_REQUEST_PRIVATE + " or " + - FreemarkerServlet.KEY_REQUEST); - } - - // Register page attributes as per spec - setAttribute(REQUEST, request); - setAttribute(RESPONSE, response); - if (session != null) - setAttribute(SESSION, session); - setAttribute(PAGE, servlet); - setAttribute(CONFIG, servlet.getServletConfig()); - setAttribute(PAGECONTEXT, this); - setAttribute(APPLICATION, servlet.getServletContext()); - } - - ObjectWrapper getObjectWrapper() { - return wrapper; - } - - @Override - public void initialize( - Servlet servlet, ServletRequest request, ServletResponse response, - String errorPageURL, boolean needsSession, int bufferSize, - boolean autoFlush) { - throw new UnsupportedOperationException(); - } - - @Override - public void release() { - } - - @Override - public void setAttribute(String name, Object value) { - setAttribute(name, value, PAGE_SCOPE); - } - - @Override - public void setAttribute(String name, Object value, int scope) { - switch(scope) { - case PAGE_SCOPE: { - try { - environment.setGlobalVariable(name, wrapper.wrap(value)); - break; - } catch (TemplateModelException e) { - throw new UndeclaredThrowableException(e); - } - } - case REQUEST_SCOPE: { - getRequest().setAttribute(name, value); - break; - } - case SESSION_SCOPE: { - getSession(true).setAttribute(name, value); - break; - } - case APPLICATION_SCOPE: { - getServletContext().setAttribute(name, value); - break; - } - default: { - throw new IllegalArgumentException("Invalid scope " + scope); - } - } - } - - @Override - public Object getAttribute(String name) { - return getAttribute(name, PAGE_SCOPE); - } - - @Override - public Object getAttribute(String name, int scope) { - switch (scope) { - case PAGE_SCOPE: { - try { - final TemplateModel tm = environment.getGlobalNamespace().get(name); - if (incompatibleImprovements >= _TemplateAPI.VERSION_INT_2_3_22 && unwrapper != null) { - return unwrapper.unwrap(tm); - } else { // Legacy behavior branch - if (tm instanceof AdapterTemplateModel) { - return ((AdapterTemplateModel) tm).getAdaptedObject(OBJECT_CLASS); - } - if (tm instanceof WrapperTemplateModel) { - return ((WrapperTemplateModel) tm).getWrappedObject(); - } - if (tm instanceof TemplateScalarModel) { - return ((TemplateScalarModel) tm).getAsString(); - } - if (tm instanceof TemplateNumberModel) { - return ((TemplateNumberModel) tm).getAsNumber(); - } - if (tm instanceof TemplateBooleanModel) { - return Boolean.valueOf(((TemplateBooleanModel) tm).getAsBoolean()); - } - if (incompatibleImprovements >= _TemplateAPI.VERSION_INT_2_3_22 - && tm instanceof TemplateDateModel) { - return ((TemplateDateModel) tm).getAsDate(); - } - return tm; - } - } catch (TemplateModelException e) { - throw new UndeclaredThrowableException("Failed to unwrapp FTL global variable", e); - } - } - case REQUEST_SCOPE: { - return getRequest().getAttribute(name); - } - case SESSION_SCOPE: { - HttpSession session = getSession(false); - if (session == null) { - return null; - } - return session.getAttribute(name); - } - case APPLICATION_SCOPE: { - return getServletContext().getAttribute(name); - } - default: { - throw new IllegalArgumentException("Invalid scope " + scope); - } - } - } - - @Override - public Object findAttribute(String name) { - Object retval = getAttribute(name, PAGE_SCOPE); - if (retval != null) return retval; - retval = getAttribute(name, REQUEST_SCOPE); - if (retval != null) return retval; - retval = getAttribute(name, SESSION_SCOPE); - if (retval != null) return retval; - return getAttribute(name, APPLICATION_SCOPE); - } - - @Override - public void removeAttribute(String name) { - removeAttribute(name, PAGE_SCOPE); - removeAttribute(name, REQUEST_SCOPE); - removeAttribute(name, SESSION_SCOPE); - removeAttribute(name, APPLICATION_SCOPE); - } - - @Override - public void removeAttribute(String name, int scope) { - switch(scope) { - case PAGE_SCOPE: { - environment.getGlobalNamespace().remove(name); - break; - } - case REQUEST_SCOPE: { - getRequest().removeAttribute(name); - break; - } - case SESSION_SCOPE: { - HttpSession session = getSession(false); - if (session != null) { - session.removeAttribute(name); - } - break; - } - case APPLICATION_SCOPE: { - getServletContext().removeAttribute(name); - break; - } - default: { - throw new IllegalArgumentException("Invalid scope: " + scope); - } - } - } - - @Override - public int getAttributesScope(String name) { - if (getAttribute(name, PAGE_SCOPE) != null) return PAGE_SCOPE; - if (getAttribute(name, REQUEST_SCOPE) != null) return REQUEST_SCOPE; - if (getAttribute(name, SESSION_SCOPE) != null) return SESSION_SCOPE; - if (getAttribute(name, APPLICATION_SCOPE) != null) return APPLICATION_SCOPE; - return 0; - } - - @Override - public Enumeration getAttributeNamesInScope(int scope) { - switch(scope) { - case PAGE_SCOPE: { - try { - return - new TemplateHashModelExEnumeration(environment.getGlobalNamespace()); - } catch (TemplateModelException e) { - throw new UndeclaredThrowableException(e); - } - } - case REQUEST_SCOPE: { - return getRequest().getAttributeNames(); - } - case SESSION_SCOPE: { - HttpSession session = getSession(false); - if (session != null) { - return session.getAttributeNames(); - } - return Collections.enumeration(Collections.EMPTY_SET); - } - case APPLICATION_SCOPE: { - return getServletContext().getAttributeNames(); - } - default: { - throw new IllegalArgumentException("Invalid scope " + scope); - } - } - } - - @Override - public JspWriter getOut() { - return jspOut; - } - - private HttpSession getSession(boolean create) { - if (session == null) { - session = request.getSession(create); - if (session != null) { - setAttribute(SESSION, session); - } - } - return session; - } - - @Override - public HttpSession getSession() { - return getSession(false); - } - - @Override - public Object getPage() { - return servlet; - } - - @Override - public ServletRequest getRequest() { - return request; - } - - @Override - public ServletResponse getResponse() { - return response; - } - - @Override - public Exception getException() { - throw new UnsupportedOperationException(); - } - - @Override - public ServletConfig getServletConfig() { - return servlet.getServletConfig(); - } - - @Override - public ServletContext getServletContext() { - return servlet.getServletContext(); - } - - @Override - public void forward(String url) throws ServletException, IOException { - //TODO: make sure this is 100% correct by looking at Jasper output - request.getRequestDispatcher(url).forward(request, response); - } - - @Override - public void include(String url) throws ServletException, IOException { - jspOut.flush(); - request.getRequestDispatcher(url).include(request, response); - } - - @Override - public void include(String url, boolean flush) throws ServletException, IOException { - if (flush) { - jspOut.flush(); - } - final PrintWriter pw = new PrintWriter(jspOut); - request.getRequestDispatcher(url).include(request, new HttpServletResponseWrapper(response) { - @Override - public PrintWriter getWriter() { - return pw; - } - - @Override - public ServletOutputStream getOutputStream() { - throw new UnsupportedOperationException("JSP-included resource must use getWriter()"); - } - }); - pw.flush(); - } - - @Override - public void handlePageException(Exception e) { - throw new UnsupportedOperationException(); - } - - @Override - public void handlePageException(Throwable e) { - throw new UnsupportedOperationException(); - } - - @Override - public BodyContent pushBody() { - return (BodyContent) pushWriter(new TagTransformModel.BodyContentImpl(getOut(), true)); - } - - @Override -public JspWriter pushBody(Writer w) { - return pushWriter(new JspWriterAdapter(w)); - } - - @Override - public JspWriter popBody() { - popWriter(); - return (JspWriter) getAttribute(OUT); - } - - Object peekTopTag(Class tagClass) { - for (ListIterator iter = tags.listIterator(tags.size()); iter.hasPrevious(); ) { - Object tag = iter.previous(); - if (tagClass.isInstance(tag)) { - return tag; - } - } - return null; - } - - void popTopTag() { - tags.remove(tags.size() - 1); - } - - void popWriter() { - jspOut = (JspWriter) outs.remove(outs.size() - 1); - setAttribute(OUT, jspOut); - } - - void pushTopTag(Object tag) { - tags.add(tag); - } - - JspWriter pushWriter(JspWriter out) { - outs.add(jspOut); - jspOut = out; - setAttribute(OUT, jspOut); - return out; - } - - private static class TemplateHashModelExEnumeration implements Enumeration { - private final TemplateModelIterator it; - - private TemplateHashModelExEnumeration(TemplateHashModelEx hashEx) throws TemplateModelException { - it = hashEx.keys().iterator(); - } - - public boolean hasMoreElements() { - try { - return it.hasNext(); - } catch (TemplateModelException tme) { - throw new UndeclaredThrowableException(tme); - } - } - - public Object nextElement() { - try { - return ((TemplateScalarModel) it.next()).getAsString(); - } catch (TemplateModelException tme) { - throw new UndeclaredThrowableException(tme); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/FreemarkerTag.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/FreemarkerTag.java b/src/main/java/freemarker/ext/jsp/FreemarkerTag.java deleted file mode 100644 index cc1f584..0000000 --- a/src/main/java/freemarker/ext/jsp/FreemarkerTag.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.tagext.BodyContent; -import javax.servlet.jsp.tagext.BodyTag; -import javax.servlet.jsp.tagext.Tag; - -import freemarker.template.SimpleHash; -import freemarker.template.Template; - -/** - * Simple implementation of JSP tag to allow use of FreeMarker templates in - * JSP. Inspired by similar class in Velocity template engine developed by - * <a href="mailto:[email protected]">Geir Magnusson Jr.</a> - */ -public class FreemarkerTag implements BodyTag { - private Tag parent; - private BodyContent bodyContent; - private PageContext pageContext; - private SimpleHash root; - private Template template; - private boolean caching = true; - private String name = ""; - - public boolean getCaching() { - return caching; - } - - public void setCaching(boolean caching) { - this.caching = caching; - } - - public void setName(String name) { - this.name = name == null ? "" : name; - } - - public Tag getParent() { - return parent; - } - - public void setParent(Tag parent) { - this.parent = parent; - } - - public int doStartTag() { - return EVAL_BODY_BUFFERED; - } - - public void setBodyContent(BodyContent bodyContent) { - this.bodyContent = bodyContent; - } - - public void setPageContext(PageContext pageContext) { - this.pageContext = pageContext; - root = null; - } - - public void doInitBody() { - } - - public int doAfterBody() { - return SKIP_BODY; - } - - public void release() { - root = null; - template = null; - name = ""; - } - - public int doEndTag() - throws JspException { - if (bodyContent == null) - return EVAL_PAGE; - - try { - if (template == null) { - template = new Template(name, bodyContent.getReader()); - } - - if (root == null) { - root = new SimpleHash(); - root.put("page", new JspContextModel(pageContext, JspContextModel.PAGE_SCOPE)); - root.put("request", new JspContextModel(pageContext, JspContextModel.REQUEST_SCOPE)); - root.put("session", new JspContextModel(pageContext, JspContextModel.SESSION_SCOPE)); - root.put("application", new JspContextModel(pageContext, JspContextModel.APPLICATION_SCOPE)); - root.put("any", new JspContextModel(pageContext, JspContextModel.ANY_SCOPE)); - } - template.process(root, pageContext.getOut()); - } catch (Exception e) { - try { - pageContext.handlePageException(e); - } catch (ServletException e2) { - throw new JspException(e2.getMessage()); - } catch (IOException e2) { - throw new JspException(e2.getMessage()); - } - } finally { - if (!caching) { - template = null; - } - } - - return EVAL_PAGE; - } -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/JspContextModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/JspContextModel.java b/src/main/java/freemarker/ext/jsp/JspContextModel.java deleted file mode 100644 index fd40e2a..0000000 --- a/src/main/java/freemarker/ext/jsp/JspContextModel.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import javax.servlet.jsp.PageContext; - -import freemarker.ext.beans.BeansWrapper; -import freemarker.template.TemplateHashModel; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; - -class JspContextModel -implements - TemplateHashModel { - public static final int ANY_SCOPE = -1; - public static final int PAGE_SCOPE = PageContext.PAGE_SCOPE; - public static final int REQUEST_SCOPE = PageContext.REQUEST_SCOPE; - public static final int SESSION_SCOPE = PageContext.SESSION_SCOPE; - public static final int APPLICATION_SCOPE = PageContext.APPLICATION_SCOPE; - - private final PageContext pageContext; - private final int scope; - - public JspContextModel(PageContext pageContext, int scope) { - this.pageContext = pageContext; - this.scope = scope; - } - - public TemplateModel get(String key) throws TemplateModelException { - Object bean = scope == ANY_SCOPE ? pageContext.findAttribute(key) : pageContext.getAttribute(key, scope); - return BeansWrapper.getDefaultInstance().wrap(bean); - } - - public boolean isEmpty() { - return false; - } -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/JspTagModelBase.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/JspTagModelBase.java b/src/main/java/freemarker/ext/jsp/JspTagModelBase.java deleted file mode 100644 index a1fb081..0000000 --- a/src/main/java/freemarker/ext/jsp/JspTagModelBase.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.math.BigDecimal; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import freemarker.core._DelayedJQuote; -import freemarker.core._DelayedShortClassName; -import freemarker.core._ErrorDescriptionBuilder; -import freemarker.core._TemplateModelException; -import freemarker.ext.beans.BeansWrapper; -import freemarker.ext.jsp.SimpleTagDirectiveModel.TemplateExceptionWrapperJspException; -import freemarker.template.ObjectWrapper; -import freemarker.template.ObjectWrapperAndUnwrapper; -import freemarker.template.Template; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; -import freemarker.template.utility.StringUtil; - -class JspTagModelBase { - protected final String tagName; - private final Class tagClass; - private final Method dynaSetter; - private final Map propertySetters = new HashMap(); - - protected JspTagModelBase(String tagName, Class tagClass) throws IntrospectionException { - this.tagName = tagName; - this.tagClass = tagClass; - BeanInfo bi = Introspector.getBeanInfo(tagClass); - PropertyDescriptor[] pda = bi.getPropertyDescriptors(); - for (int i = 0; i < pda.length; i++) { - PropertyDescriptor pd = pda[i]; - Method m = pd.getWriteMethod(); - if (m != null) { - propertySetters.put(pd.getName(), m); - } - } - // Check to see if the tag implements the JSP2.0 DynamicAttributes - // interface, to allow setting of arbitrary attributes - Method dynaSetter; - try { - dynaSetter = tagClass.getMethod("setDynamicAttribute", - new Class[] {String.class, String.class, Object.class}); - } catch (NoSuchMethodException nsme) { - dynaSetter = null; - } - this.dynaSetter = dynaSetter; - } - - Object getTagInstance() throws IllegalAccessException, InstantiationException { - return tagClass.newInstance(); - } - - void setupTag(Object tag, Map args, ObjectWrapper wrapper) - throws TemplateModelException, - InvocationTargetException, - IllegalAccessException { - if (args != null && !args.isEmpty()) { - ObjectWrapperAndUnwrapper unwrapper = - wrapper instanceof ObjectWrapperAndUnwrapper ? (ObjectWrapperAndUnwrapper) wrapper - : BeansWrapper.getDefaultInstance(); // [2.4] Throw exception in this case - final Object[] argArray = new Object[1]; - for (Iterator iter = args.entrySet().iterator(); iter.hasNext(); ) { - final Map.Entry entry = (Map.Entry) iter.next(); - final Object arg = unwrapper.unwrap((TemplateModel) entry.getValue()); - argArray[0] = arg; - final Object paramName = entry.getKey(); - Method setterMethod = (Method) propertySetters.get(paramName); - if (setterMethod == null) { - if (dynaSetter == null) { - throw new TemplateModelException("Unknown property " - + StringUtil.jQuote(paramName.toString()) - + " on instance of " + tagClass.getName()); - } else { - dynaSetter.invoke(tag, null, paramName, argArray[0]); - } - } else { - if (arg instanceof BigDecimal) { - argArray[0] = BeansWrapper.coerceBigDecimal( - (BigDecimal) arg, setterMethod.getParameterTypes()[0]); - } - try { - setterMethod.invoke(tag, argArray); - } catch (Exception e) { - final Class setterType = setterMethod.getParameterTypes()[0]; - final _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( - "Failed to set JSP tag parameter ", new _DelayedJQuote(paramName), - " (declared type: ", new _DelayedShortClassName(setterType) - + ", actual value's type: ", - (argArray[0] != null - ? (Object) new _DelayedShortClassName(argArray[0].getClass()) : "Null"), - "). See cause exception for the more specific cause..."); - if (e instanceof IllegalArgumentException && !(setterType.isAssignableFrom(String.class)) - && argArray[0] != null && argArray[0] instanceof String) { - desc.tip("This problem is often caused by unnecessary parameter quotation. Paramters " - + "aren't quoted in FTL, similarly as they aren't quoted in most languages. " - + "For example, these parameter assignments are wrong: ", - "<@my.tag p1=\"true\" p2=\"10\" p3=\"${someVariable}\" p4=\"${x+1}\" />", - ". The correct form is: ", - "<@my.tag p1=true p2=10 p3=someVariable p4=x+1 />", - ". Only string literals are quoted (regardless of where they occur): ", - "<@my.box style=\"info\" message=\"Hello ${name}!\" width=200 />", - "."); - } - throw new _TemplateModelException(e, null, desc); - } - } - } - } - } - - protected final TemplateModelException toTemplateModelExceptionOrRethrow(Exception e) throws TemplateModelException { - if (e instanceof RuntimeException && !isCommonRuntimeException((RuntimeException) e)) { - throw (RuntimeException) e; - } - if (e instanceof TemplateModelException) { - throw (TemplateModelException) e; - } - if (e instanceof TemplateExceptionWrapperJspException) { - return (TemplateModelException) e.getCause(); - } - return new _TemplateModelException(e, - "Error while invoking the ", new _DelayedJQuote(tagName), " JSP custom tag; see cause exception"); - } - - /** - * Runtime exceptions that we don't want to propagate, instead we warp them into a more helpful exception. These are - * the ones where it's very unlikely that someone tries to catch specifically these around - * {@link Template#process(Object, java.io.Writer)}. - */ - private boolean isCommonRuntimeException(RuntimeException e) { - final Class eClass = e.getClass(); - // We deliberately don't accept sub-classes. Those are possibly application specific and some want to catch them - // outside the template. - return eClass == NullPointerException.class - || eClass == IllegalArgumentException.class - || eClass == ClassCastException.class - || eClass == IndexOutOfBoundsException.class; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/JspWriterAdapter.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/JspWriterAdapter.java b/src/main/java/freemarker/ext/jsp/JspWriterAdapter.java deleted file mode 100644 index 5f69f73..0000000 --- a/src/main/java/freemarker/ext/jsp/JspWriterAdapter.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import java.io.IOException; -import java.io.Writer; - -import javax.servlet.jsp.JspWriter; - -import freemarker.template.utility.SecurityUtilities; - -class JspWriterAdapter extends JspWriter { - static final char[] NEWLINE = SecurityUtilities.getSystemProperty("line.separator", "\n").toCharArray(); - - private final Writer out; - - JspWriterAdapter(Writer out) { - super(0, true); - this.out = out; - } - - @Override - public String toString() { - return "JspWriterAdapter wrapping a " + out.toString(); - } - - @Override - public void clear() throws IOException { - throw new IOException("Can't clear"); - } - - @Override - public void clearBuffer() throws IOException { - throw new IOException("Can't clear"); - } - - @Override - public void close() throws IOException { - throw new IOException("Close not permitted."); - } - - @Override - public void flush() throws IOException { - out.flush(); - } - - @Override - public int getRemaining() { - return 0; - } - - @Override - public void newLine() throws IOException { - out.write(NEWLINE); - } - - @Override - public void print(boolean arg0) throws IOException { - out.write(arg0 ? Boolean.TRUE.toString() : Boolean.FALSE.toString()); - } - - @Override - public void print(char arg0) throws IOException { - out.write(arg0); - } - - @Override - public void print(char[] arg0) throws IOException { - out.write(arg0); - } - - @Override - public void print(double arg0) throws IOException { - out.write(Double.toString(arg0)); - } - - @Override - public void print(float arg0) throws IOException { - out.write(Float.toString(arg0)); - } - - @Override - public void print(int arg0) throws IOException { - out.write(Integer.toString(arg0)); - } - - @Override - public void print(long arg0) throws IOException { - out.write(Long.toString(arg0)); - } - - @Override - public void print(Object arg0) throws IOException { - out.write(arg0 == null ? "null" : arg0.toString()); - } - - @Override - public void print(String arg0) throws IOException { - out.write(arg0); - } - - @Override - public void println() throws IOException { - newLine(); - } - - @Override - public void println(boolean arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(char arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(char[] arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(double arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(float arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(int arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(long arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(Object arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(String arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void write(int c) throws IOException { - out.write(c); - } - - @Override - public void write(char[] arg0, int arg1, int arg2) - throws IOException { - out.write(arg0, arg1, arg2); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/PageContextFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/PageContextFactory.java b/src/main/java/freemarker/ext/jsp/PageContextFactory.java deleted file mode 100644 index 9990d4c..0000000 --- a/src/main/java/freemarker/ext/jsp/PageContextFactory.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import javax.servlet.jsp.PageContext; - -import freemarker.core.Environment; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; -import freemarker.template.utility.UndeclaredThrowableException; - -/** - */ -class PageContextFactory { - private static final Class pageContextImpl = getPageContextImpl(); - - private static Class getPageContextImpl() { - try { - try { - PageContext.class.getMethod("getELContext", (Class[]) null); - return Class.forName("freemarker.ext.jsp._FreeMarkerPageContext21"); - } catch (NoSuchMethodException e1) { - throw new IllegalStateException( - "Since FreeMarker 3.0.0, JSP support requires at least JSP 2.1."); - } - } catch (ClassNotFoundException e) { - throw new NoClassDefFoundError(e.getMessage()); - } - } - - static FreeMarkerPageContext getCurrentPageContext() throws TemplateModelException { - Environment env = Environment.getCurrentEnvironment(); - TemplateModel pageContextModel = env.getGlobalVariable(PageContext.PAGECONTEXT); - if (pageContextModel instanceof FreeMarkerPageContext) { - return (FreeMarkerPageContext) pageContextModel; - } - try { - FreeMarkerPageContext pageContext = - (FreeMarkerPageContext) pageContextImpl.newInstance(); - env.setGlobalVariable(PageContext.PAGECONTEXT, pageContext); - return pageContext; - } catch (IllegalAccessException e) { - throw new IllegalAccessError(e.getMessage()); - } catch (InstantiationException e) { - throw new UndeclaredThrowableException(e); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/SimpleTagDirectiveModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/SimpleTagDirectiveModel.java b/src/main/java/freemarker/ext/jsp/SimpleTagDirectiveModel.java deleted file mode 100644 index 9c802fb..0000000 --- a/src/main/java/freemarker/ext/jsp/SimpleTagDirectiveModel.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import java.beans.IntrospectionException; -import java.io.IOException; -import java.io.Writer; -import java.util.Map; - -import javax.servlet.jsp.JspContext; -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.tagext.JspFragment; -import javax.servlet.jsp.tagext.JspTag; -import javax.servlet.jsp.tagext.SimpleTag; -import javax.servlet.jsp.tagext.Tag; - -import freemarker.core.Environment; -import freemarker.template.TemplateDirectiveBody; -import freemarker.template.TemplateDirectiveModel; -import freemarker.template.TemplateException; -import freemarker.template.TemplateModel; - -/** - * Adapts a {@link SimpleTag}-based custom JSP tag to be a value that's callable in templates as an user-defined - * directive. For {@link Tag}-based custom JSP tags {@link TagTransformModel} is used instead. - */ -class SimpleTagDirectiveModel extends JspTagModelBase implements TemplateDirectiveModel { - protected SimpleTagDirectiveModel(String tagName, Class tagClass) throws IntrospectionException { - super(tagName, tagClass); - if (!SimpleTag.class.isAssignableFrom(tagClass)) { - throw new IllegalArgumentException(tagClass.getName() + - " does not implement either the " + Tag.class.getName() + - " interface or the " + SimpleTag.class.getName() + - " interface."); - } - } - - public void execute(Environment env, Map args, TemplateModel[] outArgs, - final TemplateDirectiveBody body) - throws TemplateException, IOException { - try { - SimpleTag tag = (SimpleTag) getTagInstance(); - final FreeMarkerPageContext pageContext = PageContextFactory.getCurrentPageContext(); - pageContext.pushWriter(new JspWriterAdapter(env.getOut())); - try { - tag.setJspContext(pageContext); - JspTag parentTag = (JspTag) pageContext.peekTopTag(JspTag.class); - if (parentTag != null) { - tag.setParent(parentTag); - } - setupTag(tag, args, pageContext.getObjectWrapper()); - if (body != null) { - tag.setJspBody(new JspFragment() { - @Override - public JspContext getJspContext() { - return pageContext; - } - - @Override - public void invoke(Writer out) throws JspException, IOException { - try { - body.render(out == null ? pageContext.getOut() : out); - } catch (TemplateException e) { - throw new TemplateExceptionWrapperJspException(e); - } - } - }); - pageContext.pushTopTag(tag); - try { - tag.doTag(); - } finally { - pageContext.popTopTag(); - } - } else { - tag.doTag(); - } - } finally { - pageContext.popWriter(); - } - } catch (Exception e) { - throw toTemplateModelExceptionOrRethrow(e); - } - } - - static final class TemplateExceptionWrapperJspException extends JspException { - - public TemplateExceptionWrapperJspException(Throwable cause) { - super("Nested content has thrown template exception", cause); - } - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/ext/jsp/TagTransformModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/jsp/TagTransformModel.java b/src/main/java/freemarker/ext/jsp/TagTransformModel.java deleted file mode 100644 index 8ff7fc1..0000000 --- a/src/main/java/freemarker/ext/jsp/TagTransformModel.java +++ /dev/null @@ -1,420 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.ext.jsp; - -import java.beans.IntrospectionException; -import java.io.CharArrayReader; -import java.io.CharArrayWriter; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; -import java.util.Map; - -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.JspWriter; -import javax.servlet.jsp.tagext.BodyContent; -import javax.servlet.jsp.tagext.BodyTag; -import javax.servlet.jsp.tagext.IterationTag; -import javax.servlet.jsp.tagext.SimpleTag; -import javax.servlet.jsp.tagext.Tag; -import javax.servlet.jsp.tagext.TryCatchFinally; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateTransformModel; -import freemarker.template.TransformControl; - -/** - * Adapts a {@link Tag}-based custom JSP tag to be a value that's callable in templates as an user-defined directive. - * For {@link SimpleTag}-based custom JSP tags {@link SimpleTagDirectiveModel} is used instead. - */ -class TagTransformModel extends JspTagModelBase implements TemplateTransformModel { - private static final Logger LOG = LoggerFactory.getLogger("freemarker.jsp"); - - private final boolean isBodyTag; - private final boolean isIterationTag; - private final boolean isTryCatchFinally; - - public TagTransformModel(String tagName, Class tagClass) throws IntrospectionException { - super(tagName, tagClass); - isIterationTag = IterationTag.class.isAssignableFrom(tagClass); - isBodyTag = isIterationTag && BodyTag.class.isAssignableFrom(tagClass); - isTryCatchFinally = TryCatchFinally.class.isAssignableFrom(tagClass); - } - - @Override - public Writer getWriter(Writer out, Map args) throws TemplateModelException { - try { - Tag tag = (Tag) getTagInstance(); - FreeMarkerPageContext pageContext = PageContextFactory.getCurrentPageContext(); - Tag parentTag = (Tag) pageContext.peekTopTag(Tag.class); - tag.setParent(parentTag); - tag.setPageContext(pageContext); - setupTag(tag, args, pageContext.getObjectWrapper()); - // If the parent of this writer is not a JspWriter itself, use - // a little Writer-to-JspWriter adapter... - boolean usesAdapter; - if (out instanceof JspWriter) { - // This is just a sanity check. If it were JDK 1.4-only, - // we'd use an assert. - if (out != pageContext.getOut()) { - throw new TemplateModelException( - "out != pageContext.getOut(). Out is " + - out + " pageContext.getOut() is " + - pageContext.getOut()); - } - usesAdapter = false; - } else { - out = new JspWriterAdapter(out); - pageContext.pushWriter((JspWriter) out); - usesAdapter = true; - } - JspWriter w = new TagWriter(out, tag, pageContext, usesAdapter); - pageContext.pushTopTag(tag); - pageContext.pushWriter(w); - return w; - } catch (Exception e) { - throw toTemplateModelExceptionOrRethrow(e); - } - } - - /** - * An implementation of BodyContent that buffers it's input to a char[]. - */ - static class BodyContentImpl extends BodyContent { - private CharArrayWriter buf; - - BodyContentImpl(JspWriter out, boolean buffer) { - super(out); - if (buffer) initBuffer(); - } - - void initBuffer() { - buf = new CharArrayWriter(); - } - - @Override - public void flush() throws IOException { - if (buf == null) { - getEnclosingWriter().flush(); - } - } - - @Override - public void clear() throws IOException { - if (buf != null) { - buf = new CharArrayWriter(); - } else { - throw new IOException("Can't clear"); - } - } - - @Override - public void clearBuffer() throws IOException { - if (buf != null) { - buf = new CharArrayWriter(); - } else { - throw new IOException("Can't clear"); - } - } - - @Override - public int getRemaining() { - return Integer.MAX_VALUE; - } - - @Override - public void newLine() throws IOException { - write(JspWriterAdapter.NEWLINE); - } - - @Override - public void close() throws IOException { - } - - @Override - public void print(boolean arg0) throws IOException { - write(arg0 ? Boolean.TRUE.toString() : Boolean.FALSE.toString()); - } - - @Override - public void print(char arg0) throws IOException { - write(arg0); - } - - @Override - public void print(char[] arg0) throws IOException { - write(arg0); - } - - @Override - public void print(double arg0) throws IOException { - write(Double.toString(arg0)); - } - - @Override - public void print(float arg0) throws IOException { - write(Float.toString(arg0)); - } - - @Override - public void print(int arg0) throws IOException { - write(Integer.toString(arg0)); - } - - @Override - public void print(long arg0) throws IOException { - write(Long.toString(arg0)); - } - - @Override - public void print(Object arg0) throws IOException { - write(arg0 == null ? "null" : arg0.toString()); - } - - @Override - public void print(String arg0) throws IOException { - write(arg0); - } - - @Override - public void println() throws IOException { - newLine(); - } - - @Override - public void println(boolean arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(char arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(char[] arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(double arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(float arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(int arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(long arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(Object arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void println(String arg0) throws IOException { - print(arg0); - newLine(); - } - - @Override - public void write(int c) throws IOException { - if (buf != null) { - buf.write(c); - } else { - getEnclosingWriter().write(c); - } - } - - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - if (buf != null) { - buf.write(cbuf, off, len); - } else { - getEnclosingWriter().write(cbuf, off, len); - } - } - - @Override - public String getString() { - return buf.toString(); - } - - @Override - public Reader getReader() { - return new CharArrayReader(buf.toCharArray()); - } - - @Override - public void writeOut(Writer out) throws IOException { - buf.writeTo(out); - } - - } - - class TagWriter extends BodyContentImpl implements TransformControl { - private final Tag tag; - private final FreeMarkerPageContext pageContext; - private boolean needPop = true; - private final boolean needDoublePop; - - TagWriter(Writer out, Tag tag, FreeMarkerPageContext pageContext, boolean needDoublePop) { - super((JspWriter) out, false); - this.needDoublePop = needDoublePop; - this.tag = tag; - this.pageContext = pageContext; - } - - @Override - public String toString() { - return "TagWriter for " + tag.getClass().getName() + " wrapping a " + getEnclosingWriter().toString(); - } - - Tag getTag() { - return tag; - } - - FreeMarkerPageContext getPageContext() { - return pageContext; - } - - @Override - public int onStart() - throws TemplateModelException { - try { - int dst = tag.doStartTag(); - switch(dst) { - case Tag.SKIP_BODY: - // EVAL_PAGE is illegal actually, but some taglibs out there - // use it, and it seems most JSP compilers allow them to and - // treat it identically to SKIP_BODY, so we're going with - // the flow and we allow it too, altough strictly speaking - // it's in violation of the spec. - case Tag.EVAL_PAGE: { - endEvaluation(); - return TransformControl.SKIP_BODY; - } - case BodyTag.EVAL_BODY_BUFFERED: { - if (isBodyTag) { - initBuffer(); - BodyTag btag = (BodyTag) tag; - btag.setBodyContent(this); - btag.doInitBody(); - } else { - throw new TemplateModelException("Can't buffer body since " + tag.getClass().getName() + " does not implement BodyTag."); - } - // Intentional fall-through - } - case Tag.EVAL_BODY_INCLUDE: { - return TransformControl.EVALUATE_BODY; - } - default: { - throw new RuntimeException("Illegal return value " + dst + " from " + tag.getClass().getName() + ".doStartTag()"); - } - } - } catch (Exception e) { - throw toTemplateModelExceptionOrRethrow(e); - } - } - - @Override - public int afterBody() - throws TemplateModelException { - try { - if (isIterationTag) { - int dab = ((IterationTag) tag).doAfterBody(); - switch(dab) { - case Tag.SKIP_BODY: - endEvaluation(); - return END_EVALUATION; - case IterationTag.EVAL_BODY_AGAIN: - return REPEAT_EVALUATION; - default: - throw new TemplateModelException("Unexpected return value " + dab + "from " + tag.getClass().getName() + ".doAfterBody()"); - } - } - endEvaluation(); - return END_EVALUATION; - } catch (Exception e) { - throw toTemplateModelExceptionOrRethrow(e); - } - } - - private void endEvaluation() throws JspException { - if (needPop) { - pageContext.popWriter(); - needPop = false; - } - if (tag.doEndTag() == Tag.SKIP_PAGE) { - LOG.warn("Tag.SKIP_PAGE was ignored from a {} tag.", tag.getClass().getName()); - } - } - - @Override - public void onError(Throwable t) throws Throwable { - if (isTryCatchFinally) { - ((TryCatchFinally) tag).doCatch(t); - } else { - throw t; - } - } - - @Override - public void close() { - if (needPop) { - pageContext.popWriter(); - } - pageContext.popTopTag(); - try { - if (isTryCatchFinally) { - ((TryCatchFinally) tag).doFinally(); - } - // No pooling yet - tag.release(); - } finally { - if (needDoublePop) { - pageContext.popWriter(); - } - } - } - - } -}
