Search in sources :

Example 46 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class JSwordRelatedVersesServiceImpl method getRelatedVerseReference.

/**
 * Gets the list of all references as a string to be passed to JSword
 *
 * @param strongs the list of all strongs
 * @param is the index searcher
 * @return the related verses
 */
private String getRelatedVerseReference(final List<String> strongs, final IndexSearcher is) {
    try {
        final BooleanQuery bq = getRelatedLuceneQuery(strongs);
        final TopScoreDocCollector collector = TopScoreDocCollector.create(50, true);
        is.search(bq, collector);
        final TopDocs topDocs = collector.topDocs();
        final ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        final StringBuilder refs = new StringBuilder(128);
        for (final ScoreDoc scoreDoc : scoreDocs) {
            final String potentialVerse = is.doc(scoreDoc.doc).get(LuceneIndex.FIELD_KEY);
            if (refs.length() > 0) {
                refs.append(' ');
            }
            refs.append(potentialVerse);
        }
        return refs.toString();
    } catch (final IOException ex) {
        throw new StepInternalException(ex.getMessage(), ex);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 47 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class AnalysisServiceImpl method getReferenceSyntax.

/**
 * Creates a lucene query to allow search for multiple chapters/entire books, without generating
 * thousands of boolean queries, because we're expanding a book into all its verses!
 *
 * @param version   the version in which look up the key
 * @param scopeType the scope type
 */
private String getReferenceSyntax(final String reference, final String version, final ScopeType scopeType) {
    final KeyWrapper key = this.jSwordPassageService.getKeyInfo(reference, version, version);
    final Key total = key.getKey();
    StringBuilder sb = new StringBuilder(32);
    switch(scopeType) {
        case PASSAGE:
        case CHAPTER:
        case NEAR_BY_CHAPTER:
            // expand all the chapters....
            int minChapter = -1;
            int maxChapter = -1;
            Verse firstVerse = null;
            Verse lastVerse;
            // need to expand between chapters....
            final Iterator<Key> iterator = total.iterator();
            Verse v = null;
            while (iterator.hasNext()) {
                final Key next = iterator.next();
                if (next instanceof Verse) {
                    v = (Verse) next;
                    if (minChapter == -1) {
                        minChapter = v.getChapter();
                        firstVerse = v;
                    }
                    int currentChapter = v.getChapter();
                    if (currentChapter != maxChapter) {
                        sb.append(v.getBook().getOSIS());
                        sb.append('.');
                        sb.append(v.getChapter());
                        sb.append(".* ");
                    }
                    maxChapter = v.getChapter();
                }
            }
            lastVerse = v;
            // need to add +1 and -1
            if (scopeType == ScopeType.NEAR_BY_CHAPTER) {
                sb.append(firstVerse.getBook().getOSIS());
                sb.append('.');
                sb.append(minChapter - 1);
                sb.append(".* ");
                sb.append(lastVerse.getBook().getOSIS());
                sb.append('.');
                sb.append(minChapter - 1);
                sb.append(".* ");
            }
            break;
        case BOOK:
            Key k = key.getKey().get(0);
            if (k instanceof Verse) {
                sb.append(((Verse) k).getBook().getOSIS());
                sb.append(OSIS_CHAPTER_STARTS_WITH);
            }
            break;
        default:
            throw new StepInternalException("Unsupported option.");
    }
    return sb.toString();
}
Also used : KeyWrapper(com.tyndalehouse.step.core.models.KeyWrapper) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Key(org.crosswire.jsword.passage.Key) Verse(org.crosswire.jsword.passage.Verse)

Example 48 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class MultiMap method putElement.

/**
 * Put element.
 *
 * @param key the key
 * @param value the value
 */
@SuppressWarnings("unchecked")
public void putElement(final K key, final V value) {
    C collection = get(key);
    if (collection == null) {
        try {
            collection = (C) this.collectionClass.newInstance();
        } catch (final InstantiationException e) {
            throw new StepInternalException("Unable to create collection of type" + this.collectionClass, e);
        } catch (final IllegalAccessException e) {
            throw new StepInternalException("Unable to create collection of type" + this.collectionClass, e);
        }
        put(key, collection);
    }
    collection.add(value);
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException)

Example 49 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class DirectoryListingInstaller method extraConfFile.

/**
 * Extracts a single conf file to read its details
 *
 * @param zipFile the zip file in question
 */
private void extraConfFile(BookDriver fakeDriver, File zipFile) {
    InputStream in = null;
    ZipInputStream zin = null;
    try {
        ConfigEntry.resetStatistics();
        in = NetUtil.getInputStream(zipFile.toURI());
        zin = new ZipInputStream(in);
        while (true) {
            ZipEntry entry = zin.getNextEntry();
            if (entry == null) {
                break;
            }
            String internal = entry.getName();
            if (internal.endsWith(SwordConstants.EXTENSION_CONF)) {
                LOGGER.trace("Reading a conf file [{}]", entry.getName());
                try {
                    int size = (int) entry.getSize();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int read;
                    while ((read = zin.read(bytes)) > 0) {
                        os.write(bytes, 0, read);
                    }
                    internal = internal.substring(0, internal.length() - 5);
                    if (internal.startsWith(SwordConstants.DIR_CONF + '/')) {
                        internal = internal.substring(7);
                    }
                    SwordBookMetaData sbmd = new SwordBookMetaData(os.toByteArray(), internal);
                    sbmd.setDriver(fakeDriver);
                    Book book = new SwordBook(sbmd, new NullBackend());
                    entries.put(book.getName(), book);
                    // assume 1 conf file per zip file
                    return;
                } catch (Exception ex) {
                    LOGGER.error("Failed to load config for entry: {}", internal, ex);
                }
            }
        }
        ConfigEntry.dumpStatistics();
    } catch (IOException ex) {
        LOGGER.error("Failed to configuration from [{}]", zipFile.getName());
        throw new StepInternalException("Error reading directory of zip files.", ex);
    } finally {
        IOUtil.close(zin);
        IOUtil.close(in);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SwordBookMetaData(org.crosswire.jsword.book.sword.SwordBookMetaData) NullBackend(org.crosswire.jsword.book.sword.NullBackend) IOException(java.io.IOException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) InstallException(org.crosswire.jsword.book.install.InstallException) ZipInputStream(java.util.zip.ZipInputStream) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) SwordBook(org.crosswire.jsword.book.sword.SwordBook) Book(org.crosswire.jsword.book.Book) SwordBook(org.crosswire.jsword.book.sword.SwordBook)

Example 50 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityConfiguration method parseProperties.

/**
 * parses the properties related to an entity configuration
 *
 * @param properties the set of properties
 */
@SuppressWarnings("unchecked")
private void parseProperties(final Properties properties) {
    try {
        final String analyzerProperty = properties.getProperty("entity.analyzer");
        if (isNotBlank(analyzerProperty)) {
            // try the default constructor
            final Class<Analyzer> analyzerClass = (Class<Analyzer>) Class.forName(analyzerProperty);
            try {
                this.analyzerInstance = analyzerClass.newInstance();
            } catch (final InstantiationException exception) {
                this.analyzerInstance = analyzerClass.getConstructor(Version.class).newInstance(Version.LUCENE_30);
            }
        } else {
            this.analyzerInstance = new StandardAnalyzer(LUCENE_30);
        }
        final String processor = properties.getProperty("entity.postProcessor");
        if (isNotBlank(processor)) {
            this.postProcessorInstance = (PostProcessor) this.injector.getInstance(Class.forName(processor));
        }
    } catch (final IllegalAccessException e) {
        throw new StepInternalException(UNABLE_TO_PARSE_CONFIGURATION_FILE, e);
    } catch (final ClassNotFoundException e) {
        throw new StepInternalException(UNABLE_TO_PARSE_CONFIGURATION_FILE, e);
    } catch (final InvocationTargetException e) {
        throw new StepInternalException(UNABLE_TO_PARSE_CONFIGURATION_FILE, e);
    } catch (final InstantiationException e) {
        throw new StepInternalException(UNABLE_TO_PARSE_CONFIGURATION_FILE, e);
    } catch (final NoSuchMethodException e) {
        throw new StepInternalException(UNABLE_TO_PARSE_CONFIGURATION_FILE, e);
    }
    parseFieldConfigs(properties);
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Version(org.apache.lucene.util.Version) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)62 IOException (java.io.IOException)25 Book (org.crosswire.jsword.book.Book)9 Key (org.crosswire.jsword.passage.Key)7 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)5 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 ParseException (org.apache.lucene.queryParser.ParseException)4 BookException (org.crosswire.jsword.book.BookException)4 Versification (org.crosswire.jsword.versification.Versification)4 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)3 KeyWrapper (com.tyndalehouse.step.core.models.KeyWrapper)3 FileInputStream (java.io.FileInputStream)3 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)3 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)3 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 AllResultsCollector (com.tyndalehouse.step.core.data.AllResultsCollector)2