KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNodeBuilder;
29 import org.apache.cocoon.components.treeprocessor.LinkedProcessingNodeBuilder;
30 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
31 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
32
33 /**
34  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
35  * @version $Id: AggregateNodeBuilder.java 157144 2005-03-11 20:13:26Z vgritsenko $
36  */

37 public class AggregateNodeBuilder extends AbstractProcessingNodeBuilder
38                                   implements LinkedProcessingNodeBuilder {
39
40     /** The views for the aggregate element */
41     private Collection JavaDoc views;
42
43     /** The built node */
44     private AggregateNode node;
45
46     public ProcessingNode buildNode(Configuration config) throws Exception JavaDoc {
47
48         // Get root node data
49
this.node = new AggregateNode(
50             VariableResolverFactory.getResolver(config.getAttribute("element"), this.manager),
51             VariableResolverFactory.getResolver(config.getAttribute("ns", ""), this.manager),
52             VariableResolverFactory.getResolver(config.getAttribute("prefix", ""), this.manager)
53         );
54         this.treeBuilder.setupNode(this.node, config);
55
56         this.views = ((SitemapLanguage)this.treeBuilder).getViewsForStatement("", "", config);
57
58         // Bug #7196 : ensure this.views is never null (see continuation of fix below)
59
if (this.views == null) {
60             this.views = new HashSet JavaDoc();
61         }
62
63         // The sitemap builder
64
SitemapLanguage sitemap = (SitemapLanguage)this.treeBuilder;
65
66         // All parts of the aggregate
67
List JavaDoc allParts = new ArrayList JavaDoc();
68
69         // For each view that a part matches, the list of all parts that match it
70
Map JavaDoc viewParts = new HashMap JavaDoc();
71
72         Configuration[] childConfigs = config.getChildren();
73         for (int i = 0; i < childConfigs.length; i++) {
74             Configuration childConfig = childConfigs[i];
75
76             if (!"part".equals(childConfig.getName())) {
77                 String JavaDoc msg = "Unknown element '" + childConfig.getName() + " in aggregate ' at " +
78                     childConfig.getLocation();
79                 throw new ConfigurationException(msg);
80             }
81
82             checkNamespace(childConfig);
83
84             AggregateNode.Part currentPart = new AggregateNode.Part(
85                 VariableResolverFactory.getResolver(childConfig.getAttribute("src"), this.manager),
86                 VariableResolverFactory.getResolver(childConfig.getAttribute("element", ""), this.manager),
87                 VariableResolverFactory.getResolver(childConfig.getAttribute("ns", ""), this.manager),
88                 VariableResolverFactory.getResolver(childConfig.getAttribute("prefix", ""), this.manager),
89                 VariableResolverFactory.getResolver(childConfig.getAttribute("strip-root", "false"), this.manager)
90             );
91
92             allParts.add(currentPart);
93
94             // Get the views for this part
95
Collection JavaDoc viewsForPart = sitemap.getViewsForStatement("", "", childConfig);
96
97             // Associate this part to all the views it belongs to
98
if (viewsForPart != null) {
99
100                 // Bug #7196 : add part view to aggregate views
101
this.views.addAll(viewsForPart);
102
103                 Iterator JavaDoc iter = viewsForPart.iterator();
104                 while(iter.hasNext()) {
105                     String JavaDoc currentView = (String JavaDoc)iter.next();
106
107                     // Get collection of parts for current view
108
Collection JavaDoc currentViewParts = (Collection JavaDoc)viewParts.get(currentView);
109                     if (currentViewParts == null) {
110                         // None for now : create the collection
111
currentViewParts = new ArrayList JavaDoc();
112                         viewParts.put(currentView, currentViewParts);
113                     }
114
115                     // Add the current part to the parts list of the view
116
currentViewParts.add(currentPart);
117                 }
118             }
119         }
120
121         if (allParts.size() == 0) {
122             String JavaDoc msg = "There must be at least one part in map:aggregate at " + config.getLocation();
123             throw new ConfigurationException(msg);
124         }
125
126         // Now convert all Collections to Array for faster traversal
127
AggregateNode.Part[] allPartsArray = (AggregateNode.Part[])allParts.toArray(
128             new AggregateNode.Part[allParts.size()]);
129
130         Iterator JavaDoc iter = viewParts.entrySet().iterator();
131         while(iter.hasNext()) {
132             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
133
134             // Get collection of parts for this entry
135
Collection JavaDoc coll = (Collection JavaDoc)entry.getValue();
136
137             // Convert to array and replace the entry value
138
entry.setValue(
139                 coll.toArray(new AggregateNode.Part[coll.size()])
140             );
141         }
142
143         node.setParts(allPartsArray, viewParts);
144
145         return node;
146
147     }
148
149     public void linkNode() throws Exception JavaDoc {
150
151         // Give the AggregateNode a Node for each view
152
SitemapLanguage sitemap = (SitemapLanguage)this.treeBuilder;
153
154         this.node.setViewNodes(sitemap.getViewNodes(this.views));
155     }
156 }
157
Popular Tags