Source code: dr/webdav/xmldata/XMLData.java
1
2 package dr.webdav.xmldata;
3
4 import java.util.Vector;
5 import java.util.Enumeration;
6
7 /** Container for a XML response of WebDAV.
8 *@author Daniel Rohde
9 */
10 public class XMLData {
11 Vector multistatuslist = new Vector();
12 public XMLData() { }
13
14 public void addMultistatus(Multistatus multistatus) {
15 multistatuslist.add(multistatus);
16 }
17
18 // Some functions for convenience use
19
20 /** Returns a vector of strings contains relative URLs
21 *@return relative urls
22 */
23 public Vector getHrefList() {
24 if (multistatuslist.size()==0) return null;
25 Vector list = new Vector();
26
27 for (Enumeration m = multistatuslist.elements(); m.hasMoreElements(); ) {
28 Multistatus multi = (Multistatus) m.nextElement();
29 Vector l=multi.getHrefList();
30 if (l!=null)
31 for (Enumeration hl = l.elements(); hl.hasMoreElements();)
32 list.add(hl.nextElement());
33 }
34 return list;
35 }
36
37
38 private String getData(String href, String what) {
39 String data = null;
40 for (Enumeration m = multistatuslist.elements(); m.hasMoreElements();) {
41 Multistatus multi = (Multistatus) m.nextElement();
42 if (what.equals("resourcetype"))
43 data=multi.getResourceType(href);
44 else if (what.equals("contentlength"))
45 data=multi.getContentLength(href);
46 else if (what.equals("contenttype"))
47 data=multi.getContentType(href);
48 else if (what.equals("creationdate"))
49 data=multi.getCreationDate(href);
50 else if (what.equals("lastmodified"))
51 data=multi.getLastModified(href);
52 else if (what.equals("etag"))
53 data=multi.getETag(href);
54 else if (what.equals("status"))
55 data=multi.getStatusLine(href);
56 if (data!=null) return data;
57 }
58 return null;
59 }
60
61
62 /** Returns resource type of a relative URL
63 *@param href relative URL
64 *@return resource type like <code>D:collection</code>
65 or <code>null </code>
66 */
67 public String getResourceType(String href) {
68 return getData(href, "resourcetype");
69 }
70
71
72 /** Returns content type of a relative URL.
73 *@param href relative URL
74 *@return content type like <code>httpd/unix-directory</code>
75 or null
76 */
77 public String getContentType(String href) {
78 return getData(href, "contenttype");
79 }
80
81
82 /** Returns creation date of a relative URL.
83 *@param href relative URL
84 *@return creation date as String or null if URL not found.
85 */
86 public String getCreationDate(String href) {
87 return getData(href, "creationdate");
88 }
89
90
91 /** Returns last modification date of a relative URL.
92 *@param href relative URL
93 *@return last modification date as String or null
94 if URL not found.
95 */
96 public String getLastModified(String href) {
97 return getData(href, "lastmodified");
98 }
99
100 /** Returns etag from HTTP response.
101 *@param href a relative URL
102 *@return etag or null if href not found.
103 */
104 public String getETag(String href) {
105 return getData(href, "etag");
106 }
107
108 /** Returns content length from HTTP response.
109 *@param href a relative URL
110 *@return content length or null if no content length is submitted
111 */
112 public String getContentLength(String href) {
113 return getData(href, "contentlength");
114 }
115
116 public boolean getStatus(String href, String what) {
117 boolean status = false;
118 for (Enumeration m = multistatuslist.elements(); m.hasMoreElements();) {
119 Multistatus multi = (Multistatus) m.nextElement();
120 if (what.equals("islocked"))
121 status = multi.isLocked(href);
122 if (status) return status;
123 }
124 return status;
125 }
126 /** Returns lock state of a relative URL.
127 *@param href relative URL
128 *@return true if locked; false otherwise
129 */
130 public boolean isLocked(String href) {
131 return getStatus(href, "islocked");
132 }
133
134 /** Returns true if URL is a collection.
135 *@param href relative URL
136 *@return true if href is
137 */
138 public boolean isCollection(String href) {
139 String rt = null;
140 if ((rt=getResourceType(href))==null) return false;
141
142 return rt.equals("D:collection");
143 }
144
145 /** Returns status line from response or propstat
146 *@param href relative URL
147 *@return status line
148 */
149 public String getStatusLine(String href) {
150 return getData(href,"status");
151 }
152
153 public String getLockDiscoveryLockToken(String href) {
154 String lockToken=null;
155 for (Enumeration m = multistatuslist.elements(); m.hasMoreElements();) {
156 Multistatus multi = (Multistatus)m.nextElement();
157 lockToken=multi.getLockDiscoveryLockToken(href);
158 if (lockToken!=null) return lockToken;
159 }
160 return lockToken;
161 }
162 }