예제 #1
0
  /**
   * This method takes a single Article Object and converts it in to a single JSON object including
   * some additional HTML formating so they can be displayed nicely
   *
   * @param article
   * @param current
   * @throws JSONException
   */
  private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
    String title = article.getTitle();
    String description =
        article.getDescription().replaceAll("\\s+", " ").replaceAll("\\<[^>]*>", "");
    String date = article.getPubDate();

    if (date != null && date.length() > 0) {
      DateFormat[] rssFormatters = {
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.UK),
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.UK)
      };

      DateFormat bulFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US);

      for (DateFormat rssFormatter : rssFormatters) {
        try {
          Date realDate = rssFormatter.parse(date);

          String bulDate = bulFormatter.format(realDate);

          date = bulDate;

          break;
        } catch (ParseException e) {
          // Can't parse, stick to the original.
          Log.e("DATE PARSER", e.getMessage());
        }
      }
    }

    int tagPos = description.indexOf("<");

    if (tagPos > 0) {
      description = description.substring(0, tagPos) + '.';
    }

    StringBuffer sb = new StringBuffer();
    sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
    sb.append(PARAGRAPH);
    sb.append(description);
    sb.append(PARAGRAPH);
    sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);

    current.put("text", Html.fromHtml(sb.toString()));
    current.put("title", BOLD_OPEN + title + BOLD_CLOSE);

    String urlString = null;

    if (article.getUrl() == null) {
      urlString = "";
    } else {
      urlString = article.getUrl().toString();
    }

    current.put("url", urlString);
  }