Revision: 49
Author: maka82
Date: Mon Aug 17 10:54:36 2009
Log: Fixed bug in regular expressions generator. It was causing application to break when back slash symbol is somewhere in source file. This allows to use this regex for highlighting code in HTML report generator. So, now matched code is highlighted in HTML report.
http://code.google.com/p/apache-rat-pd/source/detail?r=49

Modified:
 /trunk/src/main/java/org/apache/rat/pd/engines/google/RegexGenerator.java
 /trunk/src/main/java/org/apache/rat/pd/report/HtmlReportGenerator.java
/trunk/src/test/java/org/apache/rat/pd/engines/google/RegexGeneratorTest.java

=======================================
--- /trunk/src/main/java/org/apache/rat/pd/engines/google/RegexGenerator.java Sun Aug 16 16:43:54 2009 +++ /trunk/src/main/java/org/apache/rat/pd/engines/google/RegexGenerator.java Mon Aug 17 10:54:36 2009
@@ -25,8 +25,8 @@
  * standard. This class can generate regular expressions understandable to
* GoogleCodeSearch parsing provided source code. It replace reserved symbols in * POSIX extended regular expression syntax with proper regular expressions, and - * then replace regex matching whitespace to ensure that our search is non sensitive to
- * whitespace and new line characters.
+ * then replace regex matching whitespace to ensure that our search is non
+ * sensitive to whitespace and new line characters.
  *
  * @author Maka
  *
@@ -70,11 +70,12 @@
         */
protected final static String ZERO_OR_MORE_BLANK_SPACE_REGEX = "(\\\\s?)+";

