ee783a3295c111b60e08af7029facbc20e8798a5
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / WebdavUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package eu.alefzero.webdav;
21
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Locale;
26
27 import android.net.Uri;
28
29 public class WebdavUtils {
30 public static final SimpleDateFormat DISPLAY_DATE_FORMAT = new SimpleDateFormat(
31 "dd.MM.yyyy hh:mm");
32 private static final SimpleDateFormat DATETIME_FORMATS[] = {
33 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US),
34 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
35 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale.US),
36 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US),
37 new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US),
38 new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
39 new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US) };
40
41 public static String prepareXmlForPropFind() {
42 String ret = "<?xml version=\"1.0\" ?><D:propfind xmlns:D=\"DAV:\"><D:allprop/></D:propfind>";
43 return ret;
44 }
45
46 public static String prepareXmlForPatch() {
47 return "<?xml version=\"1.0\" ?><D:propertyupdate xmlns:D=\"DAV:\"></D:propertyupdate>";
48 }
49
50 public static Date parseResponseDate(String date) {
51 Date returnDate = null;
52 for (int i = 0; i < DATETIME_FORMATS.length; ++i) {
53 try {
54 returnDate = DATETIME_FORMATS[i].parse(date);
55 return returnDate;
56 } catch (ParseException e) {
57 }
58 }
59 return null;
60 }
61
62 /**
63 * Encodes a path according to URI RFC 2396.
64 *
65 * If the received path doesn't start with "/", the method adds it.
66 *
67 * @param remoteFilePath Path
68 * @return Encoded path according to RFC 2396, always starting with "/"
69 */
70 public static String encodePath(String remoteFilePath) {
71 String encodedPath = Uri.encode(remoteFilePath, "/");
72 if (!encodedPath.startsWith("/"))
73 encodedPath = "/" + encodedPath;
74 return encodedPath;
75 }
76
77 }