KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > WebdavResources


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

23 package org.apache.webdav.lib;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 /**
29  * This WebdavResources class represents a set of {@link WebdavResource WebDAV resources }.
30  *
31  * Two WebdavResource instances are considered equal, if there {@link WebdavResource#getName()
32  * getName()} method returns the same string.
33  *
34  */

35
36 public class WebdavResources {
37
38     // --------------------------------------------------------- Constructors
39

40     /**
41      * Default constuctor.
42      */

43     public WebdavResources() {
44     }
45
46
47     /**
48      * This constuctor.
49      *
50      * @param resource A resource to add.
51      */

52     public WebdavResources(WebdavResource resource) {
53         addResource(resource);
54     }
55
56
57     // --------------------------------------------------- Instance Variables
58

59
60     /**
61      * The resources for each href and its properties.
62      */

63     Hashtable JavaDoc hrefTable = new Hashtable JavaDoc();
64
65
66     // ------------------------------------------------------- Public Methods
67

68
69     /**
70      * Test if there is no resource.
71      *
72      * @return true if it's empty.
73      */

74     public boolean isEmpty() {
75         return hrefTable.isEmpty();
76     }
77
78
79     /**
80      * Test if there is a resource called the specified resource name.
81      *
82      * @param resourceName The resource name to check.
83      */

84     public boolean isThereResourceName(String JavaDoc resourceName) {
85         return hrefTable.containsKey(resourceName);
86     }
87
88
89     /**
90      * Test if there is a resource.
91      *
92      * @param resource The specified resource.
93      * @return true if it exists.
94      */

95     public boolean isThereResource(WebdavResource resource) {
96         return hrefTable.contains(resource);
97     }
98
99
100     /**
101      * Get an enumeration of the resource names.
102      *
103      * @return An enumeration of the resource names.
104      */

105     public Enumeration JavaDoc getResourceNames() {
106         return hrefTable.keys();
107     }
108
109
110     /**
111      * Get an enumeration of the resources.
112      *
113      * @return An enumeration of resources.
114      */

115     public Enumeration JavaDoc getResources() {
116         return hrefTable.elements();
117     }
118
119
120     /**
121      * Get an array of resource names.
122      *
123      * @return An array of resource names.
124      */

125     public String JavaDoc[] list() {
126
127         synchronized (hrefTable) {
128             int num = hrefTable.size();
129             String JavaDoc resourceNames[] = new String JavaDoc[num];
130
131             Enumeration JavaDoc resources = getResourceNames();
132             int i = 0;
133             while (resources.hasMoreElements()) {
134                 resourceNames[i++] = (String JavaDoc) resources.nextElement();
135             }
136
137             return resourceNames;
138         }
139     }
140
141
142     /**
143      * Get an arraay of resources.
144      *
145      * @return An array of resources.
146      */

147     public WebdavResource[] listResources() {
148
149         synchronized (hrefTable) {
150             int num = hrefTable.size();
151             WebdavResource WebdavResources[] = new WebdavResource[num];
152
153             Enumeration JavaDoc resources = getResources();
154             int i = 0;
155             while (resources.hasMoreElements()) {
156                 WebdavResources[i++] =
157                     (WebdavResource) resources.nextElement();
158             }
159
160             return WebdavResources;
161         }
162     }
163
164
165     /**
166      * Get an resource.
167      *
168      * @param resourceName The resource name.
169      * @return The wanted resource if it exists.
170      */

171     public WebdavResource getResource(String JavaDoc resourceName) {
172         return (WebdavResource) hrefTable.get(resourceName);
173     }
174
175
176     /**
177      * Maps the resource name to its resource.
178      * The resource name could be different from the displayname property.
179      * It's useful for representing itself or parent collection.
180      *
181      * @param resourceName The resource name.
182      * @param resource The resource.
183      * @see #addResource(WebdavResource)
184      */

185     public void addResource(String JavaDoc resourceName, WebdavResource resource) {
186         hrefTable.put(resourceName, resource);
187     }
188
189
190     /**
191      * Add the specified resource.
192      *
193      * @param resource The resource to add.
194      */

195     public void addResource(WebdavResource resource) {
196         hrefTable.put(resource.getName(), resource);
197     }
198
199
200     /**
201      * Remove the specified resource name.
202      *
203      * @param resourceName The specified resource name.
204      * @return The wanted resource.
205      */

206     public WebdavResource removeResource(String JavaDoc resourceName) {
207         return (WebdavResource) hrefTable.remove(resourceName);
208     }
209
210
211     /**
212      * Remove all resources.
213      */

214     public void removeAll() {
215         hrefTable.clear();
216     }
217
218
219     /**
220      * Return the string for this class.
221      */

222     public String JavaDoc toString() {
223         return hrefTable.toString();
224     }
225
226 }
227
228
229
230
231
232
233
Popular Tags