KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > CategoryNodeBuilder


1 /*
2  * Copyright 1999-2005 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;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27
28 /**
29  * Builds a generic container node.
30  *
31  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
32  * @version CVS $Id: CategoryNodeBuilder.java 290409 2005-09-20 10:12:53Z sylvain $
33  */

34
35 public class CategoryNodeBuilder extends AbstractParentProcessingNodeBuilder
36   implements Configurable, ThreadSafe {
37
38     // Prefix used for registering as a TreeBuilder attribute
39
private static String JavaDoc PREFIX = CategoryNodeBuilder.class.getName() + "/";
40
41     protected String JavaDoc name;
42
43     /**
44      * The category name is the value of the "category-name" child, or if not
45      * present, the name of the configuration element.
46      */

47     public void configure(Configuration config) throws ConfigurationException {
48         super.configure(config);
49         this.name = config.getChild("category-name").getValue(config.getAttribute("name"));
50     }
51
52     /** This builder has no parameters -- return <code>false</code> */
53     protected boolean hasParameters() {
54         return false;
55     }
56
57     public ProcessingNode buildNode(Configuration config) throws Exception JavaDoc {
58
59         CategoryNode node = new CategoryNode();
60         this.treeBuilder.setupNode(node, config);
61
62         // Get all children and associate them to their name
63
Map JavaDoc category = new HashMap JavaDoc();
64
65         List JavaDoc children = buildChildNodesList(config);
66         Iterator JavaDoc iter = children.iterator();
67         while(iter.hasNext()) {
68             NamedProcessingNode child = (NamedProcessingNode)iter.next();
69             category.put(child.getName(), child);
70         }
71
72         node.setCategory(this.name, category);
73
74         // Register node to allow lookup by other nodes
75
if ( !this.treeBuilder.registerNode(PREFIX + this.name, node) ) {
76             throw new ConfigurationException("Only one <map:" + this.name +
77                     "> is allowed in a sitemap. Another one is declared at " +
78                     config.getLocation());
79         }
80
81         return node;
82     }
83
84     public static CategoryNode getCategoryNode(TreeBuilder builder, String JavaDoc categoryName) {
85         return (CategoryNode)builder.getRegisteredNode(PREFIX + categoryName);
86     }
87
88     public static ProcessingNode getNamedNode(TreeBuilder builder, String JavaDoc categoryName, String JavaDoc nodeName)
89       throws Exception JavaDoc {
90         CategoryNode category = getCategoryNode(builder, categoryName);
91
92         return category.getNodeByName(nodeName);
93     }
94 }
95
Popular Tags