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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: SwordBook.java 2221 2012-01-25 21:32:57Z dmsmith $
21   */
22  package org.crosswire.jsword.book.sword;
23  
24  import java.io.IOException;
25  import java.util.List;
26  
27  import org.crosswire.common.activate.Activator;
28  import org.crosswire.common.activate.Lock;
29  import org.crosswire.jsword.JSOtherMsg;
30  import org.crosswire.jsword.book.BookException;
31  import org.crosswire.jsword.book.OSISUtil;
32  import org.crosswire.jsword.book.basic.AbstractPassageBook;
33  import org.crosswire.jsword.book.filter.Filter;
34  import org.crosswire.jsword.passage.Key;
35  import org.crosswire.jsword.passage.KeyUtil;
36  import org.jdom.Content;
37  import org.jdom.Element;
38  
39  /**
40   * SwordBook is a base class for all verse based Sword type books.
41   * 
42   * @see gnu.lgpl.License for license details.<br>
43   *      The copyright to this program is held by it's authors.
44   * @author Joe Walker [joe at eireneh dot com]
45   */
46  public class SwordBook extends AbstractPassageBook {
47      /**
48       * Simple ctor
49       */
50      public SwordBook(SwordBookMetaData sbmd, AbstractBackend backend) {
51          super(sbmd);
52  
53          this.filter = sbmd.getFilter();
54          this.backend = backend;
55      }
56  
57      @Override
58      public final void activate(Lock lock) {
59          super.activate(lock);
60  
61          // We don't need to activate the backend because it should be capable
62          // of doing it for itself.
63      }
64  
65      @Override
66      public final void deactivate(Lock lock) {
67          super.deactivate(lock);
68  
69          Activator.deactivate(backend);
70      }
71  
72      /* (non-Javadoc)
73       * @see org.crosswire.jsword.book.Book#contains(org.crosswire.jsword.passage.Key)
74       */
75      public boolean contains(Key key) {
76          return backend != null && backend.contains(key);
77      }
78  
79      /* (non-Javadoc)
80       * @see org.crosswire.jsword.book.Book#getRawText(org.crosswire.jsword.passage.Key)
81       */
82      public String getRawText(Key key) throws BookException {
83          if (backend == null) {
84              return "";
85          }
86  
87          String result = backend.getRawText(key);
88          assert result != null;
89          return result;
90      }
91  
92      @Override
93      public void addOSIS(Key key, Element div, List<Content> osisContent) {
94          // See if the text is marked up with verses
95          // If it is then just add it.
96          for (Content content : osisContent) {
97              if (content instanceof Element) {
98                  Element ele = (Element) content;
99                  if (ele.getName().equals(OSISUtil.OSIS_ELEMENT_VERSE)) {
100                     super.addOSIS(key, div, osisContent);
101                     return;
102                 }
103             }
104         }
105 
106         // If we get here then the text is not marked up with verse
107         // In this case we add the verse markup, if the verse is not 0.
108         if (KeyUtil.getPassage(key).getVerseAt(0).getVerse() == 0) {
109             super.addOSIS(key, div, osisContent);
110         } else {
111             Element everse = OSISUtil.factory().createVerse();
112             everse.setAttribute(OSISUtil.OSIS_ATTR_OSISID, key.getOsisID());
113             div.addContent(everse);
114             super.addOSIS(key, everse, osisContent);
115         }
116     }
117 
118     @Override
119     public void addOSIS(Key key, List<Content> contentList, List<Content> osisContent) {
120         // See if the text is marked up with verses
121         // If it is then just add it.
122         for (Content content : osisContent) {
123             if (content instanceof Element) {
124                 Element ele = (Element) content;
125                 if (ele.getName().equals(OSISUtil.OSIS_ELEMENT_VERSE)) {
126                     super.addOSIS(key, contentList, osisContent);
127                     return;
128                 }
129             }
130         }
131 
132         // If we get here then the text is not marked up with verse
133         // In this case we add the verse markup, if the verse is not 0.
134         if (KeyUtil.getPassage(key).getVerseAt(0).getVerse() == 0) {
135             super.addOSIS(key, contentList, osisContent);
136         } else {
137             Element everse = OSISUtil.factory().createVerse();
138             everse.setAttribute(OSISUtil.OSIS_ATTR_OSISID, key.getOsisID());
139             super.addOSIS(key, everse, osisContent);
140             contentList.add(everse);
141         }
142     }
143 
144     @Override
145     public boolean isWritable() {
146         return backend.isWritable();
147     }
148 
149     /* (non-Javadoc)
150      * @see org.crosswire.jsword.book.Book#setRawText(org.crosswire.jsword.passage.Key, java.lang.String)
151      */
152     public void setRawText(Key key, String rawData) throws BookException {
153         throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
154     }
155 
156     /* (non-Javadoc)
157      * @see org.crosswire.jsword.book.Book#setAliasKey(org.crosswire.jsword.passage.Key, org.crosswire.jsword.passage.Key)
158      */
159     public void setAliasKey(Key alias, Key source) throws BookException {
160         try {
161             backend.setAliasKey(alias, source);
162         } catch (IOException e) {
163             throw new BookException(JSOtherMsg.lookupText("Unable to save {0}.", alias.getOsisID()));
164         }
165     }
166 
167     /* (non-Javadoc)
168      * @see org.crosswire.jsword.book.basic.AbstractPassageBook#getFilter()
169      */
170     @Override
171     protected Filter getFilter() {
172         return filter;
173     }
174 
175     /**
176      * To read the data from the disk
177      */
178     private AbstractBackend backend;
179 
180     /**
181      * The filter to use to convert to OSIS.
182      */
183     private Filter filter;
184 }
185