Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / files / api / client / parsers / ContainerObjectXMLparser.java @ a2b9aced

History | View | Annotate | Download (5.1 kB)

1
package com.rackspace.cloud.files.api.client.parsers;
2

    
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.Map;
7

    
8
import org.xml.sax.Attributes;
9
import org.xml.sax.helpers.DefaultHandler;
10

    
11
import android.util.Log;
12

    
13
import com.rackspace.cloud.files.api.client.ContainerObjects;
14

    
15
/**
16
 * 
17
 * @author Phillip Toohill
18
 * 
19
 */
20
public class ContainerObjectXMLparser extends DefaultHandler {
21

    
22
        private ContainerObjects object;
23
        private ArrayList<ContainerObjects> files;
24
        private ArrayList<ContainerObjects> subdirs;
25
        private Map<String, String> metadata;
26
        private String key;
27

    
28
        private StringBuffer currentData;
29
        public String LOG = "ViewFilesXMLparser";
30
        
31
        public void startDocument() {
32
                Log.v(LOG, "startDocument");
33
        }
34

    
35
        public void endDocument() {
36
                Log.v(LOG, "endDocument = true "+files);
37
        }
38

    
39
        public void startElement(String uri, String name, String qName,
40
                        Attributes atts) {
41

    
42
                currentData = new StringBuffer();
43
                if ("container".equals(name)) {
44
                        files = new ArrayList<ContainerObjects>();
45
                } else if ("object".equals(name)) {
46
                        object = new ContainerObjects();
47
                } else if ("x_object_meta".equals(name)) {
48
                        metadata = new HashMap<String, String>();
49
                }
50
                 else if ("subdir".equals(name)) {
51
                         if(subdirs==null)
52
                                 subdirs=new ArrayList<ContainerObjects>();
53
                         Log.i(LOG, "inside:"+name+" "+atts.getValue(0));
54
                                object = new ContainerObjects();
55
                                object.setSubDir(true);
56
                                Log.i(LOG,true+" "+atts.getLength());
57
                                for(int i=0;i<atts.getLength();i++){
58
                                        Log.i(LOG,true+" "+atts.getQName(i)+" "+atts.getLocalName(i)+" "+atts.getValue(i));
59
                                        if(atts.getLocalName(i).equals("name")){
60
                                                object.setName(atts.getValue(i));
61
                                                break;
62
                                        }
63
                                }
64
                                object.setCName(object.getName());
65
                                subdirs.add(object);
66
                                files.add(object);
67
                                Log.i(LOG,"set name:"+object.getName());
68
                                object=null;
69
                        }
70
        }
71

    
72
        public void endElement(String uri, String name, String qName) {
73

    
74
                String value = currentData.toString().trim();
75

    
76
                if ("container".equals(name)) {
77

    
78
                } else if ("object".equals(name)) {
79
                        if (files != null) {
80
                                Log.d(LOG, object.toString());
81
                                files.add(object);
82
                        }
83
                } else if ("x_object_meta".equals(name)) {
84
                        if (object != null) {
85
                                object.setMetadata(metadata);
86
                                metadata = null;
87
                        }
88

    
89
                } else if ("key".equals(name) && metadata != null) {
90
                        key = value;
91
                } else if ("value".equals(name) && metadata != null) {
92
                        if (key != null)
93
                                metadata.put(key, value);
94
                } else if ("name".equals(name)) {
95
                        object.setCName(value);
96
                } else if ("content_type".equals(name)) {
97
                        object.setContentType(value);
98
                } else if ("hash".equals(name)) {
99
                        object.setHash(value);
100
                } else if ("bytes".equals(name)) {
101
                        object.setBytes(Integer.parseInt(value));
102
                } else if ("last_modified".equals(name)) {
103
                        object.setLastMod(value);
104

    
105
                }
106
                // x_object_modified_by
107
                // x_object_version
108
                // x_object_hash
109
                // x_object_uuid
110
                // x_object_version_timestamp
111
                // x_object_sharing
112
                else if ("x_object_modified_by".equals(name)) {
113
                        object.setModifiedBy(value);
114

    
115
                } else if ("x_object_version".equals(name)) {
116
                        object.setVersion(value);
117

    
118
                } else if ("x_object_hash".equals(name)) {
119
                        object.setObjectHash(value);
120

    
121
                } else if ("x_object_uuid".equals(name)) {
122
                        object.setObjectUUID(value);
123

    
124
                } else if ("x_object_public".equals(name)) {
125
                        object.setIsPublic(value);
126
                } else if ("x_object_version_timestamp".equals(name)) {
127
                        object.setVersionTimestamp(value);
128

    
129
                } else if ("x_object_sharing".equals(name)) {
130
                        object.setObjectSharing(value);
131

    
132
                }
133
        }
134

    
135
        public void characters(char ch[], int start, int length) {
136

    
137
                // Log.d("Rackspace-Cloud", "Characters:    \"");
138

    
139
                for (int i = start; i < start + length; i++) {
140
                        switch (ch[i]) {
141
                        case '\\':
142
                                // Log.d("Rackspace-Cloud", "\\\\");
143
                                break;
144
                        case '"':
145
                                // Log.d("Rackspace-Cloud", "\\\"");
146
                                break;
147
                        case '\n':
148
                                // Log.d("Rackspace-Cloud", "\\n");
149
                                break;
150
                        case '\r':
151
                                // Log.d("Rackspace-Cloud", "\\r");
152
                                break;
153
                        case '\t':
154
                                // Log.d("Rackspace-Cloud", "\\t");
155
                                break;
156
                        default:
157
                                // Log.d("Rackspace-Cloud", String.valueOf(ch[i]));
158
                                break;
159
                        }
160
                }
161
                // Log.d("Rackspace-Cloud", "\"\n");
162
                // String strCharacters = new String(ch, start, length);
163
                for (int i = start; i < (start + length); i++) {
164
                        currentData.append(ch[i]);
165
                }
166
        }
167

    
168
        public ContainerObjects getObject() {
169
                return object;
170
        }
171

    
172
        public void setObject(ContainerObjects object) {
173
                this.object = object;
174
        }
175

    
176
        /**
177
         * @return the files
178
         */
179
        public ArrayList<ContainerObjects> getViewFiles() {
180
                if(subdirs!=null){
181
                        Iterator<ContainerObjects> it = subdirs.iterator();
182
                        while(it.hasNext()){
183
                                ContainerObjects s = it.next();
184
                                boolean found = false;
185
                                for(ContainerObjects o : files){
186
                                        if(o.getCName().equals(s.getCName().substring(0,s.getCName().length()-1))){
187
                                                found=true;
188
                                                break;
189
                                        }
190
                                }
191
                                if(found){
192
                                        files.remove(s);
193
                                }
194
                                else
195
                                        s.setCName(s.getCName().substring(0,s.getCName().length()-1));
196
                                        
197
                        }
198
                }
199
                return files;
200
        }
201

    
202
        /**
203
         * @param files
204
         *            the servers to set
205
         */
206
        public void setFiles(ArrayList<ContainerObjects> files) {
207
                this.files = files;
208
        }
209

    
210
}