KickJava   Java API By Example, From Geeks To Geeks.

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


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.sitemap;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
25 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
26 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
27 import org.apache.cocoon.components.treeprocessor.ProcessingNodeBuilder;
28
29 /**
30  * Builds a <map:pipeline>
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @author <a HREF="mailto:gianugo@apache.org">Gianugo Rabellino</a>
34  * @version $Id: PipelineNodeBuilder.java 157541 2005-03-15 14:26:31Z vgritsenko $
35  */

36 public class PipelineNodeBuilder extends AbstractParentProcessingNodeBuilder
37                                  implements ThreadSafe {
38
39     /** This builder can have parameters -- return <code>true</code> */
40     protected boolean hasParameters() {
41         return true;
42     }
43
44     public ProcessingNode buildNode(Configuration config)
45     throws Exception JavaDoc {
46         String JavaDoc type = this.treeBuilder.getTypeForStatement(config, ProcessingPipeline.ROLE + "Selector");
47         PipelineNode node = new PipelineNode(type);
48
49         this.treeBuilder.setupNode(node, config);
50         node.setInternalOnly(config.getAttributeAsBoolean("internal-only", false));
51
52         // Main (with no "type" attribute) error handler: new in Cocoon 2.1, must have a generator
53
ProcessingNode mainHandler = null;
54
55         // 404 & 500 error handlers as in Cocoon 2.0.x, have an implicit generator
56
ProcessingNode error404Handler = null;
57         ProcessingNode error500Handler = null;
58
59         Configuration[] childConfigs = config.getChildren();
60         List JavaDoc children = new ArrayList JavaDoc();
61         for (int i = 0; i < childConfigs.length; i++) {
62             Configuration childConfig = childConfigs[i];
63             if (isChild(childConfig)) {
64
65                 ProcessingNodeBuilder builder = this.treeBuilder.createNodeBuilder(childConfig);
66                 if (builder instanceof HandleErrorsNodeBuilder) {
67                     // Error handler : check type
68
HandleErrorsNode handler = (HandleErrorsNode)builder.buildNode(childConfig);
69                     int status = handler.getStatusCode();
70
71                     switch(status) {
72                         case -1: // main handler (needs generator)
73
if (mainHandler != null) {
74                                 throw new ConfigurationException("Duplicate <handle-errors> at " + handler.getLocation());
75                             } else if (error500Handler != null || error404Handler != null) {
76                                 throw new ConfigurationException("Cannot mix <handle-errors> with and without 'type' attribute at " +
77                                                                  handler.getLocation());
78                             } else {
79                                 mainHandler = handler;
80                             }
81                             break;
82
83                         case 404:
84                             if (error404Handler != null) {
85                                 throw new ConfigurationException("Duplicate <handle-errors type='404' at " + handler.getLocation());
86                             } else if(mainHandler != null) {
87                                 throw new ConfigurationException("Cannot mix <handle-errors> with and without 'type' attribute at " +
88                                                                  handler.getLocation());
89                             } else {
90                                 error404Handler = handler;
91                             }
92                             break;
93
94                         case 500:
95                             if (error500Handler != null) {
96                                 throw new ConfigurationException("Duplicate <handle-errors type='500' at " + handler.getLocation());
97                             } else if (mainHandler != null) {
98                                 throw new ConfigurationException("Cannot mix <handle-errors> with and without 'type' attribute at " +
99                                                                  handler.getLocation());
100                             } else {
101                                 error500Handler = handler;
102                             }
103                             break;
104
105                         default:
106                             throw new ConfigurationException("Unknown handle-errors type (" + type + ") at " + handler.getLocation());
107                     }
108                 } else {
109                     // Regular builder
110
children.add(builder.buildNode(childConfig));
111                 }
112             }
113         }
114
115         node.setChildren(toNodeArray(children));
116         node.set404Handler(error404Handler);
117         // Set either main or error500 handler as only one can exist
118
node.set500Handler(error500Handler == null ? mainHandler : error500Handler);
119
120         return node;
121     }
122 }
123
Popular Tags