1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package eu
.alefzero
.webdav
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.text
.ParseException
;
24 import java
.text
.SimpleDateFormat
;
25 import java
.util
.Date
;
26 import java
.util
.LinkedList
;
27 import java
.util
.List
;
28 import java
.util
.Locale
;
30 import javax
.xml
.parsers
.DocumentBuilder
;
31 import javax
.xml
.parsers
.DocumentBuilderFactory
;
32 import javax
.xml
.parsers
.ParserConfigurationException
;
34 import org
.w3c
.dom
.Document
;
35 import org
.w3c
.dom
.Element
;
36 import org
.w3c
.dom
.Node
;
37 import org
.w3c
.dom
.NodeList
;
38 import org
.xml
.sax
.SAXException
;
40 import eu
.alefzero
.webdav
.TreeNode
.NodeProperty
;
42 import android
.util
.Log
;
44 public class WebdavUtils
{
46 public static final String RESPONSE
= "response";
47 public static final String HREF
= "href";
48 public static final String IS_HIDDEN
= "ishidden";
49 public static final String RESOURCE_TYPE
= "resourcetype";
50 public static final String CONTENT_TYPE
= "getcontenttype";
51 public static final String CONTENT_LENGTH
= "getcontentlength";
52 public static final String LAST_MODIFIED
= "getlastmodified";
53 public static final String LAST_ACCESS
= "lastaccessed";
54 public static final String CREATE_DATE
= "creationdate";
56 public static final String PROPSTAT
= "propstat";
57 public static final String STATUS
= "status";
58 public static final String PROP
= "prop";
60 private static final String DAV_NAMESPACE_PREFIX
= "DAV:";
62 public static final SimpleDateFormat DISPLAY_DATE_FORMAT
= new SimpleDateFormat("dd.MM.yyyy hh:mm");
63 private static final SimpleDateFormat DATETIME_FORMATS
[] = {
64 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale
.US
),
65 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale
.US
),
66 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale
.US
),
67 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale
.US
),
68 new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale
.US
),
69 new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale
.US
),
70 new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale
.US
)};
72 public static String
prepareXmlForPropFind() {
73 String ret
= "<?xml version=\"1.0\" ?><D:propfind xmlns:D=\"DAV:\"><D:allprop/></D:propfind>";
77 public static String
prepareXmlForPatch() {
78 return "<?xml version=\"1.0\" ?><D:propertyupdate xmlns:D=\"DAV:\"></D:propertyupdate>";
81 private static Date
parseResponseDate(String date
) {
82 Date returnDate
= null
;
83 for (int i
= 0; i
< DATETIME_FORMATS
.length
; ++i
) {
85 returnDate
= DATETIME_FORMATS
[i
].parse(date
);
87 } catch (ParseException e
) {}
92 private static String
determineDAVPrefix(Element e
) {
93 for (int i
= 0; i
< e
.getAttributes().getLength(); ++i
) {
94 String attrName
= e
.getAttributes().item(i
).getNodeName();
95 if (e
.getAttribute(attrName
).equals(DAV_NAMESPACE_PREFIX
)) {
96 return attrName
.substring(attrName
.lastIndexOf(':')+1) + ":";
102 public static List
<TreeNode
> parseResponseToNodes(InputStream response
) {
103 LinkedList
<TreeNode
> rList
= new LinkedList
<TreeNode
>();
104 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
105 DocumentBuilder builder
;
107 builder
= factory
.newDocumentBuilder();
108 Document document
= builder
.parse(response
);
109 String davPrefix
= determineDAVPrefix(document
.getDocumentElement());
111 NodeList nodes
= document
.getElementsByTagName(davPrefix
+ RESPONSE
);
112 Log
.i("WebdavUtils", "Parsing " + nodes
.getLength() + " response nodes");
114 for (int i
= 0; i
< nodes
.getLength(); ++i
) {
115 Node currentNode
= nodes
.item(i
);
116 TreeNode resultNode
= new TreeNode();
117 parseResourceType(currentNode
, resultNode
, davPrefix
);
118 parseResourceDates(currentNode
, resultNode
, davPrefix
);
119 parseDisplayName(currentNode
, resultNode
, davPrefix
);
120 rList
.add(resultNode
);
124 } catch (ParserConfigurationException e
) {
126 } catch (SAXException e
) {
128 } catch (IOException e
) {
134 private static void parseDisplayName(Node currentNode
, TreeNode resultNode
,
136 Element currentElement
= (Element
) currentNode
;
137 if (currentElement
.getElementsByTagName(davPrefix
+ HREF
).getLength() != 0) {
138 String filepath
= currentElement
.getElementsByTagName(davPrefix
+ HREF
).item(0).getFirstChild().getNodeValue();
139 resultNode
.setProperty(NodeProperty
.NAME
, filepath
);
143 private static void parseResourceDates(Node currentNode
, TreeNode resultNode
, String davPrefix
) {
144 Element currentElement
= (Element
)currentNode
;
145 if (currentElement
.getElementsByTagName(davPrefix
+ LAST_MODIFIED
).getLength() != 0) {
146 Date date
= parseResponseDate(
147 currentElement
.getElementsByTagName(davPrefix
+ LAST_MODIFIED
).item(0).getFirstChild().getNodeValue());
148 resultNode
.setProperty(NodeProperty
.LAST_MODIFIED_DATE
, String
.valueOf(date
.getTime()));
150 if (currentElement
.getElementsByTagName(davPrefix
+ CREATE_DATE
).getLength() != 0) {
151 Date date
= parseResponseDate(
152 currentElement
.getElementsByTagName(davPrefix
+ CREATE_DATE
).item(0).getFirstChild().getNodeValue());
153 resultNode
.setProperty(NodeProperty
.CREATE_DATE
, String
.valueOf(date
));
157 private static void parseResourceType(Node currentNode
, TreeNode resultNode
, String davPrefix
) {
158 Element currentElement
= (Element
)currentNode
;
159 if (currentElement
.getElementsByTagName(davPrefix
+ RESOURCE_TYPE
).getLength() != 0 &&
160 currentElement
.getElementsByTagName(davPrefix
+ RESOURCE_TYPE
).item(0).hasChildNodes()) {
161 resultNode
.setProperty(NodeProperty
.RESOURCE_TYPE
, "DIR");
163 if (currentElement
.getElementsByTagName(davPrefix
+ CONTENT_TYPE
).getLength() != 0) {
164 resultNode
.setProperty(NodeProperty
.RESOURCE_TYPE
,
165 currentElement
.getElementsByTagName(davPrefix
+ CONTENT_TYPE
).item(0).getFirstChild().getNodeValue());
167 if (currentElement
.getElementsByTagName(davPrefix
+ CONTENT_LENGTH
).getLength() != 0) {
168 resultNode
.setProperty(NodeProperty
.CONTENT_LENGTH
,
169 currentElement
.getElementsByTagName(davPrefix
+ CONTENT_LENGTH
).item(0).getFirstChild().getNodeValue());