Repository: redpen-cc/redpen
Pull request #542 (2016-02-02 09:35:48)

rename type configuration attribute to variant, leaving type support …
…in config file for backwards compatibility


Patch:
diff --git a/redpen-core/src/main/java/cc/redpen/ConfigurationLoader.java b/redpen-core/src/main/java/cc/redpen/ConfigurationLoader.java
index 6305efef..b02d67b6 100644
--- a/redpen-core/src/main/java/cc/redpen/ConfigurationLoader.java
+++ b/redpen-core/src/main/java/cc/redpen/ConfigurationLoader.java
@@ -33,14 +33,8 @@ import org.xml.sax.SAXException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.nio.charset.StandardCharsets;
-import java.util.Optional;
 
 import static cc.redpen.config.Configuration.ConfigurationBuilder;
 
@@ -141,23 +135,25 @@ public final class ConfigurationLoader {
         configBuilder = new ConfigurationBuilder();
         Element rootElement = getRootNode(doc, "redpen-conf");
 
-        Node langNode = rootElement.getAttributes().getNamedItem("lang");
-        String language = "en";
-        if (langNode != null) {
-            language = langNode.getNodeValue();
+        String language = rootElement.getAttribute("lang");
+        if (!language.isEmpty()) {
             LOG.info("Language is set to \"{}\"", language);
         } else {
             LOG.warn("No language configuration...");
             LOG.info("Set language to en");
+            language = "en";
         }
 
-        Node typeNode = rootElement.getAttributes().getNamedItem("type");
-        Optional<String> type = Optional.empty();
-        if (typeNode != null) {
-            type = Optional.of(typeNode.getNodeValue());
-            LOG.info("Type is set to \"{}\"", type.get());
+        String variant = rootElement.getAttribute("variant");
+        if (variant.isEmpty()) {
+            variant = rootElement.getAttribute("type");
+            if (!variant.isEmpty()) LOG.info("Deprecated: use \"variant\" attribute instead of \"type\"");
+        }
+
+        if (variant.isEmpty()) {
+            LOG.warn("No variant configuration...");
         } else {
-            LOG.warn("No type configuration...");
+            LOG.info("Variant is set to \"{}\"", variant);
         }
 
         // extract validator configurations
@@ -179,7 +175,7 @@ public final class ConfigurationLoader {
         NodeList symbolTableConfigElementList =
                 getSpecifiedNodeList(rootElement, "symbols");
         configBuilder.setLanguage(language);
-        type.ifPresent(configBuilder::setType);
+        if (!variant.isEmpty()) configBuilder.setVariant(variant);
 
         if (symbolTableConfigElementList != null) {
             extractSymbolConfig(symbolTableConfigElementList, language);
diff --git a/redpen-core/src/main/java/cc/redpen/config/Configuration.java b/redpen-core/src/main/java/cc/redpen/config/Configuration.java
index aad436f8..60a15b83 100644
--- a/redpen-core/src/main/java/cc/redpen/config/Configuration.java
+++ b/redpen-core/src/main/java/cc/redpen/config/Configuration.java
@@ -85,8 +85,8 @@ public class Configuration {
      *
      * @return type
      */
-    public String getType() {
-        return getSymbolTable().getType();
+    public String getVariant() {
+        return getSymbolTable().getVariant();
     }
 
     /**
@@ -102,8 +102,8 @@ public class Configuration {
      * @return unique key for this lang and type combination
      */
     public String getKey() {
-        if (getLang().equals("ja") && getType().equals("zenkaku")) return "ja";
-        return getLang() + (isEmpty(getType()) ? "" : "." + getType());
+        if (getLang().equals("ja") && getVariant().equals("zenkaku")) return "ja";
+        return getLang() + (isEmpty(getVariant()) ? "" : "." + getVariant());
     }
 
     /**
@@ -115,7 +115,7 @@ public class Configuration {
         private boolean built = false;
 
         private String lang = "en";
-        private Optional<String> type = Optional.empty();
+        private Optional<String> variant = Optional.empty();
 
         private void checkBuilt(){
             if(built){
@@ -140,16 +140,16 @@ public class Configuration {
             return this;
         }
 
-        public ConfigurationBuilder setType(String type) {
+        public ConfigurationBuilder setVariant(String variant) {
             checkBuilt();
-            this.type = Optional.of(type);
+            this.variant = Optional.of(variant);
             return this;
         }
 
         public Configuration build() {
             checkBuilt();
             built = true;
-            return new Configuration(new SymbolTable(lang, type, customSymbols), this.validatorConfigs, this.lang);
+            return new Configuration(new SymbolTable(lang, variant, customSymbols), this.validatorConfigs, this.lang);
         }
     }
 }
diff --git a/redpen-core/src/main/java/cc/redpen/config/SymbolTable.java b/redpen-core/src/main/java/cc/redpen/config/SymbolTable.java
index d58dab9e..1fc04c80 100644
--- a/redpen-core/src/main/java/cc/redpen/config/SymbolTable.java
+++ b/redpen-core/src/main/java/cc/redpen/config/SymbolTable.java
@@ -33,30 +33,29 @@ public class SymbolTable implements Serializable {
     private static final long serialVersionUID = 1612920745151501631L;
     private final Map<SymbolType, Symbol> symbolDictionary = new HashMap<>();
     private final Map<Character, Symbol> valueDictionary = new HashMap<>();
-    private String type;
+    private String variant;
     private String lang;
     private static final Logger LOG = LoggerFactory.getLogger(SymbolTable.class);
 
     /**
      * Constructor.
      */
-    SymbolTable(String lang, Optional<String> type, List<Symbol> customSymbols) {
-        super();
+    SymbolTable(String lang, Optional<String> variant, List<Symbol> customSymbols) {
         this.lang = lang;
-        this.type = type.orElse("");
+        this.variant = variant.orElse("");
         if (lang.equals("ja")) {
             LOG.info("\"ja\" is specified.");
-            if (this.type.equals("hankaku")) {
-                LOG.info("\"hankaku\" type is specified");
+            if (this.variant.equals("hankaku")) {
+                LOG.info("\"hankaku\" variant is specified");
                 JAPANESE_HANKAKU_SYMBOLS.values().forEach(this::overrideSymbol);
-            } else if (this.type.equals("zenkaku2")) {
-                LOG.info("\"zenkaku2\" type is specified");
+            } else if (this.variant.equals("zenkaku2")) {
+                LOG.info("\"zenkaku2\" variant is specified");
                 JAPANESE_SYMBOLS.values().forEach(this::overrideSymbol);
                 this.overrideSymbol(new Symbol(FULL_STOP, '．', "。."));
                 this.overrideSymbol(new Symbol(COMMA, '，', "、,"));
             } else {
-                this.type = "zenkaku";
-                LOG.info("\"zenkaku\" type is specified");
+                this.variant = "zenkaku";
+                LOG.info("\"zenkaku\" variant is specified");
                 JAPANESE_SYMBOLS.values().forEach(this::overrideSymbol);
             }
         } else {
@@ -132,8 +131,8 @@ public class SymbolTable implements Serializable {
         return lang;
     }
 
-    public String getType() {
-        return type;
+    public String getVariant() {
+        return variant;
     }
 
     @Override
@@ -146,7 +145,7 @@ public class SymbolTable implements Serializable {
         if (lang != null ? !lang.equals(that.lang) : that.lang != null) return false;
         if (symbolDictionary != null ? !symbolDictionary.equals(that.symbolDictionary) : that.symbolDictionary != null)
             return false;
-        if (type != null ? !type.equals(that.type) : that.type != null) return false;
+        if (variant != null ? !variant.equals(that.variant) : that.variant != null) return false;
         if (valueDictionary != null ? !valueDictionary.equals(that.valueDictionary) : that.valueDictionary != null)
             return false;
 
@@ -157,7 +156,7 @@ public class SymbolTable implements Serializable {
     public int hashCode() {
         int result = symbolDictionary != null ? symbolDictionary.hashCode() : 0;
         result = 31 * result + (valueDictionary != null ? valueDictionary.hashCode() : 0);
-        result = 31 * result + (type != null ? type.hashCode() : 0);
+        result = 31 * result + (variant != null ? variant.hashCode() : 0);
         result = 31 * result + (lang != null ? lang.hashCode() : 0);
         return result;
     }
@@ -167,7 +166,7 @@ public class SymbolTable implements Serializable {
         return "SymbolTable{" +
                 "symbolDictionary=" + symbolDictionary +
                 ", valueDictionary=" + valueDictionary +
-                ", type='" + type + '\'' +
+                ", type='" + variant + '\'' +
                 ", lang='" + lang + '\'' +
                 '}';
     }
diff --git a/redpen-server/src/main/java/cc/redpen/server/api/RedPenConfigurationResource.java b/redpen-server/src/main/java/cc/redpen/server/api/RedPenConfigurationResource.java
index 150fbd9b..17c976d3 100644
--- a/redpen-server/src/main/java/cc/redpen/server/api/RedPenConfigurationResource.java
+++ b/redpen-server/src/main/java/cc/redpen/server/api/RedPenConfigurationResource.java
@@ -75,7 +75,7 @@ public class RedPenConfigurationResource {
                         // add specific configuration items
                         JSONObject config = new JSONObject();
                         config.put("lang", redPen.getConfiguration().getLang());
-                        config.put("type", redPen.getConfiguration().getType());
+                        config.put("variant", redPen.getConfiguration().getVariant());
                         config.put("tokenizer", redPen.getConfiguration().getTokenizer().getClass().getName());
 
                         // add the names of the validators
diff --git a/redpen-server/src/main/resources/conf/redpen-conf-ja-hankaku.xml b/redpen-server/src/main/resources/conf/redpen-conf-ja-hankaku.xml
index c1340621..561307eb 100644
--- a/redpen-server/src/main/resources/conf/redpen-conf-ja-hankaku.xml
+++ b/redpen-server/src/main/resources/conf/redpen-conf-ja-hankaku.xml
@@ -1,4 +1,4 @@
-<redpen-conf lang="ja" type="hankaku">
+<redpen-conf lang="ja" variant="hankaku">
     <validators>
         <validator name="SentenceLength">
             <property name="max_len" value="100"/>
diff --git a/redpen-server/src/main/resources/conf/redpen-conf-ja-zenkaku2.xml b/redpen-server/src/main/resources/conf/redpen-conf-ja-zenkaku2.xml
index 259c6e6b..eb3f7628 100644
--- a/redpen-server/src/main/resources/conf/redpen-conf-ja-zenkaku2.xml
+++ b/redpen-server/src/main/resources/conf/redpen-conf-ja-zenkaku2.xml
@@ -1,4 +1,4 @@
-<redpen-conf lang="ja" type="zenkaku2">
+<redpen-conf lang="ja" variant="zenkaku2">
     <validators>
         <validator name="SentenceLength">
             <property name="max_len" value="100"/>


Test patch:
diff --git a/redpen-core/src/test/java/cc/redpen/ConfigurationLoaderTest.java b/redpen-core/src/test/java/cc/redpen/ConfigurationLoaderTest.java
index ede2b188..0585a466 100644
--- a/redpen-core/src/test/java/cc/redpen/ConfigurationLoaderTest.java
+++ b/redpen-core/src/test/java/cc/redpen/ConfigurationLoaderTest.java
@@ -20,10 +20,7 @@ package cc.redpen;
 import cc.redpen.config.Configuration;
 import org.junit.Test;
 
-import static cc.redpen.config.SymbolType.EXCLAMATION_MARK;
-import static cc.redpen.config.SymbolType.LEFT_SINGLE_QUOTATION_MARK;
-import static cc.redpen.config.SymbolType.COMMA;
-
+import static cc.redpen.config.SymbolType.*;
 import static org.junit.Assert.*;
 
 public class ConfigurationLoaderTest {
@@ -81,6 +78,7 @@ public class ConfigurationLoaderTest {
         Configuration configuration = configurationLoader.loadFromString(sampleConfigString);
 
         assertNotNull(configuration);
+        assertEquals("zenkaku", configuration.getVariant());
         assertEquals(2, configuration.getValidatorConfigs().size());
         assertEquals("SentenceLength",
                 configuration.getValidatorConfigs().get(0).getConfigurationName());
@@ -98,9 +96,9 @@ public class ConfigurationLoaderTest {
     }
 
     @Test
-    public void testLoadJapaneseConfigurationWithHankakuType() throws RedPenException{
+    public void testLoadJapaneseConfigurationWithHankakuVariant() throws RedPenException{
         String sampleConfigString =
-                "<redpen-conf lang=\"ja\" type=\"hankaku\">" +
+                "<redpen-conf lang=\"ja\" variant=\"hankaku\">" +
                         "<validators>" +
                         "<validator name=\"SentenceLength\">" +
                         "<property name=\"max_length\" value=\"200\" />" +
@@ -113,6 +111,7 @@ public class ConfigurationLoaderTest {
         Configuration configuration = configurationLoader.loadFromString(sampleConfigString);
 
         assertNotNull(configuration);
+        assertEquals("hankaku", configuration.getVariant());
         assertEquals(2, configuration.getValidatorConfigs().size());
         assertEquals("SentenceLength",
                 configuration.getValidatorConfigs().get(0).getConfigurationName());
@@ -129,11 +128,20 @@ public class ConfigurationLoaderTest {
                 .getSymbol(EXCLAMATION_MARK).getInvalidChars()[0]);
     }
 
+    @Test
+    public void testVariantCanBeSpecifiedAsTypeForBackwardsCompatibility() throws RedPenException{
+        String sampleConfigString = "<redpen-conf lang=\"ja\" type=\"hankaku\"><validators/></redpen-conf>";
+
+        ConfigurationLoader configurationLoader = new ConfigurationLoader();
+        Configuration configuration = configurationLoader.loadFromString(sampleConfigString);
+
+        assertEquals("hankaku", configuration.getVariant());
+    }
 
     @Test
-    public void testLoadJapaneseConfigurationWithZenkaku2Type() throws RedPenException{
+    public void testLoadJapaneseConfigurationWithZenkaku2Variant() throws RedPenException{
         String sampleConfigString =
-                "<redpen-conf lang=\"ja\" type=\"zenkaku2\">" +
+                "<redpen-conf lang=\"ja\" variant=\"zenkaku2\">" +
                         "<validators>" +
                         "<validator name=\"SentenceLength\">" +
                         "<property name=\"max_length\" value=\"200\" />" +
@@ -146,6 +154,7 @@ public class ConfigurationLoaderTest {
         Configuration configuration = configurationLoader.loadFromString(sampleConfigString);
 
         assertNotNull(configuration);
+        assertEquals("zenkaku2", configuration.getVariant());
         assertEquals(2, configuration.getValidatorConfigs().size());
         assertEquals("SentenceLength",
                 configuration.getValidatorConfigs().get(0).getConfigurationName());
@@ -180,6 +189,7 @@ public class ConfigurationLoaderTest {
         Configuration configuration = configurationLoader.loadFromString(sampleConfigString);
 
         assertNotNull(configuration);
+        assertEquals("en", configuration.getLang());
         assertEquals(2, configuration.getValidatorConfigs().size());
         assertEquals("SentenceLength",
                 configuration.getValidatorConfigs().get(0).getConfigurationName());
diff --git a/redpen-core/src/test/java/cc/redpen/config/ConfigurationRedPenBuilderTest.java b/redpen-core/src/test/java/cc/redpen/config/ConfigurationRedPenBuilderTest.java
index 5948af1c..c6f082f7 100644
--- a/redpen-core/src/test/java/cc/redpen/config/ConfigurationRedPenBuilderTest.java
+++ b/redpen-core/src/test/java/cc/redpen/config/ConfigurationRedPenBuilderTest.java
@@ -92,7 +92,7 @@ public class ConfigurationRedPenBuilderTest {
         Configuration config = new Configuration.ConfigurationBuilder()
                 .addValidatorConfig(new ValidatorConfiguration("InvalidExpression"))
                 .setLanguage("ja")
-                .setType("hankaku")
+                .setVariant("hankaku")
                 .build();
 
         assertNotNull(config.getSymbolTable());
diff --git a/redpen-core/src/test/java/cc/redpen/config/SymbolTableTest.java b/redpen-core/src/test/java/cc/redpen/config/SymbolTableTest.java
index 5a07b023..18d3c0fe 100644
--- a/redpen-core/src/test/java/cc/redpen/config/SymbolTableTest.java
+++ b/redpen-core/src/test/java/cc/redpen/config/SymbolTableTest.java
@@ -13,13 +13,13 @@ public class SymbolTableTest {
   @Test
   public void englishDoesNotHaveAType() throws Exception {
     SymbolTable table = new SymbolTable("en", Optional.empty(), emptyList());
-    assertEquals("", table.getType());
+    assertEquals("", table.getVariant());
   }
 
   @Test
   public void japaneseUsesZenkakuByDefault() throws Exception {
     SymbolTable table = new SymbolTable("ja", Optional.empty(), emptyList());
-    assertEquals("zenkaku", table.getType());
+    assertEquals("zenkaku", table.getVariant());
     assertEquals('。', table.getSymbol(FULL_STOP).getValue());
     assertEquals('、', table.getSymbol(COMMA).getValue());
   }
@@ -27,7 +27,7 @@ public class SymbolTableTest {
   @Test
   public void japaneseCanUseAVariationOfZenkaku() throws Exception {
     SymbolTable table = new SymbolTable("ja", Optional.of("zenkaku2"), emptyList());
-    assertEquals("zenkaku2", table.getType());
+    assertEquals("zenkaku2", table.getVariant());
     assertEquals('．', table.getSymbol(FULL_STOP).getValue());
     assertEquals('，', table.getSymbol(COMMA).getValue());
   }
@@ -35,7 +35,7 @@ public class SymbolTableTest {
   @Test
   public void japaneseCanUseHankaku() throws Exception {
     SymbolTable table = new SymbolTable("ja", Optional.of("hankaku"), emptyList());
-    assertEquals("hankaku", table.getType());
+    assertEquals("hankaku", table.getVariant());
     assertEquals('.', table.getSymbol(FULL_STOP).getValue());
     assertEquals(',', table.getSymbol(COMMA).getValue());
   }
diff --git a/redpen-server/src/test/java/cc/redpen/server/api/RedPenConfigurationResourceTest.java b/redpen-server/src/test/java/cc/redpen/server/api/RedPenConfigurationResourceTest.java
index 8a578422..ce68b99b 100644
--- a/redpen-server/src/test/java/cc/redpen/server/api/RedPenConfigurationResourceTest.java
+++ b/redpen-server/src/test/java/cc/redpen/server/api/RedPenConfigurationResourceTest.java
@@ -69,7 +69,7 @@ public class RedPenConfigurationResourceTest {
 
         JSONObject ja = redpens.getJSONObject("ja");
         assertEquals("ja", ja.getString("lang"));
-        assertEquals("zenkaku", ja.getString("type"));
+        assertEquals("zenkaku", ja.getString("variant"));
         assertEquals(JapaneseTokenizer.class.getName(), ja.getString("tokenizer"));
         assertTrue(!ja.getString("validators").isEmpty());
         assertTrue(!ja.getString("symbols").isEmpty());
diff --git a/redpen-server/src/test/java/cc/redpen/server/api/RedPenServiceTest.java b/redpen-server/src/test/java/cc/redpen/server/api/RedPenServiceTest.java
index 0648c1b4..5b60ffe6 100644
--- a/redpen-server/src/test/java/cc/redpen/server/api/RedPenServiceTest.java
+++ b/redpen-server/src/test/java/cc/redpen/server/api/RedPenServiceTest.java
@@ -17,12 +17,12 @@ public class RedPenServiceTest {
     assertEquals("en", redPens.get("en").getConfiguration().getLang());
 
     assertEquals("ja", redPens.get("ja").getConfiguration().getLang());
-    assertEquals("zenkaku", redPens.get("ja").getConfiguration().getType());
+    assertEquals("zenkaku", redPens.get("ja").getConfiguration().getVariant());
 
     assertEquals("ja", redPens.get("ja.zenkaku2").getConfiguration().getLang());
-    assertEquals("zenkaku2", redPens.get("ja.zenkaku2").getConfiguration().getType());
+    assertEquals("zenkaku2", redPens.get("ja.zenkaku2").getConfiguration().getVariant());
 
     assertEquals("ja", redPens.get("ja.hankaku").getConfiguration().getLang());
-    assertEquals("hankaku", redPens.get("ja.hankaku").getConfiguration().getType());
+    assertEquals("hankaku", redPens.get("ja.hankaku").getConfiguration().getVariant());
   }
 }
\ No newline at end of file