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.Layout;
026: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
027: import org.apache.cocoon.xml.AttributesImpl;
028: import org.apache.cocoon.xml.XMLUtils;
029: import org.xml.sax.ContentHandler;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * Add layout parameter to resulting XML stream so that they can be picked
034: * up later from a stylesheet for example. When passing parameters to the
035: * {@link org.apache.cocoon.portal.layout.renderer.aspect.impl.XSLTAspect}
036: * consider it's ability to set XSL parameters directly.
037: *
038: * <h2>Example XML:</h2>
039: * <pre>
040: * <parameter name1="value1" name2="value2" ... >
041: * <!-- output from following renderers -->
042: * </parameter>
043: * </pre>
044: *
045: * <h2>Applicable to:</h2>
046: * <ul>
047: * <li>{@link org.apache.cocoon.portal.layout.Layout}</li>
048: * </ul>
049: *
050: * <h2>Parameters</h2>
051: * <table><tbody>
052: * <tr><th>tag-name</th><td>Name of tag holding key-value pairs as attributes.</td>
053: * <td></td><td>String</td><td><code>"parameter"</code></td></tr>
054: * </tbody></table>
055: *
056: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
057: *
058: * @version CVS $Id: ParameterAspect.java 433543 2006-08-22 06:22:54Z crossley $
059: */
060: public final class ParameterAspect extends AbstractAspect {
061:
062: /* (non-Javadoc)
063: * @see org.apache.cocoon.portal.layout.renderer.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
064: */
065: public void toSAX(RendererAspectContext context, Layout layout,
066: PortalService service, ContentHandler contenthandler)
067: throws SAXException {
068:
069: final PreparedConfiguration config = (PreparedConfiguration) context
070: .getAspectConfiguration();
071:
072: Map parameter = layout.getParameters();
073: if (parameter.size() > 0) {
074: AttributesImpl attributes = new AttributesImpl();
075: Map.Entry entry;
076: for (Iterator iter = parameter.entrySet().iterator(); iter
077: .hasNext();) {
078: entry = (Map.Entry) iter.next();
079: attributes.addCDATAAttribute((String) entry.getKey(),
080: (String) entry.getValue());
081: }
082: XMLUtils.startElement(contenthandler, config.tagName,
083: attributes);
084: } else {
085: XMLUtils.startElement(contenthandler, config.tagName);
086: }
087:
088: context.invokeNext(layout, service, contenthandler);
089:
090: XMLUtils.endElement(contenthandler, config.tagName);
091: }
092:
093: protected static class PreparedConfiguration {
094: public String tagName;
095:
096: public void takeValues(PreparedConfiguration from) {
097: this .tagName = from.tagName;
098: }
099: }
100:
101: /* (non-Javadoc)
102: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
103: */
104: public Object prepareConfiguration(Parameters configuration)
105: throws ParameterException {
106: PreparedConfiguration pc = new PreparedConfiguration();
107: pc.tagName = configuration
108: .getParameter("tag-name", "parameter");
109: return pc;
110: }
111: }
|