Revision: 6694
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=6694&view=rev
Author:   dnaber
Date:     2012-04-06 09:56:34 +0000 (Fri, 06 Apr 2012)
Log Message:
-----------
some code style changes

Modified Paths:
--------------
    trunk/JLanguageTool/ltdiff/ltdiff.java

Modified: trunk/JLanguageTool/ltdiff/ltdiff.java
===================================================================
--- trunk/JLanguageTool/ltdiff/ltdiff.java      2012-04-06 09:55:31 UTC (rev 
6693)
+++ trunk/JLanguageTool/ltdiff/ltdiff.java      2012-04-06 09:56:34 UTC (rev 
6694)
@@ -1,213 +1,210 @@
+import java.io.*;
 import java.util.ArrayList;
-import java.io.File;
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.io.BufferedWriter;
-import java.io.FileWriter;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.util.Collections;
-import java.util.Comparator;
+import java.util.List;
+import java.util.Scanner;
 
-class Rule implements Comparable<Rule> {
-  public String id;
-  public String name="";
-  public ArrayList<String> correct = new ArrayList<String>();
-  public ArrayList<String> incorrect = new ArrayList<String>();
+/**
+ * Generates an HTML report of added, deleted and modified rules between 
versions of LanguageTool.
+ * See ltdiff.bash.
+ */
+class VersionDiffGenerator {
   
-  public int numberOfExamples() {
-    return correct.size()+incorrect.size();
+  public static void main(String[] args) throws IOException {
+    final VersionDiffGenerator generator = new VersionDiffGenerator();
+    generator.makeDiff(args[0]);
   }
   
-  public String examples(boolean all) {
+  private void makeDiff(String lang) throws IOException {
     
-    String s = "<div>";
+    final List<Rule> oldRules = new ArrayList<Rule>(); // rules in old 
grammar.xml
+    final List<Rule> newRules = new ArrayList<Rule>(); // rules in new 
grammar.xml
+    final List<Rule> modifiedRules = new ArrayList<Rule>();
     
-    for (int i=0; i<incorrect.size(); i++) {
-      s += "<span>7FINDERR</span>" + incorrect.get(i) + "<br/>";
-    }
-    
-    if (all) {
-      for (int i=0; i<correct.size(); i++) {
-      s += "<span>8FINDNOTERR</span>" + correct.get(i) + "<br/>";
+    for (int i = 0; i < 2; i++) {
+
+      final List<Rule> rules;
+      if (i == 0) {
+        rules = oldRules;
+      } else {
+        rules = newRules;
       }
-    }
     
-    s = s.substring(0,s.length()-5) + "</div>";
-        
-    return s;
-    
-  }
-  
-  @Override
-  public int compareTo(Rule r) {
-    return this.name.compareTo(r.name);
-  }
-}
+      final Scanner scanner = new Scanner(new FileReader(i == 0 ? "old" : 
"new"));
 
-class VersionDiffGenerator {
-  
-  public static void main(String[] args) {
-    
-    String lang = args[0];
-    
-    ArrayList<Rule> old_rules = new ArrayList<Rule>(); // rules in old 
grammar.xml
-    ArrayList<Rule> new_rules = new ArrayList<Rule>(); // rules in new 
grammar.xml
-    ArrayList<Rule> modified_rules = new ArrayList<Rule>();
-    
-    try {
+      Rule r = new Rule();
       
-      for (int i=0; i<2; i++) {
-        
-        ArrayList<Rule> rules;
-        
-        if(i==0)
-          rules = old_rules;
-        else
-          rules = new_rules;
+      // loop through all lines
+      while (scanner.hasNextLine()) {
+        String line = scanner.nextLine();
       
-        BufferedReader in = new BufferedReader(new FileReader(i==0 ? "old" : 
"new"));
-        String line;
-        
-        Rule r = new Rule();
-        
-        // loop through all lines
-        while ((line = in.readLine()) != null) {
-        
-          if (line.contains("id=\"") && line.contains("rule")) {
-            
-            if (!line.contains("name=\"")) // merge with the following line if 
the name is there (e.g. sk)
-              line += in.readLine();
-            
-            if (r.numberOfExamples() > 0) {
-              rules.add(r);
-              r = new Rule();
+        if (line.contains("id=\"") && line.contains("rule")) {
+          
+          if (!line.contains("name=\"")) { // merge with the following line if 
the name is there (e.g. sk)
+            line += scanner.nextLine();
+          }
+          
+          if (r.numberOfExamples() > 0) {
+            rules.add(r);
+            r = new Rule();
+          }
+          
+          r.id = line;
+          r.name = line;
+          
+          r.id = r.id.replaceAll(".*id=\"","").replaceAll("\".*","");
+          r.name = r.name.replaceAll(".*name=\"","").replaceAll("\".*","");
+
+          for (Rule rule : rules) { // ensure that the name is unique
+            if (r.name.equals(rule.name)) {
+              r.name += " ";
             }
-            
-            r.id = line;
-            r.name = line;
-            
-            r.id = r.id.replaceAll(".*id=\"","").replaceAll("\".*","");
-            r.name = r.name.replaceAll(".*name=\"","").replaceAll("\".*","");
-            
-            for (int j = 0; j < rules.size(); j++) { // ensure that the name 
is unique
-              if (r.name.equals(rules.get(j).name)) {
-                r.name += " ";
-              }
-            }
-            
-          } else if (line.contains("type=\"correct\"")) {
-            
-            while (!line.contains("</example>")) // merge with the following 
line(s) if the example continues there
-              line += in.readLine();
-            
r.correct.add(line.replaceAll("marker","b").replaceAll(".*<example.*?>","").replaceAll("</example>.*",""));
-            
-          } else if (line.contains("type=\"incorrect\"")) {
+          }
           
-            while (!line.contains("</example>"))
-              line += in.readLine();
-            
r.incorrect.add(line.replaceAll("marker","b").replaceAll(".*<example.*?>","").replaceAll("</example>.*",""));
-            
+        } else if (line.contains("type=\"correct\"")) {
+          
+          while (!line.contains("</example>")) { // merge with the following 
line(s) if the example continues there
+            line += scanner.nextLine();
           }
+          
r.correct.add(line.replaceAll("marker","b").replaceAll(".*<example.*?>","").replaceAll("</example>.*",""));
           
-        } // while(readLine)
+        } else if (line.contains("type=\"incorrect\"")) {
         
-        in.close();
+          while (!line.contains("</example>")) {
+            line += scanner.nextLine();
+          }
+          
r.incorrect.add(line.replaceAll("marker","b").replaceAll(".*<example.*?>","").replaceAll("</example>.*",""));
+          
+        }
         
-      }
+      } // while(readLine)
       
-    } catch (IOException e) {
-      System.err.println("Error 1: " + e.getMessage());
+      scanner.close();
+      
     }
-    
+      
     // sort rules by name
-    Collections.sort(old_rules);
-    Collections.sort(new_rules);
+    Collections.sort(oldRules);
+    Collections.sort(newRules);
     
     // create html file containing the tr elements
     
-    try {
+    final FileWriter fileWriter = new FileWriter("changes_" + lang + ".html");
+    final BufferedWriter out = new BufferedWriter(fileWriter);
+
+    for (Rule newRule1 : newRules) {
+
+      boolean found = false;
+
+      for (int j = 0; j < oldRules.size() && !found; j++) {
+
+        if (newRule1.id.equals(oldRules.get(j).id) || 
newRule1.name.equals(oldRules.get(j).name)) {
+
+          found = true;
+
+          if (newRule1.numberOfExamples() > 
oldRules.get(j).numberOfExamples()) { // if the new rules has more examples, it 
is considered to be improved
+
+            final Rule r = newRule1;
+
+            for (int k = 0; k < r.correct.size(); k++) { // remove examples 
which already exist in old rule
+
+              for (int l = 0; l < oldRules.get(j).correct.size(); l++) {
+
+                if (r.correct.get(k).equals(oldRules.get(j).correct.get(l))) {
+
+                  r.correct.remove(k);
+                  if (k > 0) k--;
+
+                } // if examples equal
+
+              } // for each old correct example
+
+            } // for each new correct example
+
+            for (int k = 0; k < r.incorrect.size(); k++) { // remove examples 
which already exist in old rule
+
+              for (int l = 0; l < oldRules.get(j).incorrect.size(); l++) {
+
+                if 
(r.incorrect.get(k).equals(oldRules.get(j).incorrect.get(l))) {
+
+                  r.incorrect.remove(k);
+                  if (k > 0) k--;
+
+                } // if examples equal
+
+              } // for each old incorrect example
+
+            } // for each new incorrect example
+
+            modifiedRules.add(r);
+
+          } // if new rules has more examples
+
+        } // if new rule is not new
+
+      } // for each old rule
+
+      if (!found) {
+        out.write("<tr class=\"new\"><td>4NEWRULE</td><td>" + newRule1.name + 
newRule1.getExamples(false) + "</td></tr>\n");
+      }
+
+    } // for each new rule
+
+    for (Rule modifiedRule : modifiedRules) {
+      out.write("<tr class=\"modified\"><td>6IMPROVEDRULE</td><td>" + 
modifiedRule.name + modifiedRule.getExamples(true) + "</td></tr>\n");
+    }
+
+    for (Rule oldRule : oldRules) {
+      boolean found = false;
+      for (Rule newRule : newRules) {
+        if (newRule.id.equals(oldRule.id) || 
newRule.name.equals(oldRule.name)) {
+          found = true;
+        }
+      }
+      if (!found) {
+        out.write("<tr class=\"removed\"><td>5REMOVEDRULE</td><td>" + 
oldRule.name + "</td></tr>\n");
+      }
+    }
     
-      FileWriter fstream = new FileWriter("changes_"+lang+".html");
-      BufferedWriter out = new BufferedWriter(fstream);
+    out.close();
+  }
+
+  class Rule implements Comparable<Rule> {
+
+    private final List<String> correct = new ArrayList<String>();
+    private final List<String> incorrect = new ArrayList<String>();
+
+    private String name = "";
+    private String id;
     
-      for (int i = 0; i < new_rules.size(); i++) {
+    int numberOfExamples() {
+      return correct.size() + incorrect.size();
+    }
+    
+    String getExamples(boolean all) {
       
-        boolean found = false;
-        
-        for (int j = 0; j < old_rules.size() && !found; j++) {
-        
-          if (new_rules.get(i).id.equals(old_rules.get(j).id) || 
new_rules.get(i).name.equals(old_rules.get(j).name)) {
-            
-            found = true;
-            
-            if (new_rules.get(i).numberOfExamples() > 
old_rules.get(j).numberOfExamples()) { // if the new rules has more examples, 
it is considered to be improved
-            
-              Rule r = new_rules.get(i);
-              
-              for (int k = 0; k < r.correct.size(); k++) { // remove examples 
which already exist in old rule
-              
-                for (int l = 0; l < old_rules.get(j).correct.size(); l++) {
-                
-                  if 
(r.correct.get(k).equals(old_rules.get(j).correct.get(l))) {
-                  
-                    r.correct.remove(k);
-                    if (k>0) k--;
-                    
-                  } // if examples equal
-                  
-                } // for each old correct example
-                
-              } // for each new correct example
-              
-              for (int k = 0; k < r.incorrect.size(); k++) { // remove 
examples which already exist in old rule
-              
-                for (int l = 0; l < old_rules.get(j).incorrect.size(); l++) {
-                
-                  if 
(r.incorrect.get(k).equals(old_rules.get(j).incorrect.get(l))) {
-                  
-                    r.incorrect.remove(k);
-                    if (k>0) k--;
-                    
-                  } // if examples equal
-                  
-                } // for each old incorrect example
-                
-              } // for each new incorrect example
-              
-              modified_rules.add(r);
-              
-            } // if new rules has more examples
-            
-          } // if new rule is not new
-          
-        } // for each old rule
-        
-        if (!found)
-          out.write("<tr class=\"new\"><td>4NEWRULE</td><td>" + 
new_rules.get(i).name + new_rules.get(i).examples(false) + "</td></tr>\n");
-        
-      } // for each new rule
-      
-      for (int i=0; i < modified_rules.size(); i++) {
-        out.write("<tr class=\"modified\"><td>6IMPROVEDRULE</td><td>" + 
modified_rules.get(i).name + modified_rules.get(i).examples(true) + 
"</td></tr>\n");
+      String s = "<div>";
+
+      for (String anIncorrect : incorrect) {
+        s += "<span>7FINDERR</span>" + anIncorrect + "<br/>";
       }
       
-      for (int i = 0; i < old_rules.size(); i++) {
-        boolean found = false;
-        for (int j = 0; j < new_rules.size(); j++)
-          if (new_rules.get(j).id.equals(old_rules.get(i).id) || 
new_rules.get(j).name.equals(old_rules.get(i).name))
-            found = true;
-        if (!found)
-          out.write("<tr class=\"removed\"><td>5REMOVEDRULE</td><td>" + 
old_rules.get(i).name + "</td></tr>\n");
+      if (all) {
+        for (String aCorrect : correct) {
+          s += "<span>8FINDNOTERR</span>" + aCorrect + "<br/>";
+        }
       }
       
-      out.close();
+      s = s.substring(0, s.length() - 5) + "</div>";
+          
+      return s;
       
-    } catch (Exception e) {
-      System.err.println("Error 2: " + e.getMessage());
     }
     
-    System.exit(0);
+    @Override
+    public int compareTo(Rule r) {
+      return this.name.compareTo(r.name);
+    }
   }
+
 }
\ No newline at end of file

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


------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Languagetool-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to