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

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

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

revision 1065 by torben, Tue Sep 14 16:00:57 2010 UTC revision 1066 by torben, Thu Sep 16 15:32:42 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.StringReader;  import org.simpleframework.xml.Serializer;
4    import org.simpleframework.xml.core.Persister;
 import javax.xml.parsers.SAXParser;  
 import javax.xml.parsers.SAXParserFactory;  
   
 import org.xml.sax.Attributes;  
 import org.xml.sax.InputSource;  
 import org.xml.sax.SAXException;  
 import org.xml.sax.XMLReader;  
 import org.xml.sax.helpers.DefaultHandler;  
5    
6  import android.util.Log;  import android.util.Log;
7    import dk.thoerup.android.traininfo.common.MetroBean;
8  import dk.thoerup.traininfo.util.AndroidTimeoutCache;  import dk.thoerup.traininfo.util.AndroidTimeoutCache;
9  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
10  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
# Line 52  public class XmlMetroProvider implements Line 44  public class XmlMetroProvider implements
44                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
45                          String doc =  DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
46                                                    
47                          InputSource source = new InputSource( new StringReader(doc));                          Serializer serializer = new Persister();
48                            
49                          SAXParserFactory spf = SAXParserFactory.newInstance();                          MetroBean metro = serializer.read(MetroBean.class, doc);
             SAXParser sp = spf.newSAXParser();  
             XMLReader xr = sp.getXMLReader();  
   
             MetroParser metroParser = new MetroParser();  
                         xr.setContentHandler(metroParser);  
                         xr.setErrorHandler(metroParser);  
                         xr.parse(source);  
                           
                         return metroParser.getMetroDepartures();  
50                                                    
51                            return metro;
52    
53                                                    
54                  } catch (Exception e) {                  } catch (Exception e) {
55                          Log.e("XmlMetroProvider", "lookupFunction", e);                          Log.e("XmlMetroProvider", "lookupFunction", e);
56                          return null;                          return null;
57                  }                                }              
58          }          }
           
           
         class MetroParser extends DefaultHandler {  
59                                    
                 StringBuilder builder = new StringBuilder(256);  
                 private MetroBean metro = new MetroBean();  
                   
                 MetroEntry tempEntry = new MetroEntry();  
   
                 public MetroBean getMetroDepartures() {  
                         return metro;  
                 }  
                                   
           
                 // 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)  
                 {                
                         builder.append(ch, start, length);  
                 }  
                   
                 @Override  
                 public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException  
                 {  
                         if (name.equalsIgnoreCase("entry"))  
                                 tempEntry = new MetroEntry();  
                           
                         builder.setLength(0); //reset StringBuilder  
                 }  
                   
                 @Override  
                 public void endElement (String uri, String name, String qName) throws SAXException  
                 {  
                         String str = builder.toString().trim();  
                           
                         if (name.equals("entry")) {                      
                                 metro.entries.add( tempEntry );                          
                         } else if (name.equals("head")) {  
                                 metro.head = str;                                
                         } else if (name.equals("operations")) {  
                                 metro.operationInfo = str;                                
                         } else if (name.equals("metro")) {  
                                 tempEntry.metro = str;  
                         } else if (name.equals("destination")) {  
                                 tempEntry.destination = str;  
                         } else if (name.equals("minutes")) {  
                                 tempEntry.minutes = str;  
                         } else if (name.equals("plan")) {  
                                 metro.plan = str;  
                         }  
                 }  
         }  
           
60  }  }

Legend:
Removed from v.1065  
changed lines
  Added in v.1066

  ViewVC Help
Powered by ViewVC 1.1.20