fix setting fileid for existing file
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / TreeNode.java
1 /* ownCloud Android client application
2 *
3 * Copyright (C) 2011 Bartek Przybylski
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.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.Map.Entry;
25
26 import org.w3c.dom.Document;
27
28 import android.text.TextUtils;
29 import android.util.Log;
30
31 public class TreeNode {
32 public enum NodeProperty {
33 NAME,
34 PARENT,
35 PATH,
36 RESOURCE_TYPE,
37 CREATE_DATE,
38 LAST_MODIFIED_DATE,
39 CONTENT_LENGTH
40 }
41
42 private LinkedList<TreeNode> mChildren;
43
44 public TreeNode() {
45 propertyMap_ = new HashMap<NodeProperty, String>();
46 mChildren = new LinkedList<TreeNode>();
47 }
48
49 public void setProperty(NodeProperty propertyName, String propertyValue) {
50 propertyMap_.put(propertyName, propertyValue);
51 }
52
53 public String getProperty(NodeProperty propertyName) {
54 return propertyMap_.get(propertyName);
55 }
56
57 void refreshData(Document document) {
58 throw new RuntimeException("Unimplemented refreshData");
59 }
60
61 public String toString() {
62 String str = "TreeNode {";
63 for (Entry<NodeProperty, String> e : propertyMap_.entrySet()) {
64 str += e.getKey() + ": " + e.getValue() + ",";
65 }
66 str += "}";
67 return str;
68 }
69
70 private HashMap<NodeProperty, String> propertyMap_;
71
72 public String stripPathFromFilename(String oc_path) {
73 if (propertyMap_.containsKey(NodeProperty.NAME)) {
74 String name = propertyMap_.get(NodeProperty.NAME);
75 name = name.replace(oc_path, "");
76 String path = "";
77 String name2 = name;
78 if (name.endsWith("/")) {
79 name = name.substring(0, name.length()-1);
80 }
81 path = name.substring(0, name.lastIndexOf('/')+1);
82 name = name.substring(name.lastIndexOf('/')+1);
83 name = name.replace("%20", " ");
84
85 propertyMap_.remove(NodeProperty.NAME);
86 propertyMap_.put(NodeProperty.NAME, name);
87 propertyMap_.remove(NodeProperty.PATH);
88 propertyMap_.put(NodeProperty.PATH, name2);
89 return path;
90 }
91 return null;
92 }
93
94 public LinkedList<TreeNode> getChildList() {
95 return mChildren;
96 }
97
98 public String[] getChildrenNames() {
99 String[] names = new String[mChildren.size()];
100 for (int i = 0; i < mChildren.size(); ++i) {
101 names[i] = mChildren.get(i).getProperty(NodeProperty.NAME);
102 }
103 return names;
104 }
105
106 public boolean hasChildren() {
107 return !mChildren.isEmpty();
108 }
109 }