2 * Copyright (C) 2010 Prasanta Paul, http://prasanta-paul.blogspot.com
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.pras.sp;
\r
19 import java.io.ByteArrayInputStream;
\r
21 import javax.xml.parsers.SAXParser;
\r
22 import javax.xml.parsers.SAXParserFactory;
\r
24 import org.xml.sax.Attributes;
\r
25 import org.xml.sax.SAXException;
\r
26 import org.xml.sax.helpers.DefaultHandler;
\r
28 import com.pras.Log;
\r
29 import com.pras.WorkSheetCell;
\r
33 * Using SAX Parser, to keep the compatibility with Android
\r
34 * @author Prasanta Paul
\r
37 public class ParseFeed extends DefaultHandler {
\r
39 private String TAG = "ParseFeed";
\r
42 final String ATTRITUBE_ETAG = "gd:etag";
\r
43 final String ATTRITUBE_SRC = "src";
\r
45 final String NODE_FEED = "feed";
\r
46 final String NODE_ENTRY = "entry";
\r
47 final String NODE_ID = "id";
\r
48 final String NODE_TITLE = "title";
\r
50 final String NODE_SUMMARY = "summary";
\r
51 final String NODE_CONTENT = "content";
\r
52 final String NODE_LINK = "link";
\r
53 final String NODE_NAME = "name";
\r
54 final String NODE_EMAIL = "email";
\r
55 // Nodes- specific to Work Sheet
\r
56 static String NODE_ROW_COUNT = "gs:rowCount";
\r
57 static String NODE_COL_COUNT = "gs:colCount";
\r
59 static String NODE_GS_DATA = "gs:data";
\r
60 static String NODE_GS_COL = "gs:column";
\r
61 // Nodes- specific to WorkSheet List Data Feed (gsx: namespace)
\r
62 final String NODE_GSX = "gsx:";
\r
64 final String NODE_GS_CELL = "gs:cell";
\r
65 final String ATTRITUBE_ROW = "row";
\r
66 final String ATTRITUBE_COL = "col";
\r
67 // Resource ID for SpreadSheet (Document List API)
\r
72 final String NODE_GD_RESOURCEID = "gd:resourceId";
\r
75 static String NODE_GS_FIELD = "gs:field";
\r
78 final String NODE_GACL_ROLE = "gAcl:role";
\r
79 final String NODE_GACL_SCOPE = "gAcl:scope";
\r
81 // [Fix_11_JAN_2011_Prasanta For Android SDK
\r
83 * Android SDK removes gs: prefix
\r
85 public static void doCustomizationForSDK(){
\r
86 NODE_GS_DATA = "data";
\r
87 NODE_GS_COL = "column";
\r
88 NODE_GS_FIELD = "field";
\r
89 NODE_ROW_COUNT = "rowCount";
\r
90 NODE_COL_COUNT = "colCount";
\r
92 // Fix_11_JAN_2011_Prasanta]
\r
99 public Feed parse(byte[] data){
\r
101 SAXParserFactory factory = SAXParserFactory.newInstance();
\r
103 SAXParser parser = factory.newSAXParser();
\r
104 parser.parse(new ByteArrayInputStream(data), this);
\r
105 }catch(Exception ex){
\r
106 Log.p(TAG, "Error in parsing: "+ ex.toString());
\r
107 ex.printStackTrace();
\r
114 public void characters(char[] ch, int start, int length)
\r
115 throws SAXException {
\r
116 super.characters(ch, start, length);
\r
118 if(node.equals(NODE_TITLE)){
\r
120 f.setTitle(new String(ch).substring(start, start+length));
\r
122 e.setTitle(new String(ch).substring(start, start+length));
\r
124 if(node.equals(NODE_SUMMARY)){
\r
126 e.setSummary(new String(ch).substring(start, start+length));
\r
128 else if(node.equals(NODE_NAME)){
\r
130 e.setAuthorName(new String(ch).substring(start, start+length));
\r
132 else if(node.equals(NODE_EMAIL)){
\r
134 e.setAuthorEmail(new String(ch).substring(start, start+length));
\r
136 else if(node.equals(NODE_ID)){
\r
138 f.setId(new String(ch).substring(start, start+length));
\r
140 e.setId(new String(ch).substring(start, start+length));
\r
142 else if(node.equals(NODE_ROW_COUNT)){
\r
144 e.setRowCount(Integer.parseInt(new String(ch).substring(start, start+length)));
\r
146 else if(node.equals(NODE_COL_COUNT)){
\r
148 e.setColCount(Integer.parseInt(new String(ch).substring(start, start+length)));
\r
150 else if(node.toLowerCase().startsWith(NODE_GSX)){
\r
151 node = node.toLowerCase();
\r
152 WorkSheetCell cell = new WorkSheetCell();
\r
153 cell.setName(node.toLowerCase().substring(node.indexOf(":") + 1));
\r
154 cell.setValue(new String(ch).substring(start, start+length));
\r
155 // TODO: Cell Data Type
\r
159 else if(node.equals(NODE_GD_RESOURCEID)){
\r
160 e.setResID(new String(ch).substring(start, start+length));
\r
162 else if(node.equals(NODE_GS_FIELD)){
\r
163 field.setValue(new String(ch).substring(start, start+length));
\r
166 else if(node.equals(NODE_GS_CELL)){
\r
167 // Cell Feed: gs:cell
\r
168 WorkSheetCell cellInfo = e.getCellInfo();
\r
169 if(cellInfo != null){
\r
170 cellInfo.setName(new String(ch).substring(start, start+length));
\r
172 e.setCellInfo(cellInfo);
\r
178 public void startDocument() throws SAXException {
\r
179 super.startDocument();
\r
184 public void startElement(String uri, String localName, String name,
\r
185 Attributes attributes) throws SAXException {
\r
186 super.startElement(uri, localName, name, attributes);
\r
188 if(name.trim().length() == 0)
\r
193 //Log.p(TAG, "LocalName: "+ localName +" Name="+ name +" URI="+ uri);
\r
195 if(node.equals(NODE_FEED)){
\r
198 //f.setEtag(attributes.getValue("http://schemas.google.com/g/2005", "gd:etag"));
\r
199 f.setEtag(attributes.getValue("gd:etag"));
\r
201 else if(node.equals(NODE_ENTRY)){
\r
202 //Log.p(TAG, "Entry...XML Feed");
\r
205 e.setETAG(attributes.getValue("gd:etag"));
\r
207 else if(node.equals(NODE_CONTENT)){
\r
210 * Fetch WorkSheet or Main access URL for any SpreadSheet or WorkSheet
\r
211 * from <link rel="self">
\r
213 e.setWorkSheetURL(attributes.getValue("src"));
\r
215 else if(node.equals(NODE_LINK)){
\r
217 // Consider Link only of Entry node
\r
218 String rel = attributes.getValue("rel");
\r
219 if(rel != null && rel.equals("alternate")){
\r
220 String href = attributes.getValue("href");
\r
221 int index = href.indexOf("?key=");
\r
223 e.setResID("spreadsheet:"+ href.substring(index + 5));
\r
225 else if(rel != null && rel.equals("edit")){
\r
227 e.setEditLink(attributes.getValue("href"));
\r
231 else if(node.equals(NODE_GS_COL)){
\r
232 String colName = attributes.getValue("name");
\r
233 //Log.p(TAG, "[gs:column:] node..."+ colName);
\r
234 if(colName != null)
\r
237 else if(node.equals(NODE_GS_FIELD)){
\r
238 String fieldName = attributes.getValue("name");
\r
239 if(fieldName != null){
\r
240 field = new Field();
\r
241 field.setColName(fieldName);
\r
242 field.setIndex(attributes.getValue("index"));
\r
245 else if(node.equals(NODE_GACL_ROLE)){
\r
247 e.setAclRole(attributes.getValue("value"));
\r
249 else if(node.equals(NODE_GACL_SCOPE)){
\r
250 // ACL Scope- Type and Value
\r
251 e.setAclScopeType(attributes.getValue("type"));
\r
252 e.setAclScopeValue(attributes.getValue("value"));
\r
254 else if(node.equals(NODE_GS_CELL)){
\r
255 // Cell Feed: gs:cell
\r
256 WorkSheetCell cellInfo = new WorkSheetCell();
\r
257 cellInfo.setRow(Integer.parseInt(attributes.getValue(ATTRITUBE_ROW)));
\r
258 cellInfo.setCol(Integer.parseInt(attributes.getValue(ATTRITUBE_COL)));
\r
260 e.setCellInfo(cellInfo);
\r
265 public void endDocument() throws SAXException {
\r
266 super.endDocument();
\r
270 public void endElement(String uri, String localName, String name)
\r
271 throws SAXException {
\r
272 super.endElement(uri, localName, name);
\r
274 if(name.trim().length() == 0)
\r
282 if(node.equals(NODE_ENTRY)){
\r