Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2003 The Visigoth Software Society. All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution, if
  *    any, must include the following acknowledgement:
  *       "This product includes software developed by the
  *        Visigoth Software Society (http://www.visigoths.org/)."
  *    Alternately, this acknowledgement may appear in the software itself,
  *    if and wherever such third-party acknowledgements normally appear.
  *
  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the 
  *    project contributors may be used to endorse or promote products derived
  *    from this software without prior written permission. For written
  *    permission, please contact visigoths@visigoths.org.
  *
  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
  *    nor may "FreeMarker" or "Visigoth" appear in their names
  *    without prior written permission of the Visigoth Software Society.
  *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED.  IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Visigoth Software Society. For more
  * information on the Visigoth Software Society, please see
  * http://www.visigoths.org/
  */
 
 package freemarker.ext.jsp;
 
 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.Tag;
 import  javax.servlet.jsp.tagext.TryCatchFinally;
 

Author(s):
Attila Szegedi
Version:
$Id: TagTransformModel.java,v 1.17.2.2 2006/07/08 14:45:34 ddekany Exp $
 
 {
     private static final Logger logger = Logger.getLogger("freemarker.jsp");
     
     private final boolean isBodyTag;
     private final boolean isIterationTag;
     private final boolean isTryCatchFinally;
             
     public TagTransformModel(Class tagClassthrows IntrospectionException {
         super(tagClass);
          = IterationTag.class.isAssignableFrom(tagClass);
          =  && BodyTag.class.isAssignableFrom(tagClass);
          = TryCatchFinally.class.isAssignableFrom(tagClass);
     }
     
     public Writer getWriter(Writer outMap argsthrows TemplateModelException
     {
         try {
             Tag tag = (Tag)getTagInstance();
             FreeMarkerPageContext pageContext = PageContextFactory.getCurrentPageContext();
            Tag parentTag = (Tag)pageContext.peekTopTag(Tag.class);
            tag.setParent(parentTag);
            tag.setPageContext(pageContext);
            setupTag(tagargspageContext.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(outtagpageContextusesAdapter);
            pageContext.pushTopTag(tag);
            pageContext.pushWriter(w);
            return w;
        }
        catch(TemplateModelException e) {
            throw e;
        }
        catch(RuntimeException e) {
            throw e;
        }
        catch(Exception e) {
            throw new TemplateModelException(e);
        }
    }

    
An implementation of BodyContent that buffers it's input to a char[].
    static class BodyContentImpl extends BodyContent {
        private CharArrayWriter buf;
        BodyContentImpl(JspWriter outboolean buffer) {
            super(out);
            if (bufferinitBuffer();
        }
        void initBuffer() {
             = new CharArrayWriter();
        }
        public void flush() throws IOException {
            if( == null) {
                getEnclosingWriter().flush();
            }
        }
        public void clear() throws IOException {
            if( != null) {
                 = new CharArrayWriter();
            }
            else {
                throw new IOException("Can't clear");
            }
        }
        public void clearBuffer() throws IOException {
            if( != null) {
                 = new CharArrayWriter();
            }
            else {
                throw new IOException("Can't clear");
            }
        }
        public int getRemaining() {
            return .;
        }
        public void newLine() throws IOException {
            write(.);
        }
        public void close() throws IOException {
        }
        public void print(boolean arg0throws IOException {
            write(arg0 ? ..toString() : ..toString());
        }
        public void print(char arg0throws IOException
        {
            write(arg0);
        }
        public void print(char[] arg0throws IOException
        {
            write(arg0);
        }
        public void print(double arg0throws IOException
        {
            write(Double.toString(arg0));
        }
        public void print(float arg0throws IOException
        {
            write(Float.toString(arg0));
        }
        public void print(int arg0throws IOException
        {
            write(Integer.toString(arg0));
        }
        public void print(long arg0throws IOException
        {
            write(Long.toString(arg0));
        }
        public void print(Object arg0throws IOException
        {
            write(arg0 == null ? "null" : arg0.toString());
        }
        public void print(String arg0throws IOException
        {
            write(arg0);
        }
        public void println() throws IOException
        {
            newLine();
        }
        public void println(boolean arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(char arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(char[] arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(double arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(float arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(int arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(long arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(Object arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void println(String arg0throws IOException
        {
            print(arg0);
            newLine();
        }
        public void write(int cthrows IOException
        {
            if( != null) {
                .write(c);
            }
            else {
                getEnclosingWriter().write(c);
            }
        }
        public void write(char[] cbufint offint lenthrows IOException
        {
            if( != null) {
                .write(cbufofflen);
            }
            else {
                getEnclosingWriter().write(cbufofflen);
            }
        }
        public String getString() {
            return .toString();
        }
        public Reader getReader() {
            return new CharArrayReader(.toCharArray());
        }
        public void writeOut(Writer outthrows IOException {
            .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 tagFreeMarkerPageContext pageContextboolean needDoublePop)
        {
            super((JspWriter)outfalse);
            this. = needDoublePop;
            this. = tag;
            this. = pageContext;
        }
        
        public String toString() {
            return "TagWriter for " + .getClass().getName() + " wrapping a " + getEnclosingWriter().toString();
        }
        Tag getTag()
        {
            return ;
        }
        
        {
            return ;
        }
        
        public int onStart()
        throws
            TemplateModelException
        {
            try {
                int dst = .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 is in violation of the spec.
                    case Tag.EVAL_PAGE: {
                        endEvaluation();
                        return .;
                    }
                    case BodyTag.EVAL_BODY_BUFFERED: {
                        if() {
                            initBuffer();
                            BodyTag btag = (BodyTag);
                            btag.setBodyContent(this);
                            btag.doInitBody();
                        }
                        else {
                            throw new TemplateModelException("Can't buffer body since " + .getClass().getName() + " does not implement BodyTag.");
                        }
                        // Intentional fall-through
                    }
                    case Tag.EVAL_BODY_INCLUDE: {
                        return .;
                    }
                    default: {
                        throw new RuntimeException("Illegal return value " + dst + " from " + .getClass().getName() + ".doStartTag()");
                    }
                }
            }
            catch(JspException e) {
                throw new TemplateModelException(e.getMessage(), e);
            }
        }
        
        public int afterBody()
        throws
            TemplateModelException
        {
            try {
                if() {
                    int dab = ((IterationTag)).doAfterBody();
                    switch(dab) {
                        case Tag.SKIP_BODY: {
                            endEvaluation();
                            return ;
                        }
                        case IterationTag.EVAL_BODY_AGAIN: {
                            return ;
                        }
                        default: {
                            throw new TemplateModelException("Unexpected return value " + dab + "from " + .getClass().getName() + ".doAfterBody()");
                        }
                    }
                }
                endEvaluation();
                return ;
            }
            catch(JspException e) {
                throw new TemplateModelException(e);
            }
        }
        
        private void endEvaluation() throws JspException {
            if() {
                .popWriter();
                 = false;
            }
            if(.doEndTag() == Tag.SKIP_PAGE) {
                .warn("Tag.SKIP_PAGE was ignored from a " + .getClass().getName() + " tag.");
            }
        }
        
        public void onError(Throwable tthrows Throwable {
            if() {
                ((TryCatchFinally)).doCatch(t);
            }
            else {
                throw t;
            }
        }
        
        public void close() {
            if() {
                .popWriter();
            }
            .popTopTag();
            try {
                if() {
                    ((TryCatchFinally)).doFinally();
                }
                // No pooling yet
                .release();
            }
            finally {
                if() {
                    .popWriter();
                }
            }
        }
        
    }
New to GrepCode? Check out our FAQ X