1 16 package org.apache.cocoon.components.flow.apples.samples; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.util.HashMap ; 21 import java.util.Locale ; 22 import java.util.Map ; 23 24 import javax.xml.transform.OutputKeys ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.sax.SAXTransformerFactory ; 28 import javax.xml.transform.sax.TransformerHandler ; 29 import javax.xml.transform.stream.StreamResult ; 30 31 import org.apache.avalon.framework.logger.AbstractLogEnabled; 32 import org.apache.avalon.framework.service.ServiceException; 33 import org.apache.avalon.framework.service.ServiceManager; 34 import org.apache.avalon.framework.service.Serviceable; 35 import org.apache.cocoon.ProcessingException; 36 import org.apache.cocoon.components.flow.apples.AppleController; 37 import org.apache.cocoon.components.flow.apples.AppleRequest; 38 import org.apache.cocoon.components.flow.apples.AppleResponse; 39 import org.apache.cocoon.forms.FormContext; 40 import org.apache.cocoon.forms.FormManager; 41 import org.apache.cocoon.forms.binding.Binding; 42 import org.apache.cocoon.forms.binding.BindingManager; 43 import org.apache.cocoon.forms.formmodel.Form; 44 import org.apache.cocoon.forms.transformation.FormsPipelineConfig; 45 import org.apache.cocoon.xml.dom.DOMStreamer; 46 import org.apache.excalibur.source.ModifiableSource; 47 import org.apache.excalibur.source.Source; 48 import org.apache.excalibur.source.SourceResolver; 49 import org.apache.excalibur.xml.dom.DOMParser; 50 import org.w3c.dom.Document ; 51 import org.xml.sax.InputSource ; 52 53 56 public class BindingWoodyApple extends AbstractLogEnabled implements AppleController, Serviceable { 57 58 private static final boolean CONTINUE = false; 59 private static final boolean FINISHED = true; 60 61 private Form form; 62 private Binding binding; 63 private Document document; 64 private ServiceManager serviceManager; 65 private String formPipeURI; 66 private String validPipeURI; 67 private String backendURI; 68 private Map wrapperContextMap; 69 private State state; 70 71 private interface State { 72 public void processRequest(AppleRequest req, AppleResponse res) throws ProcessingException; 73 } 74 75 private final State initializationDelegate = new State() { 76 public void processRequest(AppleRequest req, AppleResponse res) throws ProcessingException { 77 BindingWoodyApple.this.processInitialization(req, res); 78 } 79 }; 80 81 private final State validationDelegate = new State() { 82 public void processRequest(AppleRequest req, AppleResponse res) throws ProcessingException { 83 BindingWoodyApple.this.processValidation(req, res); 84 } 85 }; 86 87 { 88 state = initializationDelegate; 89 } 90 91 92 public void process(AppleRequest req, AppleResponse res) throws ProcessingException { 93 this.state.processRequest(req, res); 94 } 95 96 protected void processInitialization(AppleRequest req, AppleResponse res) throws ProcessingException { 97 98 String formURI = req.getSitemapParameter("form-src"); 99 String bindURI = req.getSitemapParameter("binding-src"); 100 this.backendURI = req.getSitemapParameter("documentURI"); 101 this.formPipeURI = req.getSitemapParameter("form-pipe"); 102 this.validPipeURI = req.getSitemapParameter("valid-pipe"); 103 104 FormManager formManager = null; 105 BindingManager binderManager = null; 106 SourceResolver resolver = null; 107 Source formSource = null; 108 Source bindSource = null; 109 Source documentSource = null; 110 111 try { 112 formManager = (FormManager) this.serviceManager.lookup(FormManager.ROLE); 113 binderManager = (BindingManager) this.serviceManager.lookup(BindingManager.ROLE); 114 resolver = (SourceResolver) this.serviceManager.lookup(SourceResolver.ROLE); 115 116 formSource = resolver.resolveURI(formURI); 117 this.form = formManager.createForm(formSource); 118 119 bindSource = resolver.resolveURI(bindURI); 120 this.binding = binderManager.createBinding(bindSource); 121 122 documentSource = resolver.resolveURI(this.backendURI); 123 this.document = loadDocumentFromSource(documentSource); 124 this.binding.loadFormFromModel(this.form, this.document); 125 126 this.getLogger().debug("apple initialisation finished .. "); 127 this.state = validationDelegate; 128 129 completeResult(res, this.formPipeURI, CONTINUE); 130 } catch (Exception e) { 131 throw new ProcessingException("Failed to initialize BindingWoodyApple. ", e); 132 } finally { 133 if (formManager != null) { 134 this.serviceManager.release(formManager); 135 } 136 if (binderManager != null) { 137 this.serviceManager.release(binderManager); 138 } 139 if (resolver != null) { 140 if (formSource != null) { 141 resolver.release(formSource); 142 } 143 if (bindSource != null) { 144 resolver.release(bindSource); 145 } 146 if (documentSource != null) { 147 resolver.release(documentSource); 148 } 149 this.serviceManager.release(resolver); 150 } 151 } 152 } 153 154 protected void processValidation(AppleRequest req, AppleResponse res) throws ProcessingException { 155 156 Source documentTarget = null; 157 SourceResolver resolver = null; 158 159 try { 160 FormContext formContext = new FormContext(req.getCocoonRequest(), Locale.US); 161 162 if (!this.form.process(formContext)) { 163 completeResult(res, this.formPipeURI, CONTINUE); 165 } else { 166 167 resolver = (SourceResolver) this.serviceManager.lookup(SourceResolver.ROLE); 168 documentTarget = resolver.resolveURI(makeTargetURI(this.backendURI)); 169 170 this.binding.saveFormToModel(this.form, this.document); 171 saveDocumentToSource(documentTarget, this.document); 172 173 completeResult(res, this.validPipeURI, FINISHED); 174 } 175 176 getLogger().debug("apple processing done .. "); 177 } catch (Exception e) { 178 throw new ProcessingException("Error processing BindingWoodyApple", e); 179 } finally { 180 if (resolver != null) { 181 if (documentTarget != null) { 182 resolver.release(documentTarget); 183 } 184 this.serviceManager.release(resolver); 185 } 186 } 187 } 188 189 private void completeResult(AppleResponse res, String uri, boolean finished) { 190 res.sendPage(uri, getContextMap()); 191 } 193 194 private Map getContextMap() { 195 if (this.wrapperContextMap == null) { 196 if (this.form != null) { 197 this.wrapperContextMap = new HashMap (); 198 this.wrapperContextMap.put(FormsPipelineConfig.CFORMSKEY, this.form); 199 } 200 } 201 return this.wrapperContextMap; 202 } 203 204 205 211 private String makeTargetURI(String path) { 212 final String sfx = ".xml"; 213 final String newSfx = "-result.xml"; 214 if (path.endsWith(sfx)) { 215 path = path.substring(0, path.length() - sfx.length()); 216 } 217 return path + newSfx; 218 } 219 220 221 230 private void saveDocumentToSource(Source docTarget, Document doc) 231 throws ProcessingException { 232 DOMParser parser = null; 233 OutputStream os = null; 234 String uri = docTarget.getURI(); 235 236 try { 237 parser = 238 (DOMParser) this.serviceManager.lookup(DOMParser.ROLE); 239 getLogger().debug("request to save file " + uri); 240 TransformerFactory tf = TransformerFactory.newInstance(); 241 242 if (docTarget instanceof ModifiableSource 243 && tf.getFeature(SAXTransformerFactory.FEATURE)) { 244 245 ModifiableSource ws = (ModifiableSource) docTarget; 246 os = ws.getOutputStream(); 247 SAXTransformerFactory stf = (SAXTransformerFactory ) tf; 248 TransformerHandler th = stf.newTransformerHandler(); 249 Transformer t = th.getTransformer(); 250 t.setOutputProperty(OutputKeys.INDENT, "true"); 251 t.setOutputProperty(OutputKeys.METHOD, "xml"); 252 th.setResult(new StreamResult (os)); 253 254 DOMStreamer streamer = new DOMStreamer(th); 255 streamer.stream(doc); 256 } else { 257 getLogger().error("Cannot not write to source " + uri); 258 } 259 } catch (Exception e) { 260 getLogger().error("Error parsing mock file " + uri, e); 261 throw new ProcessingException( 262 "Error parsing mock file " + uri, 263 e); 264 } finally { 265 if (parser != null) { 266 this.serviceManager.release(parser); 267 } 268 if (os != null) { 269 try { 270 os.flush(); 271 os.close(); 272 } catch (IOException e1) { 273 getLogger().warn( 274 "Failed to flush/close the outputstream. ", 275 e1); 276 } 277 } 278 } 279 } 280 281 282 289 private Document loadDocumentFromSource(Source documentSrc) 290 throws ProcessingException { 291 DOMParser parser = null; 292 try { 293 parser = 294 (DOMParser) this.serviceManager.lookup(DOMParser.ROLE); 295 getLogger().debug( 296 "request to load file " + documentSrc.getURI()); 297 InputSource input = new InputSource (documentSrc.getURI()); 298 return parser.parseDocument(input); 299 } catch (Exception e) { 300 throw new ProcessingException( 301 "failed to load file to bind to: ", 302 e); 303 } finally { 304 if (parser != null) { 305 this.serviceManager.release(parser); 306 } 307 } 308 } 309 310 311 public void service(ServiceManager serviceManager) throws ServiceException { 312 this.serviceManager = serviceManager; 313 } 314 } 315 | Popular Tags |