| 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.io.File; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.crosswire.common.crypt.Sapphire; |
| 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.BookMetaData; |
| 33 | |
import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor; |
| 34 | |
import org.crosswire.jsword.book.sword.state.OpenFileState; |
| 35 | |
import org.crosswire.jsword.book.sword.state.OpenFileStateManager; |
| 36 | |
import org.crosswire.jsword.passage.Key; |
| 37 | |
import org.crosswire.jsword.passage.KeyUtil; |
| 38 | |
import org.crosswire.jsword.passage.Passage; |
| 39 | |
import org.crosswire.jsword.passage.RestrictionType; |
| 40 | |
import org.crosswire.jsword.passage.Verse; |
| 41 | |
import org.crosswire.jsword.passage.VerseRange; |
| 42 | |
import org.jdom2.Content; |
| 43 | |
import org.slf4j.Logger; |
| 44 | |
import org.slf4j.LoggerFactory; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public abstract class AbstractBackend<T extends OpenFileState> implements StatefulFileBackedBackend<T>, Backend<T> { |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 0 | public AbstractBackend() { |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 0 | public AbstractBackend(SwordBookMetaData sbmd) { |
| 69 | 0 | bmd = sbmd; |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public BookMetaData getBookMetaData() { |
| 76 | 0 | return bmd; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public void decipher(byte[] data) { |
| 83 | 0 | String cipherKeyString = getBookMetaData().getProperty(SwordBookMetaData.KEY_CIPHER_KEY); |
| 84 | 0 | if (cipherKeyString != null) { |
| 85 | 0 | Sapphire cipherEngine = new Sapphire(cipherKeyString.getBytes()); |
| 86 | 0 | for (int i = 0; i < data.length; i++) { |
| 87 | 0 | data[i] = cipherEngine.cipher(data[i]); |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | cipherEngine.burn(); |
| 91 | |
} |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public void encipher(byte[] data) { |
| 98 | |
|
| 99 | 0 | decipher(data); |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public Key readIndex() { |
| 106 | |
|
| 107 | |
|
| 108 | 0 | return null; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public abstract boolean contains(Key key); |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public String getRawText(Key key) throws BookException { |
| 120 | 0 | T state = null; |
| 121 | |
try { |
| 122 | 0 | state = initState(); |
| 123 | 0 | return readRawContent(state, key); |
| 124 | 0 | } catch (IOException e) { |
| 125 | 0 | throw new BookException("Unable to obtain raw content from backend for key='" + key + '\'', e); |
| 126 | |
} finally { |
| 127 | 0 | OpenFileStateManager.instance().release(state); |
| 128 | |
} |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public void setAliasKey(Key alias, Key source) throws BookException { |
| 135 | 0 | T state = null; |
| 136 | |
try { |
| 137 | 0 | state = initState(); |
| 138 | 0 | setAliasKey(state, alias, source); |
| 139 | 0 | } catch (IOException e) { |
| 140 | 0 | throw new BookException(JSOtherMsg.lookupText("Unable to save {0}.", alias.getOsisID())); |
| 141 | |
} finally { |
| 142 | 0 | OpenFileStateManager.instance().release(state); |
| 143 | 0 | } |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public int getRawTextLength(Key key) { |
| 150 | |
try { |
| 151 | 0 | String raw = getRawText(key); |
| 152 | 0 | return raw == null ? 0 : raw.length(); |
| 153 | 0 | } catch (BookException e) { |
| 154 | 0 | return 0; |
| 155 | |
} |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public Key getGlobalKeyList() throws BookException { |
| 162 | |
|
| 163 | 0 | throw new UnsupportedOperationException("Fast global key list unsupported in this backend"); |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public List<Content> readToOsis(Key key, RawTextToXmlProcessor processor) throws BookException { |
| 173 | |
|
| 174 | 0 | final List<Content> content = new ArrayList<Content>(); |
| 175 | |
|
| 176 | 0 | T openFileState = null; |
| 177 | |
|
| 178 | |
try { |
| 179 | 0 | openFileState = initState(); |
| 180 | 0 | switch (this.bmd.getKeyType()) { |
| 181 | |
case LIST: |
| 182 | 0 | readNormalOsis(key, processor, content, openFileState); |
| 183 | 0 | break; |
| 184 | |
case TREE: |
| 185 | 0 | readNormalOsisSingleKey(key, processor, content, openFileState); |
| 186 | 0 | break; |
| 187 | |
case VERSE: |
| 188 | 0 | readPassageOsis(key, processor, content, openFileState); |
| 189 | 0 | break; |
| 190 | |
default: |
| 191 | 0 | throw new BookException("Book has unsupported type of key"); |
| 192 | |
} |
| 193 | |
|
| 194 | 0 | return content; |
| 195 | |
} finally { |
| 196 | 0 | OpenFileStateManager.instance().release(openFileState); |
| 197 | |
} |
| 198 | |
} |
| 199 | |
|
| 200 | |
private void readNormalOsis(Key key, RawTextToXmlProcessor processor, List<Content> content, T openFileState) throws BookException { |
| 201 | |
|
| 202 | 0 | Iterator<Key> iterator = key.iterator(); |
| 203 | |
|
| 204 | 0 | while (iterator.hasNext()) { |
| 205 | 0 | Key next = iterator.next(); |
| 206 | |
String rawText; |
| 207 | |
try { |
| 208 | 0 | rawText = readRawContent(openFileState, next); |
| 209 | 0 | processor.postVerse(next, content, rawText); |
| 210 | 0 | } catch (IOException e) { |
| 211 | |
|
| 212 | 0 | throwFailedKeyException(key, next, e); |
| 213 | 0 | } |
| 214 | 0 | } |
| 215 | 0 | } |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
private void readNormalOsisSingleKey(Key key, RawTextToXmlProcessor processor, List<Content> content, T openFileState) throws BookException { |
| 222 | |
String rawText; |
| 223 | |
try { |
| 224 | 0 | rawText = readRawContent(openFileState, key); |
| 225 | 0 | processor.postVerse(key, content, rawText); |
| 226 | 0 | } catch (IOException e) { |
| 227 | |
|
| 228 | 0 | throwFailedKeyException(key, key, e); |
| 229 | 0 | } |
| 230 | 0 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
private Verse readPassageOsis(Key key, RawTextToXmlProcessor processor, final List<Content> content, T openFileState) throws BookException { |
| 242 | 0 | Verse currentVerse = null; |
| 243 | 0 | final Passage ref = KeyUtil.getPassage(key); |
| 244 | 0 | final Iterator<VerseRange> rit = ref.rangeIterator(RestrictionType.CHAPTER); |
| 245 | 0 | while (rit.hasNext()) { |
| 246 | 0 | VerseRange range = rit.next(); |
| 247 | 0 | processor.preRange(range, content); |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | 0 | for (Key verseInRange : range) { |
| 253 | 0 | currentVerse = KeyUtil.getVerse(verseInRange); |
| 254 | |
try { |
| 255 | 0 | String rawText = readRawContent(openFileState, currentVerse); |
| 256 | 0 | processor.postVerse(verseInRange, content, rawText); |
| 257 | 0 | } catch (IOException e) { |
| 258 | |
|
| 259 | |
|
| 260 | 0 | LOGGER.debug(e.getMessage(), e); |
| 261 | 0 | } |
| 262 | |
} |
| 263 | 0 | } |
| 264 | |
|
| 265 | 0 | return currentVerse; |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
private void throwFailedKeyException(Key masterKey, Key currentKey, IOException e) throws BookException { |
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | 0 | if (currentKey == null) { |
| 286 | 0 | throw new BookException(JSMsg.gettext("Error reading {0}", masterKey.getName()), e); |
| 287 | |
} |
| 288 | 0 | throw new BookException(JSMsg.gettext("Error reading {0}", currentKey.getName()), e); |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public void create() throws IOException, BookException { |
| 295 | 0 | File dataPath = new File(SwordUtil.getExpandedDataPath(getBookMetaData())); |
| 296 | 0 | if (!dataPath.exists() && !dataPath.mkdirs()) { |
| 297 | 0 | throw new IOException("Unable to create module data path!"); |
| 298 | |
} |
| 299 | 0 | } |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
public boolean isSupported() { |
| 305 | 0 | return true; |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
public boolean isWritable() { |
| 312 | 0 | return false; |
| 313 | |
} |
| 314 | |
|
| 315 | |
private SwordBookMetaData bmd; |
| 316 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(AbstractBackend.class); |
| 317 | |
} |