gcasper 2003/08/22 05:23:41
Modified: src/blocks/webdav/java/org/apache/cocoon/components/source/impl WebDAVSource.java WebDAVSourceFactory.java Log: Implemented InspectableSource. I know, the design of the InspectableSource interface (and the SourceProperty class in particular) is debatable. I just want to first come up with an easy tryable sample before discussing it. Revision Changes Path 1.5 +178 -3 cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/components/source/impl/WebDAVSource.java Index: WebDAVSource.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/components/source/impl/WebDAVSource.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- WebDAVSource.java 27 Jul 2003 20:53:39 -0000 1.4 +++ WebDAVSource.java 22 Aug 2003 12:23:41 -0000 1.5 @@ -56,9 +56,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.StringReader; import java.util.Collection; import java.util.Iterator; import java.util.Vector; +import java.util.Enumeration; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; @@ -68,18 +70,31 @@ import org.apache.cocoon.components.source.RestrictableSource; import org.apache.cocoon.components.source.helpers.SourceCredential; import org.apache.cocoon.components.source.helpers.SourcePermission; +import org.apache.cocoon.components.source.helpers.SourceProperty; +import org.apache.cocoon.components.source.InspectableSource; import org.apache.commons.httpclient.HttpException; +import org.apache.avalon.framework.component.Composable; +import org.apache.avalon.framework.component.Component; +import org.apache.avalon.framework.component.ComponentManager; +import org.apache.avalon.framework.component.ComponentException; import org.apache.excalibur.source.ModifiableTraversableSource; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; import org.apache.excalibur.source.SourceValidity; import org.apache.excalibur.source.impl.validity.TimeStampValidity; +import org.apache.excalibur.xml.dom.DOMParser; import org.apache.util.HttpURL; import org.apache.webdav.lib.WebdavResource; import org.apache.webdav.lib.methods.DepthSupport; +import org.apache.webdav.lib.Property; +import org.apache.webdav.lib.PropertyName; +import org.apache.webdav.lib.ResponseEntity; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.InputSource; + +import org.w3c.dom.Document; /** * A source implementation to get access to WebDAV repositories. Use it @@ -90,8 +105,8 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Daniele Madama</a> * @version $Id$ */ -public class WebDAVSource - implements Source, RestrictableSource, ModifiableTraversableSource { +public class WebDAVSource implements Composable, Source, + RestrictableSource, ModifiableTraversableSource, InspectableSource { private final String NAMESPACE = "http://apache.org/cocoon/webdav/1.0"; @@ -102,6 +117,8 @@ private final String COLLECTION_NAME = "collection"; + private ComponentManager manager = null; + private String systemId; private String location; @@ -193,6 +210,18 @@ } /** + * Pass the ComponentManager to the composer. The Composable implementation + * should use the specified ComponentManager to acquire the components it needs for execution + * + * @param manager The ComponentManager which this Composable uses + * + * @throws ComponentException + */ + public void compose(ComponentManager manager) throws ComponentException { + this.manager = manager; + } + + /** * Get the scheme for this Source (webdav://). */ @@ -670,5 +699,151 @@ } } + /** + * Returns a enumeration of the properties + * + * @return Enumeration of SourceProperty + * + * @throws SourceException If an exception occurs. + */ + public SourceProperty[] getSourceProperties() throws SourceException { + + Vector sourceproperties = new Vector(); + DOMParser parser = null; + String xml = ""; + Enumeration props= null; + org.apache.webdav.lib.Property prop = null; + String propValue = null; + + try { + parser = (DOMParser)this.manager.lookup(DOMParser.ROLE); + Enumeration responses = this.resource.propfindMethod(0); + while (responses.hasMoreElements()) { + + ResponseEntity response = (ResponseEntity)responses.nextElement(); + props = response.getProperties(); + final String quote = "\""; + while (props.hasMoreElements()) { + + prop = (Property) props.nextElement(); + String localName = prop.getLocalName(); + String namespaceURI = prop.getNamespaceURI(); + String pre = "<"+localName+" xmlns="+quote+namespaceURI+quote+" >"; + String post = "</"+localName+" >"; + propValue = prop.getPropertyAsString(); + xml = pre+propValue+post; + StringReader reader = new StringReader(xml); + Document doc = parser.parseDocument(new InputSource(reader)); + SourceProperty srcProperty = new SourceProperty(doc.getDocumentElement()); + sourceproperties.addElement(srcProperty); + } + } + + } catch (Exception e) { + throw new SourceException("Could not parse property "+xml, e); + + } finally { + this.manager.release((Component) parser); + } + SourceProperty[] sourcepropertiesArray = new SourceProperty[sourceproperties.size()]; + for (int i = 0; i<sourceproperties.size(); i++) { + sourcepropertiesArray[i] = (SourceProperty) sourceproperties.elementAt(i); + } + return sourcepropertiesArray; + } + + /** + * Returns a property from a source. + * + * @param namespace Namespace of the property + * @param name Name of the property + * + * @return Property of the source. + * + * @throws SourceException If an exception occurs. + */ + public SourceProperty getSourceProperty (String namespace, String name) + throws SourceException { + + Vector sourceproperties = new Vector(); + DOMParser parser = null; + String xml = ""; + Enumeration props= null; + org.apache.webdav.lib.Property prop = null; + String propValue = null; + + try { + parser = (DOMParser)this.manager.lookup(DOMParser.ROLE); + Enumeration responses = this.resource.propfindMethod(0); + while (responses.hasMoreElements()) { + ResponseEntity response = (ResponseEntity)responses.nextElement(); + props = response.getProperties(); + final String quote = "\""; + while (props.hasMoreElements()) { + prop = (Property) props.nextElement(); + + if (namespace.equals(prop.getNamespaceURI()) + && name.equals(prop.getLocalName())){ + + String localName = prop.getLocalName(); + String namespaceURI = prop.getNamespaceURI(); + String pre = "<"+localName+" xmlns="+quote+namespaceURI+quote+" >"; + String post = "</"+localName+" >"; + propValue = prop.getPropertyAsString(); + xml = pre+propValue+post; + StringReader reader = new StringReader(xml); + Document doc = parser.parseDocument(new InputSource(reader)); + + return new SourceProperty(doc.getDocumentElement()); + } + + } + } + } catch (Exception e) { + throw new SourceException("Could not parse property "+xml, e); + } finally { + this.manager.release((Component) parser); + } + return null; + } + + /** + * Remove a specified source property. + * + * @param namespace Namespace of the property. + * @param name Name of the property. + * + * @throws SourceException If an exception occurs. + */ + public void removeSourceProperty(String namespace, String name) + throws SourceException { + + try { + this.resource.proppatchMethod(new PropertyName(namespace, name), "", false); + } catch (Exception e) { + throw new SourceException("Could not remove property ", e); + } + } + + /** + * Sets a property for a source. + * + * @param sourceproperty Property of the source + * + * @throws SourceException If an exception occurs during this operation + */ + public void setSourceProperty(SourceProperty sourceproperty) + throws SourceException { + + try { + // FIXME SourceProperty.getValueAsString only delivers Text nodes + this.resource.proppatchMethod( + new PropertyName(sourceproperty.getNamespace(),sourceproperty.getName()), + sourceproperty.getValueAsString(), true); + } catch (Exception e) { + throw new SourceException("Could not set property ", e); + } + } + } 1.4 +9 -2 cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/components/source/impl/WebDAVSourceFactory.java Index: WebDAVSourceFactory.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/components/source/impl/WebDAVSourceFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- WebDAVSourceFactory.java 17 Jul 2003 12:24:52 -0000 1.3 +++ WebDAVSourceFactory.java 22 Aug 2003 12:23:41 -0000 1.4 @@ -54,6 +54,7 @@ import java.net.MalformedURLException; import java.util.Map; +import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.logger.AbstractLogEnabled; @@ -74,7 +75,7 @@ */ public class WebDAVSourceFactory extends AbstractLogEnabled - implements SourceFactory, ThreadSafe { + implements SourceFactory, ThreadSafe, Composable { /** The component manager instance */ private ComponentManager manager = null; @@ -115,6 +116,12 @@ WebDAVSource source = WebDAVSource.newWebDAVSource(location, principal, password, protocol); + + try { + source.compose(this.manager); + } catch (ComponentException ce) { + getLogger().error("Could not lookup for component.", ce); + } return source; }