1 16 package org.apache.cocoon.components.treeprocessor.sitemap; 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.avalon.framework.CascadingRuntimeException; 25 import org.apache.avalon.framework.component.ComponentException; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.configuration.DefaultConfiguration; 29 import org.apache.cocoon.acting.Action; 30 import org.apache.cocoon.components.ExtendedComponentSelector; 31 import org.apache.cocoon.components.ComponentLocator; 32 import org.apache.cocoon.components.pipeline.OutputComponentSelector; 33 import org.apache.cocoon.components.pipeline.ProcessingPipeline; 34 import org.apache.cocoon.generation.Generator; 35 import org.apache.cocoon.matching.Matcher; 36 import org.apache.cocoon.reading.Reader; 37 import org.apache.cocoon.selection.Selector; 38 import org.apache.cocoon.serialization.Serializer; 39 import org.apache.cocoon.sitemap.SitemapComponentSelector; 40 import org.apache.cocoon.transformation.Transformer; 41 42 49 50 public class ComponentsSelector extends ExtendedComponentSelector 51 implements OutputComponentSelector, SitemapComponentSelector { 52 53 public static final int UNKNOWN = -1; 54 public static final int GENERATOR = 0; 55 public static final int TRANSFORMER = 1; 56 public static final int SERIALIZER = 2; 57 public static final int READER = 3; 58 public static final int MATCHER = 4; 59 public static final int SELECTOR = 5; 60 public static final int ACTION = 6; 61 public static final int PIPELINE = 7; 62 63 public static final String [] SELECTOR_ROLES = { 64 Generator.ROLE + "Selector", 65 Transformer.ROLE + "Selector", 66 Serializer.ROLE + "Selector", 67 Reader.ROLE + "Selector", 68 Matcher.ROLE + "Selector", 69 Selector.ROLE + "Selector", 70 Action.ROLE + "Selector", 71 ProcessingPipeline.ROLE + "Selector" 72 }; 73 74 public static final String [] COMPONENT_NAMES = { 75 "generator", 76 "transformer", 77 "serializer", 78 "reader", 79 "matcher", 80 "selector", 81 "action", 82 "pipe" 83 }; 84 85 86 private int roleId; 87 88 89 private Map hintMimeTypes; 90 91 92 private Map hintLabels; 93 94 95 private Map pipelineHints; 96 97 98 private Set knownHints = new HashSet (); 99 100 101 private SitemapComponentSelector parentSitemapSelector; 102 103 106 public void setParentLocator(ComponentLocator locator) 107 throws ComponentException { 108 super.setParentLocator(locator); 109 110 if (super.parentSelector instanceof SitemapComponentSelector) { 111 this.parentSitemapSelector = (SitemapComponentSelector)super.parentSelector; 112 } 113 } 114 115 119 protected String getComponentInstanceName() { 120 return (this.roleId == UNKNOWN) ? null : COMPONENT_NAMES[this.roleId]; 121 } 122 123 127 protected String getClassAttributeName() { 128 return (this.roleId == UNKNOWN) ? "class" : "src"; 129 } 130 131 132 public void configure(Configuration config) throws ConfigurationException { 133 134 String role = getRoleName(config); 136 this.roleId = UNKNOWN; for (int i = 0; i < SELECTOR_ROLES.length; i++) { 138 if (SELECTOR_ROLES[i].equals(role)) { 139 this.roleId = i; 140 break; 141 } 142 } 143 144 if (getLogger().isDebugEnabled()) { 145 getLogger().debug("Setting up sitemap component selector for " + 146 role + " (role id = " + this.roleId + ")"); 147 } 148 149 if (this.roleId == SERIALIZER || this.roleId == READER) { 151 this.hintMimeTypes = new HashMap (); 152 } 153 154 this.hintLabels = new HashMap (); 155 this.pipelineHints = new HashMap (); 156 157 super.configure(config); 158 } 159 160 163 public void addComponent(Object hint, Class clazz, Configuration config) throws ComponentException { 164 165 super.addComponent(hint, clazz, config); 166 167 this.knownHints.add(hint); 169 170 if (this.roleId == SERIALIZER || this.roleId == READER) { 171 String mimeType = config.getAttribute("mime-type", null); 173 if (mimeType != null) { 174 this.hintMimeTypes.put(hint, mimeType); 175 } 176 } 177 178 String label = config.getAttribute("label", null); 179 if (label != null) { 180 StringTokenizer st = new StringTokenizer (label, " ,", false); 183 String [] labels = new String [st.countTokens()]; 184 for (int i = 0; i < labels.length; i++) { 185 labels[i] = st.nextToken(); 186 } 187 this.hintLabels.put(hint, labels); 188 } 189 190 String pipelineHint = config.getAttribute("hint", null); 191 this.pipelineHints.put(hint, pipelineHint); 192 } 193 194 198 public void initialize() { 199 200 try { 202 203 DefaultConfiguration config = null; 204 205 switch(this.roleId) { 208 case GENERATOR : 209 210 config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated"); 211 config.setAttribute("name", "<notifier>"); 212 ensureExists("<notifier>", 213 org.apache.cocoon.sitemap.NotifyingGenerator.class, config); 214 215 config = new DefaultConfiguration(COMPONENT_NAMES[GENERATOR], "autogenerated"); 216 config.setAttribute("name", "<aggregator>"); 217 ensureExists("<aggregator>", 218 org.apache.cocoon.sitemap.ContentAggregator.class, config); 219 break; 220 221 case TRANSFORMER : 222 config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated"); 223 config.setAttribute("name", "<translator>"); 224 ensureExists("<translator>", 225 org.apache.cocoon.sitemap.LinkTranslator.class, config); 226 227 config = new DefaultConfiguration(COMPONENT_NAMES[TRANSFORMER], "autogenerated"); 228 config.setAttribute("name", "<gatherer>"); 229 ensureExists("<gatherer>", 230 org.apache.cocoon.sitemap.LinkGatherer.class, config); 231 break; 232 } 233 234 super.initialize(); 235 236 this.knownHints = null; 238 239 } catch(Exception e) { 240 throw new CascadingRuntimeException("Cannot setup default components", e); 241 } 242 243 } 244 245 250 private void ensureExists(Object hint, Class clazz, Configuration config) throws ComponentException { 251 252 if (! this.knownHints.contains(hint)) { 253 this.addComponent(hint, clazz, config); 254 } 255 } 256 257 260 public String getMimeTypeForHint(Object hint) { 261 262 if (this.hintMimeTypes == null) { 263 return null; 265 266 } else { 267 if (this.hasDeclaredComponent(hint)) { 268 return (String )this.hintMimeTypes.get(hint); 269 270 } else if (this.parentSitemapSelector != null) { 271 return this.parentSitemapSelector.getMimeTypeForHint(hint); 272 273 } else { 274 return null; 275 } 276 } 277 } 278 279 public boolean hasLabel(Object hint, String label) { 280 String [] labels = this.getLabels(hint); 281 if (labels != null) { 282 for (int i = 0; i < labels.length; i++) { 283 if (labels[i].equals(label)) 284 return true; 285 } 286 } 287 return false; 288 } 289 290 public String [] getLabels(Object hint) { 291 if (this.hasDeclaredComponent(hint)) { 294 return (String [])this.hintLabels.get(hint); 295 296 } else if (this.parentSitemapSelector != null) { 297 return parentSitemapSelector.getLabels(hint); 298 299 } else { 300 return null; 301 } 302 } 303 304 public String getPipelineHint(Object hint) { 305 if (this.hasDeclaredComponent(hint)) { 308 return (String )this.pipelineHints.get(hint); 309 } else if (this.parentSitemapSelector != null) { 310 return this.parentSitemapSelector.getPipelineHint(hint); 311 } else { 312 return null; 313 } 314 } 315 316 public void dispose() { 317 super.dispose(); 318 this.parentSitemapSelector = null; 319 } 320 } 321 | Popular Tags |