1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 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 37 public class AggregateNodeBuilder extends AbstractProcessingNodeBuilder 38 implements LinkedProcessingNodeBuilder { 39 40 41 private Collection views; 42 43 44 private AggregateNode node; 45 46 public ProcessingNode buildNode(Configuration config) throws Exception { 47 48 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 if (this.views == null) { 60 this.views = new HashSet (); 61 } 62 63 SitemapLanguage sitemap = (SitemapLanguage)this.treeBuilder; 65 66 List allParts = new ArrayList (); 68 69 Map viewParts = new HashMap (); 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 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 Collection viewsForPart = sitemap.getViewsForStatement("", "", childConfig); 96 97 if (viewsForPart != null) { 99 100 this.views.addAll(viewsForPart); 102 103 Iterator iter = viewsForPart.iterator(); 104 while(iter.hasNext()) { 105 String currentView = (String )iter.next(); 106 107 Collection currentViewParts = (Collection )viewParts.get(currentView); 109 if (currentViewParts == null) { 110 currentViewParts = new ArrayList (); 112 viewParts.put(currentView, currentViewParts); 113 } 114 115 currentViewParts.add(currentPart); 117 } 118 } 119 } 120 121 if (allParts.size() == 0) { 122 String msg = "There must be at least one part in map:aggregate at " + config.getLocation(); 123 throw new ConfigurationException(msg); 124 } 125 126 AggregateNode.Part[] allPartsArray = (AggregateNode.Part[])allParts.toArray( 128 new AggregateNode.Part[allParts.size()]); 129 130 Iterator iter = viewParts.entrySet().iterator(); 131 while(iter.hasNext()) { 132 Map.Entry entry = (Map.Entry )iter.next(); 133 134 Collection coll = (Collection )entry.getValue(); 136 137 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 { 150 151 SitemapLanguage sitemap = (SitemapLanguage)this.treeBuilder; 153 154 this.node.setViewNodes(sitemap.getViewNodes(this.views)); 155 } 156 } 157 | Popular Tags |