Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / parsers / CloudServersFaultXMLParser.java @ 038ac9a4

History | View | Annotate | Download (2.4 kB)

1
/**
2
 * 
3
 */
4
package com.rackspace.cloud.servers.api.client.parsers;
5

    
6
import org.xml.sax.Attributes;
7
import org.xml.sax.helpers.DefaultHandler;
8

    
9
import android.util.Log;
10

    
11
import com.rackspace.cloud.servers.api.client.CloudServersException;
12

    
13
/**
14
 * @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
15
 * 
16
 */
17
public class CloudServersFaultXMLParser extends DefaultHandler {
18

    
19
        // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
20
        // <cloudServersFault
21
        // xmlns="http://docs.rackspacecloud.com/servers/api/v1.0" code="400">
22
        // <message>422 Unprocessable Entity: Cannot perform requested action until
23
        // reboot is complete.</message>
24
        // <details>com.rackspace.cloud.service.servers.CloudServersFault: 422
25
        // Unprocessable Entity: Cannot perform requested action until reboot is
26
        // complete.</details>
27
        // </cloudServersFault>
28

    
29
        private CloudServersException exception;
30
        private StringBuffer currentData;
31

    
32
        public void startDocument() {
33
                exception = new CloudServersException();
34
        }
35

    
36
        public void endDocument() {
37
        }
38

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

    
42
                currentData = new StringBuffer();
43
                if ("cloudServersFault".equals(name)) {
44
                        exception.setCode(Integer.parseInt(atts.getValue("code")));
45
                }
46
        }
47

    
48
        public void endElement(String uri, String name, String qName) {
49
                if ("message".equals(name)) {
50
                        exception.setMessage(currentData.toString());
51
                } else if ("details".equals(name)) {
52
                        exception.setDetails(currentData.toString());
53
                }
54
        }
55

    
56
        public void characters(char ch[], int start, int length) {
57
                Log.d("Rackspace-Cloud", "Characters:    \"");
58
                for (int i = start; i < start + length; i++) {
59
                        switch (ch[i]) {
60
                        case '\\':
61
                                Log.d("Rackspace-Cloud", "\\\\");
62
                                break;
63
                        case '"':
64
                                Log.d("Rackspace-Cloud", "\\\"");
65
                                break;
66
                        case '\n':
67
                                Log.d("Rackspace-Cloud", "\\n");
68
                                break;
69
                        case '\r':
70
                                Log.d("Rackspace-Cloud", "\\r");
71
                                break;
72
                        case '\t':
73
                                Log.d("Rackspace-Cloud", "\\t");
74
                                break;
75
                        default:
76
                                Log.d("Rackspace-Cloud", String.valueOf(ch[i]));
77
                                break;
78
                        }
79
                }
80
                Log.d("Rackspace-Cloud", "\"\n");
81

    
82
                for (int i = start; i < (start + length); i++) {
83
                        currentData.append(ch[i]);
84
                }
85
        }
86

    
87
        /**
88
         * @return the exception
89
         */
90
        public CloudServersException getException() {
91
                return exception;
92
        }
93

    
94
        /**
95
         * @param exception
96
         *            the exception to set
97
         */
98
        public void setException(CloudServersException exception) {
99
                this.exception = exception;
100
        }
101

    
102
}