You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by un...@apache.org on 2003/10/27 10:30:07 UTC
cvs commit: cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source SourceDescriptor.java SourceInspector.java
unico 2003/10/27 01:30:07
Modified: src/blocks/repository/java/org/apache/cocoon/components/source/impl
XPathSourceInspector.java
SourceDescriptorManager.java
src/blocks/repository/java/org/apache/cocoon/components/source
SourceDescriptor.java SourceInspector.java
Added: src/blocks/repository/java/org/apache/cocoon/components/source/impl
AbstractConfigurableSourceDescriptor.java
AbstractConfigurableSourceInspector.java
Log:
- remove method getExposedPropertyTypes again in favor of relying on inspectors to filter unhandled property types
- code improvements
- documentation improvements
- two abstract classes to help with property type configuration
Revision Changes Path
1.3 +14 -18 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/XPathSourceInspector.java
Index: XPathSourceInspector.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/XPathSourceInspector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- XPathSourceInspector.java 23 Oct 2003 16:57:57 -0000 1.2
+++ XPathSourceInspector.java 27 Oct 2003 09:30:07 -0000 1.3
@@ -49,10 +49,12 @@
*/
package org.apache.cocoon.components.source.impl;
-import org.apache.excalibur.xml.xpath.XPathProcessor;
+import java.io.IOException;
+
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.parameters.Parameters;
@@ -62,35 +64,32 @@
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.xml.dom.DOMParser;
+import org.apache.excalibur.xml.xpath.XPathProcessor;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-import java.io.IOException;
-
/**
* This source inspector inspects XML files with a xpath expression
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
+ * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
* @version CVS $Id$
*/
public class XPathSourceInspector extends AbstractLogEnabled implements
- SourceInspector, ThreadSafe, Parameterizable {
+ SourceInspector, Composable, Parameterizable, ThreadSafe {
/**
- * The default namespace uri of the property
- * exposed by this SourceInspector.
+ * The default namespace uri of the property exposed by this SourceInspector.
* <p>
- * The value is <code>http://apache.org/cocoon/inspector/xpath/1.0</code> .
+ * The value is <code>http://apache.org/cocoon/inspector/xpath/1.0</code>.
* </p>
*/
- public static final String DEFAULT_PROPERTY_NS =
- "http://apache.org/cocoon/inspector/xpath/1.0";
+ public static final String DEFAULT_PROPERTY_NS = "http://apache.org/cocoon/inspector/xpath/1.0";
/**
- * The default property name exposed by this
- * SourceInspector.
+ * The default property name exposed by this SourceInspector.
* <p>
* The value is <code>result</code> .
* </p>
@@ -107,21 +106,21 @@
public void compose(ComponentManager manager) {
this.manager = manager;
}
-
+
public void parameterize(Parameters params) {
this.propertynamespace = params.getParameter("namespace", DEFAULT_PROPERTY_NS);
this.propertyname = params.getParameter("name", DEFAULT_PROPERTY_NAME);
this.extension = params.getParameter("extension", ".xml");
this.xpath = params.getParameter("xpath", "/*");
}
-
+
public SourceProperty getSourceProperty(Source source, String namespace, String name)
throws SourceException {
if ((namespace.equals(propertynamespace)) && (name.equals(propertyname)) &&
(source.getURI().endsWith(extension))) {
- DOMParser parser = null;
+ DOMParser parser = null;
Document doc = null;
try {
parser = (DOMParser)manager.lookup(DOMParser.ROLE);
@@ -170,9 +169,6 @@
return null;
}
- public String[] getExposedSourcePropertyTypes() {
- return new String[] {propertynamespace + "#" + propertyname};
- }
}
1.3 +50 -73 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/SourceDescriptorManager.java
Index: SourceDescriptorManager.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/SourceDescriptorManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SourceDescriptorManager.java 24 Oct 2003 08:47:33 -0000 1.2
+++ SourceDescriptorManager.java 27 Oct 2003 09:30:07 -0000 1.3
@@ -50,10 +50,8 @@
package org.apache.cocoon.components.source.impl;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.Map;
import java.util.Set;
import org.apache.avalon.framework.activity.Disposable;
@@ -70,36 +68,25 @@
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
-
import org.apache.cocoon.components.source.SourceDescriptor;
import org.apache.cocoon.components.source.SourceInspector;
import org.apache.cocoon.components.source.helpers.SourceProperty;
-
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
/**
- * This source descriptor acts as container for a list registered
- * source inspectors.
- *
- * TODO the manager currently assumes a ThreadSafe lifestyle for
- * the contained inspectors. Consider supporting other lifstyles as well.
+ * This source descriptor acts as container for a set of source inspectors.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @author <a href="mailto:unico@apache.org">Unico Hommes</a>
* @version CVS $Id$
*/
public final class SourceDescriptorManager extends AbstractLogEnabled
-implements SourceDescriptor, ThreadSafe, Contextualizable, Composable,
-Configurable, Initializable, Disposable {
-
- public static final String ROLE = SourceDescriptorManager.class.getName();
+implements SourceDescriptor, Contextualizable, Composable,
+Configurable, Initializable, Disposable, ThreadSafe {
// the registered inspectors
- private Map m_inspectors;
-
- // cached list of all supported property types
- private String[] m_types;
+ private Set m_inspectors;
private Context m_context;
private ComponentManager m_manager;
@@ -124,8 +111,7 @@
}
public void initialize() throws Exception {
-
- m_inspectors = new HashMap();
+ m_inspectors = new HashSet();
final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
final Configuration[] children = m_configuration.getChildren();
@@ -148,18 +134,12 @@
ContainerUtil.enableLogging(inspector,getLogger());
ContainerUtil.contextualize(inspector,m_context);
ContainerUtil.compose(inspector,m_manager);
+ ContainerUtil.configure(inspector,children[i]);
ContainerUtil.parameterize(inspector,
Parameters.fromConfiguration(children[i]));
ContainerUtil.initialize(inspector);
- String[] types = inspector.getExposedSourcePropertyTypes();
- for (int j = 0; j < types.length; j++) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Registering " + inspector
- + " to handle property " + types[j]);
- }
- m_inspectors.put(types[j],inspector);
- }
+ m_inspectors.add(inspector);
}
// done with these
m_configuration = null;
@@ -168,81 +148,78 @@
}
public void dispose() {
- Iterator iter = m_inspectors.values().iterator();
+ Iterator iter = m_inspectors.iterator();
while(iter.hasNext()) {
ContainerUtil.dispose(iter.next());
}
m_inspectors = null;
- m_types = null;
}
+
+ // ---------------------------------------------------- SourceDescriptor implementation
+
+ /**
+ * Loops over the registered inspectors until it finds the property.
+ */
public SourceProperty getSourceProperty(Source source, String namespace, String name)
throws SourceException {
- String propname = namespace + "#" + name;
- SourceInspector inspector = (SourceInspector) m_inspectors.get(propname);
- if (inspector != null) {
- return inspector.getSourceProperty(source,namespace,name);
- }
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("No inspector registered for property " + propname);
+ final Iterator inspectors = m_inspectors.iterator();
+ while (inspectors.hasNext()) {
+ SourceInspector inspector = (SourceInspector) inspectors.next();
+ SourceProperty property = inspector.getSourceProperty(source,namespace,name);
+ if (property != null) {
+ return property;
+ }
}
return null;
}
-
+
+ /**
+ * Aggregate all properties of all registered inspectors.
+ */
public SourceProperty[] getSourceProperties(Source source) throws SourceException {
-
- Set result = new HashSet(getExposedSourcePropertyTypes().length);
-
+ final Set result = new HashSet();
SourceInspector inspector;
SourceProperty[] properties;
-
- Iterator inspectors = m_inspectors.values().iterator();
- while(inspectors.hasNext()) {
+ final Iterator inspectors = m_inspectors.iterator();
+ while (inspectors.hasNext()) {
inspector = (SourceInspector) inspectors.next();
properties = inspector.getSourceProperties(source);
if (properties != null) {
result.addAll(Arrays.asList(properties));
}
}
-
return (SourceProperty[]) result.toArray(new SourceProperty[result.size()]);
}
- public String[] getExposedSourcePropertyTypes() {
- if (m_types == null) {
- Set types = m_inspectors.keySet();
- m_types = (String[]) types.toArray(new String[types.size()]);
- }
- return m_types;
- }
-
+ /**
+ * Loops over the registered descriptors and delegates the call.
+ */
public void removeSourceProperty(Source source, String ns, String name) throws SourceException {
- String prop = ns + "#" + name;
- SourceInspector inspector = (SourceInspector) m_inspectors.get(prop);
- if (inspector == null) {
- throw new SourceException("No descriptor registered for property " + prop);
- }
- if (!(inspector instanceof SourceDescriptor)) {
- throw new SourceException("Cannot modify a read-only property.");
+ SourceInspector inspector;
+ final Iterator inspectors = m_inspectors.iterator();
+ while (inspectors.hasNext()) {
+ inspector = (SourceInspector) inspectors.next();
+ if (inspector instanceof SourceDescriptor) {
+ ((SourceDescriptor) inspector).removeSourceProperty(source,ns,name);
+ }
}
- ((SourceDescriptor) inspector).removeSourceProperty(source,ns,name);
}
-
+
+ /**
+ * Loops over the registered descriptors and calls delegates the call.
+ */
public void setSourceProperty(Source source, SourceProperty property) throws SourceException {
-
- String prop = property.getNamespace() + "#" + property.getName();
- SourceInspector inspector = (SourceInspector) m_inspectors.get(prop);
- if (inspector == null) {
- throw new SourceException("No descriptor registered for property " + prop);
- }
- if (!(inspector instanceof SourceDescriptor)) {
- throw new SourceException("Cannot modify a read-only property.");
+ SourceInspector inspector;
+ final Iterator inspectors = m_inspectors.iterator();
+ while (inspectors.hasNext()) {
+ inspector = (SourceInspector) inspectors.next();
+ if (inspector instanceof SourceDescriptor) {
+ ((SourceDescriptor) inspector).setSourceProperty(source,property);
+ }
}
- ((SourceDescriptor) inspector).setSourceProperty(source,property);
}
-
-
}
1.1 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/AbstractConfigurableSourceDescriptor.java
Index: AbstractConfigurableSourceDescriptor.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.source.impl;
import org.apache.cocoon.components.source.SourceDescriptor;
import org.apache.cocoon.components.source.helpers.SourceProperty;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
/**
* Abstract base class for configurable SourceInspectors.
*
* @author <a href="mailto:unico@apache.org">Unico Hommes</a>
*/
public abstract class AbstractConfigurableSourceDescriptor
extends AbstractConfigurableSourceInspector implements SourceDescriptor {
public final void removeSourceProperty(Source source, String namespace, String name)
throws SourceException {
if (handlesProperty(namespace,name)) {
doRemoveSourceProperty(source,namespace,name);
}
}
public final void setSourceProperty(Source source, SourceProperty property)
throws SourceException {
if (handlesProperty(property.getNamespace(),property.getName())) {
doSetSourceProperty(source,property);
}
}
protected abstract void doRemoveSourceProperty(Source source, String namespace,String name)
throws SourceException;
protected abstract void doSetSourceProperty(Source source, SourceProperty property)
throws SourceException;
}
1.1 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/AbstractConfigurableSourceInspector.java
Index: AbstractConfigurableSourceInspector.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.source.impl;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.cocoon.components.source.SourceInspector;
import org.apache.cocoon.components.source.helpers.SourceProperty;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
/**
* Abstract base class for configurable SourceInspectors.
*
* @author <a href="mailto:unico@apache.org">Unico Hommes</a>
*/
public abstract class AbstractConfigurableSourceInspector extends AbstractLogEnabled
implements SourceInspector, Configurable {
// the set of properties this inspector is configured to handle
private Set m_properties;
public AbstractConfigurableSourceInspector() {
}
/**
* Configure this source inspector to handle properties of required types.
*/
public void configure(Configuration configuration) throws ConfigurationException {
final Configuration[] properties = configuration.getChildren("property");
m_properties = new HashSet(properties.length);
for (int i = 0; i < properties.length; i++) {
String namespace = properties[i].getAttribute("namespace");
String name = properties[i].getAttribute("name");
if (namespace.indexOf('#') != -1 || name.indexOf('#') != -1) {
final String message = "Illegal character '#' in definition at "
+ properties[i].getLocation();
throw new ConfigurationException(message);
}
m_properties.add(namespace + "#" + name);
}
}
public SourceProperty[] getSourceProperties(Source source) throws SourceException {
final Set result = new HashSet();
final Iterator properties = m_properties.iterator();
while (properties.hasNext()) {
String property = (String) properties.next();
int index = property.indexOf('#');
String namespace = property.substring(0,index);
String name = property.substring(index+1);
result.add(getSourceProperty(source,namespace,name));
}
return (SourceProperty[]) result.toArray(new SourceProperty[result.size()]);
}
public final SourceProperty getSourceProperty(Source source, String namespace, String name)
throws SourceException {
if (handlesProperty(namespace,name)) {
return doGetSourceProperty(source,namespace,name);
}
return null;
}
protected final boolean handlesProperty(String namespace, String name) {
return m_properties.contains(namespace + "#" + name);
}
protected abstract SourceProperty doGetSourceProperty(Source source, String ns, String name)
throws SourceException;
}
1.2 +3 -1 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/SourceDescriptor.java
Index: SourceDescriptor.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/SourceDescriptor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SourceDescriptor.java 23 Oct 2003 16:55:24 -0000 1.1
+++ SourceDescriptor.java 27 Oct 2003 09:30:07 -0000 1.2
@@ -56,10 +56,12 @@
/**
* A source descriptor handles modifiable source properties.
*
- * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
+ * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
*/
public interface SourceDescriptor extends SourceInspector {
+ public static final String ROLE = SourceDescriptor.class.getName();
+
/**
* Set a property on a Source.
*
1.3 +2 -12 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/SourceInspector.java
Index: SourceInspector.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/SourceInspector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SourceInspector.java 23 Oct 2003 16:54:31 -0000 1.2
+++ SourceInspector.java 27 Oct 2003 09:30:07 -0000 1.3
@@ -58,6 +58,7 @@
* A source inspector exposes source properties.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
+ * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
* @version CVS $Id$
*/
public interface SourceInspector extends Component {
@@ -88,16 +89,5 @@
*/
public SourceProperty[] getSourceProperties(Source source) throws SourceException;
- /**
- * Gets the list of all SourceProperties that are handled by this SourceInspector.
- * The Strings it returns follow the following format:
- * <p>
- * <code>
- * SourceProperty::getNamespace() + "#" + SourceProperty.getName()
- * </code>
- * </p>
- * @return the list of SourceProperties supported by this SourceInspector.
- */
- public String[] getExposedSourcePropertyTypes();
}