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.aspect.impl;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.cocoon.portal.PortalService;
024: import org.apache.cocoon.portal.layout.Layout;
025: import org.apache.cocoon.portal.layout.CompositeLayout;
026: import org.apache.cocoon.portal.layout.Item;
027: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect;
028: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
029: import org.apache.cocoon.portal.layout.renderer.Renderer;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * The renderer aspect context is passed to every renderer aspect.
035: * Using this context, a renderer aspect can get it's configuration
036: * and it can invoke (if wanted) the next aspect in the aspect chain.
037: *
038: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
039: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
040: *
041: * @version CVS $Id: DefaultRendererContext.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public final class DefaultRendererContext implements
044: RendererAspectContext {
045:
046: private Iterator iterator;
047: private Iterator configIterator;
048: private Object config;
049: private Map attributes;
050: private Map objectModel;
051: private boolean isRendering;
052: private boolean isRequired;
053:
054: public DefaultRendererContext(RendererAspectChain chain,
055: Layout layout, PortalService service) {
056: this .iterator = chain.getIterator();
057: this .configIterator = chain.getConfigIterator();
058: this .isRequired = chain.isRequired();
059: Layout entryLayout = service.getEntryLayout(null);
060: if (service.isRenderable().booleanValue()) {
061: this .isRendering = true;
062: return;
063: }
064: if (entryLayout == layout) {
065: this .isRendering = true;
066: service.setRenderable(Boolean.TRUE);
067: }
068: }
069:
070: /* (non-Javadoc)
071: * @see org.apache.cocoon.portal.layout.renderer.RendererAspectContext#invokeNext(org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
072: */
073: public void invokeNext(Layout layout, PortalService service,
074: ContentHandler handler) throws SAXException {
075: if (!this .isRendering && !this .isRequired) {
076: if (layout instanceof CompositeLayout) {
077: CompositeLayout compositeLayout = (CompositeLayout) layout;
078: for (Iterator iter = compositeLayout.getItems()
079: .iterator(); iter.hasNext();) {
080: Layout itemLayout = ((Item) iter.next())
081: .getLayout();
082: if (itemLayout != null) {
083: final String rendererName = itemLayout
084: .getRendererName();
085: final Renderer renderer = service
086: .getComponentManager().getRenderer(
087: rendererName);
088: renderer.toSAX(itemLayout, service, handler);
089: }
090: }
091: }
092: return;
093: }
094: if (iterator.hasNext()) {
095: this .config = this .configIterator.next();
096: final RendererAspect aspect = (RendererAspect) iterator
097: .next();
098: aspect.toSAX(this , layout, service, handler);
099: }
100: }
101:
102: public boolean isRendering() {
103: return this .isRendering;
104: }
105:
106: /* (non-Javadoc)
107: * @see org.apache.cocoon.portal.layout.renderer.RendererAspectContext#getConfiguration()
108: */
109: public Object getAspectConfiguration() {
110: return this .config;
111: }
112:
113: /**
114: * Set an attribute
115: */
116: public void setAttribute(String key, Object attribute) {
117: if (key != null) {
118: if (this .attributes == null) {
119: this .attributes = new HashMap(10);
120: }
121: this .attributes.put(key, attribute);
122: }
123: }
124:
125: /**
126: * Get an attribute
127: */
128: public Object getAttribute(String key) {
129: if (key != null && this .attributes != null) {
130: return this .attributes.get(key);
131: }
132: return null;
133: }
134:
135: /**
136: * Remove an attribute
137: */
138: public void removeAttribute(String key) {
139: if (this .attributes != null && key != null) {
140: this .attributes.remove(key);
141: }
142: }
143:
144: /* (non-Javadoc)
145: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext#getObjectModel()
146: */
147: public Map getObjectModel() {
148: return this .objectModel;
149: }
150:
151: /**
152: * Set the object model
153: * @param map The object model
154: */
155: public void setObjectModel(Map map) {
156: this.objectModel = map;
157: }
158:
159: }
|