| 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.List; |
| 23 | |
|
| 24 | |
import org.crosswire.jsword.JSOtherMsg; |
| 25 | |
import org.crosswire.jsword.book.BookException; |
| 26 | |
import org.crosswire.jsword.book.BookMetaData; |
| 27 | |
import org.crosswire.jsword.book.OSISUtil; |
| 28 | |
import org.crosswire.jsword.book.basic.AbstractPassageBook; |
| 29 | |
import org.crosswire.jsword.book.filter.SourceFilter; |
| 30 | |
import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor; |
| 31 | |
import org.crosswire.jsword.passage.Key; |
| 32 | |
import org.crosswire.jsword.passage.KeyUtil; |
| 33 | |
import org.crosswire.jsword.passage.NoSuchKeyException; |
| 34 | |
import org.crosswire.jsword.passage.PassageKeyFactory; |
| 35 | |
import org.crosswire.jsword.passage.VerseKey; |
| 36 | |
import org.crosswire.jsword.versification.Versification; |
| 37 | |
import org.jdom2.Attribute; |
| 38 | |
import org.jdom2.Content; |
| 39 | |
import org.jdom2.Element; |
| 40 | |
import org.slf4j.Logger; |
| 41 | |
import org.slf4j.LoggerFactory; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | public class SwordBook extends AbstractPassageBook { |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public SwordBook(SwordBookMetaData sbmd, Backend<?> backend) { |
| 57 | 0 | super(sbmd, backend); |
| 58 | |
|
| 59 | 0 | this.filter = sbmd.getFilter(); |
| 60 | |
|
| 61 | 0 | if (backend == null) { |
| 62 | 0 | throw new IllegalArgumentException("AbstractBackend must not be null."); |
| 63 | |
} |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public final Key getGlobalKeyList() { |
| 70 | 0 | if (global == null) { |
| 71 | |
try { |
| 72 | 0 | global = getBackend().getGlobalKeyList(); |
| 73 | 0 | return global; |
| 74 | 0 | } catch (UnsupportedOperationException ex) { |
| 75 | |
|
| 76 | 0 | log.debug(ex.getMessage()); |
| 77 | 0 | } catch (BookException ex) { |
| 78 | |
|
| 79 | |
|
| 80 | 0 | log.debug(ex.getMessage()); |
| 81 | 0 | } |
| 82 | |
|
| 83 | 0 | Versification v11n = super.getVersification(); |
| 84 | |
|
| 85 | 0 | global = super.createEmptyKeyList(); |
| 86 | 0 | Key all = PassageKeyFactory.instance().getGlobalKeyList(v11n); |
| 87 | |
|
| 88 | 0 | for (Key key : all) { |
| 89 | 0 | if (contains(key)) { |
| 90 | 0 | global.addAll(key); |
| 91 | |
} |
| 92 | |
} |
| 93 | |
} |
| 94 | |
|
| 95 | 0 | return global; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public Key getScope() { |
| 103 | 0 | SwordBookMetaData sbmd = (SwordBookMetaData) getBookMetaData(); |
| 104 | |
|
| 105 | 0 | if (sbmd.getProperty(BookMetaData.KEY_VERSIFICATION) == null) { |
| 106 | |
|
| 107 | 0 | return null; |
| 108 | |
} |
| 109 | |
|
| 110 | 0 | Object keyString = sbmd.getProperty(BookMetaData.KEY_SCOPE); |
| 111 | |
|
| 112 | 0 | if (keyString != null) { |
| 113 | |
try { |
| 114 | 0 | return getKey((String) keyString); |
| 115 | 0 | } catch (NoSuchKeyException ex) { |
| 116 | |
|
| 117 | 0 | log.error("Unable to parse scope from book", ex); |
| 118 | 0 | return null; |
| 119 | |
} |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | 0 | Key bookKeys = getGlobalKeyList(); |
| 125 | |
|
| 126 | |
|
| 127 | 0 | if (!(bookKeys instanceof VerseKey)) { |
| 128 | 0 | log.error("Global key list isn't a verse key. A very expensive no-op has just occurred."); |
| 129 | 0 | return null; |
| 130 | |
} |
| 131 | |
|
| 132 | 0 | return bookKeys; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public boolean contains(Key key) { |
| 139 | 0 | return getBackend().contains(key); |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public String getRawText(Key key) throws BookException { |
| 146 | 0 | return getBackend().getRawText(key); |
| 147 | |
} |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
protected List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException { |
| 151 | 0 | List<Content> result = getBackend().readToOsis(key, processor); |
| 152 | 0 | assert result != null; |
| 153 | 0 | return result; |
| 154 | |
} |
| 155 | |
|
| 156 | |
@Override |
| 157 | |
public void addOSIS(Key key, Element div, List<Content> osisContent) { |
| 158 | |
|
| 159 | |
|
| 160 | 0 | for (Content content : osisContent) { |
| 161 | 0 | if (content instanceof Element) { |
| 162 | 0 | Element ele = (Element) content; |
| 163 | 0 | if (ele.getName().equals(OSISUtil.OSIS_ELEMENT_VERSE)) { |
| 164 | 0 | super.addOSIS(key, div, osisContent); |
| 165 | 0 | return; |
| 166 | |
} |
| 167 | 0 | } |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | 0 | if (KeyUtil.getVerse(key).getVerse() == 0) { |
| 173 | 0 | super.addOSIS(key, div, osisContent); |
| 174 | |
} else { |
| 175 | |
|
| 176 | |
|
| 177 | 0 | Element everse = OSISUtil.factory().createVerse(); |
| 178 | 0 | everse.setAttribute(OSISUtil.OSIS_ATTR_OSISID, key.getOsisID()); |
| 179 | 0 | div.addContent(everse); |
| 180 | 0 | super.addOSIS(key, everse, osisContent); |
| 181 | |
} |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
@Override |
| 185 | |
public void addOSIS(Key key, List<Content> contentList, List<Content> osisContent) { |
| 186 | |
|
| 187 | 0 | if (osisContent.size() == 0) { |
| 188 | 0 | return; |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 0 | if (KeyUtil.getVerse(key).getVerse() == 0) { |
| 194 | 0 | Element div = OSISUtil.factory().createDiv(); |
| 195 | 0 | div.setAttribute(OSISUtil.OSIS_ATTR_OSISID, key.getOsisID()); |
| 196 | |
|
| 197 | 0 | div.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.GENERATED_CONTENT); |
| 198 | |
|
| 199 | 0 | div.addContent(osisContent); |
| 200 | |
|
| 201 | 0 | contentList.add(div); |
| 202 | 0 | return; |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | 0 | int start = 0; |
| 226 | 0 | int found = -1; |
| 227 | 0 | boolean wrapped = false; |
| 228 | 0 | Element preverse = null; |
| 229 | 0 | for (Content content : osisContent) { |
| 230 | 0 | if (content instanceof Element) { |
| 231 | 0 | Element ele = (Element) content; |
| 232 | 0 | String name = ele.getName(); |
| 233 | 0 | if (OSISUtil.OSIS_ELEMENT_VERSE.equals(name)) { |
| 234 | 0 | wrapped = true; |
| 235 | 0 | continue; |
| 236 | |
} |
| 237 | 0 | Attribute typeAttr = ele.getAttribute(OSISUtil.OSIS_ATTR_TYPE); |
| 238 | 0 | Attribute subTypeAttr = ele.getAttribute(OSISUtil.OSIS_ATTR_SUBTYPE); |
| 239 | 0 | if (subTypeAttr != null && "x-preverse".equals(subTypeAttr.getValue())) { |
| 240 | 0 | if (OSISUtil.OSIS_ELEMENT_DIV.equals(name) || OSISUtil.OSIS_ELEMENT_TITLE.equals(name)) { |
| 241 | 0 | preverse = ele; |
| 242 | 0 | found = start; |
| 243 | |
} |
| 244 | 0 | } else if (typeAttr != null && "psalm".equals(typeAttr.getValue()) && OSISUtil.OSIS_ELEMENT_TITLE.equals(name)) { |
| 245 | |
|
| 246 | |
|
| 247 | 0 | Attribute canonicalAttr = ele.getAttribute(OSISUtil.OSIS_ATTR_CANONICAL); |
| 248 | 0 | if (canonicalAttr == null) { |
| 249 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_CANONICAL, "true"); |
| 250 | |
} |
| 251 | 0 | if (subTypeAttr == null) { |
| 252 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_SUBTYPE, "x-preverse"); |
| 253 | 0 | preverse = ele; |
| 254 | 0 | found = start; |
| 255 | |
} |
| 256 | |
} |
| 257 | |
} |
| 258 | 0 | start++; |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | 0 | if (wrapped) { |
| 263 | 0 | super.addOSIS(key, contentList, osisContent); |
| 264 | 0 | return; |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | 0 | Element everse = OSISUtil.factory().createVerse(); |
| 270 | 0 | everse.setAttribute(OSISUtil.OSIS_ATTR_OSISID, key.getOsisID()); |
| 271 | 0 | if (preverse == null) { |
| 272 | 0 | everse.addContent(osisContent); |
| 273 | |
} else { |
| 274 | 0 | List<Content> sublist = osisContent.subList(found + 1, osisContent.size()); |
| 275 | 0 | everse.addContent(sublist); |
| 276 | |
|
| 277 | |
|
| 278 | 0 | sublist.clear(); |
| 279 | |
|
| 280 | 0 | super.addOSIS(key, contentList, osisContent); |
| 281 | |
} |
| 282 | |
|
| 283 | 0 | contentList.add(everse); |
| 284 | 0 | } |
| 285 | |
|
| 286 | |
@Override |
| 287 | |
public boolean isWritable() { |
| 288 | 0 | return getBackend().isWritable(); |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public void setRawText(Key key, String rawData) throws BookException { |
| 295 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
| 296 | |
} |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public void setAliasKey(Key alias, Key source) throws BookException { |
| 302 | 0 | getBackend().setAliasKey(alias, source); |
| 303 | 0 | } |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
@Override |
| 309 | |
protected SourceFilter getFilter() { |
| 310 | 0 | return filter; |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
private SourceFilter filter; |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
private Key global; |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | 0 | private static final Logger log = LoggerFactory.getLogger(SwordBook.class); |
| 327 | |
} |