1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.book.readings; |
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Date; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Locale; |
28 | |
import java.util.Map; |
29 | |
import java.util.MissingResourceException; |
30 | |
import java.util.ResourceBundle; |
31 | |
import java.util.TreeMap; |
32 | |
|
33 | |
import org.crosswire.common.util.CWClassLoader; |
34 | |
import org.crosswire.common.util.Language; |
35 | |
import org.crosswire.jsword.JSMsg; |
36 | |
import org.crosswire.jsword.JSOtherMsg; |
37 | |
import org.crosswire.jsword.book.BookCategory; |
38 | |
import org.crosswire.jsword.book.BookException; |
39 | |
import org.crosswire.jsword.book.FeatureType; |
40 | |
import org.crosswire.jsword.book.OSISUtil; |
41 | |
import org.crosswire.jsword.book.basic.AbstractBook; |
42 | |
import org.crosswire.jsword.book.basic.DefaultBookMetaData; |
43 | |
import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor; |
44 | |
import org.crosswire.jsword.internationalisation.LocaleProviderManager; |
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.passage.VerseRange; |
54 | |
import org.crosswire.jsword.versification.system.Versifications; |
55 | |
import org.jdom2.Content; |
56 | |
import org.jdom2.Element; |
57 | |
import org.slf4j.Logger; |
58 | |
import org.slf4j.LoggerFactory; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public class ReadingsBook extends AbstractBook implements PreferredKey { |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public ReadingsBook(ReadingsBookDriver driver, String setname, BookCategory type) { |
74 | 0 | super(null, null); |
75 | |
|
76 | 0 | hash = new TreeMap<Key, String>(); |
77 | |
|
78 | |
|
79 | |
|
80 | 0 | Locale defaultLocale = LocaleProviderManager.getLocale(); |
81 | 0 | ResourceBundle prop = ResourceBundle.getBundle(setname, defaultLocale, CWClassLoader.instance(ReadingsBookDriver.class)); |
82 | |
|
83 | |
|
84 | 0 | String name = JSMsg.gettext("Readings"); |
85 | |
try { |
86 | 0 | name = prop.getString("title"); |
87 | 0 | } catch (MissingResourceException e) { |
88 | 0 | log.warn("Missing resource: 'title' while parsing: {}", setname); |
89 | 0 | } |
90 | |
|
91 | 0 | DefaultBookMetaData bmd = new DefaultBookMetaData(driver, name, type); |
92 | 0 | bmd.setInitials(setname); |
93 | 0 | bmd.setLanguage(new Language(defaultLocale.getLanguage())); |
94 | 0 | setBookMetaData(bmd); |
95 | |
|
96 | |
|
97 | 0 | java.util.Calendar greg = new java.util.GregorianCalendar(); |
98 | 0 | greg.set(java.util.Calendar.DAY_OF_MONTH, 1); |
99 | 0 | greg.set(java.util.Calendar.MONDAY, java.util.Calendar.JANUARY); |
100 | 0 | int currentYear = greg.get(java.util.Calendar.YEAR); |
101 | |
|
102 | 0 | while (greg.get(java.util.Calendar.YEAR) == currentYear) { |
103 | 0 | String internalKey = ReadingsKey.external2internal(greg); |
104 | 0 | String readings = ""; |
105 | |
|
106 | |
try { |
107 | 0 | readings = prop.getString(internalKey); |
108 | 0 | hash.put(new ReadingsKey(greg.getTime()), readings); |
109 | 0 | } catch (MissingResourceException e) { |
110 | 0 | log.warn("Missing resource: {} while parsing: {}", internalKey, setname); |
111 | 0 | } |
112 | |
|
113 | 0 | greg.add(java.util.Calendar.DATE, 1); |
114 | 0 | } |
115 | |
|
116 | 0 | global = new SetKeyList(hash.keySet(), getName()); |
117 | 0 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public Key getPreferred() { |
123 | 0 | return new ReadingsKey(new Date()); |
124 | |
} |
125 | |
|
126 | |
public Iterator<Content> getOsisIterator(final Key key, final boolean allowEmpty, final boolean allowGenTitles) throws BookException { |
127 | 0 | if (!(key instanceof ReadingsKey)) { |
128 | |
|
129 | 0 | throw new BookException(JSMsg.gettext("Key not found {0}", key.getName())); |
130 | |
} |
131 | |
|
132 | |
|
133 | 0 | List<Content> content = new ArrayList<Content>(); |
134 | |
|
135 | 0 | Element title = OSISUtil.factory().createTitle(); |
136 | 0 | title.addContent(key.getName()); |
137 | 0 | content.add(title); |
138 | |
|
139 | 0 | String readings = hash.get(key); |
140 | 0 | if (readings == null) { |
141 | |
|
142 | 0 | throw new BookException(JSMsg.gettext("Key not found {0}", key.getName())); |
143 | |
} |
144 | |
|
145 | |
try { |
146 | |
|
147 | |
|
148 | |
|
149 | 0 | PassageKeyFactory keyf = PassageKeyFactory.instance(); |
150 | 0 | Passage ref = keyf.getKey(Versifications.instance().getVersification("KJV"), readings); |
151 | |
|
152 | 0 | Element list = OSISUtil.factory().createList(); |
153 | 0 | content.add(list); |
154 | |
|
155 | 0 | Iterator<VerseRange> it = ref.rangeIterator(RestrictionType.NONE); |
156 | 0 | while (it.hasNext()) { |
157 | 0 | Key range = it.next(); |
158 | |
|
159 | 0 | Element reading = OSISUtil.factory().createReference(); |
160 | 0 | reading.setAttribute(OSISUtil.OSIS_ATTR_REF, range.getOsisRef()); |
161 | 0 | reading.addContent(range.getName()); |
162 | |
|
163 | 0 | Element item = OSISUtil.factory().createItem(); |
164 | 0 | item.addContent(reading); |
165 | 0 | list.addContent(item); |
166 | 0 | } |
167 | 0 | } catch (NoSuchKeyException ex) { |
168 | 0 | content.add(OSISUtil.factory().createText(JSOtherMsg.lookupText("Failed to parse {0}", readings))); |
169 | 0 | } |
170 | |
|
171 | 0 | return content.iterator(); |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public boolean contains(Key key) { |
178 | 0 | return false; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public String getRawText(Key key) throws BookException { |
186 | 0 | return ""; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
@Override |
193 | |
public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException { |
194 | 0 | return Collections.emptyList(); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public boolean isWritable() { |
201 | 0 | return false; |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public void setRawText(Key key, String rawData) throws BookException { |
208 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public void setAliasKey(Key alias, Key source) throws BookException { |
215 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public Key getValidKey(String name) { |
222 | |
try { |
223 | 0 | return getKey(name); |
224 | 0 | } catch (NoSuchKeyException e) { |
225 | 0 | return createEmptyKeyList(); |
226 | |
} |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public Key getKey(String name) throws NoSuchKeyException { |
233 | 0 | DefaultKeyList reply = new DefaultKeyList(); |
234 | 0 | reply.addAll(new ReadingsKey(name, name, global)); |
235 | 0 | return reply; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public Key getGlobalKeyList() { |
242 | 0 | return global; |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
public Key createEmptyKeyList() { |
249 | 0 | return new DefaultKeyList(); |
250 | |
} |
251 | |
|
252 | |
@Override |
253 | |
public boolean hasFeature(FeatureType feature) { |
254 | 0 | return feature == FeatureType.DAILY_DEVOTIONS; |
255 | |
} |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
private Key global; |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
private Map<Key, String> hash; |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | 0 | private static final Logger log = LoggerFactory.getLogger(ReadingsBook.class); |
271 | |
} |