KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.parameters.Parameters;
19
20 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
21 import org.apache.cocoon.components.treeprocessor.AbstractProcessingNode;
22 import org.apache.cocoon.components.treeprocessor.InvokeContext;
23 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
24 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
25 import org.apache.cocoon.environment.Environment;
26 import org.apache.cocoon.sitemap.ContentAggregator;
27
28 import java.util.Map JavaDoc;
29
30 /**
31  * Aggregate sitemap node.
32  *
33  * <h3>View handling in aggregation</h3>
34  * <ul>
35  * <li>map:aggregate can have a label, but doesn't match view from-position="first" like generators
36  * </li>
37  * <li>each map:part can have a label
38  * </li>
39  * <li>if at least one of the parts has a label matching the current view, only parts matching
40  * this view are added. Otherwise, all parts are added.
41  * </li>
42  * </ul>
43  * For more info on aggregation and views, see the mail archive
44  * <a HREF="http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=100525751417953">here</a> or
45  * <a HREF="http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=100517130418424">here</a>.
46  *
47  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
48  * @version $Id: AggregateNode.java 157144 2005-03-11 20:13:26Z vgritsenko $
49  */

50 public class AggregateNode extends AbstractProcessingNode {
51
52     private VariableResolver element;
53     private VariableResolver nsURI;
54     private VariableResolver nsPrefix;
55
56     /** All parts */
57     private Part[] allParts;
58
59     /** Pre-filtered Part[] for views that have a matching label in any of the parts */
60     private Map JavaDoc viewParts;
61
62     /** View nodes to jump to */
63     private Map JavaDoc viewNodes;
64
65     public AggregateNode(VariableResolver element, VariableResolver nsURI, VariableResolver nsPrefix) {
66         this.element = element;
67         this.nsURI = nsURI;
68         this.nsPrefix = nsPrefix;
69     }
70
71     public void setParts(Part[] allParts, Map JavaDoc viewParts) {
72         this.allParts = allParts;
73         this.viewParts = viewParts;
74     }
75
76     public void setViewNodes(Map JavaDoc viewNodes) {
77         this.viewNodes = viewNodes;
78     }
79
80     public boolean invoke(Environment env, InvokeContext context)
81     throws Exception JavaDoc {
82         final boolean infoEnabled = getLogger().isInfoEnabled();
83
84         Map JavaDoc objectModel = env.getObjectModel();
85
86         // Setup aggregator
87
ProcessingPipeline processingPipeline = context.getProcessingPipeline();
88         processingPipeline.setGenerator("<aggregator>", null, Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
89
90         ContentAggregator aggregator = (ContentAggregator) processingPipeline.getGenerator();
91         aggregator.setRootElement(this.element.resolve(context, objectModel),
92                                   this.nsURI.resolve(context, objectModel),
93                                   this.nsPrefix.resolve(context, objectModel));
94
95         // Get actual parts, potentially filtered by the view
96
Part[] actualParts;
97
98         String JavaDoc cocoonView = env.getView();
99         if (cocoonView == null) {
100             // Keep all parts
101
actualParts = this.allParts;
102
103         } else {
104             // Are there some parts that match this view ?
105
actualParts = (Part[])this.viewParts.get(cocoonView);
106
107             // If not, keep all parts
108
if (actualParts == null) {
109                 actualParts = this.allParts;
110             }
111         }
112
113         // Add parts
114
for (int i = 0; i < actualParts.length; i++) {
115             Part part = actualParts[i];
116             if (part != null) {
117                 aggregator.addPart(
118                     part.source.resolve(context, objectModel),
119                     part.element.resolve(context, objectModel),
120                     part.nsURI.resolve(context, objectModel),
121                     part.stripRoot.resolve(context, objectModel),
122                     part.nsPrefix.resolve(context, objectModel)
123                 );
124             }
125         }
126
127         // Bug #7196 : Some parts matched the view: jump to that view
128
if (actualParts != this.allParts) {
129             ProcessingNode viewNode = (ProcessingNode)this.viewNodes.get(cocoonView);
130             if (viewNode != null) {
131                 if (infoEnabled) {
132                     getLogger().info("Jumping to view '" + cocoonView + "' from aggregate part at " + this.getLocation());
133                 }
134                 return viewNode.invoke(env, context);
135             }
136         }
137
138         // Check aggregate-level view
139
if (cocoonView != null && this.viewNodes != null) {
140             ProcessingNode viewNode = (ProcessingNode)this.viewNodes.get(cocoonView);
141             if (viewNode != null) {
142                 if (infoEnabled) {
143                     getLogger().info("Jumping to view '" + cocoonView + "' from aggregate at " + this.getLocation());
144                 }
145                 return viewNode.invoke(env, context);
146             }
147         }
148
149         // Return false to continue sitemap invocation
150
return false;
151     }
152
153     public static class Part {
154         protected VariableResolver source;
155         protected VariableResolver element;
156         protected VariableResolver nsURI;
157         protected VariableResolver nsPrefix;
158         protected VariableResolver stripRoot;
159
160         public Part(VariableResolver source,
161                     VariableResolver element,
162                     VariableResolver nsURI,
163                     VariableResolver nsPrefix,
164                     VariableResolver stripRoot) {
165             this.source = source;
166             this.element = element;
167             this.nsURI = nsURI;
168             this.nsPrefix = nsPrefix;
169             this.stripRoot = stripRoot;
170         }
171     }
172 }
173
Popular Tags