Android Open Source - bike-friend You Bike Station Parser






From Project

Back to project page bike-friend.

License

The source code is released under:

GNU General Public License

If you think the Android project bike-friend listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lemoulinstudio.bikefriend.webapp.ubike;
//  ww  w.ja v  a  2s .  c  om
import com.lemoulinstudio.bikefriend.webapp.StationParser;
import com.lemoulinstudio.bikefriend.webapp.entity.Station;
import com.lemoulinstudio.bikefriend.webapp.entity.StationLog;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

/**
 *
 * @author Vincent Cantin
 */
@Component
public class YouBikeStationParser implements StationParser {
  
  @Autowired
  private MongoTemplate db;
  
  private final URI uri;
  
  // Helpful for parsing the date.
  private TimeZone taiwanTimeZone;
  private SimpleDateFormat dateFormat;
  private int thisYear;
  private long now;
  
  public YouBikeStationParser() throws Exception {
    this.uri = new URI("http://www.youbike.com.tw/genxml.php");
    
    this.taiwanTimeZone = TimeZone.getTimeZone("Asia/Taipei");
    this.dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    this.dateFormat.setTimeZone(taiwanTimeZone);
  }
  
  @Override
  public URI getDataSourceUri() {
    return uri;
  }

  @Override
  public void parseAndLogData(InputStream in) throws Exception {
    this.thisYear = new GregorianCalendar(taiwanTimeZone).get(Calendar.YEAR);
    this.now = new Date().getTime();
    
    JAXBContext context = JAXBContext.newInstance(MarkerList.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Reader reader = new InputStreamReader(in, Charset.forName("UTF-8"));
    MarkerList markerList = (MarkerList) unmarshaller.unmarshal(reader);
    
    for (Marker marker : markerList.elements) {
      try {
        double[] location = new double[] {marker.latitude, marker.longitude};

        Station station = db.findOne(new Query(Criteria
                .where("provider").is(Station.Provider.YouBike)
                .and("location").is(location)),
                Station.class);

        // If the station doesn't exist, we create and save it.
        if (station == null) {
          Map<String, Station.Info> languageToInfo = new HashMap<>();
          languageToInfo.put("zh", new Station.Info(
                  marker.chineseName,
                  marker.chineseAddress,
                  null));
          languageToInfo.put("en", new Station.Info(
                  marker.englishName,
                  marker.englishAddress,
                  null));

          station = new Station(Station.Provider.YouBike, languageToInfo, location, marker.iconType == 1);

          db.insert(station);
        }

        db.insert(new StationLog(station.getId(), parseDate(marker.mday), marker.nbBikes, marker.nbEmptySlots));
      }
      catch (Exception e) {
        // We got a problem with the date parsing.
      }
    }
  }
  
  private long parseDate(String text) throws Exception {
    // Because the year is missing from the data from YouBike,
    // we assume that the year is this year.
    long date = dateFormat.parse("" + thisYear + "/" + text).getTime();
    
    if (date <= now + 60 * 1000) {
      return date;
    }

    // Handles the case where the request was made on the evening of a 31th december,
    // right before midnight. Also rejects data which is more than 7 days old.
    date = dateFormat.parse("" + (thisYear - 1) + "/" + text).getTime();
    if ((date >= now - 7 * 24 * 60 * 60 * 1000) && (date <= now + 60 * 1000)) {
      return date;
    }
    else {
      // This date doesn't seem to make sense, or is too old to be useful.
      throw new Exception();
    }
  }

}




Java Source Code List

com.lemoulinstudio.bikefriend.InternetStationProvider.java
com.lemoulinstudio.bikefriend.ParsingException.java
com.lemoulinstudio.bikefriend.StationInfoWindowAdapter.java
com.lemoulinstudio.bikefriend.StationMapActivity.java
com.lemoulinstudio.bikefriend.StationParser.java
com.lemoulinstudio.bikefriend.StationProvider.java
com.lemoulinstudio.bikefriend.Station.java
com.lemoulinstudio.bikefriend.Utils.java
com.lemoulinstudio.bikefriend.cbike.CBikeStationXmlParserV1.java
com.lemoulinstudio.bikefriend.cbike.CBikeStation.java
com.lemoulinstudio.bikefriend.cbike.KaohsiungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.ChanghuaStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaichungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaipeiStationProvider.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationCSVParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationHtmlParserV2.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationJsonParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStation.java
com.lemoulinstudio.bikefriend.webapp.StationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStationData.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStation.java
com.lemoulinstudio.bikefriend.webapp.cbike.CBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.XmlStation.java
com.lemoulinstudio.bikefriend.webapp.conf.ShutdownHook.java
com.lemoulinstudio.bikefriend.webapp.conf.StartupHook.java
com.lemoulinstudio.bikefriend.webapp.entity.StationLog.java
com.lemoulinstudio.bikefriend.webapp.entity.Station.java
com.lemoulinstudio.bikefriend.webapp.io.InputStreamSequence.java
com.lemoulinstudio.bikefriend.webapp.io.IntArrayInputStream.java
com.lemoulinstudio.bikefriend.webapp.quartz.LoadStationDataJob.java
com.lemoulinstudio.bikefriend.webapp.rest.StationResource.java
com.lemoulinstudio.bikefriend.webapp.ubike.MarkerList.java
com.lemoulinstudio.bikefriend.webapp.ubike.Marker.java
com.lemoulinstudio.bikefriend.webapp.ubike.YouBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.vo.StationLogVo.java
com.lemoulinstudio.bikefriend.webapp.vo.StationVo.java