package com.camilstaps.taize; import org.json.JSONException; import java.text.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.camilstaps.common.Date; import com.camilstaps.common.DatedString; /** * A daily reading is linked to a Date * @author Camil Staps */ public class DailyReading extends DatedString { public DailyReading(Date date, String string) { super(date, string); } /** * Get the text without the Bible reference on the end * @return */ public String getTextWithoutReference() { Matcher bible_ref_m = referenceMatcher(); if (bible_ref_m.find()) { return string.substring(0, bible_ref_m.start() - 1); } else { return string; } } /** * Get the Bible reference on the end * @return */ public BibleText getBibleReference() { final Matcher bible_ref_m = referenceMatcher(); if (!bible_ref_m.find()) { return null; } if (bible_ref_m.group(3) == null) { return new BibleText(bible_ref_m.group(1), Integer.parseInt(bible_ref_m.group(2))); } else { 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))); } } /** * A regex matcher to find the Bible reference * @return */ private Matcher referenceMatcher() { Pattern bible_ref = Pattern.compile("\\((\\d? ?[a-zA-Z]+) (\\d{1,3})(?::|,)?(\\d+)?-?(\\d+?)?\\)"); return bible_ref.matcher(string); } /** * Parse a JSON-encoded DailyReading to a DailyReading * @param s the JSON-encoded dailyReading * @return * @throws JSONException if the string is no valid JSON * @throws ParseException if the date in the JSON is no valid Date */ public static DailyMeditation fromString(String s) throws JSONException, ParseException { return (DailyMeditation) DatedString.fromString(s, DailyMeditation.class); } }