1 23 package org.apache.webdav.lib.properties; 24 25 import java.util.ArrayList ; 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 ; 32 import org.w3c.dom.NodeList ; 33 34 45 public class LockDiscoveryProperty extends BaseProperty { 46 47 48 50 51 54 public static final String TAG_NAME = "lockdiscovery"; 55 56 57 59 60 63 public LockDiscoveryProperty(ResponseEntity response, Element element) { 64 super(response, element); 65 } 66 67 68 70 71 76 public Lock[] getActiveLocks() { 77 NodeList children = element.getChildNodes(); 78 if (children == null || children.getLength() == 0) 79 return null; 80 ArrayList locks = new ArrayList (); 81 for (int i = 0; i < children.getLength(); i++) { 82 try { 83 Element child = (Element ) children.item(i); 84 String namespace = DOMUtils.getElementNamespaceURI(child); 85 if (namespace != null && namespace.equals("DAV:")) { 86 String localName = DOMUtils.getElementLocalName(child); 87 if ("activelock".equals(localName)) { 88 locks.add(parseLock(child)); 89 } 90 } 91 } catch (ClassCastException e) { 92 } 93 } 94 return (Lock[]) locks.toArray(new Lock[locks.size()]); 95 } 96 97 98 100 101 104 protected Lock parseLock(Element element) { 105 106 int ls = -1; 107 Element child = DOMUtils.getFirstElement(element, "DAV:", "lockscope"); 108 if (child != null) { 109 Element 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 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 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; } 151 } catch (NumberFormatException ex) { 152 d = -1; } 154 } 155 } 156 } 157 158 String 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 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 e) { 171 } 172 } 173 } 174 175 String lockToken = null; 176 child = DOMUtils.getFirstElement(element, "DAV:", "locktoken"); 177 if (child != null) { 178 Element href = DOMUtils.getFirstElement(child, "DAV:", "href"); 179 if (href != null) { 180 lockToken = DOMUtils.getTextValue(href); 181 } 182 } 183 184 String principalUrl = null; 185 child = DOMUtils.getFirstElement(element, "DAV:", "principal-URL"); 186 if (child != null) { 187 Element 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 getPropertyAsString() { 198 Lock[] locks = getActiveLocks(); 199 200 if ((locks==null) || (locks.length==0)) 201 return ""; 202 203 StringBuffer tmp = new StringBuffer (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 |