1 16 package org.apache.cocoon.generation; 17 18 import org.apache.commons.collections.ArrayStack; 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.avalon.framework.component.ComponentException; 21 import org.apache.avalon.framework.component.ComponentManager; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.ResourceNotFoundException; 28 import org.apache.cocoon.caching.CacheableProcessingComponent; 29 import org.apache.cocoon.components.language.generator.ProgramGenerator; 30 import org.apache.cocoon.components.source.SourceUtil; 31 import org.apache.cocoon.environment.SourceResolver; 32 import org.apache.cocoon.xml.AbstractXMLPipe; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceException; 35 import org.apache.excalibur.source.SourceValidity; 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.SAXException ; 38 39 import java.io.IOException ; 40 import java.io.Serializable ; 41 import java.util.Map ; 42 43 61 public class ServerPagesGenerator extends ServletGenerator 62 implements Disposable, CacheableProcessingComponent, Configurable { 63 66 protected ProgramGenerator programGenerator = null; 67 68 protected AbstractServerPage generator = null; 69 70 71 private Source inputSource; 72 73 private CompletionPipe completionPipe; 74 75 81 public void compose(ComponentManager manager) throws ComponentException { 82 super.compose(manager); 83 84 if (programGenerator == null) { 85 this.programGenerator = 86 (ProgramGenerator) manager.lookup(ProgramGenerator.ROLE); 87 } 88 } 89 90 public void configure(Configuration config) throws ConfigurationException { 91 boolean autoComplete = config.getChild("autocomplete-documents").getValueAsBoolean(false); 92 93 if (autoComplete) { 94 this.completionPipe = new CompletionPipe(); 95 this.completionPipe.enableLogging(getLogger()); 96 } 97 98 this.markupLanguage = config.getChild("markup-language").getValue(DEFAULT_MARKUP_LANGUAGE); 99 this.programmingLanguage = config.getChild("programming-language").getValue(DEFAULT_PROGRAMMING_LANGUAGE); 100 } 101 102 110 public Serializable getKey() { 111 Object key = generator.getKey(); 112 if (key == null) { 113 return this.inputSource.getURI(); 114 } 115 return this.inputSource.getURI() + '-' + key; 116 } 117 118 126 public SourceValidity getValidity() { 127 return generator.getValidity(); 130 } 131 132 135 protected String markupLanguage; 136 137 140 protected String programmingLanguage; 141 142 145 public final static String DEFAULT_MARKUP_LANGUAGE = "xsp"; 146 147 150 public final static String DEFAULT_PROGRAMMING_LANGUAGE = "java"; 151 152 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 153 throws ProcessingException, SAXException , IOException { 154 155 super.setup(resolver, objectModel, src, par); 156 157 String markupLanguage = this.parameters.getParameter( 158 "markup-language", this.markupLanguage); 159 String programmingLanguage = this.parameters.getParameter( 160 "programming-language", this.programmingLanguage); 161 162 try { 163 this.inputSource = this.resolver.resolveURI(src); 164 } catch (SourceException se) { 165 throw SourceUtil.handle("Error during resolving of '" + src + "'.", se); 166 } 168 169 try { 170 this.generator = (AbstractServerPage) programGenerator.load(this.manager, 171 this.inputSource, markupLanguage, programmingLanguage, this.resolver); 172 } catch (ProcessingException e) { 173 throw e; 174 } catch (Exception e) { 175 getLogger().warn("setup()", e); 176 throw new ProcessingException(e.getMessage(), e); 177 } catch (NoClassDefFoundError e) { 178 getLogger().warn("Failed to load class: " + e); 182 throw new ResourceNotFoundException(e.getMessage()); 183 } 184 185 generator.enableLogging(getLogger()); 187 188 generator.setup(super.resolver, super.objectModel, super.source, super.parameters); 189 } 190 191 202 public void generate() throws IOException , SAXException , ProcessingException { 203 204 if (this.completionPipe != null) { 205 generator.setConsumer(this.completionPipe); 206 if (this.xmlConsumer != null) { 207 this.completionPipe.setConsumer(this.xmlConsumer); 208 } else { 209 this.completionPipe.setContentHandler(this.contentHandler); 210 this.completionPipe.setLexicalHandler(this.lexicalHandler); 211 } 212 } else { 213 if (this.xmlConsumer != null) { 214 generator.setConsumer(this.xmlConsumer); 215 } else { 216 generator.setContentHandler(this.contentHandler); 217 generator.setLexicalHandler(this.lexicalHandler); 218 } 219 } 220 221 org.xml.sax.helpers.LocatorImpl locator = new org.xml.sax.helpers.LocatorImpl (); 223 locator.setSystemId(this.inputSource.getURI()); 224 this.contentHandler.setDocumentLocator(locator); 225 226 try { 228 generator.generate(); 229 } catch (IOException e) { 230 getLogger().debug("IOException in generate()", e); 231 throw e; 232 } catch (SAXException e) { 233 getLogger().debug("SAXException in generate()", e); 234 throw e; 235 } catch (ProcessingException e) { 236 getLogger().debug("ProcessingException in generate()", e); 237 throw e; 238 } catch (Exception e) { 239 getLogger().debug("Exception in generate()", e); 240 throw new ProcessingException("Exception in ServerPagesGenerator.generate()", e); 241 } finally { 242 if (generator != null) { 243 programGenerator.release(generator); 244 } 245 generator = null; 246 } 247 248 if (this.completionPipe != null) { 249 this.completionPipe.flushEvents(); 250 } 251 } 252 253 256 public void recycle() { 257 if (this.generator != null) { 258 this.programGenerator.release(this.generator); 259 this.generator = null; 260 } 261 if (this.inputSource != null) { 262 this.resolver.release(this.inputSource); 263 this.inputSource = null; 264 } 265 if (this.completionPipe != null) { 266 this.completionPipe.recycle(); 267 this.completionPipe = null; 268 } 269 super.recycle(); 270 } 271 272 275 public void dispose() { 276 this.manager.release(this.programGenerator); 277 this.programGenerator = null; 278 this.manager = null; 279 } 280 281 282 283 private final static int DOCUMENT = 0; 285 private final static int ELEMENT = 1; 286 private final static int PREFIX_MAPPING = 2; 287 private final static int CDATA = 3; 288 private final static int DTD = 4; 289 private final static int ENTITY = 5; 290 291 private final static Integer DOCUMENT_OBJ = new Integer (DOCUMENT); 293 private final static Integer ELEMENT_OBJ = new Integer (ELEMENT); 294 private final static Integer PREFIX_MAPPING_OBJ = new Integer (PREFIX_MAPPING); 295 private final static Integer CDATA_OBJ = new Integer (CDATA); 296 private final static Integer DTD_OBJ = new Integer (DTD); 297 private final static Integer ENTITY_OBJ = new Integer (ENTITY); 298 299 public class CompletionPipe extends AbstractXMLPipe { 300 301 305 protected ArrayStack eventStack = new ArrayStack(); 306 307 310 public void startDocument() throws SAXException { 311 super.startDocument(); 312 this.eventStack.push(DOCUMENT_OBJ); 313 } 314 315 318 public void endDocument() throws SAXException { 319 this.eventStack.pop(); 320 super.endDocument(); 321 } 322 323 326 public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) 327 throws SAXException { 328 super.startElement(namespaceURI, localName, rawName, atts); 329 this.eventStack.push(rawName); 330 this.eventStack.push(localName); 331 this.eventStack.push(namespaceURI); 332 this.eventStack.push(ELEMENT_OBJ); 333 } 334 335 338 public void endElement(String namespaceURI, String localName, String rawName) 339 throws SAXException { 340 this.eventStack.pop(); this.eventStack.pop(); this.eventStack.pop(); this.eventStack.pop(); super.endElement(namespaceURI, localName, rawName); 345 } 346 347 350 public void startPrefixMapping(String prefix, String uri) throws SAXException { 351 super.startPrefixMapping(prefix, uri); 352 this.eventStack.push(prefix); 353 this.eventStack.push(PREFIX_MAPPING_OBJ); 354 } 355 356 359 public void endPrefixMapping(String prefix) throws SAXException { 360 this.eventStack.pop(); this.eventStack.pop(); super.endPrefixMapping(prefix); 363 } 364 365 public void startCDATA() throws SAXException { 366 super.startCDATA(); 367 this.eventStack.push(CDATA_OBJ); 368 } 369 370 public void endCDATA() throws SAXException { 371 this.eventStack.pop(); 372 super.endCDATA(); 373 } 374 375 public void startDTD(String name, String publicId, String systemId) 376 throws SAXException { 377 super.startDTD(name, publicId, systemId); 378 this.eventStack.push(DTD_OBJ); 379 } 380 381 public void endDTD() throws SAXException { 382 this.eventStack.pop(); 383 super.endDTD(); 384 } 385 386 public void startEntity(String name) throws SAXException { 387 super.startEntity(name); 388 this.eventStack.push(name); 389 this.eventStack.push(ENTITY_OBJ); 390 } 391 392 public void endEntity(String name) throws SAXException { 393 this.eventStack.pop(); this.eventStack.pop(); super.endEntity(name); 396 } 397 398 public void flushEvents() throws SAXException { 399 400 if (this.getLogger().isWarnEnabled()) { 401 if (this.eventStack.size() > 0) { 402 this.getLogger().warn("Premature end of document generated by " + inputSource.getURI()); 403 } 404 } 405 406 while (this.eventStack.size() != 0) { 408 int event = ((Integer ) eventStack.pop()).intValue(); 409 410 switch (event) { 411 case DOCUMENT: 412 super.endDocument(); 413 break; 414 415 case ELEMENT: 416 String namespaceURI = (String ) eventStack.pop(); 417 String localName = (String ) eventStack.pop(); 418 String rawName = (String ) eventStack.pop(); 419 super.endElement(namespaceURI, localName, rawName); 420 break; 421 422 case PREFIX_MAPPING: 423 super.endPrefixMapping((String ) eventStack.pop()); 424 break; 425 426 case CDATA: 427 super.endCDATA(); 428 break; 429 430 case DTD: 431 super.endDTD(); 432 break; 433 434 case ENTITY: 435 super.endEntity((String ) eventStack.pop()); 436 break; 437 } 438 } 439 } 440 441 public void recycle() { 442 this.eventStack.clear(); 443 super.recycle(); 444 } 445 } 446 } 447 | Popular Tags |