1 16 17 package org.apache.cocoon.transformation; 18 19 import net.sourceforge.chaperon.build.ParserAutomatonBuilder; 20 import net.sourceforge.chaperon.model.grammar.Grammar; 21 import net.sourceforge.chaperon.model.grammar.GrammarFactory; 22 import net.sourceforge.chaperon.process.ParserAutomaton; 23 import net.sourceforge.chaperon.process.ParserProcessor; 24 25 import org.apache.avalon.excalibur.pool.Recyclable; 26 import org.apache.avalon.framework.activity.Disposable; 27 import org.apache.avalon.framework.logger.LogEnabled; 28 import org.apache.avalon.framework.logger.Logger; 29 import org.apache.avalon.framework.parameters.ParameterException; 30 import org.apache.avalon.framework.parameters.Parameterizable; 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.service.Serviceable; 35 36 import org.apache.cocoon.ProcessingException; 37 import org.apache.cocoon.caching.CacheableProcessingComponent; 38 import org.apache.cocoon.components.source.SourceUtil; 39 import org.apache.cocoon.environment.SourceResolver; 40 import org.apache.cocoon.xml.XMLConsumer; 41 42 import org.apache.excalibur.source.Source; 43 import org.apache.excalibur.source.SourceException; 44 import org.apache.excalibur.source.SourceValidity; 45 import org.apache.excalibur.store.Store; 46 47 import org.xml.sax.SAXException ; 48 49 import java.io.IOException ; 50 import java.io.Serializable ; 51 52 import java.util.Map ; 53 54 83 public class ParserTransformer extends ParserProcessor 84 implements Transformer, LogEnabled, Serviceable, Parameterizable, 85 Recyclable, Disposable, CacheableProcessingComponent 86 { 87 private String grammar = null; 88 private Source grammarSource = null; 89 private Logger logger = null; 90 private ServiceManager manager = null; 91 private SourceResolver resolver = null; 92 93 98 public void enableLogging(Logger logger) 99 { 100 this.logger = logger; 101 102 } 106 107 113 public void service(ServiceManager manager) 114 { 115 this.manager = manager; 116 } 117 118 125 public void parameterize(Parameters parameters) throws ParameterException 126 { 127 setFlatten(parameters.getParameterAsBoolean("flatten", false)); 128 129 setLocalizable(parameters.getParameterAsBoolean("localizable", false)); 131 } 132 133 138 public void setConsumer(XMLConsumer consumer) 139 { 140 setContentHandler(consumer); 141 setLexicalHandler(consumer); 142 } 143 144 157 public void setup(SourceResolver resolver, Map objectmodel, String src, Parameters parameters) 158 throws ProcessingException, SAXException , IOException 159 { 160 this.resolver = resolver; 161 162 setFailSafe(parameters.getParameterAsBoolean("failsafe", false)); 163 164 Store store = null; 165 try 166 { 167 this.grammar = src; 168 169 this.grammarSource = resolver.resolveURI(this.grammar); 170 171 store = (Store)this.manager.lookup(Store.TRANSIENT_STORE); 173 174 ParserAutomatonEntry entry = (ParserAutomatonEntry)store.get(this.grammarSource.getURI()); 175 176 if ((entry==null) || (entry.getValidity()==null) || 178 ((entry.getValidity().isValid(this.grammarSource.getValidity()))<=0)) 179 { 180 this.logger.info("(Re)building the automaton from '"+this.grammarSource.getURI()+"'"); 181 182 if (this.grammarSource.getInputStream()==null) 184 throw new ProcessingException("Source '"+this.grammarSource.getURI()+"' not found"); 185 186 GrammarFactory factory = new GrammarFactory(); 187 SourceUtil.toSAX(this.manager, this.grammarSource, null, factory); 188 189 Grammar grammar = factory.getGrammar(); 192 193 if (grammar==null) 194 throw new ProcessingException("Error while reading the grammar from "+src); 195 196 ParserAutomatonBuilder builder = 197 new ParserAutomatonBuilder(grammar ); 198 199 ParserAutomaton automaton = builder.getParserAutomaton(); 200 setParserAutomaton(builder.getParserAutomaton()); 201 202 this.logger.info("Store automaton into store for '"+this.grammarSource.getURI()+"'"); 203 store.store(this.grammarSource.getURI(), 204 new ParserAutomatonEntry(automaton, this.grammarSource.getValidity())); 205 } 206 else 207 { 208 this.logger.info("Getting automaton from store for '"+this.grammarSource.getURI()+"'"); 209 setParserAutomaton(entry.getParserAutomaton()); 210 } 211 } 212 catch (SourceException se) 213 { 214 throw new ProcessingException("Error during resolving of '"+src+"'.", se); 215 } 216 catch (ServiceException se) 217 { 218 throw new ProcessingException("Could not lookup for service", se); 219 } 220 finally 221 { 222 if (store!=null) 223 this.manager.release(store); 224 } 225 } 226 227 232 public Serializable getKey() 233 { 234 return this.grammarSource.getURI(); 235 } 236 237 243 public SourceValidity getValidity() 244 { 245 return this.grammarSource.getValidity(); 246 } 247 248 251 public void recycle() 252 { 253 if ((this.resolver!=null) && (this.grammarSource!=null)) 254 { 255 this.resolver.release(this.grammarSource); 256 this.grammarSource = null; 257 } 258 } 259 260 263 public void dispose() 264 { 265 if ((this.resolver!=null) && (this.grammarSource!=null)) 266 { 267 this.resolver.release(this.grammarSource); 268 this.grammarSource = null; 269 } 270 271 this.manager = null; 272 } 273 274 277 public static class ParserAutomatonEntry implements Serializable { 278 private SourceValidity validity = null; 279 private ParserAutomaton automaton = null; 280 281 287 public ParserAutomatonEntry(ParserAutomaton automaton, SourceValidity validity) 288 { 289 this.automaton = automaton; 290 this.validity = validity; 291 } 292 293 298 public SourceValidity getValidity() 299 { 300 return this.validity; 301 } 302 303 308 public ParserAutomaton getParserAutomaton() 309 { 310 return this.automaton; 311 } 312 313 private void writeObject(java.io.ObjectOutputStream out) 314 throws IOException 315 { 316 out.writeObject(validity); 317 out.writeObject(automaton); 318 } 319 320 private void readObject(java.io.ObjectInputStream in) 321 throws IOException , ClassNotFoundException 322 { 323 validity = (SourceValidity)in.readObject(); 324 automaton = (ParserAutomaton)in.readObject(); 325 } 326 } 327 } 328 | Popular Tags |