Revision: 7148
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=7148&view=rev
Author:   milek_pl
Date:     2012-06-01 13:06:18 +0000 (Fri, 01 Jun 2012)
Log Message:
-----------
enable per-language configuration

Modified Paths:
--------------
    trunk/JLanguageTool/CHANGES.txt
    trunk/JLanguageTool/src/java/org/languagetool/Language.java
    trunk/JLanguageTool/src/java/org/languagetool/gui/Configuration.java
    trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java
    trunk/JLanguageTool/src/java/org/languagetool/openoffice/ConfigThread.java
    trunk/JLanguageTool/src/java/org/languagetool/openoffice/Main.java
    
trunk/JLanguageTool/src/java/org/languagetool/server/LanguageToolHttpHandler.java
    trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java
    trunk/JLanguageTool/src/test/org/languagetool/gui/ConfigurationTest.java

Modified: trunk/JLanguageTool/CHANGES.txt
===================================================================
--- trunk/JLanguageTool/CHANGES.txt     2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/CHANGES.txt     2012-06-01 13:06:18 UTC (rev 7148)
@@ -65,6 +65,10 @@
   that are configured in the GUI (if the appropriate checkbox is set). 
   This way the user can control the behavior of the server easily.
   
+ -The configuration file now stores settings individually for all languages. 
This means
+  that you can enable spellcheck for American English, disable it for Polish, 
and British 
+  English, and all this settings will be saved separately.
+  
  -Two new options for the HTTP Server added: "disabled" and "enabled", which 
is used 
   to disable or enable rules in the same way as on the command-line. 
 

Modified: trunk/JLanguageTool/src/java/org/languagetool/Language.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/Language.java 2012-06-01 
11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/java/org/languagetool/Language.java 2012-06-01 
13:06:18 UTC (rev 7148)
@@ -212,15 +212,8 @@
    * if available. Otherwise, get the untranslated name.
    */
   public final String getTranslatedName(final ResourceBundle messages) {
-
-         String name = getShortName();
-
-         if (getCountryVariants().length == 1) {
-                 name += "-" + getCountryVariants()[0];
-         }
-
          try {
-                 return messages.getString(name);
+                 return messages.getString(getShortNameWithVariant());
          } catch (final MissingResourceException e) {
                  try {
                          return messages.getString(getShortName());
@@ -229,8 +222,22 @@
                  }
          }
   }
-
+  
   /**
+   * Get the short name of the language with a country variant, if it is
+   * a single-variant language. For generic language classes, get only a two- 
or
+   * three-character code.
+   */
+  public final String getShortNameWithVariant() {
+         String name = getShortName();
+         if (getCountryVariants().length == 1) {
+                 name += "-" + getCountryVariants()[0];
+         }
+         return name;
+  }
+  
+  
+  /**
    * Start symbols used by {@link 
org.languagetool.rules.GenericUnpairedBracketsRule}.
    * Note that the array must be of equal length as {@link 
#getUnpairedRuleEndSymbols()} and the sequence of
    * starting symbols must match exactly the sequence of ending symbols.
@@ -271,6 +278,19 @@
   }
   
   /**
+   * Return all languages supported by LanguageTool.
+   * @return A list of all languages, including external ones.
+   */
+  public static List<Language> getAllLanguages() {
+         List<Language> langList = new ArrayList<Language>();
+         for (Language lang : LANGUAGES) {
+                 langList.add(lang);
+         }
+         langList.addAll(externalLanguages);
+         return langList;
+  }
+  
+  /**
    * Get the Language object for the given short language name.
    * 
    * @param shortLanguageCode e.g. <code>en</code> or <code>de</code>

Modified: trunk/JLanguageTool/src/java/org/languagetool/gui/Configuration.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/gui/Configuration.java        
2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/java/org/languagetool/gui/Configuration.java        
2012-06-01 13:06:18 UTC (rev 7148)
@@ -58,26 +58,31 @@
   private boolean guiConfig;
   private int serverPort = HTTPServer.DEFAULT_PORT;
 
+  private final HashMap<String, String> configForOtherLangs;
+  
   /**
    * Uses the configuration file from the default location.
+   * @param lang The language for the configuration, used to distinguish 
+   * rules that are enabled or disabled per language.
    *  
    * @throws IOException
    */
