| 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.basic; |
| 21 | |
|
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Set; |
| 24 | |
import java.util.concurrent.CopyOnWriteArrayList; |
| 25 | |
|
| 26 | |
import org.crosswire.common.activate.Lock; |
| 27 | |
import org.crosswire.common.util.Language; |
| 28 | |
import org.crosswire.jsword.JSOtherMsg; |
| 29 | |
import org.crosswire.jsword.book.Book; |
| 30 | |
import org.crosswire.jsword.book.BookCategory; |
| 31 | |
import org.crosswire.jsword.book.BookDriver; |
| 32 | |
import org.crosswire.jsword.book.BookException; |
| 33 | |
import org.crosswire.jsword.book.BookMetaData; |
| 34 | |
import org.crosswire.jsword.book.FeatureType; |
| 35 | |
import org.crosswire.jsword.book.sword.Backend; |
| 36 | |
import org.crosswire.jsword.book.sword.processing.NoOpRawTextProcessor; |
| 37 | |
import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor; |
| 38 | |
import org.crosswire.jsword.index.IndexStatus; |
| 39 | |
import org.crosswire.jsword.index.IndexStatusEvent; |
| 40 | |
import org.crosswire.jsword.index.IndexStatusListener; |
| 41 | |
import org.crosswire.jsword.index.search.DefaultSearchRequest; |
| 42 | |
import org.crosswire.jsword.index.search.SearchRequest; |
| 43 | |
import org.crosswire.jsword.index.search.Searcher; |
| 44 | |
import org.crosswire.jsword.index.search.SearcherFactory; |
| 45 | |
import org.crosswire.jsword.passage.Key; |
| 46 | |
import org.jdom2.Content; |
| 47 | |
import org.jdom2.Document; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public abstract class AbstractBook implements Book { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 0 | public AbstractBook(BookMetaData bmd, Backend backend) { |
| 65 | 0 | this.bmd = bmd; |
| 66 | 0 | this.backend = backend; |
| 67 | 0 | this.listeners = new CopyOnWriteArrayList<IndexStatusListener>(); |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public Key getScope() { |
| 74 | 0 | return getGlobalKeyList(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public final BookMetaData getBookMetaData() { |
| 81 | 0 | return bmd; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public final void setBookMetaData(BookMetaData bmd) { |
| 88 | 0 | this.bmd = bmd; |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
public final Backend getBackend() { |
| 92 | 0 | return backend; |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public void activate(Lock lock) { |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void deactivate(Lock lock) { |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public Key find(String request) throws BookException { |
| 111 | 0 | return find(new DefaultSearchRequest(request)); |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public Key find(SearchRequest request) throws BookException { |
| 118 | 0 | if (searcher == null) { |
| 119 | |
try { |
| 120 | 0 | searcher = SearcherFactory.createSearcher(this); |
| 121 | 0 | } catch (InstantiationException ex) { |
| 122 | 0 | throw new BookException(JSOtherMsg.lookupText("Failed to initialize the search index"), ex); |
| 123 | 0 | } |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | return searcher.search(request); |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public Book getBook() { |
| 135 | 0 | return this; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public BookDriver getDriver() { |
| 142 | 0 | return bmd == null ? null : bmd.getDriver(); |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public String getDriverName() { |
| 149 | 0 | return bmd == null ? null : bmd.getDriverName(); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public boolean match(String name) { |
| 156 | 0 | if (name == null) { |
| 157 | 0 | return false; |
| 158 | |
} |
| 159 | 0 | if (name.equals(getInitials())) { |
| 160 | 0 | return true; |
| 161 | |
} |
| 162 | 0 | if (name.equals(getName())) { |
| 163 | 0 | return true; |
| 164 | |
} |
| 165 | 0 | if (name.equalsIgnoreCase(getInitials())) { |
| 166 | 0 | return true; |
| 167 | |
} |
| 168 | 0 | if (name.equalsIgnoreCase(getName())) { |
| 169 | 0 | return true; |
| 170 | |
} |
| 171 | 0 | if (name.startsWith(getInitials())) { |
| 172 | 0 | return true; |
| 173 | |
} |
| 174 | 0 | if (name.startsWith(getName())) { |
| 175 | 0 | return true; |
| 176 | |
} |
| 177 | 0 | return false; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public IndexStatus getIndexStatus() { |
| 184 | 0 | return bmd.getIndexStatus(); |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public void setIndexStatus(IndexStatus newStatus) { |
| 191 | 0 | IndexStatus oldStatus = bmd.getIndexStatus(); |
| 192 | 0 | bmd.setIndexStatus(newStatus); |
| 193 | 0 | firePropertyChange(oldStatus, newStatus); |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public String getAbbreviation() { |
| 200 | 0 | return bmd.getAbbreviation(); |
| 201 | |
} |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public String getInitials() { |
| 207 | 0 | return bmd.getInitials(); |
| 208 | |
} |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public Language getLanguage() { |
| 214 | 0 | return bmd.getLanguage(); |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public String getName() { |
| 221 | 0 | return bmd.getName(); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public String getOsisID() { |
| 228 | 0 | return bmd.getOsisID(); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public Set<String> getPropertyKeys() { |
| 235 | 0 | return bmd.getPropertyKeys(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public String getProperty(String key) { |
| 242 | 0 | return bmd.getProperty(key); |
| 243 | |
} |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public void putProperty(String key, String value) { |
| 249 | 0 | bmd.putProperty(key, value, false); |
| 250 | 0 | } |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
public void putProperty(String key, String value, boolean forFrontend) { |
| 256 | 0 | bmd.putProperty(key, value, forFrontend); |
| 257 | 0 | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
public BookCategory getBookCategory() { |
| 263 | 0 | return bmd.getBookCategory(); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public boolean isLeftToRight() { |
| 270 | 0 | return bmd.isLeftToRight(); |
| 271 | |
} |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
public boolean isSupported() { |
| 277 | 0 | return bmd.isSupported(); |
| 278 | |
} |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public boolean isEnciphered() { |
| 284 | 0 | return bmd.isEnciphered(); |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public boolean isLocked() { |
| 291 | 0 | return bmd.isLocked(); |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
public boolean unlock(String unlockKey) { |
| 298 | 0 | boolean state = bmd.unlock(unlockKey); |
| 299 | 0 | if (state) { |
| 300 | |
|
| 301 | 0 | return isUnlockKeyValid(); |
| 302 | |
} |
| 303 | 0 | return state; |
| 304 | |
} |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
private boolean isUnlockKeyValid() { |
| 313 | |
try { |
| 314 | 0 | Key key = getGlobalKeyList(); |
| 315 | 0 | if (key == null) { |
| 316 | |
|
| 317 | 0 | return true; |
| 318 | |
} |
| 319 | |
|
| 320 | 0 | if (key.getCardinality() > 0) { |
| 321 | 0 | key = key.get(0); |
| 322 | |
} |
| 323 | |
|
| 324 | 0 | getOsis(key, new NoOpRawTextProcessor()); |
| 325 | |
|
| 326 | 0 | return true; |
| 327 | 0 | } catch (BookException ex) { |
| 328 | 0 | return false; |
| 329 | |
} |
| 330 | |
} |
| 331 | |
|
| 332 | |
protected abstract List<Content> getOsis(Key key, RawTextToXmlProcessor noOpRawTextProcessor) throws BookException; |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public String getUnlockKey() { |
| 338 | 0 | return bmd.getUnlockKey(); |
| 339 | |
} |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
public boolean isQuestionable() { |
| 345 | 0 | return bmd.isQuestionable(); |
| 346 | |
} |
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
public boolean hasFeature(FeatureType feature) { |
| 352 | 0 | return bmd.hasFeature(feature); |
| 353 | |
} |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
public void addIndexStatusListener(IndexStatusListener listener) { |
| 359 | 0 | if (listeners == null) { |
| 360 | 0 | listeners = new CopyOnWriteArrayList<IndexStatusListener>(); |
| 361 | |
} |
| 362 | 0 | listeners.add(listener); |
| 363 | 0 | } |
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
public void removeIndexStatusListener(IndexStatusListener listener) { |
| 369 | 0 | if (listeners == null) { |
| 370 | 0 | return; |
| 371 | |
} |
| 372 | |
|
| 373 | 0 | listeners.remove(listener); |
| 374 | 0 | } |
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
protected void firePropertyChange(IndexStatus oldStatus, IndexStatus newStatus) { |
| 388 | 0 | if (oldStatus != null && newStatus != null && oldStatus.equals(newStatus)) { |
| 389 | 0 | return; |
| 390 | |
} |
| 391 | |
|
| 392 | 0 | IndexStatusEvent ev = new IndexStatusEvent(this, newStatus); |
| 393 | 0 | for (IndexStatusListener listener : listeners) { |
| 394 | 0 | listener.statusChanged(ev); |
| 395 | |
} |
| 396 | 0 | } |
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
public Document toOSIS() { |
| 402 | 0 | return bmd.toOSIS(); |
| 403 | |
} |
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
@Override |
| 409 | |
public boolean equals(Object obj) { |
| 410 | |
|
| 411 | 0 | if (obj == null) { |
| 412 | 0 | return false; |
| 413 | |
} |
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | 0 | if (!obj.getClass().equals(this.getClass())) { |
| 421 | 0 | return false; |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | 0 | Book that = (Book) obj; |
| 426 | |
|
| 427 | 0 | return bmd.equals(that.getBookMetaData()); |
| 428 | |
} |
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
@Override |
| 434 | |
public int hashCode() { |
| 435 | 0 | return bmd.hashCode(); |
| 436 | |
} |
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
public int compareTo(Book obj) { |
| 442 | 0 | return this.bmd.compareTo(obj.getBookMetaData()); |
| 443 | |
} |
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
@Override |
| 449 | |
public String toString() { |
| 450 | 0 | return bmd.toString(); |
| 451 | |
} |
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
private Searcher searcher; |
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
private BookMetaData bmd; |
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
private Backend backend; |
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
private List<IndexStatusListener> listeners; |
| 473 | |
} |