KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > ant > CollectionScanner


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/CollectionScanner.java,v 1.3.2.2 2004/08/22 10:36:47 luetzkendorf Exp $
3  * $Revision: 1.3.2.2 $
4  * $Date: 2004/08/22 10:36:47 $
5  * ========================================================================
6  * Copyright 2004 The Apache Software Foundation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================================================================
20  */

21 package org.apache.webdav.ant;
22
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.HttpURL;
32 import org.apache.commons.httpclient.URIException;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.webdav.lib.PropertyName;
36 import org.apache.webdav.lib.methods.DepthSupport;
37 import org.apache.webdav.lib.methods.PropFindMethod;
38 import org.apache.webdav.lib.properties.ResourceTypeProperty;
39
40 /**
41  * Scan a collection of WebDAV resources to find ones that match a
42  * specified pattern.
43  *
44  */

45 public class CollectionScanner extends Scanner {
46
47     private HttpURL baseURL = null;
48     private HttpClient client = null;
49     private ResourceProperties properties = null;
50
51     private Vector JavaDoc propertyNames = new Vector JavaDoc();
52
53     public CollectionScanner() {
54        propertyNames.add(Utils.RESOURCETYPE);
55        propertyNames.add(Utils.GETLASTMODIFIED);
56     }
57
58     /**
59      * Scans the base URL for resources that match at least one include
60      * pattern, and don't match any exclude patterns.
61      *
62      * For each resource the properties are stored and may be used after
63      * scanning - may be for further selecting. (see {@link #getProperties()}
64      * and {@link #addProperty(PropertyName)}).
65      *
66      * @exception IllegalStateException when baseurl was set incorrecly
67      * @exception ScanException when a WebDAV or other error occurs
68      */

69     public void scan() {
70
71         if (baseURL == null) {
72            throw new IllegalStateException JavaDoc(
73                  "BaseURL must be set before calling the scan() method");
74         }
75
76         // initialize member variables
77
filesIncluded = new ArrayList JavaDoc();
78         filesExcluded = new ArrayList JavaDoc();
79         filesNotIncluded = new ArrayList JavaDoc();
80         dirsIncluded = new ArrayList JavaDoc();
81         dirsExcluded = new ArrayList JavaDoc();
82         dirsNotIncluded = new ArrayList JavaDoc();
83         this.properties = new ResourceProperties();
84         
85         try {
86            readCollection(baseURL);
87         }
88 // catch (IOException e) {
89
// throw new ScanException(e.getMessage(), e);
90
// }
91
catch (Exception JavaDoc e) {
92            e.printStackTrace();
93         }
94     }
95
96    protected void readCollection(HttpURL collURL)
97          throws URIException
98    {
99       if (!collURL.getPath().endsWith(SEPARATOR)) {
100          collURL = Utils.createHttpURL(collURL, "");
101          collURL.setPath(collURL.getPath() + SEPARATOR);
102       }
103       
104       // get a list of all resources from the given URL
105
PropFindMethod propFind = new PropFindMethod(collURL.getURI(),
106                                                    DepthSupport.DEPTH_1,
107                                                    PropFindMethod.BY_NAME);
108       propFind.setPropertyNames(propertyNames.elements());
109       propFind.setFollowRedirects(true);
110       try {
111          this.client.executeMethod(propFind);
112       }
113       catch (IOException JavaDoc e) {
114          Utils.makeBuildException("Can't read collection content!", e);
115       }
116       
117       List JavaDoc subCollections = new ArrayList JavaDoc();
118       this.properties.storeProperties(propFind);
119       
120       // this collection
121
addResource(collURL.getPath(), true);
122       
123       // for each content element, check resource type and classify
124
for (Enumeration JavaDoc e = propFind.getAllResponseURLs(); e.hasMoreElements(); )
125       {
126          String JavaDoc href = (String JavaDoc) e.nextElement();
127          
128          ResourceTypeProperty property =
129                                 this.properties.getResourceType(collURL, href);
130          
131          if (property != null) {
132             if (property.isCollection()) {
133                if (!href.endsWith(SEPARATOR)) href = href + SEPARATOR;
134                // the collection URL itself may be in the list of
135
// response URL; filter them out to avoid recursion
136
HttpURL sub = Utils.createHttpURL(collURL, href);
137                if (!sub.equals(collURL)) {
138                   subCollections.add(Utils.createHttpURL(collURL, href));
139                }
140             } else {
141                addResource(href, false);
142             }
143          } else {
144             throw new BuildException("Can't determine resourcetype.");
145          }
146       }
147       
148       // read all sub collections
149
for(Iterator JavaDoc i = subCollections.iterator(); i.hasNext();) {
150          readCollection((HttpURL)i.next());
151       }
152    }
153
154    protected void addResource(String JavaDoc href, boolean isCollection)
155             throws ScanException
156    {
157       try {
158          String JavaDoc path = (Utils.createHttpURL(getBaseURL(), href)).getPath();
159          String JavaDoc relPath = path.substring(getBaseURL().getPath().length());
160          if (relPath.startsWith(SEPARATOR)) {
161             relPath = relPath.substring(1);
162          }
163          if (isCollection) {
164             if (isIncluded(relPath)) {
165                if (isExcluded(relPath)) {
166                   dirsExcluded.add(relPath);
167                } else {
168                   dirsIncluded.add(relPath);
169                }
170             } else {
171                dirsNotIncluded.add(relPath);
172             }
173          } else {
174             if (isIncluded(relPath)) {
175                if (isExcluded(relPath)) {
176                   filesExcluded.add(relPath);
177                } else {
178                   filesIncluded.add(relPath);
179                }
180            } else {
181                filesNotIncluded.add(relPath);
182            }
183          }
184        }
185        catch (URIException e) {
186           throw new ScanException(
187              "The XML response returned an invalid URL: " + e.getMessage(), e);
188        }
189     }
190
191     public HttpURL getBaseURL() {
192         return this.baseURL;
193     }
194
195     public void setBaseURL(HttpURL baseURL) {
196         this.baseURL = baseURL;
197     }
198
199     public void setHttpClient(HttpClient client) {
200         this.client = client;
201     }
202     
203     public ResourceProperties getProperties()
204     {
205        return this.properties;
206     }
207
208     /**
209      * Adds a property which the scanner retrieves while scanning.
210      *
211      * @param property Name of the property to be retrieved.
212      */

213     public void addProperty(PropertyName property) {
214        if (property == null) throw new NullPointerException JavaDoc();
215        this.propertyNames.add(property);
216     }
217 }
218
Popular Tags