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