1 16 package org.apache.cocoon.portal.layout.renderer.aspect.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.sax.SAXResult ; 27 import javax.xml.transform.sax.TransformerHandler ; 28 29 import org.apache.avalon.framework.activity.Disposable; 30 import org.apache.avalon.framework.parameters.ParameterException; 31 import org.apache.avalon.framework.parameters.Parameters; 32 import org.apache.avalon.framework.service.ServiceException; 33 import org.apache.avalon.framework.service.ServiceManager; 34 import org.apache.avalon.framework.configuration.Configuration; 35 import org.apache.avalon.framework.configuration.ConfigurationException; 36 import org.apache.avalon.framework.configuration.Configurable; 37 import org.apache.cocoon.portal.PortalService; 38 import org.apache.cocoon.portal.layout.Layout; 39 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext; 40 import org.apache.cocoon.sitemap.PatternException; 41 import org.apache.cocoon.xml.IncludeXMLConsumer; 42 import org.apache.cocoon.components.variables.VariableResolverFactory; 43 import org.apache.cocoon.components.variables.VariableResolver; 44 import org.apache.excalibur.source.Source; 45 import org.apache.excalibur.source.SourceResolver; 46 import org.apache.excalibur.xml.xslt.XSLTProcessor; 47 import org.apache.excalibur.xml.xslt.XSLTProcessorException; 48 import org.xml.sax.ContentHandler ; 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.ext.LexicalHandler ; 51 52 91 public class XSLTAspect 92 extends AbstractAspect 93 implements Disposable, Configurable { 94 95 protected List variables = new ArrayList (); 96 97 protected Parameters parameters = null; 98 99 protected VariableResolverFactory variableFactory; 100 101 104 public void service(ServiceManager manager) throws ServiceException { 105 super.service(manager); 106 this.variableFactory = (VariableResolverFactory) this.manager.lookup(VariableResolverFactory.ROLE); 107 } 108 109 110 113 public void configure(Configuration config) throws ConfigurationException { 114 Configuration parameterItems = config.getChild("parameters", false); 115 116 if (parameterItems != null) { 117 this.parameters = Parameters.fromConfiguration(parameterItems); 118 } 119 } 120 121 124 public void toSAX(RendererAspectContext context, 125 Layout layout, 126 PortalService service, 127 ContentHandler handler) 128 throws SAXException { 129 PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration(); 130 131 XSLTProcessor processor = null; 132 Source stylesheet = null; 133 SourceResolver resolver = null; 134 try { 135 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 136 stylesheet = resolver.resolveURI(this.getStylesheetURI(config, layout)); 137 processor = (XSLTProcessor) this.manager.lookup(config.xsltRole); 138 TransformerHandler transformer = processor.getTransformerHandler(stylesheet); 139 if (config.parameters.size() > 0) { 141 Map.Entry entry; 142 Transformer theTransformer = transformer.getTransformer(); 143 Iterator iter = config.parameters.entrySet().iterator(); 144 while (iter.hasNext()) { 145 entry = (Map.Entry ) iter.next(); 146 String value = getParameterValue(entry); 147 theTransformer.setParameter((String ) entry.getKey(), value); 148 } 149 } 150 151 Map parameter = layout.getParameters(); 152 if (parameter.size() > 0) { 153 Map.Entry entry; 154 Transformer theTransformer = transformer.getTransformer(); 155 for (Iterator iter = parameter.entrySet().iterator(); iter.hasNext();) { 156 entry = (Map.Entry ) iter.next(); 157 theTransformer.setParameter((String )entry.getKey(), entry.getValue()); 158 } 159 } 160 SAXResult result = new SAXResult (new IncludeXMLConsumer((handler))); 161 if (handler instanceof LexicalHandler ) { 162 result.setLexicalHandler((LexicalHandler ) handler); 163 } 164 transformer.setResult(result); 165 transformer.startDocument(); 166 context.invokeNext(layout, service, transformer); 167 168 transformer.endDocument(); 169 } catch (XSLTProcessorException xpe) { 170 throw new SAXException ("XSLT Exception.", xpe); 171 } catch (IOException io) { 172 throw new SAXException ("Error in resolving.", io); 173 } catch (ServiceException ce) { 174 throw new SAXException ("Unable to lookup component.", ce); 175 } finally { 176 if (null != resolver) { 177 resolver.release(stylesheet); 178 this.manager.release(resolver); 179 } 180 this.manager.release(processor); 181 } 182 } 183 184 protected String getStylesheetURI(PreparedConfiguration config, Layout layout) 185 throws SAXException { 186 try { 188 String stylesheet = config.stylesheet.resolve(); 189 return stylesheet; 190 } catch (PatternException pe) { 191 throw new SAXException ("Pattern exception during variable resolving.", pe); 192 } 193 } 194 195 protected String getParameterValue(Map.Entry entry) throws SAXException { 196 try { 197 return ((VariableResolver)entry.getValue()).resolve(); 198 } catch (PatternException pe) { 199 throw new SAXException ("Unable to get value for parameter " + entry.getKey(), pe); 200 } 201 } 202 203 204 protected static class PreparedConfiguration { 205 public VariableResolver stylesheet; 206 public String xsltRole; 207 public Map parameters = new HashMap (); 208 209 public void takeValues(PreparedConfiguration from) { 210 this.stylesheet = from.stylesheet; 211 this.xsltRole = from.xsltRole; 212 this.parameters = from.parameters; 213 } 214 } 215 216 219 public Object prepareConfiguration(Parameters configuration) 220 throws ParameterException { 221 PreparedConfiguration pc = new PreparedConfiguration(); 222 pc.xsltRole = configuration.getParameter("xslt-processor-role", XSLTProcessor.ROLE); 223 String stylesheet = configuration.getParameter("style"); 224 try { 225 pc.stylesheet = this.variableFactory.lookup( stylesheet ); 226 } catch (PatternException pe) { 227 throw new ParameterException("Unknown pattern for stylesheet " + stylesheet, pe); 228 } 229 this.variables.add(pc.stylesheet); 230 if (this.parameters != null) { 231 String [] name = this.parameters.getNames(); 232 for (int i=0; i < name.length; ++i) { 233 try { 234 VariableResolver resolver = 235 this.variableFactory.lookup(this.parameters.getParameter(name[i])); 236 this.variables.add(resolver); 237 pc.parameters.put(name[i], resolver); 238 } catch (PatternException e) { 239 throw new ParameterException("Invalid value for parameter " + name[i], e); 240 } 241 } 242 } 243 return pc; 244 } 245 246 249 public void dispose() { 250 if ( this.manager != null ) { 251 Iterator vars = this.variables.iterator(); 252 while ( vars.hasNext() ) { 253 this.variableFactory.release( (VariableResolver) vars.next() ); 254 } 255 this.variables.clear(); 256 this.manager.release( this.variableFactory); 257 this.manager = null; 258 this.variableFactory = null; 259 } 260 } 261 } | Popular Tags |