GsonAwareParser.java

  1. /**
  2.  * Copyright 2012 the original author or authors
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *         http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.googlecode.phisix.api.parser;

  17. import java.io.Reader;
  18. import java.lang.reflect.Type;
  19. import java.text.ParseException;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.TimeZone;

  23. import org.apache.commons.lang3.time.DateParser;
  24. import org.apache.commons.lang3.time.FastDateFormat;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. import com.google.gson.Gson;
  28. import com.google.gson.GsonBuilder;
  29. import com.google.gson.JsonArray;
  30. import com.google.gson.JsonElement;
  31. import com.google.gson.JsonNull;
  32. import com.google.gson.JsonObject;
  33. import com.google.gson.JsonParser;
  34. import com.google.gson.reflect.TypeToken;
  35. import com.googlecode.phisix.api.model.Stock;
  36. import com.googlecode.phisix.api.model.Stocks;

  37. /**
  38.  * {@link Parser} that delegates to a {@link JsonParser}.
  39.  *
  40.  * @author Edge Dalmacio
  41.  *
  42.  */
  43. public class GsonAwareParser implements Parser<Reader, Stocks> {

  44.     private static final Logger LOGGER = LoggerFactory.getLogger(GsonAwareParser.class);
  45.     private static final TimeZone ASIA_MANILA = TimeZone.getTimeZone("Asia/Manila");
  46.     private static final DateParser dateParser = FastDateFormat.getInstance("MM/dd/yyyy hh:mm a", ASIA_MANILA);
  47.     private final Gson gson;
  48.    
  49.     public GsonAwareParser() {
  50.         Type type = new TypeToken<Stock>() {}.getType();
  51.         gson = new GsonBuilder()
  52.             .registerTypeAdapter(type, new PhisixDeserializer())
  53.             .create();
  54.     }

  55.     @Override
  56.     public Stocks parse(Reader source) {
  57.         Stocks stocks = new Stocks();
  58.        
  59.         JsonElement parse = JsonParser.parseReader(source);
  60.         if (JsonNull.INSTANCE.equals(parse)) {
  61.             return stocks;
  62.         }
  63.         JsonArray jsonArray = parse.getAsJsonArray();
  64.         Type type = new TypeToken<Stock>() {}.getType();
  65.        
  66. //      boolean isFirst = true;
  67.         for (JsonElement jsonElement : jsonArray) {
  68. //          if (isFirst) {
  69. //              isFirst = !isFirst;
  70. //              stocks.setAsOf(parseAsOfDate(jsonElement.getAsJsonObject()));
  71. //              continue;
  72. //          }
  73.             Stock stock = gson.fromJson(jsonElement, type);
  74.             if (stock != null) {
  75.                 stocks.getStocks().add(stock);
  76.             }
  77.         }
  78.         if (stocks.getAsOf() == null) {
  79.             Calendar calendar = Calendar.getInstance(ASIA_MANILA);
  80.             calendar.set(Calendar.HOUR_OF_DAY, 0);
  81.             calendar.set(Calendar.MINUTE, 0);
  82.             calendar.set(Calendar.SECOND, 0);
  83.             calendar.set(Calendar.MILLISECOND, 0);
  84.             stocks.setAsOf(calendar);
  85.         }
  86.        
  87.         return stocks;
  88.     }
  89.    
  90.     protected Calendar parseAsOfDate(JsonObject jsonObject) {
  91.         String asOfDate = jsonObject.get("securityAlias").getAsString();
  92.         Calendar calendar = null;
  93.         try {
  94.             Date date = dateParser.parse(asOfDate);
  95.             calendar = Calendar.getInstance(ASIA_MANILA);
  96.             calendar.setTime(date);
  97.         } catch (ParseException e) {
  98.             if (LOGGER.isWarnEnabled()) {
  99.                 LOGGER.warn(e.getMessage(), e);
  100.             }
  101.         }
  102.         return calendar;
  103.     }

  104. }