1 16 17 package org.apache.cocoon.transformation; 18 19 import net.sourceforge.chaperon.model.extended.ExtendedGrammar; 20 import net.sourceforge.chaperon.process.extended.ExtendedDirectParserProcessor; 21 22 import org.apache.avalon.excalibur.pool.Recyclable; 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.logger.LogEnabled; 25 import org.apache.avalon.framework.logger.Logger; 26 import org.apache.avalon.framework.parameters.ParameterException; 27 import org.apache.avalon.framework.parameters.Parameterizable; 28 import org.apache.avalon.framework.parameters.Parameters; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.avalon.framework.service.Serviceable; 32 33 import org.apache.cocoon.ProcessingException; 34 import org.apache.cocoon.caching.CacheableProcessingComponent; 35 import org.apache.cocoon.components.source.SourceUtil; 36 import org.apache.cocoon.environment.SourceResolver; 37 import org.apache.cocoon.xml.XMLConsumer; 38 39 import org.apache.excalibur.source.Source; 40 import org.apache.excalibur.source.SourceException; 41 import org.apache.excalibur.source.SourceValidity; 42 import org.apache.excalibur.store.Store; 43 44 import org.exolab.castor.mapping.Mapping; 45 import org.exolab.castor.mapping.MappingException; 46 import org.exolab.castor.xml.UnmarshalHandler; 47 import org.exolab.castor.xml.Unmarshaller; 48 49 import org.xml.sax.InputSource ; 50 import org.xml.sax.SAXException ; 51 52 import java.io.IOException ; 53 import java.io.Serializable ; 54 55 import java.util.Map ; 56 57 61 public class ExtendedParserTransformer extends ExtendedDirectParserProcessor 62 implements Transformer, LogEnabled, Serviceable, Parameterizable, 63 Recyclable, Disposable, CacheableProcessingComponent 64 { 65 private String grammar = null; 66 private Source grammarSource = null; 67 private Logger logger = null; 68 private ServiceManager manager = null; 69 private SourceResolver resolver = null; 70 71 76 public void enableLogging(Logger logger) 77 { 78 this.logger = logger; 79 80 } 83 84 91 public void service(ServiceManager manager) 92 { 93 this.manager = manager; 94 } 95 96 103 public void parameterize(Parameters parameters) throws ParameterException 104 { 105 } 107 108 113 public void setConsumer(XMLConsumer consumer) 114 { 115 setContentHandler(consumer); 116 setLexicalHandler(consumer); 117 } 118 119 132 public void setup(SourceResolver resolver, Map objectmodel, String src, Parameters parameters) 133 throws ProcessingException, SAXException , IOException 134 { 135 this.resolver = resolver; 136 137 Store store = null; 138 139 try 140 { 141 this.grammar = src; 142 143 this.grammarSource = resolver.resolveURI(this.grammar); 144 145 store = (Store)this.manager.lookup(Store.TRANSIENT_STORE); 147 148 GrammarEntry entry = (GrammarEntry)store.get(this.grammarSource.getURI()); 149 150 if ((entry==null) || (entry.getValidity()==null) || 152 ((entry.getValidity().isValid(this.grammarSource.getValidity()))<=0)) 153 { 154 this.logger.info("(Re)building the grammar from '"+this.grammarSource.getURI()+"'"); 155 156 if (this.grammarSource.getInputStream()==null) 157 throw new ProcessingException("Source '"+this.grammarSource.getURI()+"' not found"); 158 159 Mapping mapping = new Mapping(); 160 161 mapping.loadMapping(new InputSource (ExtendedGrammar.class.getResource("mapping.xml") 162 .openStream())); 163 164 Unmarshaller unmarshaller = new Unmarshaller(ExtendedGrammar.class); 165 unmarshaller.setMapping(mapping); 166 167 UnmarshalHandler unmarshalHandler = unmarshaller.createHandler(); 168 SourceUtil.toSAX(this.manager, this.grammarSource, null, 169 Unmarshaller.getContentHandler(unmarshalHandler)); 170 171 ExtendedGrammar grammar = (ExtendedGrammar)unmarshalHandler.getObject(); 172 173 if (grammar==null) 174 throw new ProcessingException("Error while reading the grammar from "+src); 175 176 setExtendedGrammar(grammar); 177 178 this.logger.info("Store grammar into store for '"+this.grammarSource.getURI()+"'"); 179 store.store(this.grammarSource.getURI(), 180 new GrammarEntry(grammar, this.grammarSource.getValidity())); 181 } 182 else 183 { 184 this.logger.info("Getting grammar from store for '"+this.grammarSource.getURI()+"'"); 185 setExtendedGrammar(entry.getExtendedGrammar()); 186 } 187 } 188 catch (MappingException me) 189 { 190 throw new ProcessingException("Error while reading the grammar", me); 191 } 192 catch (SourceException se) 193 { 194 throw new ProcessingException("Error during resolving of '"+src+"'.", se); 195 } 196 catch (ServiceException se) 197 { 198 throw new ProcessingException("Could not lookup for service", se); 199 } 200 finally 201 { 202 if (store!=null) 203 this.manager.release(store); 204 } 205 } 206 207 212 public Serializable getKey() 213 { 214 return this.grammarSource.getURI(); 215 } 216 217 223 public SourceValidity getValidity() 224 { 225 return this.grammarSource.getValidity(); 226 } 227 228 231 public void recycle() 232 { 233 if ((this.resolver!=null) && (this.grammarSource!=null)) 234 { 235 this.resolver.release(this.grammarSource); 236 this.grammarSource = null; 237 } 238 } 239 240 243 public void dispose() 244 { 245 if ((this.resolver!=null) && (this.grammarSource!=null)) 246 { 247 this.resolver.release(this.grammarSource); 248 this.grammarSource = null; 249 } 250 251 this.manager = null; 252 } 253 254 257 public static class GrammarEntry implements Serializable { 258 private SourceValidity validity = null; 259 private ExtendedGrammar grammar = null; 260 261 267 public GrammarEntry(ExtendedGrammar grammar, SourceValidity validity) 268 { 269 this.grammar = grammar; 270 this.validity = validity; 271 } 272 273 278 public SourceValidity getValidity() 279 { 280 return this.validity; 281 } 282 283 288 public ExtendedGrammar getExtendedGrammar() 289 { 290 return this.grammar; 291 } 292 293 private void writeObject(java.io.ObjectOutputStream out) 294 throws IOException 295 { 296 out.writeObject(validity); 297 out.writeObject(grammar); 298 } 299 300 private void readObject(java.io.ObjectInputStream in) 301 throws IOException , ClassNotFoundException 302 { 303 validity = (SourceValidity)in.readObject(); 304 grammar = (ExtendedGrammar)in.readObject(); 305 } 306 } 307 } 308 | Popular Tags |