1
20 package org.crosswire.jsword.book.sword;
21
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.crosswire.common.activate.Activator;
28 import org.crosswire.common.activate.Lock;
29 import org.crosswire.jsword.JSMsg;
30 import org.crosswire.jsword.JSOtherMsg;
31 import org.crosswire.jsword.book.BookException;
32 import org.crosswire.jsword.book.basic.AbstractBook;
33 import org.crosswire.jsword.book.filter.SourceFilter;
34 import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor;
35 import org.crosswire.jsword.passage.DefaultKeyList;
36 import org.crosswire.jsword.passage.Key;
37 import org.crosswire.jsword.passage.NoSuchKeyException;
38 import org.crosswire.jsword.passage.ReadOnlyKeyList;
39 import org.crosswire.jsword.passage.VerseRange;
40 import org.jdom2.Content;
41
42
48 public class SwordGenBook extends AbstractBook {
49
55 protected SwordGenBook(SwordBookMetaData sbmd, Backend backend) {
56 super(sbmd, backend);
57
58 if (backend == null) {
59 throw new IllegalArgumentException("AbstractBackend must not be null.");
60 }
61
62 this.filter = sbmd.getFilter();
63 map = null;
64 set = null;
65 global = null;
66 active = false;
67 }
68
69
72 @Override
73 public final void activate(Lock lock) {
74 super.activate(lock);
75
76 set = getBackend().readIndex();
77
78 map = new HashMap<String, Key>();
79 for (Key key : set) {
80 map.put(key.getOsisRef(), key);
81 }
82
83 global = new ReadOnlyKeyList(set, false);
84
85 active = true;
86
87 }
90
91
94 @Override
95 public final void deactivate(Lock lock) {
96 super.deactivate(lock);
97
98 map = null;
99 set = null;
100 global = null;
101
102 active = false;
103 }
104
105
108 public Iterator<Content> getOsisIterator(Key key, final boolean allowEmpty, final boolean allowGenTitle) throws BookException {
109 checkActive();
110
111 assert key != null;
112
113 return getBackend().readToOsis(key, new RawTextToXmlProcessor() {
114 public void preRange(VerseRange range, List<Content> partialDom) {
115 }
117
118 public void postVerse(Key verse, List<Content> partialDom, String rawText) {
119 partialDom.addAll(filter.toOSIS(SwordGenBook.this, verse, rawText));
120 }
121
122 public void init(List<Content> partialDom) {
123 }
125 }).iterator();
126 }
127
128
131 public String getRawText(Key key) throws BookException {
132 return getBackend().getRawText(key);
133 }
134
135
138 public boolean contains(Key key) {
139 return getBackend().contains(key);
140 }
141
142
146 @Override
147 public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException {
148 checkActive();
149
150 assert key != null;
151
152 return getBackend().readToOsis(key, processor);
153 }
154
155
158 public boolean isWritable() {
159 return getBackend().isWritable();
160 }
161
162
165 public void setRawText(Key key, String rawData) throws BookException {
166 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
167 }
168
169
172 public void setAliasKey(Key alias, Key source) throws BookException {
173 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
174 }
175
176
179 public Key getGlobalKeyList() {
180 checkActive();
181
182 return global;
183 }
184
185
188 public Key getValidKey(String name) {
189 try {
190 return getKey(name);
191 } catch (NoSuchKeyException e) {
192 return createEmptyKeyList();
193 }
194 }
195
196
199 public Key getKey(String text) throws NoSuchKeyException {
200 checkActive();
201
202 Key key = map.get(text);
203 if (key != null) {
204 return key;
205 }
206
207 for (String keyName : map.keySet()) {
209 if (keyName.equalsIgnoreCase(text)) {
210 return map.get(keyName);
211 }
212 }
213
214 for (String keyName : map.keySet()) {
216 if (keyName.startsWith(text)) {
217 return map.get(keyName);
218 }
219 }
220
221 for (String keyName : map.keySet()) {
223 if (keyName.indexOf(text) != -1) {
224 return map.get(keyName);
225 }
226 }
227
228 throw new NoSuchKeyException(JSMsg.gettext("No entry for '{0}' in {1}.", text, getInitials()));
233 }
234
235
238 public Key createEmptyKeyList() {
239 return new DefaultKeyList();
240 }
241
242
245 private void checkActive() {
246 if (!active) {
247 Activator.activate(this);
248 }
249 }
250
251
254 private Key global;
255
256
259 private boolean active;
260
261
264 private Map<String, Key> map;
265
266
269 private Key set;
270
271
274 protected SourceFilter filter;
275
276 }
277