package com.camilstaps.taize; import org.json.JSONException; import org.json.JSONObject; import java.text.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.camilstaps.common.Date; /** * Created by camilstaps on 7-4-15. */ public class DailyReading { private final Date date; private final String text; public DailyReading(Date date, String text) { this.date = date; this.text = text; } public String getText() { return text; } public Date getDate() { return date; } public String getTextWithoutReference() { Matcher bible_ref_m = referenceMatcher(); if (bible_ref_m.find()) { return text.substring(0, bible_ref_m.start() - 1); } else { return text; } } @Override public String toString() { JSONObject json = new JSONObject(); try { json.put("date", date.toString()); json.put("text", text); } catch (JSONException e) {} return json.toString(); } public static DailyReading fromString(String s) throws JSONException, ParseException { JSONObject json = new JSONObject(s); return new DailyReading(new Date(json.getString("date")), json.getString("text")); } public BibleText getBibleReference() { final Matcher bible_ref_m = referenceMatcher(); if (!bible_ref_m.find()) { return null; } return new BibleText(bible_ref_m.group(1), Integer.parseInt(bible_ref_m.group(2)), Integer.parseInt(bible_ref_m.group(3)), Integer.parseInt(bible_ref_m.group(2)), Integer.parseInt(bible_ref_m.group(4))); } private Matcher referenceMatcher() { Pattern bible_ref = Pattern.compile("\\((\\d? ?[a-zA-Z]+) (\\d{1,2})(?::|,)(\\d+)-?(\\d+?)\\)"); return bible_ref.matcher(text); } }