-       protected final static String[] regExpresions = { L_PARENTHESIS,
-                       DOLLAR_SIGN, R_PARENTHESIS, L_SQUARE_BRACKET, 
R_SQUARE_BRACKET,
-                       L_CURLY_BRACKET, R_CURLY_BRACKET, DOT, QUESTION_MARK, 
ASTERIX,
-                       POWER, PLUS_SIGN, MINUS_SIGN, VERTICAL_LINE, LES_THEN, 
GREAT_THEN,
-                       SEMICOLOMN, COLON, EQUALS, SLASH, DOUBLE_QUOTES };
+       protected final static String[] regExpresions = { BACK_SLASH,
+                       L_PARENTHESIS, DOLLAR_SIGN, R_PARENTHESIS, 
L_SQUARE_BRACKET,
+                       R_SQUARE_BRACKET, L_CURLY_BRACKET, R_CURLY_BRACKET, DOT,
+                       QUESTION_MARK, ASTERIX, POWER, PLUS_SIGN, MINUS_SIGN,
+                       VERTICAL_LINE, LES_THEN, GREAT_THEN, SEMICOLOMN, COLON, 
EQUALS,
+                       SLASH, DOUBLE_QUOTES };

        /**
         * Returns regular expression of source code
@@ -95,6 +96,7 @@

        /**
         * Replace strings matching whitespace with proper regex
+        *
         * @param code
         *            string with multiple blank spaces
         * @return string without multiple blank spaces
@@ -105,8 +107,8 @@
        }

        /**
-        * Replace reserved symbols in
- * POSIX extended regular expression syntax with proper regular expressions + * Replace reserved symbols in POSIX extended regular expression syntax with
+        * proper regular expressions
         *
         * @param code
         *            string with symbols listed in RegexGenerator.regExpresions
@@ -115,12 +117,15 @@
         */
        protected String replaceNonLiteralWithProperRegex(String code) {
                for (String regex : regExpresions) {
-                       if (!regex.equals(DOLLAR_SIGN)) {
-                               code = code.replaceAll(regex, BACK_SLASH + 
regex);
-                       } else {
+                       if (regex.equals(DOLLAR_SIGN)) {
                                // for some reason, String.replaceAll("\$", 
"anything") always
                                // breaks
                                code = code.replace("$", "\\$");
+                       } else if (regex.equals(BACK_SLASH)) {
+                               // bug-fix for code consist backslash
+                               code = code.replace(BACK_SLASH, "\\\\");
+                       } else {
+                               code = code.replaceAll(regex, BACK_SLASH + 
regex);
                        }
                }
                return code;
@@ -137,7 +142,12 @@
         */
        protected String addWhitespaceAroundNonLiteral(String code) {
                for (String regex : regExpresions) {
-                       code = code.replaceAll(regex, BLANK_SPACE + regex + 
BLANK_SPACE);
+                       // in words beginning with
+                       // "\" it should not be whitespace inserted after "\".
+                       if (!regex.equals(BACK_SLASH)) {
+                               code = code
+                                               .replaceAll(regex, BLANK_SPACE 
+ regex + BLANK_SPACE);
+                       }
                }
                code = code.replaceAll(ONE_OR_MORE_BLANK_SPACE, BLANK_SPACE);
                return code.trim();
=======================================
--- /trunk/src/main/java/org/apache/rat/pd/report/HtmlReportGenerator.java Sun Aug 16 16:43:54 2009 +++ /trunk/src/main/java/org/apache/rat/pd/report/HtmlReportGenerator.java Mon Aug 17 10:54:36 2009
@@ -105,7 +105,8 @@
        }

        /**
-        * @return create search result list using html template
+        * @param reportEntry this object will be shown as HTML
+        * @return create search result list using HTML template
         */
        private String createSearchResultList(ReportEntry reportEntry) {
                String toRet = "";
@@ -118,9 +119,11 @@
                                        searchResult.getPackageName());
                        current = current.replace(this.SEARCH_RESULT_LICENSE, 
searchResult
                                        .getLicence());
-                       current = current.replace(this.SEARCH_RESULT_CODE, 
formatBetter(
-                                       searchResult.getMatchedCode(),
- searchResult.getCodeForQuery()).replaceAll("\\n", "<BR>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
+                       //TODO result code must be placed here - nor queried 
code
+                       current = current
+                                       .replace(this.SEARCH_RESULT_CODE, 
formatBetter(
+                                                       
searchResult.getMatchedCode(),
+                                                       
searchResult.getCodeForQuery()));
                        current = current.replace(this.SEARCH_RESULT_OWNER, 
searchResult
                                        .getOwner());
                        current = current.replace(this.SEARCH_RESULT_LANGUAGE, 
searchResult
@@ -134,14 +137,23 @@
                return toRet;
        }

+       /**
+        * @param matchedCode list of code parts which will be highlighted
+        * @param codeInClass code to format
+        * @return code formated using HTML tags
+        */
protected String formatBetter(List<String> matchedCode, String codeInClass) {
                String toRet = codeInClass;
-//             for (int i = 0; i < matchedCode.size(); i++) {
-//                     toRet = toRet.replaceAll("(?i:("
-//                                     + 
regexGenerator.stringToRegex(matchedCode.get(i)) + "))",
-//                                     "<b><span style=\"color: #800000\">" + 
matchedCode.get(i)
-//                                                     + "</span></b>");
-//             }
+               for (int i = 0; i < matchedCode.size(); i++) {
+                       toRet = toRet.replaceAll("(?i:("
+                                       + 
regexGenerator.stringToRegex(matchedCode.get(i)) + "))",
+                                       "<b><span style=\"color: #800000\">" + 
matchedCode.get(i)
+                                                       + "</span></b>");
+               }
+               // basic code highlighting
+               toRet = toRet.replaceAll("\\n",
+               "<BR>").replaceAll(" ", " &nbsp; ").replaceAll("\\t",
+               " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ");
                return toRet;
        }

=======================================
--- /trunk/src/test/java/org/apache/rat/pd/engines/google/RegexGeneratorTest.java Sun Aug 16 16:43:54 2009 +++ /trunk/src/test/java/org/apache/rat/pd/engines/google/RegexGeneratorTest.java Mon Aug 17 10:54:36 2009
@@ -30,17 +30,17 @@
private String TEST_CASE_1 = "public static void main(String[] args)";//()[] private String TEST_CASE_2 = "public static void main(String[] args){}";//{} private String TEST_CASE_3 = "private List<String> getArray(){ return array }";//less then and greater then
-       private String TEST_CASE_4 = "this.getClass()";//dot
-       private String TEST_CASE_5 = "for(String s : list)";//two dots
- private String TEST_CASE_6 = "evaluate == 1 ? true : false";//question mark
-       private String TEST_CASE_7 = "/*comment*/";//slash and asterix
-       private String TEST_CASE_8 = "1+2=3";//plus sign
-       private String TEST_CASE_9 = "3-2=1";//minus sign
-       private String TEST_CASE_10 = "this.number = number;";//semicilumn
-       private String TEST_CASE_11 = "\"Hello World\"";//semicilumn
-       private String TEST_CASE_12 = "$";//dollar sign
-       private String TEST_CASE_13 = "^";//power sign
-       private String TEST_CASE_14 = "\\";//back slash
+       private String TEST_CASE_4 = "this.getClass()";// dot
+       private String TEST_CASE_5 = "for(String s : list)";// two dots
+ private String TEST_CASE_6 = "evaluate == 1 ? true : false";// question mark
+       private String TEST_CASE_7 = "/*comment*/";// slash and asterix
+       private String TEST_CASE_8 = "1+2=3";// plus sign
+       private String TEST_CASE_9 = "3-2=1";// minus sign
+       private String TEST_CASE_10 = "this.number = number;";// semicilumn
+       private String TEST_CASE_11 = "\"Hello World\"";// semicilumn
+       private String TEST_CASE_12 = "$";// dollar sign
+       private String TEST_CASE_13 = "^";// power sign
+       private String TEST_CASE_14 = "\\";// back slash

        public void testAddWhitespaceAroundDot() {
                String regex = regexGenerator
@@ -104,44 +104,51 @@
                                "/(\\s?)+\\*(\\s?)+comment(\\s?)+\\*(\\s?)+/", 
regex);

                System.out.println(TEST_CASE_7 + " --------> " + regex);
-
+

                regex = regexGenerator.stringToRegex(TEST_CASE_8);
                assertEquals("I except that regex is .....",
                                "1(\\s?)+\\+(\\s?)+2(\\s?)+=(\\s?)+3", regex);

                System.out.println(TEST_CASE_8 + " --------> " + regex);
-
+
                regex = regexGenerator.stringToRegex(TEST_CASE_9);
                assertEquals("I except that regex is .....",
                                "3(\\s?)+\\-(\\s?)+2(\\s?)+=(\\s?)+1", regex);

                System.out.println(TEST_CASE_9 + " --------> " + regex);
-
+
                regex = regexGenerator.stringToRegex(TEST_CASE_10);
                assertEquals("I except that regex is .....",
-                               
"this(\\s?)+\\.(\\s?)+number(\\s?)+=(\\s?)+number(\\s?)+;", regex);
+                               
"this(\\s?)+\\.(\\s?)+number(\\s?)+=(\\s?)+number(\\s?)+;",
+                               regex);

                System.out.println(TEST_CASE_10 + " --------> " + regex);
-
+
                regex = regexGenerator.stringToRegex(TEST_CASE_11);
                assertEquals("I except that regex is .....",
                                "\"(\\s?)+Hello(\\s?)+World(\\s?)+\"", regex);

                System.out.println(TEST_CASE_11 + " --------> " + regex);
-
+               // there was a bug causing application to break when code 
consist
+               // dollar character
                regex = regexGenerator.stringToRegex(TEST_CASE_12);
-               assertEquals("I except that regex is .....",
-                               "\\$", regex);
+               assertEquals("I except that regex is .....", "\\$", regex);

                System.out.println(TEST_CASE_12 + " --------> " + regex);
-
+
                regex = regexGenerator.stringToRegex(TEST_CASE_13);
-               assertEquals("I except that regex is .....",
-                               "\\^", regex);
+               assertEquals("I except that regex is .....", "\\^", regex);

                System.out.println(TEST_CASE_13 + " --------> " + regex);
-
+
+               // there was a bug causing application to break when code 
consist
+               // backslash character
+               regex = regexGenerator.stringToRegex(TEST_CASE_14);
+               assertEquals("I except that regex is .....", "\\\\", regex);
+
+               System.out.println(TEST_CASE_14 + " --------> " + regex);
+
        }

        public void testAddWhitespaceAroundNonLiteral() {

Reply via email to