1 5 6 package org.infohazard.maverick.transform; 7 8 import java.io.IOException ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import javax.servlet.RequestDispatcher ; 13 import javax.servlet.ServletException ; 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 25 class DocumentTransform implements Transform 26 { 27 30 protected String path; 31 34 protected String wrappedName; 35 36 39 private static Log log = LogFactory.getLog(DocumentTransform.class); 40 41 46 public DocumentTransform(String path, String 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 57 public TransformStep createStep(TransformContext tctx) throws ServletException 58 { 59 return new Step(tctx); 60 } 61 62 65 protected class Step extends StringTransformStep 66 { 67 72 public Step(TransformContext tctx) throws ServletException 73 { 74 super(tctx); 75 } 76 77 80 public void go(String input) throws IOException , ServletException 81 { 82 if (log.isDebugEnabled()) 83 log.debug("Wrapping text with length " + input.length()); 84 85 if (this.getTransformCtx().getTransformParams() != null) 87 { 88 Iterator entriesIt = this.getTransformCtx().getTransformParams().entrySet().iterator(); 89 while (entriesIt.hasNext()) 90 { 91 Map.Entry entry = (Map.Entry )entriesIt.next(); 92 this.getTransformCtx().getRequest() 93 .setAttribute((String )entry.getKey(), entry.getValue()); 94 } 95 } 96 97 this.getTransformCtx().getRequest().setAttribute(wrappedName, input); 99 100 RequestDispatcher disp = this.getTransformCtx().getRequest().getRequestDispatcher(path); 101 if (null == disp) 102 throw new ServletException ("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 |