/cityLibProject/cityLib/src/main/java/eu/ttbox/velib/service/download/cyclocity/CyclocityServiceParser.java

https://github.com/jmorille/android · Java · 139 lines · 114 code · 14 blank · 11 comment · 32 complexity · c495dc088987248a106c600a6a695e9c MD5 · raw file

  1. package eu.ttbox.velib.service.download.cyclocity;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.parsers.ParserConfigurationException;
  8. import javax.xml.parsers.SAXParser;
  9. import javax.xml.parsers.SAXParserFactory;
  10. import org.w3c.dom.Document;
  11. import org.xml.sax.SAXException;
  12. import android.content.ContentValues;
  13. import android.util.Log;
  14. import eu.ttbox.velib.model.Arrondissement;
  15. import eu.ttbox.velib.model.Station;
  16. import eu.ttbox.velib.model.VelibProvider;
  17. import eu.ttbox.velib.service.database.Velo.VeloColumns;
  18. import eu.ttbox.velib.service.download.VeloServiceParser;
  19. import eu.ttbox.velib.service.geo.GeoUtils;
  20. public class CyclocityServiceParser implements VeloServiceParser {
  21. private String TAG = getClass().getSimpleName();
  22. private void readArrondissement(ArrayList<Arrondissement> arrondissements) {
  23. if (arrondissements != null && !arrondissements.isEmpty()) {
  24. int i = 0;
  25. for (Arrondissement arrondissement : arrondissements) {
  26. if (Log.isLoggable(TAG, Log.DEBUG))
  27. Log.d(TAG, String.format("%s : %s", ++i, arrondissement));
  28. }
  29. }
  30. }
  31. @Override
  32. public ArrayList<Station> parseInputStreamForStations(InputStream in, VelibProvider provider) throws IOException{
  33. SAXParserFactory factory = SAXParserFactory.newInstance();
  34. try {
  35. SAXParser parser = factory.newSAXParser();
  36. CyclocityCartoParserXMLHandler handler = new CyclocityCartoParserXMLHandler(provider);
  37. parser.parse(in, handler);
  38. // Manage Getting data
  39. ArrayList<Arrondissement> arrondissements = handler.getDataArrondissement();
  40. readArrondissement(arrondissements);
  41. // double[] boundyBoxE6 = arrondissement.getBoundyBoxE6();
  42. double[] boundyBoxE6 = handler.getStationsBoundyBoxE6();
  43. double[] previousBox = provider.getBoundyBoxE6();
  44. boolean isRedifineBoundy = GeoUtils.isRedefineBox(boundyBoxE6, previousBox);
  45. if (isRedifineBoundy) {
  46. // provider.setBoundyBoxE6(boundyBoxE6);
  47. Log.w(TAG, "BoundyBox for VelibProvider " + provider + " is Min " + String.format("(%f, %f)", boundyBoxE6[0], boundyBoxE6[1]) + " / Max "
  48. + String.format("(%f, %f)", boundyBoxE6[2], boundyBoxE6[3]));
  49. }
  50. return handler.getData();
  51. // } catch (IOException e) {
  52. // throw new RuntimeException("IOException : " + e.getMessage(), e);
  53. } catch (SAXException e) {
  54. throw new RuntimeException("SAXException : " + e.getMessage(), e);
  55. } catch (ParserConfigurationException e) {
  56. throw new RuntimeException("ParserConfigurationException : " + e.getMessage(), e);
  57. }
  58. }
  59. @Override
  60. public Station parseInputStreamForStationDispo(InputStream content, Station station) {
  61. DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
  62. try {
  63. DocumentBuilder docBuilder = fabrique.newDocumentBuilder();
  64. Document d = docBuilder.parse(content);
  65. // NodeList rootNode = d.getElementsByTagName("station");
  66. String availableString = d.getElementsByTagName("available").item(0).getTextContent();
  67. String totalString = d.getElementsByTagName("total").item(0).getTextContent();
  68. String freeString = d.getElementsByTagName("free").item(0).getTextContent();
  69. String ticketString = d.getElementsByTagName("ticket").item(0).getTextContent();
  70. // Result
  71. if (availableString != null && availableString.length() > 0) {
  72. station.setStationCycle(Integer.valueOf(availableString).intValue());
  73. }
  74. if (totalString != null && totalString.length() > 0) {
  75. station.setVeloTotal(Integer.valueOf(totalString).intValue());
  76. }
  77. if (freeString != null && freeString.length() > 0) {
  78. station.setStationParking(Integer.valueOf(freeString).intValue());
  79. }
  80. if (ticketString != null && ticketString.length() > 0) {
  81. station.setVeloTicket(Integer.valueOf(ticketString).intValue());
  82. } else {
  83. station.setVeloTicket(0);
  84. }
  85. // Manage Version Date
  86. station.setVeloUpdated(System.currentTimeMillis());
  87. return station;
  88. } catch (Exception e) {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. public ContentValues parseInputStreamForStationDispo(InputStream content ) {
  93. DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
  94. try {
  95. DocumentBuilder docBuilder = fabrique.newDocumentBuilder();
  96. Document d = docBuilder.parse(content);
  97. // NodeList rootNode = d.getElementsByTagName("station");
  98. String availableString = d.getElementsByTagName("available").item(0).getTextContent();
  99. String totalString = d.getElementsByTagName("total").item(0).getTextContent();
  100. String freeString = d.getElementsByTagName("free").item(0).getTextContent();
  101. String ticketString = d.getElementsByTagName("ticket").item(0).getTextContent();
  102. // Result
  103. ContentValues values = new ContentValues(6);
  104. if (availableString != null && availableString.length() > 0) {
  105. values.put(VeloColumns.COL_STATION_CYCLE, Integer.valueOf(availableString));
  106. }
  107. if (totalString != null && totalString.length() > 0) {
  108. values.put(VeloColumns.COL_STATION_TOTAL, Integer.valueOf(totalString));
  109. }
  110. if (freeString != null && freeString.length() > 0) {
  111. values.put(VeloColumns.COL_STATION_PARKING, Integer.valueOf(freeString));
  112. }
  113. if (ticketString != null && ticketString.length() > 0) {
  114. values.put(VeloColumns.COL_STATION_TICKET, Integer.valueOf(ticketString));
  115. } else {
  116. values.put(VeloColumns.COL_STATION_TICKET, Integer.valueOf(0));
  117. }
  118. // Manage Version Date
  119. values.put(VeloColumns.COL_STATION_TICKET, Long.valueOf(System.currentTimeMillis()));
  120. return values;
  121. } catch (Exception e) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. }