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.basic;
21  
22  import java.net.URI;
23  
24  import org.crosswire.common.util.Language;
25  import org.crosswire.jsword.book.BookDriver;
26  import org.crosswire.jsword.book.BookException;
27  import org.crosswire.jsword.book.BookMetaData;
28  import org.crosswire.jsword.book.FeatureType;
29  import org.crosswire.jsword.book.KeyType;
30  import org.crosswire.jsword.index.IndexStatus;
31  import org.jdom2.Document;
32  
33  /**
34   * An implementation of the Property Change methods from BookMetaData.
35   *
36   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
37   * @author Joe Walker
38   */
39  
40  /**
41   * @author DM Smith
42   * @see gnu.lgpl.License The GNU Lesser General Public License for details.<br>
43   * The copyright to this program is held by its authors.
44   */
45  public abstract class AbstractBookMetaData implements BookMetaData {
46  
47      /* (non-Javadoc)
48       * @see org.crosswire.jsword.book.BookMetaData#getKeyType()
49       */
50      public KeyType getKeyType() {
51          return KeyType.LIST;
52      }
53  
54      /* (non-Javadoc)
55       * @see org.crosswire.jsword.book.BookMetaData#getDriver()
56       */
57      public BookDriver getDriver() {
58          return driver;
59      }
60  
61      /* (non-Javadoc)
62       * @see org.crosswire.jsword.book.BookMetaData#getDriverName()
63       */
64      public String getDriverName() {
65          if (getDriver() == null) {
66              return null;
67          }
68  
69          return getDriver().getDriverName();
70      }
71  
72      /* (non-Javadoc)
73       * @see org.crosswire.jsword.book.BookMetaData#hasFeature(org.crosswire.jsword.book.FeatureType)
74       */
75      public boolean hasFeature(FeatureType feature) {
76          return false;
77      }
78  
79      /* (non-Javadoc)
80       * @see org.crosswire.jsword.book.BookMetaData#getOsisID()
81       */
82      public String getOsisID() {
83          return getBookCategory().getName() + '.' + getInitials();
84      }
85  
86      /* (non-Javadoc)
87       * @see org.crosswire.jsword.book.BookMetaData#isSupported()
88       */
89      public boolean isSupported() {
90          return true;
91      }
92  
93      /* (non-Javadoc)
94       * @see org.crosswire.jsword.book.BookMetaData#isEnciphered()
95       */
96      public boolean isEnciphered() {
97          return false;
98      }
99  
100     /* (non-Javadoc)
101      * @see org.crosswire.jsword.book.BookMetaData#isLocked()
102      */
103     public boolean isLocked() {
104         return false;
105     }
106 
107     /* (non-Javadoc)
108      * @see org.crosswire.jsword.book.BookMetaData#unlock(java.lang.String)
109      */
110     public boolean unlock(String unlockKey) {
111         return false;
112     }
113 
114     /* (non-Javadoc)
115      * @see org.crosswire.jsword.book.BookMetaData#getUnlockKey()
116      */
117     public String getUnlockKey() {
118         return null;
119     }
120 
121     /* (non-Javadoc)
122      * @see org.crosswire.jsword.book.BookMetaData#isQuestionable()
123      */
124     public boolean isQuestionable() {
125         return false;
126     }
127 
128     /* (non-Javadoc)
129      * @see org.crosswire.jsword.book.BookMetaData#getLanguage()
130      */
131     public Language getLanguage() {
132         return this.language;
133     }
134 
135     /* (non-Javadoc)
136      * @see org.crosswire.jsword.book.BookMetaData#setLanguage(org.crosswire.common.util.Language)
137      */
138     public void setLanguage(Language language) {
139         this.language = language;
140     }
141 
142     /* (non-Javadoc)
143      * @see org.crosswire.jsword.book.BookMetaData#getLibrary()
144      */
145     public URI getLibrary() {
146         return library;
147     }
148 
149     /* (non-Javadoc)
150      * @see org.crosswire.jsword.book.BookMetaData#setLibrary(java.net.URI)
151      */
152     public void setLibrary(URI library) throws BookException {
153         this.library = library;
154     }
155 
156     /* (non-Javadoc)
157      * @see org.crosswire.jsword.book.BookMetaData#setLocation(java.net.URI)
158      */
159     public void setLocation(URI location) {
160         this.location = location;
161     }
162 
163     /* (non-Javadoc)
164      * @see org.crosswire.jsword.book.BookMetaData#getLocation()
165      */
166     public URI getLocation() {
167         return location;
168     }
169 
170     /* (non-Javadoc)
171      * @see org.crosswire.jsword.book.BookMetaData#getIndexStatus()
172      */
173     public IndexStatus getIndexStatus() {
174         return indexStatus;
175     }
176 
177     /* (non-Javadoc)
178      * @see org.crosswire.jsword.book.BookMetaData#setIndexStatus(org.crosswire.jsword.index.IndexStatus)
179      */
180     public void setIndexStatus(IndexStatus newValue) {
181         indexStatus = newValue;
182     }
183 
184     /* (non-Javadoc)
185      * @see org.crosswire.jsword.book.BookMetaData#reload()
186      */
187     public void reload() throws BookException {
188         // over ride this if partial loads are allowed
189     }
190 
191     /* (non-Javadoc)
192      * @see org.crosswire.jsword.book.BookMetaData#putProperty(java.lang.String, java.lang.String)
193      */
194     public void putProperty(String key, String value) {
195         putProperty(key, value, false);
196     }
197 
198     /* (non-Javadoc)
199      * @see org.crosswire.jsword.book.BookMetaData#toOSIS()
200      */
201     public Document toOSIS() {
202         throw new UnsupportedOperationException("If you want to use this, implement it.");
203     }
204 
205     /**
206      * @param driver The driver to set.
207      */
208     public void setDriver(BookDriver driver) {
209         this.driver = driver;
210     }
211 
212     @Override
213     public boolean equals(Object obj) {
214         // Since this can not be null
215         if (obj == null) {
216             return false;
217         }
218 
219         // We might consider checking for equality against all BookMetaDatas?
220         // However currently we don't.
221 
222         // Check that that is the same as this
223         // Don't use instanceof since that breaks inheritance
224         if (!obj.getClass().equals(this.getClass())) {
225             return false;
226         }
227 
228         // The real bit ...
229         BookMetaData that = (BookMetaData) obj;
230 
231         return getBookCategory().equals(that.getBookCategory()) && getName().equals(that.getName()) && getInitials().equals(that.getInitials());
232     }
233 
234     @Override
235     public int hashCode() {
236         return getName().hashCode();
237     }
238 
239     /* 
240      * The sort order should be based on initials rather than name because name often begins with general words like 'The ...'
241      * (non-Javadoc)
242      * @see java.lang.Comparable#compareTo(java.lang.Object)
243      */
244     public int compareTo(BookMetaData obj) {
245         int result = this.getBookCategory().compareTo(obj.getBookCategory());
246         if (result == 0) {
247             result = this.getAbbreviation().compareTo(obj.getAbbreviation());
248         }
249         if (result == 0) {
250             result = this.getInitials().compareTo(obj.getInitials());
251         }
252         if (result == 0) {
253             result = this.getName().compareTo(obj.getName());
254         }
255         return result;
256     }
257 
258     @Override
259     public String toString() {
260         String internal = getInitials();
261         String abbreviation = getAbbreviation();
262         if (internal.equals(abbreviation)) {
263             return internal;
264         }
265         StringBuffer buf = new StringBuffer(internal);
266         buf.append('(');
267         buf.append(abbreviation);
268         buf.append(')');
269         return buf.toString();
270     }
271 
272     private BookDriver driver;
273     private IndexStatus indexStatus = IndexStatus.UNDONE;
274     private Language language;
275     private URI library;
276     private URI location;
277 
278 }
279