KickJava   Java API By Example, From Geeks To Geeks.

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


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;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.cocoon.util.StringUtils;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Base class for parent <code>ProcessingNodeBuilders</code>, providing services for parsing
30  * children nodes.
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @version CVS $Id: AbstractParentProcessingNodeBuilder.java 53751 2004-10-04 22:06:25Z vgritsenko $
34  */

35 public abstract class AbstractParentProcessingNodeBuilder extends AbstractProcessingNodeBuilder
36                                                           implements Configurable {
37
38     protected Collection JavaDoc allowedChildren;
39
40     protected Collection JavaDoc forbiddenChildren;
41
42     protected Collection JavaDoc ignoredChildren;
43
44     /**
45      * Configure the sets of allowed, forbidden and ignored children nodes.
46      */

47     public void configure(Configuration config) throws ConfigurationException {
48         this.allowedChildren = getStringCollection(config.getChild("allowed-children"));
49         this.forbiddenChildren = getStringCollection(config.getChild("forbidden-children"));
50         this.ignoredChildren = getStringCollection(config.getChild("ignored-children"));
51     }
52
53     /**
54      * Checks if a child element and is allowed, and if not throws a <code>ConfigurationException</code>.
55      *
56      * @param child the child configuration to check.
57      * @return <code>true</code> if this child should be considered or <code>false</code>
58      * if it should be ignored.
59      * @throws ConfigurationException if this child isn't allowed.
60      */

61     protected boolean isChild(Configuration child) throws ConfigurationException {
62
63         checkNamespace(child);
64
65         String JavaDoc name = child.getName();
66
67         // Is this a parameter of a parameterizable node builder ?
68
if (isParameter(child)) {
69             return false;
70         }
71
72         // Is this element to be ignored ?
73
if (ignoredChildren != null && ignoredChildren.contains(name)) {
74             if (getLogger().isDebugEnabled()) {
75                 getLogger().debug("Element '" + name + "' is ignored for building children of element '" +
76                                   child.getName() + "'");
77             }
78
79             return false;
80         }
81
82         // Is it allowed ?
83
if ( (allowedChildren != null && !allowedChildren.contains(name)) ||
84              (forbiddenChildren != null && forbiddenChildren.contains(name)) ) {
85             String JavaDoc msg = "Element '" + name + "' is not allowed at " + child.getLocation();
86             throw new ConfigurationException(msg);
87         }
88
89         return true;
90     }
91
92     protected boolean isParameter(Configuration config) throws ConfigurationException {
93         String JavaDoc name = config.getName();
94         if (name.equals(this.treeBuilder.getParameterName())) {
95             if (this.hasParameters()) {
96                 return true;
97             } else {
98                 String JavaDoc msg = "Element '" + name + "' has no parameters at " + config.getLocation();
99                 throw new ConfigurationException(msg);
100             }
101         }
102         return false;
103     }
104
105     /**
106      * Create the <code>ProcessingNode</code>s for the children of a given node.
107      * Child nodes are controlled to be actually allowed in this node.
108      */

109     protected List JavaDoc buildChildNodesList(Configuration config) throws Exception JavaDoc {
110
111         Configuration[] children = config.getChildren();
112         List JavaDoc result = new ArrayList JavaDoc();
113
114         for (int i = 0; i < children.length; i++) {
115
116             Configuration child = children[i];
117             try {
118                  if (isChild(child)) {
119                     // OK : get a builder.
120
ProcessingNodeBuilder childBuilder = this.treeBuilder.createNodeBuilder(child);
121                     result.add(childBuilder.buildNode(child));
122                 }
123
124             } catch(ConfigurationException ce) {
125                 throw ce;
126             } catch(Exception JavaDoc e) {
127                 String JavaDoc msg = "Error while creating node '" + child.getName() + "' at " + child.getLocation();
128                 throw new ConfigurationException(msg, e);
129             }
130         }
131
132         return result;
133     }
134
135     protected ProcessingNode[] buildChildNodes(Configuration config) throws Exception JavaDoc {
136         return toNodeArray(buildChildNodesList(config));
137     }
138
139     /**
140      * Convenience function that converts a <code>List</code> of <code>ProcessingNode</code>s
141      * to an array.
142      */

143     public static ProcessingNode[] toNodeArray(List JavaDoc list) {
144         return (ProcessingNode[])list.toArray(new ProcessingNode[list.size()]);
145     }
146
147     /**
148      * Splits the value of a Configuration in a Collection of Strings. Splitting
149      * occurs at space characters (incl. line breaks) and comma.
150      *
151      * @return a collection of Strings, or null if <code>config</code> has no value.
152      */

153     private Collection JavaDoc getStringCollection(Configuration config) {
154         String JavaDoc s = config.getValue(null);
155
156         return (s == null) ? null : Arrays.asList(StringUtils.split(s, ", \t\n\r"));
157     }
158 }
159
Popular Tags