KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > transform > DocumentTransform


1 /*
2  * $Id: DocumentTransform.java,v 1.7 2004/06/07 20:38:11 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/transform/DocumentTransform.java,v $
4  */

5
6 package org.infohazard.maverick.transform;
7
8 import java.io.IOException JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import javax.servlet.RequestDispatcher JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.infohazard.maverick.flow.Transform;
18 import org.infohazard.maverick.flow.TransformContext;
19 import org.infohazard.maverick.flow.TransformStep;
20
21 /**
22  * This Transform wraps the input one or more times by putting
23  * the output from the previous step in the request attributes.
24  */

25 class DocumentTransform implements Transform
26 {
27     /**
28      * Transform path.
29      */

30     protected String JavaDoc path;
31     /**
32      * The name.
33      */

34     protected String JavaDoc wrappedName;
35
36     /**
37      * Logger.
38      */

39     private static Log log = LogFactory.getLog(DocumentTransform.class);
40
41     /**
42      * Construct with path and name.
43      * @param path path
44      * @param wrappedName name
45      */

46     public DocumentTransform(String JavaDoc path, String JavaDoc wrappedName)
47     {
48         this.path = path;
49         this.wrappedName = wrappedName;
50         
51         log.debug("Created DocumentTransform with wrapped name \"" + wrappedName + "\" and path " + path);
52     }
53     
54     /**
55      * @see org.infohazard.maverick.flow.Transform#createStep(org.infohazard.maverick.flow.TransformContext)
56      */

57     public TransformStep createStep(TransformContext tctx) throws ServletException JavaDoc
58     {
59         return new Step(tctx);
60     }
61     
62     /**
63      * Transform step.
64      */

65     protected class Step extends StringTransformStep
66     {
67         /**
68          * Construct.
69          * @param tctx transform context
70          * @throws ServletException
71          */

72         public Step(TransformContext tctx) throws ServletException JavaDoc
73         {
74             super(tctx);
75         }
76     
77         /**
78          * @see org.infohazard.maverick.flow.TransformStep#go(java.lang.String)
79          */

80         public void go(String JavaDoc input) throws IOException JavaDoc, ServletException JavaDoc
81         {
82             if (log.isDebugEnabled())
83                 log.debug("Wrapping text with length " + input.length());
84             
85             // Populate params, if applicable
86
if (this.getTransformCtx().getTransformParams() != null)
87             {
88                 Iterator JavaDoc entriesIt = this.getTransformCtx().getTransformParams().entrySet().iterator();
89                 while (entriesIt.hasNext())
90                 {
91                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entriesIt.next();
92                     this.getTransformCtx().getRequest()
93                             .setAttribute((String JavaDoc)entry.getKey(), entry.getValue());
94                 }
95             }
96             
97             // Set up request attribute with input
98
this.getTransformCtx().getRequest().setAttribute(wrappedName, input);
99
100             RequestDispatcher JavaDoc disp = this.getTransformCtx().getRequest().getRequestDispatcher(path);
101             if (null == disp)
102                 throw new ServletException JavaDoc("RequestDispatcher could not be created for " + path);
103
104             if (log.isDebugEnabled())
105                 log.debug("Transforming ("
106                             + (this.getNext().isLast() ? "forward" : "include")
107                             + ") with document: " + path);
108             
109             if (this.getNext().isLast())
110                 disp.forward(this.getTransformCtx().getRequest(), this.getNext().getResponse());
111             else
112                 disp.include(this.getTransformCtx().getRequest(), this.getNext().getResponse());
113
114             this.getNext().done();
115         }
116     }
117 }
118
Popular Tags