OSDN Git Service

android-2.1_r1 snapshot
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / picasa / GDataParser.java
1 package com.cooliris.picasa;
2
3 import org.xml.sax.Attributes;
4 import org.xml.sax.ContentHandler;
5 import org.xml.sax.Locator;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.AttributesImpl;
8
9 import android.text.format.Time;
10
11 public final class GDataParser implements ContentHandler {
12     public static final String APP_NAMESPACE = "http://www.w3.org/2007/app";
13     public static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
14     public static final String GD_NAMESPACE = "http://schemas.google.com/g/2005";
15     public static final String GPHOTO_NAMESPACE = "http://schemas.google.com/photos/2007";
16     public static final String MEDIA_RSS_NAMESPACE = "http://search.yahoo.com/mrss/";
17     public static final String GML_NAMESPACE = "http://www.opengis.net/gml";
18     private static final String FEED_ELEMENT = "feed";
19     private static final String ENTRY_ELEMENT = "entry";
20
21     private static final int STATE_DOCUMENT = 0;
22     private static final int STATE_FEED = 1;
23     private static final int STATE_ENTRY = 2;
24
25     private static final int NUM_LEVELS = 5;
26
27     private Entry mEntry = null;
28     private EntryHandler mHandler = null;
29     private int mState = STATE_DOCUMENT;
30     private int mLevel = 0;
31     private String[] mUri = new String[NUM_LEVELS];
32     private String[] mName = new String[NUM_LEVELS];
33     private AttributesImpl[] mAttributes = new AttributesImpl[NUM_LEVELS];
34     private final StringBuilder mValue = new StringBuilder(128);
35
36     public interface EntryHandler {
37         void handleEntry(Entry entry);
38     }
39
40     public GDataParser() {
41         AttributesImpl[] attributes = mAttributes;
42         for (int i = 0; i != NUM_LEVELS; ++i) {
43             attributes[i] = new AttributesImpl();
44         }
45     }
46
47     public void setEntry(Entry entry) {
48         mEntry = entry;
49     }
50
51     public void setHandler(EntryHandler handler) {
52         mHandler = handler;
53     }
54
55     public static long parseAtomTimestamp(String timestamp) {
56         Time time = new Time();
57         time.parse3339(timestamp);
58         return time.toMillis(true);
59     }
60
61     public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
62         switch (mState) {
63         case STATE_DOCUMENT:
64             // Expect an atom:feed element.
65             if (uri.equals(ATOM_NAMESPACE) && localName.equals(FEED_ELEMENT)) {
66                 mState = STATE_FEED;
67             } else {
68                 throw new SAXException();
69             }
70             break;
71         case STATE_FEED:
72             // Expect a feed property element or an atom:entry element.
73             if (uri.equals(ATOM_NAMESPACE) && localName.equals(ENTRY_ELEMENT)) {
74                 mState = STATE_ENTRY;
75                 mEntry.clear();
76             } else {
77                 startProperty(uri, localName, attrs);
78             }
79             break;
80         case STATE_ENTRY:
81             startProperty(uri, localName, attrs);
82             break;
83         }
84     }
85
86     public void endElement(String uri, String localName, String qName) throws SAXException {
87         if (mLevel > 0) {
88             // Handle property exit.
89             endProperty();
90         } else {
91             // Handle state exit.
92             switch (mState) {
93             case STATE_DOCUMENT:
94                 throw new SAXException();
95             case STATE_FEED:
96                 mState = STATE_DOCUMENT;
97                 break;
98             case STATE_ENTRY:
99                 mState = STATE_FEED;
100                 mHandler.handleEntry(mEntry);
101                 break;
102             }
103         }
104     }
105
106     private void startProperty(String uri, String localName, Attributes attrs) {
107         // Push element information onto the property stack.
108         int level = mLevel + 1;
109         mLevel = level;
110         mValue.setLength(0);
111         mUri[level] = uri;
112         mName[level] = localName;
113         mAttributes[level].setAttributes(attrs);
114     }
115
116     private void endProperty() {
117         // Apply property to the entry, then pop the stack.
118         int level = mLevel;
119         mEntry.setPropertyFromXml(mUri[level], mName[level], mAttributes[level], mValue.toString());
120         mLevel = level - 1;
121     }
122
123     public void characters(char[] ch, int start, int length) throws SAXException {
124         mValue.append(ch, start, length);
125     }
126
127     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
128         // Ignored.
129     }
130
131     public void processingInstruction(String target, String data) throws SAXException {
132         // Ignored.
133     }
134
135     public void setDocumentLocator(Locator locator) {
136         // Ignored.
137     }
138
139     public void skippedEntity(String name) throws SAXException {
140         // Ignored.
141     }
142
143     public void startDocument() throws SAXException {
144         // Ignored.
145     }
146
147     public void endDocument() throws SAXException {
148         // Ignored.
149     }
150
151     public void startPrefixMapping(String prefix, String uri) throws SAXException {
152         // Ignored.
153     }
154
155     public void endPrefixMapping(String prefix) throws SAXException {
156         // Ignored.
157     }
158 }