1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *      http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * © CrossWire Bible Society, 2005 - 2016
18   *
19   */
20  package org.crosswire.jsword.book.sword;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.crosswire.jsword.JSOtherMsg;
27  import org.crosswire.jsword.book.BookException;
28  import org.crosswire.jsword.book.OSISUtil;
29  import org.crosswire.jsword.book.basic.AbstractBook;
30  import org.crosswire.jsword.book.filter.SourceFilter;
31  import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor;
32  import org.crosswire.jsword.passage.DefaultKeyList;
33  import org.crosswire.jsword.passage.DefaultLeafKeyList;
34  import org.crosswire.jsword.passage.Key;
35  import org.crosswire.jsword.passage.NoSuchKeyException;
36  import org.jdom2.Content;
37  import org.jdom2.Element;
38  
39  /**
40   * A Sword version of Dictionary.
41   * 
42   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
43   * @author Joe Walker
44   */
45  public class SwordDictionary extends AbstractBook {
46      /**
47       * Construct an SwordDictionary given the BookMetaData and the AbstractBackend.
48       * 
49       * @param sbmd the metadata that describes the book
50       * @param backend the means by which the resource is accessed
51       */
52      protected SwordDictionary(SwordBookMetaData sbmd, Backend backend) {
53          super(sbmd, backend);
54  
55          if (!(backend instanceof AbstractKeyBackend)) {
56              throw new IllegalArgumentException("AbstractBackend must be an AbstractKeyBackened");
57          }
58  
59          this.filter = sbmd.getFilter();
60      }
61  
62      /* (non-Javadoc)
63       * @see org.crosswire.jsword.book.Book#getOsisIterator(org.crosswire.jsword.passage.Key, boolean, boolean)
64       */
65      public Iterator<Content> getOsisIterator(final Key key, boolean allowEmpty, final boolean allowGenTitles) throws BookException {
66          assert key != null;
67  
68          List<Content> content = new ArrayList<Content>();
69          Element title = OSISUtil.factory().createGeneratedTitle();
70          title.addContent(key.getName());
71          content.add(title);
72  
73          String txt = getBackend().getRawText(key);
74          List<Content> osisContent = filter.toOSIS(this, key, txt);
75          content.addAll(osisContent);
76  
77          return content.iterator();
78      }
79  
80      /* (non-Javadoc)
81       * @see org.crosswire.jsword.book.Book#getRawText(org.crosswire.jsword.passage.Key)
82       */
83      public String getRawText(Key key) throws BookException {
84          return getBackend().getRawText(key);
85      }
86  
87      /* (non-Javadoc)
88       * @see org.crosswire.jsword.book.Book#contains(org.crosswire.jsword.passage.Key)
89       */
90      public boolean contains(Key key) {
91          Backend backend = getBackend();
92          return backend != null && backend.contains(key);
93      }
94  
95      /* (non-Javadoc)
96       * @see org.crosswire.jsword.book.Book#getRawText(org.crosswire.jsword.passage.Key)
97       */
98      @Override
99      public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException {
100         assert key != null;
101 
102         return getBackend().readToOsis(key, processor);
103     }
104 
105     /* (non-Javadoc)
106      * @see org.crosswire.jsword.book.Book#isWritable()
107      */
108     public boolean isWritable() {
109         return getBackend().isWritable();
110     }
111 
112     /* (non-Javadoc)
113      * @see org.crosswire.jsword.book.Book#setRawText(org.crosswire.jsword.passage.Key, java.lang.String)
114      */
115     public void setRawText(Key key, String rawData) throws BookException {
116         throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
117     }
118 
119     /* (non-Javadoc)
120      * @see org.crosswire.jsword.book.Book#setAliasKey(org.crosswire.jsword.passage.Key, org.crosswire.jsword.passage.Key)
121      */
122     public void setAliasKey(Key alias, Key source) throws BookException {
123         throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
124     }
125 
126     /* (non-Javadoc)
127      * @see org.crosswire.jsword.book.Book#getGlobalKeyList()
128      */
129     public Key getGlobalKeyList() {
130         return (AbstractKeyBackend) getBackend();
131     }
132 
133     /* (non-Javadoc)
134      * @see org.crosswire.jsword.book.Book#getValidKey(java.lang.String)
135      */
136     public Key getValidKey(String name) {
137         try {
138             return getKey(name);
139         } catch (NoSuchKeyException e) {
140             return createEmptyKeyList();
141         }
142     }
143 
144     /* (non-Javadoc)
145      * @see org.crosswire.jsword.book.Book#getKey(java.lang.String)
146      */
147     public Key getKey(String text) throws NoSuchKeyException {
148         AbstractKeyBackend keyBackend = (AbstractKeyBackend) getBackend();
149 
150         int pos = keyBackend.indexOf(new DefaultLeafKeyList(text));
151         if (pos < 0) {
152             if (keyBackend.getCardinality() > -pos - 1) {
153                 return keyBackend.get(-pos - 1);
154             }
155             return keyBackend.get(keyBackend.getCardinality() - 1);
156         }
157         return keyBackend.get(pos);
158     }
159 
160     /* (non-Javadoc)
161      * @see org.crosswire.jsword.book.Book#createEmptyKeyList()
162      */
163     public Key createEmptyKeyList() {
164         return new DefaultKeyList();
165     }
166 
167     /**
168      * The filter to use to convert to OSIS.
169      */
170     private SourceFilter filter;
171 
172 }
173