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.Iterator;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.parameters.ParameterException;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.portal.PortalService;
025: import org.apache.cocoon.portal.layout.Item;
026: import org.apache.cocoon.portal.layout.Layout;
027: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
028: import org.apache.cocoon.xml.AttributesImpl;
029: import org.apache.cocoon.xml.XMLUtils;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * Add several contents.
035: *
036: * <h2>Example XML:</h2>
037: * <pre>
038: * <composite>
039: * <item param1="value1" param2="value2">
040: * <!-- included content from following renderers for this item's layout-->
041: * </item>
042: * <item>
043: * <!-- included content from following renderers for this item's layout-->
044: * </item>
045: * <item param1="value1">
046: * <!-- included content from following renderers for this item's layout-->
047: * </item>
048: * </composite>
049: * </pre>
050: *
051: * <h2>Applicable to:</h2>
052: * <ul>
053: * <li>{@link org.apache.cocoon.portal.layout.CompositeLayout}</li>
054: * </ul>
055: *
056: * <h2>Parameters</h2>
057: * <table><tbody>
058: * <tr><th>root-tag</th><td>Enclose result in root tag?</td><td></td><td>boolean</td><td><code>true</code></td></tr>
059: * <tr><th>tag-name</th><td>Name of root tag to use.</td><td></td><td>String</td><td><code>"composite"</code></td></tr>
060: * <tr><th>item-tag</th><td>Enclose each item in item tag?</td><td></td><td>boolean</td><td><code>true</code></td></tr>
061: * <tr><th>item-tag-name</th><td>Name of item tag to use.</td><td></td><td>String</td><td><code>"item"</code></td></tr>
062: * <tr><th>root-tag-id</th><td>Value of optional id attribute for the root tag.</td><td></td><td>String</td><td><code>-</code></td></tr>
063: * <tr><th>root-tag-class</th><td>Value of optional class attribute for the root tag.</td><td></td><td>String</td><td><code>-</code></td></tr>
064: * </tbody></table>
065: *
066: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
067: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
068: *
069: * @version CVS $Id:CompositeContentAspect.java 30932 2004-07-29 17:35:38Z vgritsenko $
070: */
071: public class CompositeContentAspect extends AbstractCompositeAspect {
072:
073: protected static final String ITEM_STRING = "item";
074:
075: /**
076: * @see org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractCompositeAspect#toSAX(org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
077: */
078: public void toSAX(RendererAspectContext context, Layout layout,
079: PortalService service, ContentHandler handler)
080: throws SAXException {
081: PreparedConfiguration config = (PreparedConfiguration) context
082: .getAspectConfiguration();
083: if (config.rootTag) {
084: final AttributesImpl ai = new AttributesImpl();
085: if (config.rootTagClass != null) {
086: ai.addCDATAAttribute("class", config.rootTagClass);
087: }
088: if (config.rootTagId != null) {
089: ai.addCDATAAttribute("id", config.rootTagId);
090: }
091: XMLUtils.startElement(handler, config.tagName, ai);
092: }
093: super .toSAX(context, layout, service, handler);
094: if (config.rootTag) {
095: XMLUtils.endElement(handler, config.tagName);
096: }
097:
098: }
099:
100: /**
101: * @see org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractCompositeAspect#processItem(org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext, org.apache.cocoon.portal.layout.Item, org.xml.sax.ContentHandler, org.apache.cocoon.portal.PortalService)
102: */
103: protected void processItem(RendererAspectContext context,
104: Item item, ContentHandler handler, PortalService service)
105: throws SAXException {
106: final PreparedConfiguration config = (PreparedConfiguration) context
107: .getAspectConfiguration();
108: Layout layout = item.getLayout();
109:
110: if (config.itemTag) {
111: Map parameters = item.getParameters();
112: if (parameters.size() == 0) {
113: XMLUtils.startElement(handler, config.itemTagName);
114: } else {
115: AttributesImpl attributes = new AttributesImpl();
116:
117: Map.Entry entry;
118: for (Iterator iter = parameters.entrySet().iterator(); iter
119: .hasNext();) {
120: entry = (Map.Entry) iter.next();
121: attributes.addCDATAAttribute((String) entry
122: .getKey(), (String) entry.getValue());
123: }
124: XMLUtils.startElement(handler, config.itemTagName,
125: attributes);
126: }
127: }
128: processLayout(layout, service, handler);
129: if (config.itemTag) {
130: XMLUtils.endElement(handler, config.itemTagName);
131: }
132: }
133:
134: protected class PreparedConfiguration {
135: public String tagName;
136: public boolean rootTag;
137: public boolean itemTag;
138: public String itemTagName;
139: public String rootTagId;
140: public String rootTagClass;
141:
142: public void takeValues(PreparedConfiguration from) {
143: this .tagName = from.tagName;
144: this .rootTag = from.rootTag;
145: this .itemTag = from.itemTag;
146: this .itemTagName = from.itemTagName;
147: this .rootTagId = from.rootTagId;
148: this .rootTagClass = from.rootTagClass;
149: }
150: }
151:
152: /**
153: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
154: */
155: public Object prepareConfiguration(Parameters configuration)
156: throws ParameterException {
157: PreparedConfiguration pc = new PreparedConfiguration();
158: pc.tagName = configuration
159: .getParameter("tag-name", "composite");
160: pc.rootTag = configuration.getParameterAsBoolean("root-tag",
161: true);
162: pc.itemTag = configuration.getParameterAsBoolean("item-tag",
163: true);
164: pc.itemTagName = configuration.getParameter("item-tag-name",
165: ITEM_STRING);
166: pc.rootTagId = configuration.getParameter("root-tag-id", null);
167: pc.rootTagClass = configuration.getParameter("root-tag-class",
168: null);
169: return pc;
170: }
171:
172: }
|