1 16 17 package org.apache.cocoon.transformation; 18 19 import net.sourceforge.chaperon.build.LexicalAutomatonBuilder; 20 import net.sourceforge.chaperon.model.lexicon.Lexicon; 21 import net.sourceforge.chaperon.model.lexicon.LexiconFactory; 22 import net.sourceforge.chaperon.process.LexicalAutomaton; 23 import net.sourceforge.chaperon.process.LexicalProcessor; 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 44 import org.apache.excalibur.source.Source; 45 import org.apache.excalibur.source.SourceException; 46 import org.apache.excalibur.source.SourceValidity; 47 import org.apache.excalibur.store.Store; 48 49 import org.xml.sax.SAXException ; 50 51 import java.io.IOException ; 52 import java.io.Serializable ; 53 54 import java.util.Map ; 55 56 83 public class LexicalTransformer extends LexicalProcessor 84 implements Transformer, LogEnabled, Serviceable, Recyclable, Disposable, 85 Parameterizable, CacheableProcessingComponent 86 { 87 private String lexicon = null; 88 private Source lexiconSource = 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 } 105 106 113 public void service(ServiceManager manager) 114 { 115 this.manager = manager; 116 } 117 118 125 public void parameterize(Parameters parameters) throws ParameterException 126 { 127 setLocalizable(parameters.getParameterAsBoolean("localizable", false)); 129 } 130 131 136 public void setConsumer(XMLConsumer consumer) 137 { 138 setContentHandler(consumer); 139 setLexicalHandler(consumer); 140 } 141 142 155 public void setup(SourceResolver resolver, Map objectmodel, String src, Parameters parameters) 156 throws ProcessingException, SAXException , IOException 157 { 158 this.resolver = resolver; 159 160 Store store = null; 161 162 try 163 { 164 this.lexicon = src; 165 166 this.lexiconSource = resolver.resolveURI(this.lexicon); 167 168 store = (Store)this.manager.lookup(Store.TRANSIENT_STORE); 170 171 LexicalAutomatonEntry entry = (LexicalAutomatonEntry)store.get(this.lexiconSource.getURI()); 172 173 if ((entry==null) || (entry.getValidity()==null) || 175 (entry.getValidity().isValid(this.lexiconSource.getValidity())<=0)) 176 { 177 this.logger.info("(Re)building the automaton from '"+this.lexiconSource.getURI()+"'"); 178 179 if (this.lexiconSource.getInputStream()==null) 180 throw new ProcessingException("Source '"+this.lexiconSource.getURI()+"' not found"); 181 182 LexiconFactory factory = new LexiconFactory(); 183 SourceUtil.toSAX(this.manager, this.lexiconSource, null, factory); 184 185 Lexicon lexicon = factory.getLexicon(); 186 187 LexicalAutomatonBuilder builder = 188 new LexicalAutomatonBuilder(lexicon); 189 190 LexicalAutomaton automaton = builder.getLexicalAutomaton(); 191 setLexicalAutomaton(automaton); 192 193 this.logger.info("Store automaton into store for '"+this.lexiconSource.getURI()+"'"); 194 195 store.store(this.lexiconSource.getURI(), 196 new LexicalAutomatonEntry(automaton, this.lexiconSource.getValidity())); 197 } 198 else 199 { 200 this.logger.info("Getting automaton from store for '"+this.lexiconSource.getURI()+"'"); 201 setLexicalAutomaton(entry.getLexicalAutomaton()); 202 } 203 } 204 catch (SourceException se) 205 { 206 throw new ProcessingException("Error during resolving of '"+src+"'.", se); 207 } 208 catch (ServiceException se) 209 { 210 throw new ProcessingException("Could not lookup for service", se); 211 } 212 finally 213 { 214 if (store!=null) 215 this.manager.release(store); 216 } 217 } 218 219 224 public Serializable getKey() 225 { 226 return this.lexiconSource.getURI(); 227 } 228 229 235 public SourceValidity getValidity() 236 { 237 return this.lexiconSource.getValidity(); 238 } 239 240 243 public void recycle() 244 { 245 if ((this.resolver!=null) && (this.lexiconSource!=null)) 246 { 247 this.resolver.release(this.lexiconSource); 248 this.lexiconSource = null; 249 } 250 } 251 252 255 public void dispose() 256 { 257 if ((this.resolver!=null) && (this.lexiconSource!=null)) 258 { 259 this.resolver.release(this.lexiconSource); 260 this.lexiconSource = null; 261 } 262 263 this.manager = null; 264 } 265 266 269 public static class LexicalAutomatonEntry implements Serializable { 270 private SourceValidity validity = null; 271 private LexicalAutomaton automaton = null; 272 273 279 public LexicalAutomatonEntry(LexicalAutomaton automaton, SourceValidity validity) 280 { 281 this.automaton = automaton; 282 this.validity = validity; 283 } 284 285 290 public SourceValidity getValidity() 291 { 292 return this.validity; 293 } 294 295 300 public LexicalAutomaton getLexicalAutomaton() 301 { 302 return this.automaton; 303 } 304 305 private void writeObject(java.io.ObjectOutputStream out) 306 throws IOException 307 { 308 out.writeObject(validity); 309 out.writeObject(automaton); 310 } 311 312 private void readObject(java.io.ObjectInputStream in) 313 throws IOException , ClassNotFoundException 314 { 315 validity = (SourceValidity)in.readObject(); 316 automaton = (LexicalAutomaton)in.readObject(); 317 } 318 } 319 } 320 | Popular Tags |