Parcourir la source

fix #878 mapping output is a single line

Shinsuke Sugaya il y a 8 ans
Parent
commit
47df8a9baa

+ 3 - 3
src/main/java/org/codelibs/fess/dict/kuromoji/KuromojiFile.java

@@ -121,17 +121,17 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
             String line = null;
             while ((line = reader.readLine()) != null) {
                 // Remove comments
-                line = line.replaceAll("#.*$", StringUtil.EMPTY);
+                final String replacedLine = line.replaceAll("#.*$", StringUtil.EMPTY).trim();
 
                 // Skip empty lines or comment lines
-                if (line.trim().length() == 0) {
+                if (replacedLine.length() == 0) {
                     if (updater != null) {
                         updater.write(line);
                     }
                     continue;
                 }
 
-                final String[] values = KuromojiCSVUtil.parse(line);
+                final String[] values = KuromojiCSVUtil.parse(replacedLine);
                 String token = null;
                 String segmentation = null;
                 String reading = null;

+ 17 - 5
src/main/java/org/codelibs/fess/dict/mapping/CharMappingFile.java

@@ -39,8 +39,12 @@ import org.codelibs.fess.Constants;
 import org.codelibs.fess.dict.DictionaryException;
 import org.codelibs.fess.dict.DictionaryFile;
 import org.dbflute.optional.OptionalEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class CharMappingFile extends DictionaryFile<CharMappingItem> {
+    private static final Logger logger = LoggerFactory.getLogger(CharMappingFile.class);
+
     private static final String MAPPING = "mapping";
 
     List<CharMappingItem> mappingItemList;
@@ -124,10 +128,10 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
             String line = null;
             while ((line = reader.readLine()) != null) {
                 // Remove comments
-                line = line.replaceAll("#.*$", StringUtil.EMPTY);
+                final String replacedLine = line.replaceAll("#.*$", StringUtil.EMPTY).trim();
 
                 // Skip empty lines or comment lines
-                if (line.trim().length() == 0) {
+                if (replacedLine.length() == 0) {
                     if (updater != null) {
                         updater.write(line);
                     }
@@ -137,17 +141,25 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
                 String[] inputs;
                 String output;
 
-                final Matcher m = parsePattern.matcher(line.trim());
+                final Matcher m = parsePattern.matcher(replacedLine);
 
                 if (!m.find()) {
-                    throw new DictionaryException("Failed to parse " + path);
+                    logger.warn("Failed to parse " + line + " in " + path);
+                    if (updater != null) {
+                        updater.write("# " + line);
+                    }
+                    continue;
                 }
 
                 inputs = m.group(1).trim().split(",");
                 output = m.group(2).trim();
 
                 if (inputs == null || output == null || inputs.length == 0) {
-                    throw new DictionaryException("Failed to parse " + path);
+                    logger.warn("Failed to parse " + line + " in " + path);
+                    if (updater != null) {
+                        updater.write("# " + line);
+                    }
+                    continue;
                 }
 
                 id++;

+ 2 - 2
src/main/java/org/codelibs/fess/dict/mapping/CharMappingItem.java

@@ -33,7 +33,7 @@ public class CharMappingItem extends DictionaryItem {
     public CharMappingItem(final long id, final String[] inputs, final String output) {
         this.id = id;
         this.inputs = inputs;
-        this.output = output;
+        this.output = output == null ? null : output.replace("\n", " ");
         Arrays.sort(inputs);
 
         if (id == 0) {
@@ -56,7 +56,7 @@ public class CharMappingItem extends DictionaryItem {
     }
 
     public void setNewOutput(final String newOutput) {
-        this.newOutput = newOutput;
+        this.newOutput = newOutput == null ? null : newOutput.replace("\n", " ");
     }
 
     public String[] getInputs() {

+ 1 - 2
src/main/webapp/WEB-INF/view/admin/dict/mapping/admin_dict_mapping_edit.jsp

@@ -105,8 +105,7 @@
 												key="labels.dict_mapping_target" /></label>
 										<div class="col-sm-9">
 											<la:errors property="output" />
-											<la:textarea property="output" rows="5"
-												styleClass="form-control" />
+											<la:text property="output" styleClass="form-control" />
 										</div>
 									</div>
 								</div>