]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/gss-lib-2.2/src/com/pras/sp/ParseFeed.java
Added gss-lib-2.2.
[Dictionary.git] / jars / gss-lib-2.2 / src / com / pras / sp / ParseFeed.java
1 /*\r
2  * Copyright (C) 2010 Prasanta Paul, http://prasanta-paul.blogspot.com\r
3  *\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
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package com.pras.sp;\r
18 \r
19 import java.io.ByteArrayInputStream;\r
20 \r
21 import javax.xml.parsers.SAXParser;\r
22 import javax.xml.parsers.SAXParserFactory;\r
23 \r
24 import org.xml.sax.Attributes;\r
25 import org.xml.sax.SAXException;\r
26 import org.xml.sax.helpers.DefaultHandler;\r
27 \r
28 import com.pras.Log;\r
29 import com.pras.WorkSheetCell;\r
30 \r
31 \r
32 /**\r
33  * Using SAX Parser, to keep the compatibility with Android\r
34  * @author Prasanta Paul\r
35  *\r
36  */\r
37 public class ParseFeed extends DefaultHandler {\r
38 \r
39         private String TAG = "ParseFeed";\r
40         \r
41         // Attributes\r
42         final String ATTRITUBE_ETAG = "gd:etag";\r
43         final String ATTRITUBE_SRC = "src";\r
44         // Nodes\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
49         // Table Feed\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
58         // Table Feed\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
63         // Cell based Feed\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
68         /**\r
69          * Not in use; \r
70          * @deprecated\r
71          */\r
72         final String NODE_GD_RESOURCEID = "gd:resourceId";\r
73         \r
74         // Table Record\r
75         static String NODE_GS_FIELD = "gs:field";\r
76         \r
77         // ACL Fields\r
78         final String NODE_GACL_ROLE = "gAcl:role";\r
79         final String NODE_GACL_SCOPE = "gAcl:scope";\r
80         \r
81         // [Fix_11_JAN_2011_Prasanta For Android SDK\r
82         /*\r
83          * Android SDK removes gs: prefix\r
84          */\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
91         }\r
92         // Fix_11_JAN_2011_Prasanta]\r
93         \r
94         Feed f = null;\r
95         Entry e = null;\r
96         Field field = null;\r
97         String node = null;\r
98         \r
99         public Feed parse(byte[] data){\r
100                 \r
101                 SAXParserFactory factory = SAXParserFactory.newInstance();\r
102                 try{\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
108                 }\r
109                 \r
110                 return f;\r
111         }\r
112 \r
113         @Override\r
114         public void characters(char[] ch, int start, int length)\r
115                         throws SAXException {\r
116                 super.characters(ch, start, length);\r
117                 \r
118                 if(node.equals(NODE_TITLE)){\r
119                         if(e == null)\r
120                                 f.setTitle(new String(ch).substring(start, start+length));\r
121                         else\r
122                                 e.setTitle(new String(ch).substring(start, start+length));\r
123                 }\r
124                 if(node.equals(NODE_SUMMARY)){\r
125                         if(e != null)\r
126                                 e.setSummary(new String(ch).substring(start, start+length));\r
127                 }\r
128                 else if(node.equals(NODE_NAME)){\r
129                         if(e != null)\r
130                                 e.setAuthorName(new String(ch).substring(start, start+length));\r
131                 }\r
132                 else if(node.equals(NODE_EMAIL)){\r
133                         if(e != null)\r
134                                 e.setAuthorEmail(new String(ch).substring(start, start+length));\r
135                 }\r
136                 else if(node.equals(NODE_ID)){\r
137                         if(e == null)\r
138                                 f.setId(new String(ch).substring(start, start+length));\r
139                         else\r
140                                 e.setId(new String(ch).substring(start, start+length));\r
141                 }\r
142                 else if(node.equals(NODE_ROW_COUNT)){\r
143                         if(e != null)\r
144                                 e.setRowCount(Integer.parseInt(new String(ch).substring(start, start+length)));\r
145                 }\r
146                 else if(node.equals(NODE_COL_COUNT)){\r
147                         if(e != null)\r
148                                 e.setColCount(Integer.parseInt(new String(ch).substring(start, start+length)));\r
149                 }\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
156                         if(e != null)\r
157                                 e.addCell(cell);\r
158                 }\r
159                 else if(node.equals(NODE_GD_RESOURCEID)){\r
160                         e.setResID(new String(ch).substring(start, start+length));\r
161                 }\r
162                 else if(node.equals(NODE_GS_FIELD)){\r
163                         field.setValue(new String(ch).substring(start, start+length));\r
164                         e.addField(field);\r
165                 }\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
171                                 if(e != null)\r
172                                         e.setCellInfo(cellInfo);\r
173                         }\r
174                 }\r
175         }\r
176 \r
177         @Override\r
178         public void startDocument() throws SAXException {\r
179                 super.startDocument();\r
180                 f = new Feed();\r
181         }\r
182 \r
183         @Override\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
187                 \r
188                 if(name.trim().length() == 0)\r
189                         node = localName;\r
190                 else\r
191                         node = name;\r
192                 \r
193                 //Log.p(TAG, "LocalName: "+ localName +" Name="+ name +" URI="+ uri);\r
194                 \r
195                 if(node.equals(NODE_FEED)){\r
196                         f = new Feed();\r
197                         // read ETag\r
198                         //f.setEtag(attributes.getValue("http://schemas.google.com/g/2005", "gd:etag"));\r
199                         f.setEtag(attributes.getValue("gd:etag"));\r
200                 }\r
201                 else if(node.equals(NODE_ENTRY)){\r
202                         //Log.p(TAG, "Entry...XML Feed");\r
203                         e = new Entry();\r
204                         // read ETag\r
205                         e.setETAG(attributes.getValue("gd:etag"));\r
206                 }\r
207                 else if(node.equals(NODE_CONTENT)){\r
208                         /*\r
209                          * TODO:\r
210                          * Fetch WorkSheet or Main access URL for any SpreadSheet or WorkSheet\r
211                          * from <link rel="self">\r
212                          */\r
213                         e.setWorkSheetURL(attributes.getValue("src"));\r
214                 }\r
215                 else if(node.equals(NODE_LINK)){\r
216                         if(e != null){\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
222                                         if(index != -1)\r
223                                                 e.setResID("spreadsheet:"+ href.substring(index + 5));\r
224                                 }\r
225                                 else if(rel != null && rel.equals("edit")){\r
226                                         // Read Edit Link\r
227                                         e.setEditLink(attributes.getValue("href"));\r
228                                 }\r
229                         }\r
230                 }\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
235                                 e.addCol(colName);\r
236                 }\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
243                         }\r
244                 }\r
245                 else if(node.equals(NODE_GACL_ROLE)){\r
246                         //ACL Role\r
247                         e.setAclRole(attributes.getValue("value"));\r
248                 }\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
253                 }\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
259                         if(e != null)\r
260                                 e.setCellInfo(cellInfo);\r
261                 }\r
262         }\r
263         \r
264         @Override\r
265         public void endDocument() throws SAXException {\r
266                 super.endDocument();\r
267         }\r
268 \r
269         @Override\r
270         public void endElement(String uri, String localName, String name)\r
271                         throws SAXException {\r
272                 super.endElement(uri, localName, name);\r
273                 \r
274                 if(name.trim().length() == 0)\r
275                         node = localName;\r
276                 else\r
277                         node = name;\r
278                 \r
279                 if(node == null)\r
280                         return;\r
281                 \r
282                 if(node.equals(NODE_ENTRY)){\r
283                         // end of entry\r
284                         f.addEntry(e);\r
285                         e = null;\r
286                 }\r
287         }\r
288 }\r