KickJava   Java API By Example, From Geeks To Geeks.

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


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.aspect.impl;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.ParameterException;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.portal.PortalService;
24 import org.apache.cocoon.portal.layout.Layout;
25 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
26 import org.apache.cocoon.xml.AttributesImpl;
27 import org.apache.cocoon.xml.XMLUtils;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Add layout parameter to resulting XML stream so that they can be picked
33  * up later from a stylesheet for example. When passing parameters to the
34  * {@link org.apache.cocoon.portal.layout.renderer.aspect.impl.XSLTAspect}
35  * consider it's ability to set XSL parameters directly.
36  *
37  * <h2>Example XML:</h2>
38  * <pre>
39  * &lt;parameter name1="value1" name2="value2" ... &gt;
40  * &lt;!-- output from following renderers --&gt;
41  * &lt;/parameter&gt;
42  * </pre>
43  *
44  * <h2>Applicable to:</h2>
45  * <ul>
46  * <li>{@link org.apache.cocoon.portal.layout.Layout}</li>
47  * </ul>
48  *
49  * <h2>Parameters</h2>
50  * <table><tbody>
51  * <tr><th>tag-name</th><td>Name of tag holding key-value pairs as attributes.</td>
52  * <td></td><td>String</td><td><code>"parameter"</code></td></tr>
53  * </tbody></table>
54  *
55  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
56  *
57  * @version CVS $Id: ParameterAspect.java 331660 2005-11-08 01:22:30Z rgoers $
58  */

59 public final class ParameterAspect extends AbstractAspect {
60
61     /* (non-Javadoc)
62      * @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)
63      */

64     public void toSAX(RendererAspectContext context,
65                         Layout layout,
66                         PortalService service,
67                         ContentHandler JavaDoc contenthandler)
68     throws SAXException JavaDoc {
69         
70         final PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration();
71
72         Map JavaDoc parameter = layout.getParameters();
73         if (parameter.size() > 0) {
74             AttributesImpl attributes = new AttributesImpl();
75             Map.Entry JavaDoc entry;
76             for (Iterator JavaDoc iter = parameter.entrySet().iterator(); iter.hasNext();) {
77                 entry = (Map.Entry JavaDoc) iter.next();
78                 attributes.addCDATAAttribute((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
79             }
80             XMLUtils.startElement(contenthandler, config.tagName, attributes);
81         } else {
82             XMLUtils.startElement(contenthandler, config.tagName);
83         }
84
85         context.invokeNext( layout, service, contenthandler );
86
87         XMLUtils.endElement(contenthandler, config.tagName);
88     }
89
90     protected static class PreparedConfiguration {
91         public String JavaDoc tagName;
92         
93         public void takeValues(PreparedConfiguration from) {
94             this.tagName = from.tagName;
95         }
96     }
97     
98     /* (non-Javadoc)
99      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
100      */

101     public Object JavaDoc prepareConfiguration(Parameters configuration)
102     throws ParameterException {
103         PreparedConfiguration pc = new PreparedConfiguration();
104         pc.tagName = configuration.getParameter("tag-name", "parameter");
105         return pc;
106     }
107 }
108
Popular Tags