KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
21 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
22 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
23 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
24 import org.apache.cocoon.selection.Selector;
25 import org.apache.cocoon.selection.SwitchSelector;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @version CVS $Id: SelectNodeBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35
36 public class SelectNodeBuilder extends AbstractParentProcessingNodeBuilder {
37
38     private static final String JavaDoc SELECTOR_ROLE = Selector.ROLE + "Selector";
39
40     public ProcessingNode buildNode(Configuration config) throws Exception JavaDoc {
41
42         String JavaDoc type = this.treeBuilder.getTypeForStatement(config, SELECTOR_ROLE);
43
44         // Lists of ProcessingNode[] and test resolvers for each "when"
45
List JavaDoc whenChildren = new ArrayList JavaDoc();
46         List JavaDoc whenTests = new ArrayList JavaDoc();
47
48         // Nodes for otherwise (if any)
49
ProcessingNode[] otherwiseNodes = null;
50
51         Configuration[] childrenConfig = config.getChildren();
52         for (int i = 0; i < childrenConfig.length; i++) {
53
54             Configuration childConfig = childrenConfig[i];
55             String JavaDoc name = childConfig.getName();
56
57             if ("when".equals(name)) {
58
59                 checkNamespace(childConfig);
60                 whenTests.add(
61                     VariableResolverFactory.getResolver(childConfig.getAttribute("test"), this.manager)
62                 );
63                 whenChildren.add(buildChildNodes(childConfig));
64
65             } else if ("otherwise".equals(name)) {
66
67                 checkNamespace(childConfig);
68                 if (otherwiseNodes != null) {
69                     String JavaDoc msg = "Duplicate " + name + " (only one is allowed) at " + childConfig.getLocation();
70                     getLogger().error(msg);
71                     throw new ConfigurationException(msg);
72                 }
73
74                 otherwiseNodes = buildChildNodes(childConfig);
75
76             } else if (isParameter(childConfig)) {
77                 // ignore it. It is handled automatically in setupNode()
78

79             } else {
80                 // Unknown element
81
String JavaDoc msg = "Unknown element '" + name + "' in select at " + childConfig.getLocation();
82                 throw new ConfigurationException(msg);
83             }
84         }
85
86         ProcessingNode[][] whenChildrenNodes = (ProcessingNode[][])whenChildren.toArray(new ProcessingNode[0][0]);
87         VariableResolver[] whenResolvers = (VariableResolver[])whenTests.toArray(new VariableResolver[whenTests.size()]);
88
89         // Get the type and class for this selector
90
ComponentsSelector compSelector = (ComponentsSelector)this.manager.lookup(SELECTOR_ROLE);
91
92         Class JavaDoc clazz = null;
93         try {
94             // Find selector class
95
Selector selector = (Selector)compSelector.select(type);
96             try {
97                 clazz = selector.getClass();
98             } finally {
99                 compSelector.release(selector);
100             }
101         } finally {
102             this.manager.release(compSelector);
103         }
104
105         if (SwitchSelector.class.isAssignableFrom(clazz)) {
106             SwitchSelectNode node = new SwitchSelectNode(type);
107             this.treeBuilder.setupNode(node, config);
108             node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
109             return node;
110         } else {
111             SelectNode node = new SelectNode(type);
112             this.treeBuilder.setupNode(node, config);
113             node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
114             return node;
115         }
116     }
117 }
118
Popular Tags