/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 315 by torben, Fri Sep 11 08:34:57 2009 UTC revision 1007 by torben, Tue Aug 3 06:12:10 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.StringReader;  import java.io.StringReader;
 import java.util.ArrayList;  
 import java.util.List;  
4    
5  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParser;
6  import javax.xml.parsers.SAXParserFactory;  import javax.xml.parsers.SAXParserFactory;
# Line 15  import org.xml.sax.helpers.DefaultHandle Line 13  import org.xml.sax.helpers.DefaultHandle
13    
14  import android.util.Log;  import android.util.Log;
15  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.traininfo.DepartureBean;
16    import dk.thoerup.traininfo.DepartureEntry;
17    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
18  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
19    import dk.thoerup.traininfo.util.XmlUtil;
20    
21  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider implements DepartureProvider {
22    
23          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static int CACHE_TIMEOUT = 60*1000;
24                    
25            
26            AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
27                    
28                    
29                    
30          DepartureBean tempDeparture;          DepartureEntry tempDeparture;
31          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
32                    
33    
34            
35          @Override          @Override
36          public void lookupDepartures(int stationID) {          public DepartureBean lookupDepartures(int stationID, boolean arrival) {        
37                  departures.clear();                  
38                    String key = "" + stationID + ":" + arrival;
39                    
40                    DepartureBean departures = departureCache.get(key);
41            
42                    if (departures == null) {                      
43                            departures = lookupDeparturesWorker(stationID, arrival);
44                            
45                            if (departures != null) {                      
46                                    departureCache.put(key, departures);
47                            }
48                            
49                    } else {
50                            Log.i("XmlDepartureProvider", "cache hit !!!");
51                    }              
52                    
53                    return departures;
54            }
55            
56            private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
57    
58                  try                  try
59                  {                        {      
60                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;                          int iArrival = arrival ? 1 : 0;
61                          String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID;                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
62                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
63                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
64                                                    
# Line 41  public class XmlDepartureProvider extend Line 68  public class XmlDepartureProvider extend
68              SAXParser sp = spf.newSAXParser();              SAXParser sp = spf.newSAXParser();
69              XMLReader xr = sp.getXMLReader();              XMLReader xr = sp.getXMLReader();
70    
71                          xr.setContentHandler(this);              DepartureParser departureParser = new DepartureParser();
72                          xr.setErrorHandler(this);                          xr.setContentHandler(departureParser);
73                            xr.setErrorHandler(departureParser);
74                          xr.parse(source);                          xr.parse(source);
75                            
76                            return departureParser.getDepartures();
77                            
78                  } catch (Exception e) {                  } catch (Exception e) {
79                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
80                  }                          return null;
81                    }              
82          }          }
83                    
84          @Override  
85          public List<DepartureBean> getDepartures() {          class DepartureParser extends DefaultHandler {
                 return departures;  
         }  
           
         // this can be called several times fore the same text-node if there are many chardata / lines  
         @Override  
         public void characters (char ch[], int start, int length)  
         {                
                 for (int i= start; i<start+length; i++)  
                         builder.append(ch[i]);    
         }  
           
         @Override  
         public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException  
         {  
                 if (name.equalsIgnoreCase("train"))  
                         tempDeparture = new DepartureBean();  
86                                    
87                  builder.setLength(0); //reset StringBuilder                  private DepartureBean departures = new DepartureBean();
88          }  
89                    public DepartureBean getDepartures() {
90                            return departures;
91                    }
92                    
93          @Override                  // this can be called several times fore the same text-node if there are many chardata / lines
94          public void endElement (String uri, String name, String qName) throws SAXException                  @Override
95          {                  public void characters (char ch[], int start, int length)
96                  if (name.equals("train")) {                  {              
97                          departures.add( tempDeparture );                          for (int i= start; i<start+length; i++)
98                  } else if (name.equals("time")) {                                  builder.append(ch[i]);  
99                          tempDeparture.setTime(builder.toString().trim());                  }
100                  } else if (name.equals("updated")) {                  
101                          tempDeparture.setLastUpdate(builder.toString().trim());                  @Override
102                  } else if (name.equals("trainnumber")) {                  public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
103                          tempDeparture.setTrainNumber(builder.toString().trim());                  {
104                  } else if (name.equals("destination")) {                          if (name.equalsIgnoreCase("train"))
105                          tempDeparture.setDestination(builder.toString().trim());                                  tempDeparture = new DepartureEntry();
106                  } else if (name.equals("origin")) {                          
107                          tempDeparture.setOrigin(builder.toString().trim());                          builder.setLength(0); //reset StringBuilder
108                  } else if (name.equals("location")) {                  }
109                          tempDeparture.setLocation(builder.toString().trim());                  
110                  } else if (name.equals("status")) {                  @Override
111                          tempDeparture.setStatus(builder.toString().trim());                  public void endElement (String uri, String name, String qName) throws SAXException
112                  } else if (name.equals("note")) {                  {
113                          tempDeparture.setNote(builder.toString().trim());                          if (name.equals("train")) {                    
114                  }                                  departures.entries.add( tempDeparture );
115                            } else if (name.equals("time")) {
116                                    tempDeparture.setTime(builder.toString().trim());
117                            } else if (name.equals("updated")) {
118                                    tempDeparture.setLastUpdate(builder.toString().trim());
119                            } else if (name.equals("trainnumber")) {
120                                    tempDeparture.setTrainNumber(builder.toString().trim());
121                            } else if (name.equals("destination")) {
122                                    tempDeparture.setDestination(builder.toString().trim());
123                            } else if (name.equals("origin")) {
124                                    tempDeparture.setOrigin(builder.toString().trim());
125                            } else if (name.equals("location")) {
126                                    tempDeparture.setLocation(builder.toString().trim());
127                            } else if (name.equals("status")) {
128                                    tempDeparture.setStatus(builder.toString().trim());
129                            } else if (name.equals("note")) {
130                                    tempDeparture.setNote(builder.toString().trim());
131                            } else if (name.equals("type")) {
132                                    tempDeparture.setType(builder.toString().trim());
133                            } else if (name.equals("notification")) {
134                                    departures.notifications.add( builder.toString().trim() );
135                            }
136                    }
137          }          }
138  }  }

Legend:
Removed from v.315  
changed lines
  Added in v.1007

  ViewVC Help
Powered by ViewVC 1.1.20