1 16 package org.apache.cocoon.portal.layout.renderer.impl; 17 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 21 import javax.xml.transform.sax.SAXResult ; 22 import javax.xml.transform.sax.TransformerHandler ; 23 24 import org.apache.avalon.framework.activity.Disposable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.avalon.framework.thread.ThreadSafe; 31 import org.apache.cocoon.portal.PortalService; 32 import org.apache.cocoon.portal.layout.Layout; 33 import org.apache.cocoon.portal.layout.renderer.Renderer; 34 import org.apache.cocoon.xml.IncludeXMLConsumer; 35 import org.apache.excalibur.source.Source; 36 import org.apache.excalibur.source.SourceResolver; 37 import org.apache.excalibur.xml.xslt.XSLTProcessor; 38 import org.xml.sax.ContentHandler ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.ext.LexicalHandler ; 41 42 50 public abstract class AbstractRenderer 51 extends AbstractLogEnabled 52 implements Renderer, Serviceable, Disposable, ThreadSafe { 53 54 protected ServiceSelector rendererSelector; 55 protected ServiceManager manager; 56 57 60 public void service(ServiceManager manager) throws ServiceException { 61 this.manager = manager; 62 } 63 64 public String getStylesheetURI(Layout layout) { 65 return null; 66 } 67 68 public boolean useStylesheet() { 69 return false; 70 } 71 72 75 public void dispose() { 76 if (null != this.manager) { 77 this.manager.release(this.rendererSelector); 78 this.manager = null; 79 this.rendererSelector = null; 80 } 81 } 82 83 86 public void toSAX(Layout layout, PortalService service, ContentHandler handler) throws SAXException { 87 if (this.useStylesheet()) { 88 XSLTProcessor processor = null; 89 Source stylesheet = null; 90 SourceResolver resolver = null; 91 try { 92 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 93 stylesheet = resolver.resolveURI(this.getStylesheetURI(layout)); 94 processor = (XSLTProcessor) this.manager.lookup(XSLTProcessor.ROLE); 95 TransformerHandler transformer = processor.getTransformerHandler(stylesheet); 96 SAXResult result = new SAXResult (new IncludeXMLConsumer((handler))); 97 if (handler instanceof LexicalHandler ) { 98 result.setLexicalHandler((LexicalHandler ) handler); 99 } 100 transformer.setResult(result); 101 transformer.startDocument(); 102 this.process(layout, service, transformer); 103 transformer.endDocument(); 104 } catch (Exception ce) { 105 throw new SAXException ("Unable to lookup component.", ce); 106 } finally { 107 if (null != resolver) { 108 resolver.release(stylesheet); 109 this.manager.release(resolver); 110 } 111 this.manager.release(processor); 112 } 113 } else { 114 this.process(layout, service, handler); 115 } 116 } 117 118 121 protected void processLayout(Layout layout, PortalService service, ContentHandler handler) throws SAXException { 122 final String rendererName = layout.getRendererName(); 123 Renderer renderer = null; 124 renderer = service.getComponentManager().getRenderer(rendererName); 125 renderer.toSAX(layout, service, handler); 126 } 127 128 protected abstract void process(Layout layout, PortalService service, ContentHandler handler) throws SAXException ; 129 130 133 public Iterator getAspectDescriptions() { 134 return Collections.EMPTY_LIST.iterator(); 135 } 136 137 } 138 | Popular Tags |