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   * Copyright: 2015
18   *     The copyright to this program is held by its authors.
19   */
20  package org.crosswire.jsword.book.sword;
21  
22  import java.io.File;
23  import java.net.URI;
24  
25  import org.crosswire.common.util.CWProject;
26  import org.crosswire.jsword.book.MetaDataLocator;
27  
28  /**
29   * The different levels of configuration. The order in this enum is important as it determines the precedence.
30   * The higher the ordinal, the higher the precedence (i.e. override).
31   *
32   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
33   * @author DM Smith
34   */
35  public enum SwordMetaDataLocator implements MetaDataLocator {
36      SWORD {
37          @Override
38          public File getReadLocation() {
39              //this is not implemented, because the SWORD directory could be in two locations.
40              //this should never be used as the config file is always obtainable seen as its lack
41              //prevents the use of the module
42              return null;
43          }
44          @Override
45          public File getWriteLocation() {
46              //this is not implemented, because the SWORD directory could be in two locations.
47              //this should never be used as the config file is always obtainable seen as its lack
48              //prevents the use of the module
49              return null;
50          }
51      },
52      JSWORD {
53          @Override
54          public File getReadLocation() {
55              URI[] dirs = CWProject.instance().getProjectResourceDirs();
56              if (dirs.length > 1) {
57                  return getFile(dirs[1]);
58              }
59              return null;
60          }
61          @Override
62          public File getWriteLocation() {
63              URI[] dirs = CWProject.instance().getProjectResourceDirs();
64              if (dirs.length > 0) {
65                  return getFile(dirs[0]);
66              }
67              return null;
68          }
69      },
70      FRONTEND {
71          @Override
72          public File getReadLocation() {
73              return getFile(CWProject.instance().getReadableFrontendProjectDir());
74          }
75          @Override
76          public File getWriteLocation() {
77              return getFile(CWProject.instance().getWritableFrontendProjectDir());
78          }
79      };
80  
81      /**
82       * Safely creates the file location, or null if the parent can't exist
83       *
84       * @param u the parent URI
85       * @return the file representing the config
86       */
87      protected static File getFile(URI u) {
88          if (u == null) {
89              return null;
90          }
91  
92          final File parent = new File(u);
93          return new File(parent, SwordConstants.DIR_CONF_OVERRIDE);
94      }
95  
96      public abstract File getReadLocation();
97      public abstract File getWriteLocation();
98  }
99