KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/properties/SupportedLockProperty.java,v 1.4 2004/08/02 15:45:50 unico Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/08/02 15:45:50 $
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.util.DOMUtils;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * @version $Revision: 1.4 $
35  */

36 public class SupportedLockProperty extends BaseProperty {
37
38
39     // -------------------------------------------------------------- Constants
40

41
42     /**
43      * The property name.
44      */

45     public static final String JavaDoc TAG_NAME = "supportedlock";
46
47
48     // ----------------------------------------------------------- Constructors
49

50
51     /**
52      * Default constructor for the property.
53      */

54     public SupportedLockProperty(ResponseEntity response, Element JavaDoc element) {
55         super(response, element);
56     }
57
58
59     // --------------------------------------------------------- Public Methods
60

61
62
63     /**
64      * Get the lockentry in this supportedlock property.
65      *
66      * @return Lock[] A lock array or null when there is no lock.
67      */

68     public Lock[] getLockEntries() {
69         NodeList JavaDoc children = element.getChildNodes();
70         if (children == null || children.getLength() == 0)
71             return null;
72         ArrayList JavaDoc locks = new ArrayList JavaDoc();
73         for (int i = 0; i < children.getLength(); i++) {
74             try {
75                 Element JavaDoc child = (Element JavaDoc) children.item(i);
76                 String JavaDoc namespace = DOMUtils.getElementNamespaceURI(child);
77                 if (namespace != null && namespace.equals("DAV:")) {
78                     String JavaDoc localName = DOMUtils.getElementLocalName(child);
79                     if ("lockentry".equals(localName)) {
80                         locks.add(parseLock(child));
81                     }
82                 }
83             } catch (ClassCastException JavaDoc e) {
84             }
85         }
86         return (Lock[]) locks.toArray(new Lock[locks.size()]);
87     }
88
89
90     // ------------------------------------------------------ Protected Methods
91

92
93     /**
94      * Parse a lock.
95      */

96     protected Lock parseLock(Element JavaDoc element) {
97
98         int ls = -1;
99         Element JavaDoc child = DOMUtils.getFirstElement(element, "DAV:", "lockscope");
100         if (child != null) {
101             Element JavaDoc lockScope =
102                 DOMUtils.getFirstElement(child, "DAV:", "exclusive");
103             if (lockScope != null) {
104                 ls = Lock.SCOPE_EXCLUSIVE;
105             }
106             lockScope = DOMUtils.getFirstElement(child, "DAV:", "shared");
107             if (lockScope != null) {
108                 ls = Lock.SCOPE_SHARED;
109             }
110         }
111
112         int lt = -1;
113         child = DOMUtils.getFirstElement(element, "DAV:", "locktype");
114         if (child != null) {
115             Element JavaDoc lockType =
116                 DOMUtils.getFirstElement(child, "DAV:", "write");
117             if (lockType != null) {
118                 lt = Lock.TYPE_WRITE;
119             }
120         }
121
122         return new Lock(ls, lt);
123     }
124
125     public String JavaDoc getPropertyAsString() {
126         Lock[] locks = getLockEntries();
127
128         if ((locks==null) || (locks.length==0))
129             return "";
130
131         StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(locks[0].toString());
132         for (int i=1; i<locks.length ; i++) {
133             tmp.append(", ");
134             tmp.append(locks[i].toString());
135         }
136
137         return tmp.toString();
138     }
139 }
140
141
142
Popular Tags