001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.layout.renderer.impl;
018:
019: import java.util.Collections;
020: import java.util.Iterator;
021:
022: import javax.xml.transform.sax.SAXResult;
023: import javax.xml.transform.sax.TransformerHandler;
024:
025: import org.apache.avalon.framework.activity.Disposable;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.ServiceSelector;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032: import org.apache.cocoon.portal.PortalService;
033: import org.apache.cocoon.portal.layout.Layout;
034: import org.apache.cocoon.portal.layout.renderer.Renderer;
035: import org.apache.cocoon.xml.IncludeXMLConsumer;
036: import org.apache.excalibur.source.Source;
037: import org.apache.excalibur.source.SourceResolver;
038: import org.apache.excalibur.xml.xslt.XSLTProcessor;
039: import org.xml.sax.ContentHandler;
040: import org.xml.sax.SAXException;
041: import org.xml.sax.ext.LexicalHandler;
042:
043: /**
044: * Base class for all renderers.
045: *
046: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
047: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
048: *
049: * @version CVS $Id: AbstractRenderer.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public abstract class AbstractRenderer extends AbstractLogEnabled
052: implements Renderer, Serviceable, Disposable, ThreadSafe {
053:
054: protected ServiceSelector rendererSelector;
055: protected ServiceManager manager;
056:
057: /* (non-Javadoc)
058: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
059: */
060: public void service(ServiceManager manager) throws ServiceException {
061: this .manager = manager;
062: }
063:
064: public String getStylesheetURI(Layout layout) {
065: return null;
066: }
067:
068: public boolean useStylesheet() {
069: return false;
070: }
071:
072: /**
073: * @see org.apache.avalon.framework.activity.Disposable#dispose()
074: */
075: public void dispose() {
076: if (null != this .manager) {
077: this .manager.release(this .rendererSelector);
078: this .manager = null;
079: this .rendererSelector = null;
080: }
081: }
082:
083: /**
084: * Stream out raw layout
085: */
086: public void toSAX(Layout layout, PortalService service,
087: ContentHandler handler) throws SAXException {
088: if (this .useStylesheet()) {
089: XSLTProcessor processor = null;
090: Source stylesheet = null;
091: SourceResolver resolver = null;
092: try {
093: resolver = (SourceResolver) this .manager
094: .lookup(SourceResolver.ROLE);
095: stylesheet = resolver.resolveURI(this
096: .getStylesheetURI(layout));
097: processor = (XSLTProcessor) this .manager
098: .lookup(XSLTProcessor.ROLE);
099: TransformerHandler transformer = processor
100: .getTransformerHandler(stylesheet);
101: SAXResult result = new SAXResult(
102: new IncludeXMLConsumer((handler)));
103: if (handler instanceof LexicalHandler) {
104: result.setLexicalHandler((LexicalHandler) handler);
105: }
106: transformer.setResult(result);
107: transformer.startDocument();
108: this .process(layout, service, transformer);
109: transformer.endDocument();
110: } catch (Exception ce) {
111: throw new SAXException("Unable to lookup component.",
112: ce);
113: } finally {
114: if (null != resolver) {
115: resolver.release(stylesheet);
116: this .manager.release(resolver);
117: }
118: this .manager.release(processor);
119: }
120: } else {
121: this .process(layout, service, handler);
122: }
123: }
124:
125: /**
126: * Process a Layout
127: */
128: protected void processLayout(Layout layout, PortalService service,
129: ContentHandler handler) throws SAXException {
130: final String rendererName = layout.getRendererName();
131: Renderer renderer = null;
132: renderer = service.getComponentManager().getRenderer(
133: rendererName);
134: renderer.toSAX(layout, service, handler);
135: }
136:
137: protected abstract void process(Layout layout,
138: PortalService service, ContentHandler handler)
139: throws SAXException;
140:
141: /**
142: * Return the aspects required for this renderer
143: */
144: public Iterator getAspectDescriptions() {
145: return Collections.EMPTY_LIST.iterator();
146: }
147:
148: }
|