KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > layout > renderer > impl > AbstractRenderer


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.layout.renderer.impl;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import javax.xml.transform.sax.SAXResult JavaDoc;
22 import javax.xml.transform.sax.TransformerHandler JavaDoc;
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 JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.ext.LexicalHandler JavaDoc;
41
42 /**
43  * Base class for all renderers.
44  *
45  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
46  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
47  *
48  * @version CVS $Id: AbstractRenderer.java 30932 2004-07-29 17:35:38Z vgritsenko $
49  */

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     /* (non-Javadoc)
58      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
59      */

60     public void service(ServiceManager manager) throws ServiceException {
61         this.manager = manager;
62     }
63
64     public String JavaDoc getStylesheetURI(Layout layout) {
65         return null;
66     }
67
68     public boolean useStylesheet() {
69         return false;
70     }
71
72     /**
73      * @see org.apache.avalon.framework.activity.Disposable#dispose()
74      */

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     /**
84      * Stream out raw layout
85      */

86     public void toSAX(Layout layout, PortalService service, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
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 JavaDoc transformer = processor.getTransformerHandler(stylesheet);
96                 SAXResult JavaDoc result = new SAXResult JavaDoc(new IncludeXMLConsumer((handler)));
97                 if (handler instanceof LexicalHandler JavaDoc) {
98                     result.setLexicalHandler((LexicalHandler JavaDoc) handler);
99                 }
100                 transformer.setResult(result);
101                 transformer.startDocument();
102                 this.process(layout, service, transformer);
103                 transformer.endDocument();
104             } catch (Exception JavaDoc ce) {
105                 throw new SAXException JavaDoc("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     /**
119      * Process a Layout
120      */

121     protected void processLayout(Layout layout, PortalService service, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
122         final String JavaDoc 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 JavaDoc handler) throws SAXException JavaDoc;
129
130     /**
131      * Return the aspects required for this renderer
132      */

133     public Iterator JavaDoc getAspectDescriptions() {
134         return Collections.EMPTY_LIST.iterator();
135     }
136     
137 }
138
Popular Tags