1 package com.cooliris.picasa;
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;
9 import android.text.format.Time;
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";
21 private static final int STATE_DOCUMENT = 0;
22 private static final int STATE_FEED = 1;
23 private static final int STATE_ENTRY = 2;
25 private static final int NUM_LEVELS = 5;
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);
36 public interface EntryHandler {
37 void handleEntry(Entry entry);
40 public GDataParser() {
41 AttributesImpl[] attributes = mAttributes;
42 for (int i = 0; i != NUM_LEVELS; ++i) {
43 attributes[i] = new AttributesImpl();
47 public void setEntry(Entry entry) {
51 public void setHandler(EntryHandler handler) {
55 public static long parseAtomTimestamp(String timestamp) {
56 Time time = new Time();
57 time.parse3339(timestamp);
58 return time.toMillis(true);
61 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
64 // Expect an atom:feed element.
65 if (uri.equals(ATOM_NAMESPACE) && localName.equals(FEED_ELEMENT)) {
68 throw new SAXException();
72 // Expect a feed property element or an atom:entry element.
73 if (uri.equals(ATOM_NAMESPACE) && localName.equals(ENTRY_ELEMENT)) {
77 startProperty(uri, localName, attrs);
81 startProperty(uri, localName, attrs);
86 public void endElement(String uri, String localName, String qName) throws SAXException {
88 // Handle property exit.
94 throw new SAXException();
96 mState = STATE_DOCUMENT;
100 mHandler.handleEntry(mEntry);
106 private void startProperty(String uri, String localName, Attributes attrs) {
107 // Push element information onto the property stack.
108 int level = mLevel + 1;
112 mName[level] = localName;
113 mAttributes[level].setAttributes(attrs);
116 private void endProperty() {
117 // Apply property to the entry, then pop the stack.
119 mEntry.setPropertyFromXml(mUri[level], mName[level], mAttributes[level], mValue.toString());
123 public void characters(char[] ch, int start, int length) throws SAXException {
124 mValue.append(ch, start, length);
127 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
131 public void processingInstruction(String target, String data) throws SAXException {
135 public void setDocumentLocator(Locator locator) {
139 public void skippedEntity(String name) throws SAXException {
143 public void startDocument() throws SAXException {
147 public void endDocument() throws SAXException {
151 public void startPrefixMapping(String prefix, String uri) throws SAXException {
155 public void endPrefixMapping(String prefix) throws SAXException {