gcasper     2003/08/27 10:36:07

  Modified:    src/blocks/webdav/java/org/apache/cocoon/components/source/impl
                        WebDAVSource.java
               src/blocks/webdav/java/org/apache/cocoon/transformation
                        SourcepropsWritingTransformer.java
  Log:
  -changed SourcepropsWritingTransformer to use XML syntax for property names
   and namespaces
  -fixed and cleaned up WebDAVSource
  
  Revision  Changes    Path
  1.7       +26 -48    
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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- WebDAVSource.java 22 Aug 2003 21:33:56 -0000      1.6
  +++ WebDAVSource.java 27 Aug 2003 17:36:07 -0000      1.7
  @@ -56,12 +56,13 @@
   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.Properties;
   import java.util.Vector;
   import java.util.Enumeration;
   
  +import javax.xml.transform.OutputKeys;
   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.sax.SAXTransformerFactory;
   import javax.xml.transform.sax.TransformerHandler;
  @@ -72,9 +73,9 @@
   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.cocoon.xml.XMLUtils;
   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;
  @@ -82,7 +83,6 @@
   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;
  @@ -92,9 +92,11 @@
   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;
  +import org.w3c.dom.DocumentFragment;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.NodeList;
   
   /**
    *  A source implementation to get access to WebDAV repositories. Use it
  @@ -709,41 +711,24 @@
        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());
  +                     SourceProperty srcProperty = new 
SourceProperty(prop.getElement());
                        sourceproperties.addElement(srcProperty);
                    }
                }
   
            } catch (Exception e) {
  -             throw new SourceException("Could not parse property "+xml, e);
  -
  -         } finally {
  -             this.manager.release((Component) parser);
  +             throw new SourceException("Error getting properties", e);
            }
            SourceProperty[] sourcepropertiesArray = new 
SourceProperty[sourceproperties.size()];
            for (int i = 0; i<sourceproperties.size(); i++) {
  @@ -765,43 +750,24 @@
       public SourceProperty getSourceProperty (String namespace, String name)
       throws SourceException {
   
  -          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());
  +                          return new SourceProperty(prop.getElement());
                         }
  -
                     }
                 }
             } catch (Exception e) {
  -              throw new SourceException("Could not parse property "+xml, e);
  -          } finally {
  -              this.manager.release((Component) parser);
  +              throw new SourceException("Error getting property: "+name, e);
             }
             return null;
       }
  @@ -835,14 +801,26 @@
       throws SourceException {
   
           try {
  -            // FIXME SourceProperty.getValueAsString only delivers Text nodes
  +
  +            Document doc = sourceproperty.getValue().getOwnerDocument();
  +            DocumentFragment frag = doc.createDocumentFragment();
  +            NodeList list = sourceproperty.getValue().getChildNodes();
  +            for (int i=0; i<list.getLength(); i++) {
  +                if (list.item(i) instanceof Element)
  +                frag.appendChild((Element)list.item(i));
  +            }
  +
  +            Properties format = new Properties();
  +            format.put(OutputKeys.METHOD, "xml");
  +            format.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
  +            String prop = XMLUtils.serializeNode(frag, format);
  +            
               this.resource.proppatchMethod(
                      new 
PropertyName(sourceproperty.getNamespace(),sourceproperty.getName()),
  -                   sourceproperty.getValueAsString(), true);
  +                   prop, true);
           } catch (Exception e) {
               throw new SourceException("Could not set property ", e);
           }
       }
  -
   
   }
  
  
  
  1.3       +35 -65    
cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/transformation/SourcepropsWritingTransformer.java
  
  Index: SourcepropsWritingTransformer.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/webdav/java/org/apache/cocoon/transformation/SourcepropsWritingTransformer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SourcepropsWritingTransformer.java        27 Aug 2003 08:33:42 -0000      
1.2
  +++ SourcepropsWritingTransformer.java        27 Aug 2003 17:36:07 -0000      
1.3
  @@ -51,21 +51,19 @@
   package org.apache.cocoon.transformation;
   
   import java.io.IOException;
  -import java.io.StringReader;
   import java.util.Map;
  -import java.util.Vector;
   
  -import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.source.InspectableSource;
   import org.apache.cocoon.components.source.helpers.SourceProperty;
   import org.apache.cocoon.environment.SourceResolver;
   import org.apache.excalibur.source.Source;
  -import org.apache.excalibur.xml.dom.DOMParser;
  -import org.w3c.dom.Document;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.DocumentFragment;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
   import org.xml.sax.Attributes;
  -import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
   
   /**
  @@ -78,7 +76,9 @@
    *   &lt;source:patch 
xmlns:source="http://apache.org/cocoon/propwrite/1.0"&gt;
    *     
&lt;source:source&gt;webdav://localhost/webdav/step1/repo/contentA.xml&lt;/source:source&gt;
    *     &lt;source:prop name="author" 
namespace="meta"&gt;me&lt;/source:prop&gt;
  - *     &lt;source:prop name="category" 
namespace="meta"&gt;catA&lt;/source:prop&gt;
  + *     &lt;source:props&gt;
  + *       &lt;myns:author xmlns:myns="meta"&gt;me&lt;/myns:author&gt;
  + *     &lt;/source:props&gt;
    *   &lt;/source:patch&gt;
    *   ...
    * &lt;/page&gt;
  @@ -95,17 +95,13 @@
           /** incoming elements */
       public static final String PATCH_ELEMENT = "patch";
       public static final String SOURCE_ELEMENT = "source";
  -    public static final String PROP_ELEMENT = "prop";
  -
  -    /** main tag attributes */
  -    public static final String NAME_ATTRIBUTE = "name";
  -    public static final String NAMESPACE_ATTRIBUTE = "namespace";
  +    public static final String PROPS_ELEMENT = "props";
   
       /** The current state */
  -    private static final int STATE_OUTSIDE  = 0;
  -    private static final int STATE_PATCH    = 1;
  -    private static final int STATE_SOURCE   = 2;
  -    private static final int STATE_PROP     = 3;
  +    private static final int STATE_OUTSIDE   = 0;
  +    private static final int STATE_PATCH     = 1;
  +    private static final int STATE_SOURCE    = 2;
  +    private static final int STATE_PROPS     = 3;
   
       private int state;
       
  @@ -166,12 +162,10 @@
               this.state = STATE_SOURCE;
               this.startTextRecording();
   
  -        // Element: prop
  -        } else if (this.state == STATE_PATCH && name.equals(PROP_ELEMENT)) {
  -            this.state = STATE_PROP;
  -            this.stack.push(attr.getValue(NAME_ATTRIBUTE));
  -            this.stack.push(attr.getValue(NAMESPACE_ATTRIBUTE));
  -            this.startTextRecording();
  +        // Element: props
  +        } else if (this.state == STATE_PATCH && name.equals(PROPS_ELEMENT)) {
  +            this.state = STATE_PROPS;
  +            this.startRecording();
   
           } else {
               super.startTransformingElement(uri, name, raw, attr);
  @@ -205,33 +199,24 @@
           // Element: patch
           if ((name.equals(PATCH_ELEMENT) && this.state == STATE_PATCH)) {
               this.state = STATE_OUTSIDE;
  -
               String sourceName = null;
  -            Vector props = new Vector();
  -            String tag;
  +            String tag = null;
  +            DocumentFragment frag = null;
               do {
  -                String[] prop = new String[3];
                   tag = (String)this.stack.pop();
                   if (tag.equals(SOURCE_ELEMENT)) {
                       sourceName = (String)this.stack.pop();
  -                } else if (tag.equals(PROP_ELEMENT)) {
  -                    prop[2] = (String)this.stack.pop();
  -                    prop[1] = (String)this.stack.pop();
  -                    prop[0] = (String)this.stack.pop();
  -                    props.addElement(prop);
  +                } else if (tag.equals(PROPS_ELEMENT)) {
  +                    frag = (DocumentFragment)this.stack.pop();
                   }
               } while ( !tag.equals("END") );
  -            
  -            String propName = null;
  -            String propNamespace = null;
  -            String propValue = null;
  -            String[] propsArray = new String[3];
  -            for (int i = 0; i<props.size(); i++) {
  -                propsArray = (String[]) props.elementAt(i);
  -                propName = propsArray[0]; 
  -                propNamespace = propsArray[1]; 
  -                propValue = propsArray[2]; 
  -                this.patchSource(sourceName, propName, propNamespace, 
propValue);
  +            NodeList list = frag.getChildNodes();
  +            Node node = null;
  +            for (int i=0; i<list.getLength(); i++) {
  +                node = list.item(i);
  +                if (node instanceof Element) {
  +                    this.patchSource(sourceName, (Element)node);
  +                }
               }
   
           // Element: source
  @@ -241,13 +226,11 @@
               this.stack.push(sourceName);
               this.stack.push(SOURCE_ELEMENT);
   
  -        // Element: prop
  -        } else if (name.equals(PROP_ELEMENT) == true && this.state == 
STATE_PROP) {
  +        // Element: props
  +        } else if (name.equals(PROPS_ELEMENT) == true && this.state == 
STATE_PROPS) {
               this.state = STATE_PATCH;
  -            // FIXME TextRecorder only gets characters events
  -            String propValue = this.endTextRecording();
  -            this.stack.push(propValue);
  -            this.stack.push(PROP_ELEMENT);
  +            this.stack.push(this.endRecording());
  +            this.stack.push(PROPS_ELEMENT);
   
           // default
           } else {
  @@ -259,34 +242,21 @@
           }
       }
   
  -    private void patchSource(String src, String name, String namespace, 
String value)
  +    private void patchSource(String src, Element value)
       throws ProcessingException, IOException, SAXException {
  -
  -        if (src != null && name != null && namespace != null && value != 
null) {
  -
  -            DOMParser parser = null;
  -            final String quote = "\"";
  +        if (src != null && value != null) {
               try {
  -                parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                   Source source = this.resolver.resolveURI(src);
                   if (source instanceof InspectableSource) {
  -                    String pre = "<"+name+" 
xmlns="+quote+namespace+quote+">";
  -                    String post = "</"+name+">";
  -                    String xml = pre+value+post;
  -                    StringReader reader = new StringReader(xml);
  -                    Document doc = parser.parseDocument(new 
InputSource(reader));
  -                    SourceProperty property = new 
SourceProperty(doc.getDocumentElement());
  +                    SourceProperty property = new SourceProperty(value);
                       ((InspectableSource)source).setSourceProperty(property);
   
                   } else {
                       this.getLogger().error("Cannot set properties on " + src 
+
                                              ": not an inspectable source");
                   }
  -
               } catch (Exception e) {
                   throw new ProcessingException("Error setting properties on 
"+src, e);
  -            } finally {
  -                this.manager.release((Component) parser);
               }
           } else {
               this.getLogger().error("Error setting properties on "+src);
  
  
  

Reply via email to