1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.apache.avalon.excalibur.logger.LoggerManager; 28 import org.apache.avalon.framework.component.ComponentManager; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.configuration.DefaultConfiguration; 32 import org.apache.cocoon.components.CocoonComponentManager; 33 import org.apache.cocoon.components.treeprocessor.CategoryNode; 34 import org.apache.cocoon.components.treeprocessor.CategoryNodeBuilder; 35 import org.apache.cocoon.components.treeprocessor.DefaultTreeBuilder; 36 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 37 import org.apache.cocoon.generation.Generator; 38 import org.apache.cocoon.serialization.Serializer; 39 import org.apache.cocoon.sitemap.PatternException; 40 import org.apache.cocoon.sitemap.SitemapComponentSelector; 41 import org.apache.cocoon.util.StringUtils; 42 import org.apache.regexp.RE; 43 44 50 51 public class SitemapLanguage extends DefaultTreeBuilder { 52 53 private static final String COMMA_SPLIT_REGEXP = "[\\s]*,[\\s]*"; 55 private static final String EQUALS_SPLIT_REGEXP = "[\\s]*=[\\s]*"; 56 57 61 protected ComponentManager createComponentManager(Configuration tree) throws Exception { 62 63 Configuration config = tree.getChild("components", false); 66 67 if (config == null) { 68 if (getLogger().isDebugEnabled()) { 69 getLogger().debug("Sitemap has no components definition at " + tree.getLocation()); 70 } 71 config = new DefaultConfiguration("", ""); 72 } 73 74 final CocoonComponentManager manager = new CocoonComponentManager(this.parentManager); 75 76 manager.enableLogging(getLogger()); 77 78 final LoggerManager loggerManager = (LoggerManager) this.parentManager.lookup(LoggerManager.ROLE); 79 manager.setLoggerManager(loggerManager); 80 81 if (null != this.context ) { 82 manager.contextualize(this.context); 83 } 84 85 if (null != this.roleManager) { 86 manager.setRoleManager(this.roleManager); 87 } 88 89 manager.configure(config); 90 manager.initialize(); 91 92 return manager; 93 } 94 95 97 98 private Map labelViews = new HashMap (); 99 100 101 private CategoryNode viewsNode; 102 103 104 private boolean isBuildingView = false; 105 106 107 private boolean isBuildingErrorHandler = false; 108 109 112 public static final String FIRST_POS_LABEL = "!first!"; 113 114 117 public static final String LAST_POS_LABEL = "!last!"; 118 119 public void recycle() { 120 super.recycle(); 121 122 this.labelViews.clear(); 124 this.viewsNode = null; 125 this.isBuildingView = false; 126 this.isBuildingErrorHandler = false; 127 } 128 129 132 public void setBuildingView(boolean building) { 133 this.isBuildingView = building; 134 } 135 136 139 public boolean isBuildingView() { 140 return this.isBuildingView; 141 } 142 143 146 public void setBuildingErrorHandler(boolean building) { 147 this.isBuildingErrorHandler = building; 148 } 149 150 153 public boolean isBuildingErrorHandler() { 154 return this.isBuildingErrorHandler; 155 } 156 157 164 public void addViewForLabel(String label, String view) { 165 if (getLogger().isDebugEnabled()) { 166 getLogger().debug("views:addViewForLabel(" + label + ", " + view + ")"); 167 } 168 Set views = (Set )this.labelViews.get(label); 169 if (views == null) { 170 views = new HashSet (); 171 this.labelViews.put(label, views); 172 } 173 174 views.add(view); 175 } 176 177 186 public Collection getViewsForStatement(String role, String hint, Configuration statement) throws Exception { 187 188 String statementLabels = statement.getAttribute("label", null); 189 190 if (this.isBuildingView) { 191 if (statementLabels != null) { 193 String msg = "Cannot put a 'label' attribute inside view definition at " + statement.getLocation(); 194 throw new ConfigurationException(msg); 195 } 196 197 return null; 199 } 200 201 Set views = null; 203 204 Set labels = new HashSet (); 206 207 if (role != null && role.length() > 0) { 209 SitemapComponentSelector selector = null; 210 try { 211 selector = (SitemapComponentSelector)this.manager.lookup(role + "Selector"); 212 String [] compLabels = selector.getLabels(hint); 213 if (compLabels != null) { 214 for (int i = 0; i < compLabels.length; i++) { 215 labels.add(compLabels[i]); 216 } 217 } 218 } catch(Exception e) { 219 getLogger().warn("No selector for role " + role); 221 } finally { 222 this.manager.release( selector ); 223 } 224 } 225 226 if (statementLabels != null) { 228 labels.addAll(splitLabels(statementLabels)); 229 } 230 231 if (Generator.ROLE.equals(role)) { 233 labels.add("!first!"); 234 } else if (Serializer.ROLE.equals(role)) { 235 labels.add("!last!"); 236 } 237 238 views = new HashSet (); 240 241 Iterator labelIter = labels.iterator(); 243 while(labelIter.hasNext()) { 244 245 Collection coll = (Collection )this.labelViews.get(labelIter.next()); 247 if (coll != null) { 248 Iterator viewIter = coll.iterator(); 249 while(viewIter.hasNext()) { 250 String viewName = (String )viewIter.next(); 251 252 views.add(viewName); 253 } 254 } 255 } 256 257 if (views.size() == 0) { 259 views = null; 260 261 if (getLogger().isDebugEnabled()) { 262 getLogger().debug(statement.getName() + " has no views at " + statement.getLocation()); 263 } 264 } else { 265 if (getLogger().isDebugEnabled()) { 266 StringBuffer buf = new StringBuffer (statement.getName() + " will match views ["); 268 Iterator iter = views.iterator(); 269 while(iter.hasNext()) { 270 buf.append(iter.next()).append(" "); 271 } 272 buf.append("] at ").append(statement.getLocation()); 273 274 getLogger().debug(buf.toString()); 275 } 276 } 277 278 return views; 279 } 280 281 284 protected void linkNodes() throws Exception { 285 this.viewsNode = CategoryNodeBuilder.getCategoryNode(this, "views"); 287 288 super.linkNodes(); 289 } 290 291 298 public Map getViewNodes(Collection viewNames) throws Exception { 299 if (viewNames == null || viewNames.size() == 0) { 300 return null; 301 } 302 303 if (this.viewsNode == null) { 304 return null; 305 } 306 307 Map result = new HashMap (); 308 309 Iterator iter = viewNames.iterator(); 310 while(iter.hasNext()) { 311 String viewName = (String )iter.next(); 312 result.put(viewName, viewsNode.getNodeByName(viewName)); 313 } 314 315 return result; 316 } 317 318 327 public Map getHintsForStatement(String role, String hint, Configuration statement) throws Exception { 328 339 String statementHintParams = statement.getAttribute("pipeline-hints", null); 340 String componentHintParams = null; 341 String hintParams = null; 342 343 SitemapComponentSelector selector = null; 346 try { 347 selector = (SitemapComponentSelector)this.manager.lookup(role + "Selector"); 348 componentHintParams = selector.getPipelineHint(hint); 349 } catch (Exception ex) { 350 if (getLogger().isWarnEnabled()) { 351 getLogger().warn("pipeline-hints: Component Exception: could not " + 352 "check for component level hints " + ex); 353 } 354 } finally { 355 this.manager.release(selector); 356 } 357 358 if (componentHintParams != null) { 359 hintParams = componentHintParams; 360 361 if (statementHintParams != null) { 362 hintParams = hintParams + "," + statementHintParams; 363 } 364 } else { 365 hintParams = statementHintParams; 366 } 367 368 if (hintParams == null) { 371 return null; 372 } 373 374 Map params = new HashMap (); 375 376 RE commaSplit = new RE(COMMA_SPLIT_REGEXP); 377 RE equalsSplit = new RE(EQUALS_SPLIT_REGEXP); 378 379 String [] expressions = commaSplit.split(hintParams.trim()); 380 381 if (getLogger().isDebugEnabled()) { 382 getLogger().debug("pipeline-hints: (aggregate-hint) " + hintParams); 383 } 384 385 for (int i=0; i<expressions.length;i++) { 386 String [] nameValuePair = equalsSplit.split(expressions[i]); 387 388 try { 389 if (nameValuePair.length < 2) { 390 if (getLogger().isDebugEnabled()) { 391 getLogger().debug("pipeline-hints: (name) " + nameValuePair[0] 392 + "\npipeline-hints: (value) [implicit] true"); 393 } 394 395 params.put( VariableResolverFactory.getResolver(nameValuePair[0], this.manager), 396 VariableResolverFactory.getResolver("true", this.manager)); 397 } else { 398 if (getLogger().isDebugEnabled()) { 399 getLogger().debug("pipeline-hints: (name) " + nameValuePair[0] 400 + "\npipeline-hints: (value) " + nameValuePair[1]); 401 } 402 403 params.put( VariableResolverFactory.getResolver(nameValuePair[0], this.manager), 404 VariableResolverFactory.getResolver(nameValuePair[1], this.manager)); 405 } 406 } catch(PatternException pe) { 407 String msg = "Invalid pattern '" + hintParams + "' at " + statement.getLocation(); 408 getLogger().error(msg, pe); 409 throw new ConfigurationException(msg, pe); 410 } 411 } 412 413 return params; 414 } 415 416 421 private static final Collection splitLabels(String labels) { 422 if (labels == null) { 423 return Collections.EMPTY_SET; 424 } else { 425 return Arrays.asList(StringUtils.split(labels, ", \t\n\r")); 426 } 427 } 428 } 429 | Popular Tags |