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, 2015 - 2016
18   */
19  package org.crosswire.jsword.book;
20  
21  import java.io.File;
22  import java.net.URI;
23  
24  import org.crosswire.common.util.CWProject;
25  
26  /**
27   * A MetaDataLocator allows one to define where BookMetaData for a Book may be found.
28   *
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author DM Smith
31   */
32  public enum MetaDataLocator {
33      TRANSIENT {
34          @Override
35          public File getReadLocation() {
36              return null;
37          }
38          @Override
39          public File getWriteLocation() {
40              return null;
41          }
42      },
43      JSWORD {
44          @Override
45          public File getReadLocation() {
46              URI[] dirs = CWProject.instance().getProjectResourceDirs();
47              if (dirs.length > 1) {
48                  return getFile(dirs[1]);
49              }
50              return null;
51          }
52          @Override
53          public File getWriteLocation() {
54              URI[] dirs = CWProject.instance().getProjectResourceDirs();
55              if (dirs.length > 0) {
56                  return getFile(dirs[0]);
57              }
58              return null;
59          }
60      },
61      FRONTEND {
62          @Override
63          public File getReadLocation() {
64              return getFile(CWProject.instance().getReadableFrontendProjectDir());
65          }
66          @Override
67          public File getWriteLocation() {
68              return getFile(CWProject.instance().getWritableFrontendProjectDir());
69          }
70      };
71  
72      /**
73       * Safely creates the file location, or null if the parent can't exist
74       *
75       * @param u the parent URI
76       * @return the file representing the config
77       */
78      protected static File getFile(URI u) {
79          if (u == null) {
80              return null;
81          }
82  
83          final File parent = new File(u);
84          final File override = new File(parent, DIR_CONF_OVERRIDE);
85          override.mkdirs();
86          return override;
87      }
88  
89      public abstract File getReadLocation();
90      public abstract File getWriteLocation();
91  
92      /**
93       * The configuration directory for overrides
94       */
95      private static final String DIR_CONF_OVERRIDE = "jsword-mods.d";
96  
97  }
98