1
22 package org.crosswire.jsword.book.readings;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.MissingResourceException;
31 import java.util.ResourceBundle;
32 import java.util.TreeMap;
33
34 import org.crosswire.common.util.CWClassLoader;
35 import org.crosswire.common.util.Language;
36 import org.crosswire.common.util.Logger;
37 import org.crosswire.jsword.JSMsg;
38 import org.crosswire.jsword.JSOtherMsg;
39 import org.crosswire.jsword.book.BookCategory;
40 import org.crosswire.jsword.book.BookException;
41 import org.crosswire.jsword.book.FeatureType;
42 import org.crosswire.jsword.book.OSISUtil;
43 import org.crosswire.jsword.book.basic.AbstractBook;
44 import org.crosswire.jsword.book.basic.DefaultBookMetaData;
45 import org.crosswire.jsword.passage.DefaultKeyList;
46 import org.crosswire.jsword.passage.Key;
47 import org.crosswire.jsword.passage.NoSuchKeyException;
48 import org.crosswire.jsword.passage.Passage;
49 import org.crosswire.jsword.passage.PassageKeyFactory;
50 import org.crosswire.jsword.passage.PreferredKey;
51 import org.crosswire.jsword.passage.RestrictionType;
52 import org.crosswire.jsword.passage.SetKeyList;
53 import org.crosswire.jsword.versification.system.Versifications;
54 import org.jdom.Content;
55 import org.jdom.Element;
56
57
64 public class ReadingsBook extends AbstractBook implements PreferredKey {
65
68 public ReadingsBook(ReadingsBookDriver driver, String setname, BookCategory type) {
69 super(null);
71 hash = new TreeMap<Key, String>();
72
73 Locale defaultLocale = Locale.getDefault();
74 ResourceBundle prop = ResourceBundle.getBundle(setname, defaultLocale, CWClassLoader.instance(ReadingsBookDriver.class));
75
76 String name = JSMsg.gettext("Readings");
78 try {
79 name = prop.getString("title");
80 } catch (MissingResourceException e) {
81 log.warn("Missing resource: 'title' while parsing: " + setname);
82 }
83
84 DefaultBookMetaData bmd = new DefaultBookMetaData(driver, name, type);
85 bmd.setInitials(setname);
86 bmd.setLanguage(new Language(Locale.getDefault().getLanguage()));
87 setBookMetaData(bmd);
88
89 java.util.Calendar greg = new java.util.GregorianCalendar();
91 greg.set(java.util.Calendar.DAY_OF_MONTH, 1);
92 greg.set(java.util.Calendar.MONDAY, java.util.Calendar.JANUARY);
93 int currentYear = greg.get(java.util.Calendar.YEAR);
94
95 while (greg.get(java.util.Calendar.YEAR) == currentYear) {
96 String internalKey = ReadingsKey.external2internal(greg);
97 String readings = "";
98
99 try {
100 readings = prop.getString(internalKey);
101 hash.put(new ReadingsKey(greg.getTime()), readings);
102 } catch (MissingResourceException e) {
103 log.warn("Missing resource: " + internalKey + " while parsing: " + setname);
104 }
105
106 greg.add(java.util.Calendar.DATE, 1);
107 }
108
109 global = new SetKeyList(hash.keySet(), getName());
110 }
111
112
115 public Key getPreferred() {
116 return new ReadingsKey(new Date());
117 }
118
119 public Iterator<Content> getOsisIterator(Key key, boolean allowEmpty) throws BookException {
120 if (!(key instanceof ReadingsKey)) {
121 throw new BookException(JSMsg.gettext("Key not found {0}", key.getName()));
123 }
124
125 List<Content> content = new ArrayList<Content>();
127
128 Element title = OSISUtil.factory().createTitle();
129 title.addContent(key.getName());
130 content.add(title);
131
132 String readings = hash.get(key);
133 if (readings == null) {
134 throw new BookException(JSMsg.gettext("Key not found {0}", key.getName()));
136 }
137
138 try {
139 PassageKeyFactory keyf = PassageKeyFactory.instance();
141 Passage ref = (Passage) keyf.getKey(Versifications.instance().getDefaultVersification(), readings);
142
143 Element list = OSISUtil.factory().createList();
144 content.add(list);
145
146 Iterator<Key> it = ref.rangeIterator(RestrictionType.NONE);
147 while (it.hasNext()) {
148 Key range = it.next();
149
150 Element reading = OSISUtil.factory().createReference();
151 reading.setAttribute(OSISUtil.OSIS_ATTR_REF, range.getOsisRef());
152 reading.addContent(range.getName());
153
154 Element item = OSISUtil.factory().createItem();
155 item.addContent(reading);
156 list.addContent(item);
157 }
158 } catch (NoSuchKeyException ex) {
159 content.add(OSISUtil.factory().createText(JSOtherMsg.lookupText("Failed to parse {0}", readings)));
160 }
161
162 return content.iterator();
163 }
164
165
168 public boolean contains(Key key) {
169 return false;
170 }
171
172
175 public String getRawText(Key key) throws BookException {
176 return "";
177 }
178
179
182 public boolean isWritable() {
183 return false;
184 }
185
186
189 public void setRawText(Key key, String rawData) throws BookException {
190 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
191 }
192
193
196 public void setAliasKey(Key alias, Key source) throws BookException {
197 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
198 }
199
200
203 public Key getValidKey(String name) {
204 try {
205 return getKey(name);
206 } catch (NoSuchKeyException e) {
207 return createEmptyKeyList();
208 }
209 }
210
211
214 public Key getKey(String name) throws NoSuchKeyException {
215 DefaultKeyList reply = new DefaultKeyList();
216 reply.addAll(new ReadingsKey(name, name, global));
217 return reply;
218 }
219
220
223 public Key getGlobalKeyList() {
224 return global;
225 }
226
227
230 public Key createEmptyKeyList() {
231 return new DefaultKeyList();
232 }
233
234 @Override
235 public boolean hasFeature(FeatureType feature) {
236 return feature == FeatureType.DAILY_DEVOTIONS;
237 }
238
239
242 private Key global;
243
244
247 private Map<Key, String> hash;
248
249
252 private static final Logger log = Logger.getLogger(ReadingsBook.class);
253 }
254