KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > AbstractConfigurableSourceInspector


1 /*
2  * Copyright 1999-2004 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.source.impl;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
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 /**
32  * Abstract base class for SourceInspectors that want to
33  * configure the set of properties they handle beforehand.
34  *
35  * <p>
36  * Knowing which properties an inspector handles beforehand
37  * greatly improves property management performance.
38  * </p>
39  *
40  * @author <a HREF="mailto:unico@apache.org">Unico Hommes</a>
41  */

42 public abstract class AbstractConfigurableSourceInspector extends AbstractLogEnabled
43 implements SourceInspector, Configurable {
44
45     // the set of properties this inspector is configured to handle
46
private Set JavaDoc m_properties;
47
48
49     // ---------------------------------------------------- lifecycle
50

51     public AbstractConfigurableSourceInspector() {
52     }
53
54     /**
55      * Configure this source inspector to handle properties of required types.
56      * <p>
57      * Configuration is in the form of a set of property elements as follows:<br>
58      * <code>&lt;property name="owner" namespace="meta"&gt;</code>
59      * </p>
60      */

61     public void configure(Configuration configuration) throws ConfigurationException {
62         final Configuration[] properties = configuration.getChildren("property");
63         m_properties = new HashSet JavaDoc(properties.length);
64         for (int i = 0; i < properties.length; i++) {
65             String JavaDoc namespace = properties[i].getAttribute("namespace");
66             String JavaDoc name = properties[i].getAttribute("name");
67             if (namespace.indexOf('#') != -1 || name.indexOf('#') != -1) {
68                 final String JavaDoc message = "Illegal character '#' in definition at "
69                     + properties[i].getLocation();
70                 throw new ConfigurationException(message);
71             }
72             String JavaDoc property = namespace + "#" + name;
73             if (getLogger().isDebugEnabled()) {
74                 getLogger().debug("Handling '" + property + "'");
75             }
76             m_properties.add(property);
77         }
78     }
79
80     // ---------------------------------------------------- SourceInspector methods
81

82     /**
83      * Iterates over the configured set of properties to handle,
84      * for each property calls <code>doGetSourceProperty()</code>,
85      * and returns the list of properties thus obtained. Subclasses
86      * may want to overide this behavior to improve performance.
87      */

88     public SourceProperty[] getSourceProperties(Source source) throws SourceException {
89         final Set JavaDoc result = new HashSet JavaDoc();
90         final Iterator JavaDoc properties = m_properties.iterator();
91         while (properties.hasNext()) {
92             String JavaDoc property = (String JavaDoc) properties.next();
93             int index = property.indexOf('#');
94             String JavaDoc namespace = property.substring(0,index);
95             String JavaDoc 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     /**
105      * Checks if this inspector is configured to handle the requested property
106      * and if so forwards the call to <code>doGetSourceProperty</code>.
107      */

108     public final SourceProperty getSourceProperty(Source source, String JavaDoc namespace, String JavaDoc 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     // ---------------------------------------------------- abstract methods
122

123     /**
124      * Do the actual work of getting the requested SourceProperty for the given Source.
125      */

126     protected abstract SourceProperty doGetSourceProperty(Source source, String JavaDoc ns, String JavaDoc name)
127         throws SourceException;
128
129
130     // ---------------------------------------------------- utility methods
131

132     /**
133      * Check if this inspector is configured to handle properties of
134      * the given type.
135      */

136     public final boolean handlesProperty(String JavaDoc namespace, String JavaDoc name) {
137         String JavaDoc 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     /**
148      * Provide subclasses access to the set of configured properties.
149      */

150     protected final Set JavaDoc getPropertyTypes() {
151         return m_properties;
152     }
153
154 }
155
Popular Tags