1 16 package org.apache.cocoon.acting; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 27 import org.apache.avalon.excalibur.component.ComponentHandler; 28 29 import org.apache.cocoon.components.sax.XMLByteStreamCompiler; 30 import org.apache.cocoon.components.sax.XMLByteStreamFragment; 31 import org.apache.cocoon.environment.ObjectModelHelper; 32 import org.apache.cocoon.environment.Redirector; 33 import org.apache.cocoon.environment.Request; 34 import org.apache.cocoon.environment.SourceResolver; 35 import org.apache.cocoon.generation.ServerPagesGenerator; 36 import org.apache.cocoon.xml.AbstractXMLConsumer; 37 38 74 public class ServerPagesAction 75 extends ConfigurableComposerAction 76 implements Disposable, ThreadSafe { 77 78 public static final String REDIRECTOR_OBJECT = "xsp-action:redirector"; 79 public static final String ACTION_RESULT_OBJECT = "xsp-action:result"; 80 public static final String ACTION_SUCCESS_OBJECT = "xsp-action:success"; 81 82 ComponentHandler generatorHandler; 83 84 public void configure(Configuration conf) 85 throws ConfigurationException { 86 try { 87 this.generatorHandler = ComponentHandler.getComponentHandler( 88 ServerPagesGenerator.class, 89 conf, 90 this.manager, 91 null, null, null, null, "N/A" ); 97 98 this.generatorHandler.enableLogging(getLogger()); 99 this.generatorHandler.initialize(); 100 101 } catch(Exception e) { 102 throw new ConfigurationException("Cannot set up component handler", e); 103 } 104 } 105 106 109 public void dispose() { 110 if (this.generatorHandler != null) { 111 this.generatorHandler.dispose(); 112 this.generatorHandler = null; 113 } 114 } 115 116 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, 117 String source, Parameters parameters) 118 throws Exception { 119 120 if (this.getLogger().isDebugEnabled()) { 121 getLogger().debug("serverpage source: " + source); 122 } 123 124 String outputKey = parameters.getParameter("output-attribute", null); 125 Map resultMap = new HashMap (); 126 Object success = null; 127 128 ServerPagesGenerator generator = (ServerPagesGenerator)this.generatorHandler.get(); 130 131 XMLByteStreamCompiler compiler = null; 133 134 try { 135 generator.enableLogging(getLogger()); 136 generator.compose(this.manager); 137 generator.setup(resolver, objectModel, source, parameters); 138 139 if (outputKey == null) { 141 generator.setConsumer(new AbstractXMLConsumer() { } ); } else { 144 compiler = new XMLByteStreamCompiler(); 146 generator.setConsumer(compiler); 147 } 148 149 objectModel.put(REDIRECTOR_OBJECT, redirector); 151 objectModel.put(ACTION_RESULT_OBJECT, resultMap); 152 153 generator.generate(); 155 success = objectModel.get(ACTION_SUCCESS_OBJECT); 156 157 } finally { 158 generatorHandler.put(generator); 160 161 objectModel.remove(REDIRECTOR_OBJECT); 163 objectModel.remove(ACTION_RESULT_OBJECT); 164 objectModel.remove(ACTION_SUCCESS_OBJECT); 165 } 166 167 if (outputKey != null) { 168 if (success == null) { 170 success = Boolean.TRUE; 171 } 172 173 if (success == Boolean.TRUE) { 174 Request req = ObjectModelHelper.getRequest(objectModel); 176 req.setAttribute(outputKey, new XMLByteStreamFragment(compiler.getSAXFragment())); 177 } 178 } 179 180 return (success == Boolean.TRUE) ? resultMap : null; 181 } 182 } 183 | Popular Tags |