KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > properties > LockDiscoveryProperty


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/properties/LockDiscoveryProperty.java,v 1.4.2.1 2004/10/11 08:17:20 luetzkendorf Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/10/11 08:17:20 $
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.properties;
24
25 import java.util.ArrayList JavaDoc;
26 import org.apache.webdav.lib.BaseProperty;
27 import org.apache.webdav.lib.Lock;
28 import org.apache.webdav.lib.ResponseEntity;
29 import org.apache.webdav.lib.methods.DepthSupport;
30 import org.apache.webdav.lib.util.DOMUtils;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33
34 /**
35  * This class represents a listing of who has lock, what type of lock he has,
36  * the timeout type and the time remaining on the timeout, and the associated
37  * lock token. The server is free to withhold any or all of this information
38  * if the requesting principal does not have sufficient access rights to see
39  * the requested data.
40  *
41  * <!ELEMENT lockdiscovery (activelock)* >
42  *
43  * @version $Revision: 1.4.2.1 $
44  */

45 public class LockDiscoveryProperty extends BaseProperty {
46
47
48     // -------------------------------------------------------------- Constants
49

50
51     /**
52      * The property name.
53      */

54     public static final String JavaDoc TAG_NAME = "lockdiscovery";
55
56
57     // ----------------------------------------------------------- Constructors
58

59
60     /**
61      * Default constructor for the property.
62      */

63     public LockDiscoveryProperty(ResponseEntity response, Element JavaDoc element) {
64         super(response, element);
65     }
66
67
68     // --------------------------------------------------------- Public Methods
69

70
71     /**
72      * Get the activelock in this lockdiscovery property.
73      *
74      * @return Lock[] A lock array or null when there is no lock.
75      */

76     public Lock[] getActiveLocks() {
77         NodeList JavaDoc children = element.getChildNodes();
78         if (children == null || children.getLength() == 0)
79             return null;
80         ArrayList JavaDoc locks = new ArrayList JavaDoc();
81         for (int i = 0; i < children.getLength(); i++) {
82             try {
83                 Element JavaDoc child = (Element JavaDoc) children.item(i);
84                 String JavaDoc namespace = DOMUtils.getElementNamespaceURI(child);
85                 if (namespace != null && namespace.equals("DAV:")) {
86                     String JavaDoc localName = DOMUtils.getElementLocalName(child);
87                     if ("activelock".equals(localName)) {
88                         locks.add(parseLock(child));
89                     }
90                 }
91             } catch (ClassCastException JavaDoc e) {
92             }
93         }
94         return (Lock[]) locks.toArray(new Lock[locks.size()]);
95     }
96
97
98     // ------------------------------------------------------ Protected Methods
99

100
101     /**
102      * Parse a lock.
103      */

104     protected Lock parseLock(Element JavaDoc element) {
105
106         int ls = -1;
107         Element JavaDoc child = DOMUtils.getFirstElement(element, "DAV:", "lockscope");
108         if (child != null) {
109             Element JavaDoc lockScope =
110                 DOMUtils.getFirstElement(child, "DAV:", "exclusive");
111             if (lockScope != null) {
112                 ls = Lock.SCOPE_EXCLUSIVE;
113             }
114             lockScope = DOMUtils.getFirstElement(child, "DAV:", "shared");
115             if (lockScope != null) {
116                 ls = Lock.SCOPE_SHARED;
117             }
118         }
119
120         int lt = -1;
121         child = DOMUtils.getFirstElement(element, "DAV:", "locktype");
122         if (child != null) {
123             Element JavaDoc lockType = DOMUtils.getFirstElement(child, "DAV:", "write");
124             if (lockType != null) {
125                 lt = Lock.TYPE_WRITE;
126             } else {
127                 lockType = DOMUtils.getFirstElement(child, "DAV:", "transaction");
128                 if (lockType != null) {
129                     lt = Lock.TYPE_TRANSACTION;
130                 }
131             }
132         }
133
134         int d = -1;
135         child = DOMUtils.getFirstElement(element, "DAV:", "depth");
136         if (child != null) {
137             String JavaDoc depth = DOMUtils.getTextValue(child);
138             if (depth != null) {
139                 if ("0".equals(depth)) {
140                     d = DepthSupport.DEPTH_0;
141                 } else if ("1".equals(depth)) {
142                     d = DepthSupport.DEPTH_1;
143                 } else if ("infinity".equalsIgnoreCase(depth)) {
144                     d = DepthSupport.DEPTH_INFINITY;
145                 } else {
146                     try {
147                         d = Integer.parseInt(depth);
148                         if (d<0) {
149                             d = -1; // unknown
150
}
151                     } catch (NumberFormatException JavaDoc ex) {
152                         d = -1; // unknown
153
}
154                 }
155             }
156         }
157
158         String JavaDoc owner = null;
159         child = DOMUtils.getFirstElement(element, "DAV:", "owner");
160         owner = DOMUtils.getTextValue(child);
161
162         int t = -1;
163         child = DOMUtils.getFirstElement(element, "DAV:", "timeout");
164         if (child != null) {
165             String JavaDoc timeout = DOMUtils.getTextValue(child);
166             int at = timeout.indexOf('-');
167             if (at > 0) {
168                 try {
169                     t = Integer.parseInt(timeout.substring(at + 1));
170                 } catch (NumberFormatException JavaDoc e) {
171                 }
172             }
173         }
174
175         String JavaDoc lockToken = null;
176         child = DOMUtils.getFirstElement(element, "DAV:", "locktoken");
177         if (child != null) {
178             Element JavaDoc href = DOMUtils.getFirstElement(child, "DAV:", "href");
179             if (href != null) {
180                 lockToken = DOMUtils.getTextValue(href);
181             }
182         }
183         
184         String JavaDoc principalUrl = null;
185         child = DOMUtils.getFirstElement(element, "DAV:", "principal-URL");
186         if (child != null) {
187             Element JavaDoc href = DOMUtils.getFirstElement(child, "DAV:", "href");
188             if (href != null) {
189                 principalUrl = DOMUtils.getTextValue(href);
190             }
191         }
192
193         return new Lock(ls, lt, d, owner, t, lockToken, principalUrl);
194
195     }
196
197     public String JavaDoc getPropertyAsString() {
198         Lock[] locks = getActiveLocks();
199
200         if ((locks==null) || (locks.length==0))
201             return "";
202
203         StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(locks[0].toString());
204         for (int i=1; i<locks.length ; i++) {
205             tmp.append(", ");
206             tmp.append(locks[i].toString());
207         }
208
209         return tmp.toString();
210     }
211
212 }
213
214
Popular Tags