source: other-projects/tipple-android/tipple-lib/src/org/greenstone/android/tipple/base/GreenstoneXMLRetriever.java@ 26899

Last change on this file since 26899 was 26899, checked in by davidb, 11 years ago

Tipple reborn after Chris's Summer of Code 2013

File size: 6.9 KB
Line 
1package org.greenstone.android.tipple.base;
2
3import java.io.BufferedReader;
4import java.io.InputStream;
5import java.io.InputStreamReader;
6import java.net.HttpURLConnection;
7import java.net.URL;
8import java.util.ArrayList;
9import java.util.HashMap;
10
11import javax.xml.parsers.DocumentBuilder;
12import javax.xml.parsers.DocumentBuilderFactory;
13
14import org.w3c.dom.Document;
15import org.w3c.dom.NamedNodeMap;
16import org.w3c.dom.Node;
17import org.w3c.dom.NodeList;
18
19import android.util.Log;
20
21
22
23
24public class GreenstoneXMLRetriever
25{
26 private String lib_name = null;
27 //private String server_url = null;
28
29 private String port = null;
30
31 /*
32 * vals contains the names of the Greenstone article metadata, and vals_translated is what they map to.
33 *
34 * For example, if my article in the GS collection "tipple-universe" has the metadata "tp.Longitude", this class will
35 * map it to "longitude", which is what Tipple is looking for when it's displaying locations.
36 *
37 */
38 private final String[] vals = new String[] { "tp.Longitude", "tp.Latitude", "Title", "Radius", "Text", "Elevation" };
39 private final String[] vals_translated = new String[] { "longitude", "latitude", "title", "radius", "text", "elevation" };
40
41 private Document getDocFromUrl(String uri)
42 {
43 Document doc = null;
44
45 try
46 {
47 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48 //factory.setValidating(true);
49 factory.setIgnoringElementContentWhitespace(true);
50
51 URL url = new URL(uri);
52 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
53 connection.setRequestMethod("GET");
54 connection.connect();
55 InputStream stream = connection.getInputStream();
56
57 DocumentBuilder builder = factory.newDocumentBuilder();
58 doc = builder.parse(stream);
59 }
60 catch(Exception ex)
61 {
62 ex.printStackTrace();
63 }
64
65 return doc;
66 }
67
68 private HashMap<String,String> retrieveDataFromHashId(String hash_id)
69 {
70 HashMap<String,String> datas = null;
71
72
73 try
74 {
75 Document doc = getDocFromUrl("http://localhost:" + port + "/greenstone3/library?a=d&d=" + hash_id + "&c=" + lib_name + "&sib=&ed=1&o=xml");
76
77 datas = new HashMap<String,String>();
78
79 NodeList nodeList = doc.getElementsByTagName("metadata");
80 for (int i = 0; i < nodeList.getLength(); i++)
81 {
82
83 Node node = nodeList.item(i);
84
85 if( node.hasAttributes() )
86 {
87
88 NamedNodeMap map = node.getAttributes();
89 Node n = map.getNamedItem("name");
90
91 if(n != null)
92 {
93 String s = n.getTextContent();
94
95 // iterate through the attribs we are looking for... ie tp.Lat, tp.Lng, tp.Radius
96 for(int x = 0; x < vals.length; x++)
97 {
98 // if the attribute of the node is e.g. "tp.Lat", etc. then let's grab the content of this node
99 if(s.equals(vals[x]))
100 {
101 datas.put( vals_translated[x], node.getTextContent() );
102 }
103 }
104 }
105 }
106 }
107
108 nodeList = doc.getElementsByTagName("nodeContent");
109 Node node = nodeList.item(0);
110
111
112 /*
113 * TODO:
114 * I have hardcoded radius for now, but you should add this as another metadata to the
115 * collection "tipple-universe".
116 *
117 */
118
119 datas.put("radius", "0.2");
120 datas.put("text", node.getTextContent());
121 datas.put("elevation", "0");
122
123 }
124 catch(Exception ex)
125 {
126 ex.printStackTrace();
127 }
128
129 // if we got all the necessary data (lng,lat,radius,desc, and title)
130 if( datas.size() == vals.length) // vals.length + the radius + the text content
131 {
132 return datas;
133 }
134
135 // otherwise return null
136 return null;
137 }
138
139 public GreenstoneXMLRetriever(String port, String lib_name)
140 {
141 //this.server_url = server_url;
142 this.port = port;
143 this.lib_name = lib_name;
144 }
145
146 public ArrayList<String> query(double lng, double lat)
147 {
148 ArrayList<String> all_locations = new ArrayList<String>();
149
150 String[] latlngs = convertLatLng(lng, lat);
151 String short_lng = latlngs[0];
152 String short_lat = latlngs[1];
153
154 // ok, now construct a url
155 String query_url = "http://localhost:" + port + "/greenstone3/library?a=q&s=RawQuery&rt=rd&c=" +
156 lib_name +
157 "&s1.rawquery=" +
158 "(LA:" +
159 short_lat +
160 "+AND+" +
161 "LN:" +
162 short_lng +
163 ")" +
164 "&excerptid=jsonNodes&o=xml";
165
166 Log.d("GreenstoneXMLRetriever::query()", "Query URL constructed is: " + query_url);
167
168 Document doc = getDocFromUrl(query_url);
169
170 // presumably, if the doc is not null, we actually were able to access localhost
171 if(doc != null)
172 {
173
174 NodeList nodeList = doc.getElementsByTagName("documentNode");
175 for (int i = 0; i < nodeList.getLength(); i++)
176 {
177
178 Node node = nodeList.item(i);
179 if( node.hasAttributes() )
180 {
181
182 NamedNodeMap map = node.getAttributes();
183 Node n = map.getNamedItem("nodeID");
184
185 if(n != null)
186 {
187 String nodeID = n.getTextContent();
188 all_locations.add(nodeID);
189
190 Log.d("GreenstoneXMLRetriever::query()", "HashID found from querying GS3 server: " + nodeID);
191 }
192 }
193 }
194
195 return all_locations;
196
197 }
198
199 // otherwise presumably we couldn't connect to localhost, so return null
200 return null;
201
202 }
203
204 public String[] convertLatLng(double lng, double lat)
205 {
206 // ok, convert the doubles into strings
207 String lng_short = String.valueOf(lng);
208 String lat_short = String.valueOf(lat);
209
210 // remove everything after the decimal place
211 lng_short = lng_short.substring(0, lng_short.lastIndexOf('.'));
212 lat_short = lat_short.substring(0, lat_short.lastIndexOf('.'));
213
214 // if longitude is negative
215 if(lng_short.charAt(0) == '-')
216 {
217 // then its west
218 lng_short = lng_short.substring(1) + 'W';
219 }
220 else
221 {
222 // otherwise it's east
223 lng_short = lng_short + 'E';
224 }
225
226 // if latitude is negative
227 if(lat_short.charAt(0) == '-')
228 {
229 lat_short = lat_short.substring(1) + 'S';
230 }
231 else
232 {
233 lat_short = lat_short + 'N';
234 }
235
236 return new String[] { lng_short, lat_short };
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 public ArrayList< HashMap<String,String> > retrieveDataFromHashIds(ArrayList<String> hash_ids)
274 {
275 ArrayList< HashMap<String,String> > datas_container = new ArrayList< HashMap<String,String> >();
276
277
278 for(int x = 0; x < hash_ids.size(); x++)
279 {
280 HashMap<String,String> hm = retrieveDataFromHashId( hash_ids.get(x) );
281
282 // prepare hashmap (ie rename keys)
283 if( hm != null)
284 {
285 datas_container.add(hm);
286 }
287 }
288
289 return datas_container;
290 }
291
292}
Note: See TracBrowser for help on using the repository browser.