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.components.treeprocessor.sitemap;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
022: import org.apache.cocoon.components.treeprocessor.ProcessingNode;
023: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
024: import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
025: import org.apache.cocoon.selection.Selector;
026: import org.apache.cocoon.selection.SwitchSelector;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: /**
032: *
033: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
034: * @version CVS $Id: SelectNodeBuilder.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036:
037: public class SelectNodeBuilder extends
038: AbstractParentProcessingNodeBuilder {
039:
040: private static final String SELECTOR_ROLE = Selector.ROLE
041: + "Selector";
042:
043: public ProcessingNode buildNode(Configuration config)
044: throws Exception {
045:
046: String type = this .treeBuilder.getTypeForStatement(config,
047: SELECTOR_ROLE);
048:
049: // Lists of ProcessingNode[] and test resolvers for each "when"
050: List whenChildren = new ArrayList();
051: List whenTests = new ArrayList();
052:
053: // Nodes for otherwise (if any)
054: ProcessingNode[] otherwiseNodes = null;
055:
056: Configuration[] childrenConfig = config.getChildren();
057: for (int i = 0; i < childrenConfig.length; i++) {
058:
059: Configuration childConfig = childrenConfig[i];
060: String name = childConfig.getName();
061:
062: if ("when".equals(name)) {
063:
064: checkNamespace(childConfig);
065: whenTests
066: .add(VariableResolverFactory.getResolver(
067: childConfig.getAttribute("test"),
068: this .manager));
069: whenChildren.add(buildChildNodes(childConfig));
070:
071: } else if ("otherwise".equals(name)) {
072:
073: checkNamespace(childConfig);
074: if (otherwiseNodes != null) {
075: String msg = "Duplicate " + name
076: + " (only one is allowed) at "
077: + childConfig.getLocation();
078: getLogger().error(msg);
079: throw new ConfigurationException(msg);
080: }
081:
082: otherwiseNodes = buildChildNodes(childConfig);
083:
084: } else if (isParameter(childConfig)) {
085: // ignore it. It is handled automatically in setupNode()
086:
087: } else {
088: // Unknown element
089: String msg = "Unknown element '" + name
090: + "' in select at " + childConfig.getLocation();
091: throw new ConfigurationException(msg);
092: }
093: }
094:
095: ProcessingNode[][] whenChildrenNodes = (ProcessingNode[][]) whenChildren
096: .toArray(new ProcessingNode[0][0]);
097: VariableResolver[] whenResolvers = (VariableResolver[]) whenTests
098: .toArray(new VariableResolver[whenTests.size()]);
099:
100: // Get the type and class for this selector
101: ComponentsSelector compSelector = (ComponentsSelector) this .manager
102: .lookup(SELECTOR_ROLE);
103:
104: Class clazz = null;
105: try {
106: // Find selector class
107: Selector selector = (Selector) compSelector.select(type);
108: try {
109: clazz = selector.getClass();
110: } finally {
111: compSelector.release(selector);
112: }
113: } finally {
114: this .manager.release(compSelector);
115: }
116:
117: if (SwitchSelector.class.isAssignableFrom(clazz)) {
118: SwitchSelectNode node = new SwitchSelectNode(type);
119: this .treeBuilder.setupNode(node, config);
120: node.setCases(whenChildrenNodes, whenResolvers,
121: otherwiseNodes);
122: return node;
123: } else {
124: SelectNode node = new SelectNode(type);
125: this.treeBuilder.setupNode(node, config);
126: node.setCases(whenChildrenNodes, whenResolvers,
127: otherwiseNodes);
128: return node;
129: }
130: }
131: }
|