summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/java/de/danielnaber/languagetool/gui/Configuration.java
blob: 932e1fe27d64899dfe218c93b966204ac49bd990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package de.danielnaber.languagetool.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.server.HTTPServer;

/**
 * Configuration -- currently this is just a list of disabled rule IDs.
 * Configuration is loaded from and stored to a properties file.
 * 
 * @author Daniel Naber
 */
public class Configuration {

  private static final String CONFIG_FILE = "languagetool.properties";
  private static final String DISABLED_RULES_CONFIG_KEY = "disabledRules";
  private static final String ENABLED_RULES_CONFIG_KEY = "enabledRules";
  private static final String DISABLED_CATEGORIES_CONFIG_KEY = "disabledCategories";
  private static final String MOTHER_TONGUE_CONFIG_KEY = "motherTongue";
  private static final String SERVER_RUN_CONFIG_KEY = "serverMode";
  private static final String SERVER_PORT_CONFIG_KEY = "serverPort";

  private File configFile;

  private Set<String> disabledRuleIds = new HashSet<String>();
  private Set<String> enabledRuleIds = new HashSet<String>();
  private Set<String> disabledCategoryNames = new HashSet<String>();
  private Language motherTongue;
  private boolean runServer;
  private int serverPort = HTTPServer.DEFAULT_PORT;

  public Configuration(final File baseDir, final String filename)
      throws IOException {
    if (!baseDir.isDirectory()) {
      throw new IllegalArgumentException("Not a directory: " + baseDir);
    }
    configFile = new File(baseDir, filename);
    loadConfiguration();
  }

  public Configuration(final File baseDir) throws IOException {
    this(baseDir, CONFIG_FILE);
  }

  public Set<String> getDisabledRuleIds() {
    return disabledRuleIds;
  }

  public Set<String> getEnabledRuleIds() {
    return enabledRuleIds;
  }

  public Set<String> getDisabledCategoryNames() {
    return disabledCategoryNames;
  }

  public void setDisabledRuleIds(final Set<String> ruleIDs) {
    disabledRuleIds = ruleIDs;
  }

  public void setEnabledRuleIds(final Set<String> ruleIDs) {
    enabledRuleIds = ruleIDs;
  }

  public void setDisabledCategoryNames(final Set<String> categoryNames) {
    disabledCategoryNames = categoryNames;
  }

  public Language getMotherTongue() {
    return motherTongue;
  }

  public void setMotherTongue(final Language motherTongue) {
    this.motherTongue = motherTongue;
  }

  public boolean getRunServer() {
    return runServer;
  }

  public void setRunServer(final boolean runServer) {
    this.runServer = runServer;
  }

  public int getServerPort() {
    return serverPort;
  }

  public void setServerPort(final int serverPort) {
    this.serverPort = serverPort;
  }

  private void loadConfiguration() throws IOException {

    // FIXME: disabling a rule X in language Y should not disable it in all
    // languages - need to add a language parameter

    FileInputStream fis = null;
    try {
      fis = new FileInputStream(configFile);
      final Properties props = new Properties();
      props.load(fis);
      final String val = (String) props.get(DISABLED_RULES_CONFIG_KEY);
      if (val != null) {
        final String[] ids = val.split(",");
        disabledRuleIds.addAll(Arrays.asList(ids));
      }

      final String enRul = (String) props.get(ENABLED_RULES_CONFIG_KEY);
      if (enRul != null) {
        final String[] ids = enRul.split(",");
        enabledRuleIds.addAll(Arrays.asList(ids));
      }

      final String cat = (String) props.get(DISABLED_CATEGORIES_CONFIG_KEY);
      if (cat != null) {
        final String[] names = cat.split(",");
        disabledCategoryNames.addAll(Arrays.asList(names));
      }

      final String motherTongueStr = (String) props
          .get(MOTHER_TONGUE_CONFIG_KEY);
      if (motherTongueStr != null) {
        motherTongue = Language.getLanguageForShortName(motherTongueStr);
      }
      final String runServerString = (String) props.get(SERVER_RUN_CONFIG_KEY);
      if (runServerString != null) {
        runServer = runServerString.equals("true");
      }
      final String serverPortString = (String) props
          .get(SERVER_PORT_CONFIG_KEY);
      if (serverPortString != null) {
        serverPort = Integer.parseInt(serverPortString);
      }
    } catch (final FileNotFoundException e) {
      // file not found: okay, leave disabledRuleIds empty
    } finally {
      if (fis != null) {
        fis.close();
      }
    }
  }

  public void saveConfiguration() throws IOException {
    final Properties props = new Properties();

    if (disabledRuleIds == null) {
      props.setProperty(DISABLED_RULES_CONFIG_KEY, "");
    } else {
      final StringBuilder sb = new StringBuilder();
      for (final Iterator<String> iter = disabledRuleIds.iterator(); iter
          .hasNext();) {
        final String id = iter.next();
        sb.append(id);
        if (iter.hasNext()) {
          sb.append(',');
        }
      }
      props.setProperty(DISABLED_RULES_CONFIG_KEY, sb.toString());
    }

    if (enabledRuleIds == null) {
      props.setProperty(ENABLED_RULES_CONFIG_KEY, "");
    } else {
      final StringBuilder sb = new StringBuilder();
      for (final Iterator<String> iter = enabledRuleIds.iterator(); iter.hasNext();) {
        final String id = iter.next();
        sb.append(id);
        if (iter.hasNext()) {
          sb.append(',');
        }
      }
      props.setProperty(ENABLED_RULES_CONFIG_KEY, sb.toString());
    }

    if (disabledCategoryNames == null) {
      props.setProperty(DISABLED_CATEGORIES_CONFIG_KEY, "");
    } else {
      final StringBuilder sb = new StringBuilder();
      for (final Iterator<String> iter = disabledCategoryNames.iterator(); iter
          .hasNext();) {
        final String name = iter.next();
        sb.append(name);
        if (iter.hasNext()) {
          sb.append(',');
        }
      }
      props.setProperty(DISABLED_CATEGORIES_CONFIG_KEY, sb.toString());
    }

    if (motherTongue != null) {
      props.setProperty(MOTHER_TONGUE_CONFIG_KEY, motherTongue.getShortName());
    }
    props.setProperty(SERVER_RUN_CONFIG_KEY, Boolean.valueOf(runServer)
        .toString());
    props.setProperty(SERVER_PORT_CONFIG_KEY, Integer.valueOf(serverPort)
        .toString());
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(configFile);
      props.store(fos, "LanguageTool configuration");
    } finally {
      if (fos != null) {
        fos.close();
      }
    }
  }

}