1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Set ; 21 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.cocoon.components.source.SourceInspector; 27 import org.apache.cocoon.components.source.helpers.SourceProperty; 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceException; 30 31 42 public abstract class AbstractConfigurableSourceInspector extends AbstractLogEnabled 43 implements SourceInspector, Configurable { 44 45 private Set m_properties; 47 48 49 51 public AbstractConfigurableSourceInspector() { 52 } 53 54 61 public void configure(Configuration configuration) throws ConfigurationException { 62 final Configuration[] properties = configuration.getChildren("property"); 63 m_properties = new HashSet (properties.length); 64 for (int i = 0; i < properties.length; i++) { 65 String namespace = properties[i].getAttribute("namespace"); 66 String name = properties[i].getAttribute("name"); 67 if (namespace.indexOf('#') != -1 || name.indexOf('#') != -1) { 68 final String message = "Illegal character '#' in definition at " 69 + properties[i].getLocation(); 70 throw new ConfigurationException(message); 71 } 72 String property = namespace + "#" + name; 73 if (getLogger().isDebugEnabled()) { 74 getLogger().debug("Handling '" + property + "'"); 75 } 76 m_properties.add(property); 77 } 78 } 79 80 82 88 public SourceProperty[] getSourceProperties(Source source) throws SourceException { 89 final Set result = new HashSet (); 90 final Iterator properties = m_properties.iterator(); 91 while (properties.hasNext()) { 92 String property = (String ) properties.next(); 93 int index = property.indexOf('#'); 94 String namespace = property.substring(0,index); 95 String name = property.substring(index+1); 96 SourceProperty sp = doGetSourceProperty(source,namespace,name); 97 if (sp != null) { 98 result.add(sp); 99 } 100 } 101 return (SourceProperty[]) result.toArray(new SourceProperty[result.size()]); 102 } 103 104 108 public final SourceProperty getSourceProperty(Source source, String namespace, String name) 109 throws SourceException { 110 111 if (handlesProperty(namespace,name)) { 112 if (getLogger().isDebugEnabled()) { 113 getLogger().debug("Getting property " + namespace + "#" 114 + name + " for source " + source.getURI()); 115 } 116 return doGetSourceProperty(source,namespace,name); 117 } 118 return null; 119 } 120 121 123 126 protected abstract SourceProperty doGetSourceProperty(Source source, String ns, String name) 127 throws SourceException; 128 129 130 132 136 public final boolean handlesProperty(String namespace, String name) { 137 String propname; 138 if (namespace == null) { 139 propname = "#" + name; 140 } 141 else { 142 propname = namespace + "#" + name; 143 } 144 return m_properties.contains(propname); 145 } 146 147 150 protected final Set getPropertyTypes() { 151 return m_properties; 152 } 153 154 } 155 | Popular Tags |