-  public Configuration() throws IOException {
-         this(new File(System.getProperty("user.home")), CONFIG_FILE);
+  public Configuration(Language lang) throws IOException {
+         this(new File(System.getProperty("user.home")), CONFIG_FILE, lang);
   }
   
-  public Configuration(final File baseDir, final String filename)
+  public Configuration(final File baseDir, final String filename, Language 
lang)
       throws IOException {
     if (!baseDir.isDirectory()) {
       throw new IllegalArgumentException("Not a directory: " + baseDir);
     }
     configFile = new File(baseDir, filename);
-    loadConfiguration();
+    configForOtherLangs = new HashMap<String, String>();
+    loadConfiguration(lang);
   }
 
-  public Configuration(final File baseDir) throws IOException {
-    this(baseDir, CONFIG_FILE);
+  public Configuration(final File baseDir, Language lang) throws IOException {
+    this(baseDir, CONFIG_FILE, lang);
   }
 
   public Set<String> getDisabledRuleIds() {
@@ -145,20 +150,22 @@
     this.serverPort = serverPort;
   }
 
-  private void loadConfiguration() throws IOException {
+  private void loadConfiguration(Language lang) throws IOException {
 
-    // FIXME: disabling a rule X in language Y should not disable it in all
-    // languages - need to add a language parameter
-
+       final String qualifier = getQualifier(lang);
+       
     FileInputStream fis = null;
     try {
       fis = new FileInputStream(configFile);
       final Properties props = new Properties();
       props.load(fis);
 
-      disabledRuleIds.addAll(getListFromProperties(props, 
DISABLED_RULES_CONFIG_KEY));
-      enabledRuleIds.addAll(getListFromProperties(props, 
ENABLED_RULES_CONFIG_KEY));
-      disabledCategoryNames.addAll(getListFromProperties(props, 
DISABLED_CATEGORIES_CONFIG_KEY));
+      disabledRuleIds.addAll(getListFromProperties(props, 
DISABLED_RULES_CONFIG_KEY
+                 + qualifier));
+      enabledRuleIds.addAll(getListFromProperties(props, 
ENABLED_RULES_CONFIG_KEY
+                 + qualifier));
+      disabledCategoryNames.addAll(getListFromProperties(props, 
DISABLED_CATEGORIES_CONFIG_KEY
+                 + qualifier));
       
       final String motherTongueStr = (String) 
props.get(MOTHER_TONGUE_CONFIG_KEY);
       if (motherTongueStr != null) {
@@ -173,6 +180,10 @@
       if (serverPortString != null) {
         serverPort = Integer.parseInt(serverPortString);
       }
+      
+      //store config for other languages
+      loadconfigForOtherLangs(lang, props);
+      
     } catch (final FileNotFoundException e) {
       // file not found: okay, leave disabledRuleIds empty
     } finally {
@@ -181,7 +192,31 @@
       }
     }
   }
+  
+  private void loadconfigForOtherLangs(final Language lang, final Properties 
prop) {
+         for (Language otherLang : Language.getAllLanguages()) {
+                 if (!otherLang.equals(lang)) {                         
+                         storeConfigKeyFromProp(prop, DISABLED_RULES_CONFIG_KEY
+                                         + "." + 
otherLang.getShortNameWithVariant());
+                         
+                         storeConfigKeyFromProp(prop, ENABLED_RULES_CONFIG_KEY
+                                         + "." + 
otherLang.getShortNameWithVariant());                   
+                         
+                         storeConfigKeyFromProp(prop, 
DISABLED_CATEGORIES_CONFIG_KEY
+                                         + "." + 
otherLang.getShortNameWithVariant());                   
+                 }
+         }
+  }
 
+   
+  private void storeConfigKeyFromProp(final Properties prop, final String key) 
{
+         if (prop.containsKey(key)) {
+                 configForOtherLangs.put(key, 
+                                 prop.getProperty(key));
+         }
+  }
+  
+
   private Collection<? extends String> getListFromProperties(Properties props, 
String key) {
     final String value = (String) props.get(key);
     final List<String> list = new ArrayList<String>();
@@ -192,18 +227,37 @@
     return list;
   }
 
-  public void saveConfiguration() throws IOException {
+  private final String getQualifier(Language lang) {
+           String qualifier = "";
+           if (lang != null) {
+               qualifier = "." + lang.getShortNameWithVariant();
+           }
+           return qualifier;     
+  }
+  
+  public void saveConfiguration(Language lang) throws IOException {
     final Properties props = new Properties();
-    addListToProperties(props, DISABLED_RULES_CONFIG_KEY, disabledRuleIds);
-    addListToProperties(props, ENABLED_RULES_CONFIG_KEY, enabledRuleIds);
-    addListToProperties(props, DISABLED_CATEGORIES_CONFIG_KEY, 
disabledCategoryNames);
+    
+    final String qualifier = getQualifier(lang);
+    
+    addListToProperties(props, 
+               DISABLED_RULES_CONFIG_KEY + qualifier, disabledRuleIds);
+    addListToProperties(props, 
+               ENABLED_RULES_CONFIG_KEY + qualifier, enabledRuleIds);
+    addListToProperties(props, 
+               DISABLED_CATEGORIES_CONFIG_KEY + qualifier, 
disabledCategoryNames);
     if (motherTongue != null) {
       props.setProperty(MOTHER_TONGUE_CONFIG_KEY, motherTongue.getShortName());
     }
     props.setProperty(AUTO_DETECT_CONFIG_KEY, 
Boolean.valueOf(autoDetect).toString());
     props.setProperty(USE_GUI_CONFIG_KEY, 
Boolean.valueOf(guiConfig).toString());
     props.setProperty(SERVER_RUN_CONFIG_KEY, 
Boolean.valueOf(runServer).toString());
-    props.setProperty(SERVER_PORT_CONFIG_KEY, 
Integer.valueOf(serverPort).toString());    
+    props.setProperty(SERVER_PORT_CONFIG_KEY, 
Integer.valueOf(serverPort).toString());
+    
+    for (final String key : configForOtherLangs.keySet()) {
+       props.setProperty(key, configForOtherLangs.get(key));
+    }
+    
     final FileOutputStream fos = new FileOutputStream(configFile);
     try {
       props.store(fos, "LanguageTool configuration");

Modified: trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-06-01 
11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-06-01 
13:06:18 UTC (rev 7148)
@@ -70,7 +70,7 @@
 
   private final ResourceBundle messages;
 
-  private final Configuration config;
+  private Configuration config;
 
   private JFrame frame;
   private JTextArea textArea;
@@ -88,7 +88,7 @@
 
   private Main() throws IOException {
     LanguageIdentifierTools.addLtProfiles();  
-    config = new Configuration(new File(System.getProperty("user.home")), 
CONFIG_FILE);
+    config = new Configuration(new File(System.getProperty("user.home")), 
CONFIG_FILE, null);
     messages = JLanguageTool.getMessageBundle();
     maybeStartServer();
   }
@@ -299,7 +299,7 @@
     config.setUseGUIConfig(configDialog.getUseGUIConfig());
     config.setServerPort(configDialog.getServerPort());
     try { //save config - needed for the server
-        config.saveConfiguration();
+        config.saveConfiguration(langTool.getLanguage());
       } catch (final IOException e) {
         Tools.showError(e);
       }
@@ -369,7 +369,7 @@
   void quit() {
     stopServer();
     try {
-      config.saveConfiguration();
+      config.saveConfiguration(getCurrentLanguageTool().getLanguage());
     } catch (final IOException e) {
       Tools.showError(e);
     }
@@ -442,6 +442,7 @@
       final ConfigurationDialog configDialog = getCurrentConfigDialog();
       langTool = new JLanguageTool(getCurrentLanguage(), configDialog
           .getMotherTongue());
+      config = new Configuration(new File(System.getProperty("user.home")), 
CONFIG_FILE, getCurrentLanguage());
       langTool.activateDefaultPatternRules();
       langTool.activateDefaultFalseFriendRules();
       final Set<String> disabledRules = configDialog.getDisabledRuleIds();
@@ -471,7 +472,7 @@
   }
 
   private void checkTextAndDisplayResults() {
-    final Language lang = getCurrentLanguage();
+    final Language lang = getCurrentLanguage();    
     if (StringTools.isEmpty(textArea.getText().trim())) {
       textArea.setText(messages.getString("enterText2"));
     } else {
@@ -556,6 +557,7 @@
   private int checkText(final JLanguageTool langTool, final String text,
       final StringBuilder sb) throws IOException {
     final long startTime = System.currentTimeMillis();
+    
     final List<RuleMatch> ruleMatches = langTool.check(text);
     final long startTimeMatching = System.currentTimeMillis();
     int i = 0;

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/openoffice/ConfigThread.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/openoffice/ConfigThread.java  
2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/java/org/languagetool/openoffice/ConfigThread.java  
2012-06-01 13:06:18 UTC (rev 7148)
@@ -67,7 +67,7 @@
       config.setEnabledRuleIds(cfgDialog.getEnabledRuleIds());
       config.setDisabledCategoryNames(cfgDialog.getDisabledCategoryNames());
       config.setMotherTongue(cfgDialog.getMotherTongue());
-      config.saveConfiguration();
+      config.saveConfiguration(docLanguage);
       if (mainThread != null) {
         mainThread.resetDocument();
       }

Modified: trunk/JLanguageTool/src/java/org/languagetool/openoffice/Main.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/openoffice/Main.java  
2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/java/org/languagetool/openoffice/Main.java  
2012-06-01 13:06:18 UTC (rev 7148)
@@ -119,22 +119,25 @@
 
   private XComponentContext xContext;
 
-  public Main(final XComponentContext xCompContext) {
-    try {
-      changeContext(xCompContext);
-      final File homeDir = getHomeDir();
-      config = new Configuration(homeDir, CONFIG_FILE);
-      disabledRules = config.getDisabledRuleIds();
-      if (disabledRules == null) {
-        disabledRules = new HashSet<String>();
-      }
-      disabledRulesUI = new HashSet<String>(disabledRules);
-      xEventListeners = new ArrayList<XLinguServiceEventListener>();
-    } catch (final Throwable t) {
-      showError(t);
-    }
+  public Main(final XComponentContext xCompContext) {    
+      changeContext(xCompContext);            
+      xEventListeners = new ArrayList<XLinguServiceEventListener>();    
   }
 
+  private void prepareConfig(final Language lang) {
+         try {
+                 final File homeDir = getHomeDir();
+                 config = new Configuration(homeDir, CONFIG_FILE, lang);
+                 disabledRules = config.getDisabledRuleIds();
+                 if (disabledRules == null) {
+                         disabledRules = new HashSet<String>();
+                 }
+                 disabledRulesUI = new HashSet<String>(disabledRules);
+         } catch (final Throwable t) {
+                 showError(t);
+         }
+  }
+
   public final void changeContext(final XComponentContext xCompContext) {
     xContext = xCompContext;
   }
@@ -277,6 +280,7 @@
             return paRes;
           }
           try {
+               prepareConfig(docLanguage);                               
             langTool = new JLanguageTool(docLanguage, 
config.getMotherTongue());
             langTool.activateDefaultPatternRules();
             langTool.activateDefaultFalseFriendRules();
@@ -742,7 +746,7 @@
     disabledRulesUI.add(ruleId);
     config.setDisabledRuleIds(disabledRulesUI);
     try {
-      config.saveConfiguration();
+      config.saveConfiguration(langTool.getLanguage());
     } catch (final Throwable t) {
       showError(t);
     }
@@ -759,7 +763,7 @@
   public void resetIgnoreRules() {
     config.setDisabledRuleIds(disabledRules);
     try {
-      config.saveConfiguration();
+      config.saveConfiguration(langTool.getLanguage());
     } catch (final Throwable t) {
       showError(t);
     }

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/server/LanguageToolHttpHandler.java
===================================================================
--- 
trunk/JLanguageTool/src/java/org/languagetool/server/LanguageToolHttpHandler.java
   2012-06-01 11:28:04 UTC (rev 7147)
+++ 
trunk/JLanguageTool/src/java/org/languagetool/server/LanguageToolHttpHandler.java
   2012-06-01 13:06:18 UTC (rev 7148)
@@ -30,7 +30,7 @@
   private final boolean verbose;
   private final boolean internalServer;
   
-  private final Configuration config;
+  private Configuration config;
   
   private boolean useQuerySettings;
   
@@ -42,7 +42,7 @@
     this.verbose = verbose;
     this.allowedIps = allowedIps;
     this.internalServer = internal;
-    config = new Configuration();
+    config = new Configuration(null);
   }
 
   @Override
@@ -216,6 +216,7 @@
     final JLanguageTool newLanguageTool = new JLanguageTool(lang, 
motherTongue);
     newLanguageTool.activateDefaultPatternRules();
     newLanguageTool.activateDefaultFalseFriendRules();
+    config = new Configuration(lang);
     if (!useQuerySettings && internalServer && config.getUseGUIConfig()) { // 
use the GUI config values
        configureGUI(lang, newLanguageTool);
     }

Modified: trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java
===================================================================
--- trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java     
2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/test/org/languagetool/LanguageTest.java     
2012-06-01 13:06:18 UTC (rev 7148)
@@ -29,6 +29,12 @@
                assertEquals(Language.AMERICAN_ENGLISH, 
Language.getLanguageForShortName("en-US"));
                assertEquals(Language.GERMAN, 
Language.getLanguageForShortName("de"));
        }
+       
+       @Test
+       public void testGetShortNameWithVariant() {
+               assertEquals("en-US", 
Language.AMERICAN_ENGLISH.getShortNameWithVariant());
+               assertEquals("de", Language.GERMAN.getShortNameWithVariant());
+       }
 
   @Test(expected=IllegalArgumentException.class)
        public void testInvalidShortName1() {

Modified: 
trunk/JLanguageTool/src/test/org/languagetool/gui/ConfigurationTest.java
===================================================================
--- trunk/JLanguageTool/src/test/org/languagetool/gui/ConfigurationTest.java    
2012-06-01 11:28:04 UTC (rev 7147)
+++ trunk/JLanguageTool/src/test/org/languagetool/gui/ConfigurationTest.java    
2012-06-01 13:06:18 UTC (rev 7148)
@@ -25,13 +25,15 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import org.languagetool.Language;
+
 public class ConfigurationTest extends TestCase {
 
   public void testSaveAndLoadConfiguration() throws Exception {
     final File tempFile = 
File.createTempFile(ConfigurationTest.class.getSimpleName(), ".cfg");
-    createConfiguration(tempFile);
+    createConfiguration(tempFile, null);
     try {
-      final Configuration conf = new Configuration(tempFile.getParentFile(), 
tempFile.getName());
+      final Configuration conf = new Configuration(tempFile.getParentFile(), 
tempFile.getName(), null);
       final Set<String> disabledRuleIds = conf.getDisabledRuleIds();
       assertTrue(disabledRuleIds.contains("FOO1"));
       assertTrue(disabledRuleIds.contains("Foo2"));
@@ -44,11 +46,54 @@
     }
   }
   
-  private void createConfiguration(File configFile) throws Exception {
-    final Configuration conf = new Configuration(configFile.getParentFile(), 
configFile.getName());
+  private void createConfiguration(File configFile, Language lang) throws 
Exception {
+    final Configuration conf = new Configuration(configFile.getParentFile(), 
configFile.getName(), lang);
     conf.setDisabledRuleIds(new HashSet<String>(Arrays.asList("FOO1", 
"Foo2")));
     conf.setEnabledRuleIds(new HashSet<String>(Arrays.asList("enabledRule")));
-    conf.saveConfiguration();
+    conf.saveConfiguration(lang);
   }
 
+  public void testSaveAndLoadConfigurationForManyLanguages() throws Exception {
+           final File tempFile = 
File.createTempFile(ConfigurationTest.class.getSimpleName(), ".cfg");
+           createConfiguration(tempFile, Language.AMERICAN_ENGLISH);
+           try {
+             Configuration conf = new Configuration(tempFile.getParentFile(), 
tempFile.getName(), 
+                         Language.AMERICAN_ENGLISH);
+             Set<String> disabledRuleIds = conf.getDisabledRuleIds();
+             assertTrue(disabledRuleIds.contains("FOO1"));
+             assertTrue(disabledRuleIds.contains("Foo2"));
+             assertEquals(2, disabledRuleIds.size());
+             Set<String> enabledRuleIds = conf.getEnabledRuleIds();
+             assertTrue(enabledRuleIds.contains("enabledRule"));
+             assertEquals(1, enabledRuleIds.size());
+             
+             //now change language
+             
+             conf = new Configuration(tempFile.getParentFile(), 
tempFile.getName(), 
+                         Language.BELARUSIAN);
+             disabledRuleIds = conf.getDisabledRuleIds();
+             assertTrue(disabledRuleIds.isEmpty());          
+             enabledRuleIds = conf.getEnabledRuleIds();
+             assertTrue(enabledRuleIds.isEmpty());
+             
+             conf.setEnabledRuleIds(new 
HashSet<String>(Arrays.asList("enabledBYRule")));
+             conf.saveConfiguration(Language.BELARUSIAN);
+             
+             //and back...
+             conf = new Configuration(tempFile.getParentFile(), 
tempFile.getName(), 
+                         Language.AMERICAN_ENGLISH);
+             disabledRuleIds = conf.getDisabledRuleIds();
+             assertTrue(disabledRuleIds.contains("FOO1"));
+             assertTrue(disabledRuleIds.contains("Foo2"));
+             assertEquals(2, disabledRuleIds.size());      
+             enabledRuleIds = conf.getEnabledRuleIds();
+             assertTrue(enabledRuleIds.contains("enabledRule"));
+             assertEquals(1, enabledRuleIds.size());                           
      
+             
+             
+           } finally {
+              tempFile.delete();
+           }
+         }
+  
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Languagetool-cvs mailing list
Languagetool-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to