KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > ComponentsSelector


1 /*
2  * Copyright 1999-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.components.treeprocessor.sitemap;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.avalon.framework.CascadingRuntimeException;
25 import org.apache.avalon.framework.component.ComponentException;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.configuration.DefaultConfiguration;
29 import org.apache.cocoon.acting.Action;
30 import org.apache.cocoon.components.ExtendedComponentSelector;
31 import org.apache.cocoon.components.ComponentLocator;
32 import org.apache.cocoon.components.pipeline.OutputComponentSelector;
33 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
34 import org.apache.cocoon.generation.Generator;
35 import org.apache.cocoon.matching.Matcher;
36 import org.apache.cocoon.reading.Reader;
37 import org.apache.cocoon.selection.Selector;
38 import org.apache.cocoon.serialization.Serializer;
39 import org.apache.cocoon.sitemap.SitemapComponentSelector;
40 import org.apache.cocoon.transformation.Transformer;
41
42 /**
43  * Component selector for sitemap components.
44  *
45  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
46  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
47  * @version CVS $Id: ComponentsSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
48  */

49
50 public class ComponentsSelector extends ExtendedComponentSelector
51                                 implements OutputComponentSelector, SitemapComponentSelector {
52
53     public static final int UNKNOWN = -1;
54     public static final int GENERATOR = 0;
55     public static final int TRANSFORMER = 1;
56     public static final int SERIALIZER = 2;
57     public static final int READER = 3;
58     public static final int MATCHER = 4;
59     public static final int SELECTOR = 5;
60     public static final int ACTION = 6;
61     public static final int PIPELINE = 7;
62
63     public static final String JavaDoc[] SELECTOR_ROLES = {
64         Generator.ROLE + "Selector",
65         Transformer.ROLE + "Selector",
66         Serializer.ROLE + "Selector",
67         Reader.ROLE + "Selector",
68         Matcher.ROLE + "Selector",
69         Selector.ROLE + "Selector",
70         Action.ROLE + "Selector",
71         ProcessingPipeline.ROLE + "Selector"
72     };
73
74     public static final String JavaDoc[] COMPONENT_NAMES = {
75         "generator",
76         "transformer",
77         "serializer",
78         "reader",
79         "matcher",
80         "selector",
81         "action",
82         "pipe"
83     };
84
85     /** The role as an integer */
86     private int roleId;
87
88     /** The mime-type for hints */
89     private Map JavaDoc hintMimeTypes;
90
91     /** The labels for hints */
92     private Map JavaDoc hintLabels;
93
94     /** The pipeline-hint Map */
95     private Map JavaDoc pipelineHints;
96
97     /** The set of known hints, used to add standard components (see ensureExists) */
98     private Set JavaDoc knownHints = new HashSet JavaDoc();
99
100     /** The parent selector, if it's of the current class */
101     private SitemapComponentSelector parentSitemapSelector;
102     
103     /* (non-Javadoc)
104      * @see org.apache.cocoon.components.ParentAware#setParentInformation(org.apache.avalon.framework.component.ComponentManager, java.lang.String)
105      */

106     public void setParentLocator(ComponentLocator locator)
107     throws ComponentException {
108         super.setParentLocator(locator);
109
110         if (super.parentSelector instanceof SitemapComponentSelector) {
111             this.parentSitemapSelector = (SitemapComponentSelector)super.parentSelector;
112         }
113     }
114
115     /**
116      * Return the component instance name according to the selector role
117      * (e.g. "action" for "org.apache.cocoon.acting.Action").
118      */

119     protected String JavaDoc getComponentInstanceName() {
120         return (this.roleId == UNKNOWN) ? null : COMPONENT_NAMES[this.roleId];
121     }
122
123     /**
124      * Get the attribute for class names. This is "src" for known roles, and
125      * "class" (the default) for other roles.
126      */

127     protected String JavaDoc getClassAttributeName() {
128         return (this.roleId == UNKNOWN) ? "class" : "src";
129     }
130
131
132     public void configure(Configuration config) throws ConfigurationException {
133         
134         // Who are we ?
135
String JavaDoc role = getRoleName(config);
136         this.roleId = UNKNOWN; // unknown
137
for (int i = 0; i < SELECTOR_ROLES.length; i++) {
138             if (SELECTOR_ROLES[i].equals(role)) {
139                 this.roleId = i;
140                 break;
141             }
142         }
143
144         if (getLogger().isDebugEnabled()) {
145             getLogger().debug("Setting up sitemap component selector for " +
146                               role + " (role id = " + this.roleId + ")");
147         }
148
149         // Only matchers and serializers can have a MIME type
150
if (this.roleId == SERIALIZER || this.roleId == READER) {
151             this.hintMimeTypes = new HashMap JavaDoc();
152         }
153
154         this.hintLabels = new HashMap JavaDoc();
155         this.pipelineHints = new HashMap JavaDoc();
156
157         super.configure(config);
158     }
159
160     /**
161      * Add a component in this selector. If needed, also register it's MIME type.
162      */

163     public void addComponent(Object JavaDoc hint, Class JavaDoc clazz, Configuration config) throws ComponentException {
164
165         super.addComponent(hint, clazz, config);
166
167         // Add to known hints
168
this.knownHints.add(hint);
169
170         if (this.roleId == SERIALIZER || this.roleId == READER) {
171             // Get mime-type
172
String JavaDoc mimeType = config.getAttribute("mime-type", null);
173             if (mimeType != null) {
174                 this.hintMimeTypes.put(hint, mimeType);
175             }
176         }
177
178         String JavaDoc label = config.getAttribute("label", null);
179         if (label != null) {
180             // Empty '' attribute will result in empty array,
181
// overriding all labels on the component declared in the parent.
182
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(label, " ,", false);
183             String JavaDoc[] labels = new String JavaDoc[st.countTokens()];
184             for (int i = 0; i < labels.length; i++) {
185                 labels[i] = st.nextToken();
186             }
187             this.hintLabels.put(hint, labels);
188         }
189
190         String JavaDoc pipelineHint = config.getAttribute("hint", null);
191         this.pipelineHints.put(hint, pipelineHint);
192     }
193
194     /**
195      * Ensure system-defined components exist (e.g. &lt;aggregator&gt;) and initialize
196      * the selector.
197      */

198     public void initialize() /*throws Exception*/ {
199
200         // FIXME : need to catch exceptions since ECS doesn't propagate the throws clause of Initializable
201
try {
202
203             DefaultConfiguration config = null;
204
205             // Ensure all system-defined hints exist.
206
// NOTE : checking this here means they can be user-defined in the sitemap
207
switch(this.roleId) {
208                 case GENERATOR :
209
210                     config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated");
211                     config.setAttribute("name", "<notifier>");
212                     ensureExists("<notifier>",
213                         org.apache.cocoon.sitemap.NotifyingGenerator.class, config);
214
215                     config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated");
216                     config.setAttribute("name", "<aggregator>");
217                     ensureExists("<aggregator>",
218                         org.apache.cocoon.sitemap.ContentAggregator.class, config);
219                 break;
220
221                 case TRANSFORMER :
222                     config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated");
223                     config.setAttribute("name", "<translator>");
224                     ensureExists("<translator>",
225                         org.apache.cocoon.sitemap.LinkTranslator.class, config);
226
227                     config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated");
228                     config.setAttribute("name", "<gatherer>");
229                     ensureExists("<gatherer>",
230                         org.apache.cocoon.sitemap.LinkGatherer.class, config);
231                 break;
232             }
233
234             super.initialize();
235
236             // Don't keep known hints (they're no more needed)
237
this.knownHints = null;
238
239         } catch(Exception JavaDoc e) {
240             throw new CascadingRuntimeException("Cannot setup default components", e);
241         }
242
243     }
244
245     /**
246      * Ensure a component exists or add it otherwhise. We cannot simply call hasComponent()
247      * since it requires to be initialized, and we want to add components, and this must
248      * be done before initialization.
249      */

250     private void ensureExists(Object JavaDoc hint, Class JavaDoc clazz, Configuration config) throws ComponentException {
251
252         if (! this.knownHints.contains(hint)) {
253             this.addComponent(hint, clazz, config);
254         }
255     }
256
257     /**
258      * Get the MIME type for a given hint.
259      */

260     public String JavaDoc getMimeTypeForHint(Object JavaDoc hint) {
261
262         if (this.hintMimeTypes == null) {
263             // Not a component that has mime types
264
return null;
265
266         } else {
267             if (this.hasDeclaredComponent(hint)) {
268                 return (String JavaDoc)this.hintMimeTypes.get(hint);
269                 
270             } else if (this.parentSitemapSelector != null) {
271                 return this.parentSitemapSelector.getMimeTypeForHint(hint);
272                 
273             } else {
274                 return null;
275             }
276         }
277     }
278
279     public boolean hasLabel(Object JavaDoc hint, String JavaDoc label) {
280         String JavaDoc[] labels = this.getLabels(hint);
281         if (labels != null) {
282             for (int i = 0; i < labels.length; i++) {
283                 if (labels[i].equals(label))
284                     return true;
285             }
286         }
287         return false;
288     }
289
290     public String JavaDoc[] getLabels(Object JavaDoc hint) {
291         // If this hint is declared locally, use its labels (if any), otherwise inherit
292
// those of the parent.
293
if (this.hasDeclaredComponent(hint)) {
294             return (String JavaDoc[])this.hintLabels.get(hint);
295             
296         } else if (this.parentSitemapSelector != null) {
297             return parentSitemapSelector.getLabels(hint);
298             
299         } else {
300             return null;
301         }
302     }
303
304     public String JavaDoc getPipelineHint(Object JavaDoc hint) {
305         // If this hint is declared locally, use its hints (if any), otherwise inherit
306
// those of the parent.
307
if (this.hasDeclaredComponent(hint)) {
308             return (String JavaDoc)this.pipelineHints.get(hint);
309         } else if (this.parentSitemapSelector != null) {
310             return this.parentSitemapSelector.getPipelineHint(hint);
311         } else {
312             return null;
313         }
314     }
315
316     public void dispose() {
317         super.dispose();
318         this.parentSitemapSelector = null;
319     }
320 }
321
Popular Tags