| 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.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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 0 | public class SwordGenBook extends AbstractBook { |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
protected SwordGenBook(SwordBookMetaData sbmd, Backend backend) { |
| 56 | 0 | super(sbmd, backend); |
| 57 | |
|
| 58 | 0 | if (backend == null) { |
| 59 | 0 | throw new IllegalArgumentException("AbstractBackend must not be null."); |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | this.filter = sbmd.getFilter(); |
| 63 | 0 | map = null; |
| 64 | 0 | set = null; |
| 65 | 0 | global = null; |
| 66 | 0 | active = false; |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public final void activate(Lock lock) { |
| 74 | 0 | super.activate(lock); |
| 75 | |
|
| 76 | 0 | set = getBackend().readIndex(); |
| 77 | |
|
| 78 | 0 | map = new HashMap<String, Key>(); |
| 79 | 0 | for (Key key : set) { |
| 80 | 0 | map.put(key.getOsisRef(), key); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | global = new ReadOnlyKeyList(set, false); |
| 84 | |
|
| 85 | 0 | active = true; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | 0 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public final void deactivate(Lock lock) { |
| 96 | 0 | super.deactivate(lock); |
| 97 | |
|
| 98 | 0 | map = null; |
| 99 | 0 | set = null; |
| 100 | 0 | global = null; |
| 101 | |
|
| 102 | 0 | active = false; |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public Iterator<Content> getOsisIterator(Key key, final boolean allowEmpty, final boolean allowGenTitle) throws BookException { |
| 109 | 0 | checkActive(); |
| 110 | |
|
| 111 | 0 | assert key != null; |
| 112 | |
|
| 113 | 0 | return getBackend().readToOsis(key, new RawTextToXmlProcessor() { |
| 114 | |
public void preRange(VerseRange range, List<Content> partialDom) { |
| 115 | |
|
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public void postVerse(Key verse, List<Content> partialDom, String rawText) { |
| 119 | 0 | partialDom.addAll(filter.toOSIS(SwordGenBook.this, verse, rawText)); |
| 120 | 0 | } |
| 121 | |
|
| 122 | |
public void init(List<Content> partialDom) { |
| 123 | |
|
| 124 | 0 | } |
| 125 | |
}).iterator(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public String getRawText(Key key) throws BookException { |
| 132 | 0 | return getBackend().getRawText(key); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public boolean contains(Key key) { |
| 139 | 0 | return getBackend().contains(key); |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
@Override |
| 147 | |
public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException { |
| 148 | 0 | checkActive(); |
| 149 | |
|
| 150 | 0 | assert key != null; |
| 151 | |
|
| 152 | 0 | return getBackend().readToOsis(key, processor); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public boolean isWritable() { |
| 159 | 0 | return getBackend().isWritable(); |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public void setRawText(Key key, String rawData) throws BookException { |
| 166 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public void setAliasKey(Key alias, Key source) throws BookException { |
| 173 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
public Key getGlobalKeyList() { |
| 180 | 0 | checkActive(); |
| 181 | |
|
| 182 | 0 | return global; |
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public Key getValidKey(String name) { |
| 189 | |
try { |
| 190 | 0 | return getKey(name); |
| 191 | 0 | } catch (NoSuchKeyException e) { |
| 192 | 0 | return createEmptyKeyList(); |
| 193 | |
} |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public Key getKey(String text) throws NoSuchKeyException { |
| 200 | 0 | checkActive(); |
| 201 | |
|
| 202 | 0 | Key key = map.get(text); |
| 203 | 0 | if (key != null) { |
| 204 | 0 | return key; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | 0 | for (String keyName : map.keySet()) { |
| 209 | 0 | if (keyName.equalsIgnoreCase(text)) { |
| 210 | 0 | return map.get(keyName); |
| 211 | |
} |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | 0 | for (String keyName : map.keySet()) { |
| 216 | 0 | if (keyName.startsWith(text)) { |
| 217 | 0 | return map.get(keyName); |
| 218 | |
} |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | 0 | for (String keyName : map.keySet()) { |
| 223 | 0 | if (keyName.indexOf(text) != -1) { |
| 224 | 0 | return map.get(keyName); |
| 225 | |
} |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | 0 | throw new NoSuchKeyException(JSMsg.gettext("No entry for '{0}' in {1}.", text, getInitials())); |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public Key createEmptyKeyList() { |
| 239 | 0 | return new DefaultKeyList(); |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
private void checkActive() { |
| 246 | 0 | if (!active) { |
| 247 | 0 | Activator.activate(this); |
| 248 | |
} |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
private Key global; |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
private boolean active; |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
private Map<String, Key> map; |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
private Key set; |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
protected SourceFilter filter; |
| 275 | |
|
| 276 | |
} |