Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BZ1004869] jcr2vfs: fix comments (# -> //) migration
* end of lines were thrown away, thats fixed now * comments _not_ starting at the start of line are migrated too e.g. 'some text # comment' 'some text // comment' * hash tags inside double and single qoutes are not touched * DSL debug mark '#/' is not touched * escaped quotes (\' and \") are ignored
- Loading branch information
Petr Siroky
committed
Sep 14, 2013
1 parent
62f4d49
commit b633aa7
Showing
4 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
.../src/main/java/org/drools/workbench/jcr2vfsmigration/migrater/util/DRLMigrationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2013 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.drools.workbench.jcr2vfsmigration.migrater.util; | ||
|
||
public class DRLMigrationUtils { | ||
|
||
/** | ||
* Replaces the hash tag (#) character with two slashes (//), but only for non String occurrences and '#/' occurrences used | ||
* for DSL and DSLR debugging. | ||
* <p/> | ||
* For example: | ||
* for >>some text with hash tag #<<< is returned >>>some text with hash tag //<<< | ||
* but for >>>hash tag inside quotes "#"<<< is returned the same string >>>hash tag inside quotes "#"<<< | ||
*/ | ||
public static String migrateStartOfCommentChar(String source) { | ||
boolean isSingleQuoted = false; | ||
boolean isDoubleQuoted = false; | ||
StringBuilder sbResult = new StringBuilder(); | ||
for (int charIndex = 0; charIndex < source.length(); charIndex++) { | ||
char currentChar = source.charAt(charIndex); | ||
switch (currentChar) { | ||
case '\'': | ||
boolean isSingleQuoteEscaped = charIndex > 0 && source.charAt(charIndex - 1) == '\\'; | ||
if (!isSingleQuoteEscaped) { | ||
if (isDoubleQuoted) { | ||
isSingleQuoted = false; | ||
} else { | ||
isSingleQuoted = !isSingleQuoted; | ||
} | ||
} else { | ||
// single quote is escaped -> do nothing | ||
} | ||
sbResult.append(currentChar); | ||
break; | ||
|
||
case '"': | ||
boolean isDoubleQuoteEscaped = charIndex > 0 && source.charAt(charIndex - 1) == '\\'; | ||
if (!isDoubleQuoteEscaped) { | ||
if (isSingleQuoted) { | ||
isDoubleQuoted = false; | ||
} else { | ||
isDoubleQuoted = !isDoubleQuoted; | ||
} | ||
} else { | ||
// double quote is escaped -> do nothing | ||
} | ||
sbResult.append(currentChar); | ||
break; | ||
|
||
case '#': | ||
// '#/' is used in DSL and DSLR for debugging so don't replace it | ||
boolean isDslDebugChar = (charIndex + 1 <= source.length() - 1) && source.charAt(charIndex + 1) == '/'; | ||
if (isSingleQuoted || isDoubleQuoted || isDslDebugChar) { | ||
sbResult.append(currentChar); | ||
} else { | ||
sbResult.append("//"); | ||
} | ||
break; | ||
|
||
default: | ||
sbResult.append(currentChar); | ||
} | ||
} | ||
return sbResult.toString(); | ||
} | ||
|
||
} |
18 changes: 7 additions & 11 deletions
18
...ion-core/src/test/java/org/drools/workbench/jcr2vfsmigration/Jcr2VfsMigrationAppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../test/java/org/drools/workbench/jcr2vfsmigration/migrater/util/DRLMigrationUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2013 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.drools.workbench.jcr2vfsmigration.migrater.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class DRLMigrationUtilsTest { | ||
|
||
@Test | ||
public void shouldMigrateStartOfCommentInLineStartingWithWhiteSpaces() { | ||
migrateCommentsAndAssertResult(" \t \t some text # some comment", " \t \t some text // some comment"); | ||
} | ||
|
||
@Test | ||
public void shouldMigrateStartOfCommentInLineStartingWithComment() { | ||
migrateCommentsAndAssertResult("# some comment", "// some comment"); | ||
} | ||
|
||
@Test | ||
public void shouldNotMigrateStartOfCommentInsideDoubleQuotes() { | ||
migrateCommentsAndAssertResult("hash tag \" # inside double quotes\"", "hash tag \" # inside double quotes\""); | ||
} | ||
|
||
@Test | ||
public void shouldNotMigrateStartOfCommentInsideSingleQuotes() { | ||
migrateCommentsAndAssertResult("hash tag ' # inside single quotes'", "hash tag ' # inside single quotes'"); | ||
} | ||
|
||
@Test | ||
public void shouldNotMigrateDslDebugConstruct() { | ||
// '#/' is used for debugging in DSL and DSLR -> the hash tag in there must be ignored | ||
migrateCommentsAndAssertResult("some text #/", "some text #/"); | ||
migrateCommentsAndAssertResult("#/ some text # some comment", "#/ some text // some comment"); | ||
} | ||
|
||
@Test | ||
public void shouldMigrateStartOfCommentIgnoringEscapedQuotes() { | ||
migrateCommentsAndAssertResult( | ||
"\"smtg \\\" # - should not be migrated\\\"\"", | ||
"\"smtg \\\" # - should not be migrated\\\"\""); | ||
migrateCommentsAndAssertResult( | ||
"'smtg \\\' # - should not be migrated\\\''", | ||
"'smtg \\\' # - should not be migrated\\\''"); | ||
} | ||
|
||
@Test | ||
public void shouldMigrateStartOfCommentInMultilineInput() { | ||
String input = "#/ some DSL debug value\n" + | ||
"rule \"test rule\"\n" + | ||
"when\n" + | ||
" Object(\"'#'this has sign should not be migrated\") # some comment\n" + | ||
"then\n" + | ||
" # some other comment\n" + | ||
" System.out.println(\"#8 this one neither\");\n" + | ||
"end\n"; | ||
|
||
String expectedResult = "#/ some DSL debug value\n" + | ||
"rule \"test rule\"\n" + | ||
"when\n" + | ||
" Object(\"'#'this has sign should not be migrated\") // some comment\n" + | ||
"then\n" + | ||
" // some other comment\n" + | ||
" System.out.println(\"#8 this one neither\");\n" + | ||
"end\n"; | ||
migrateCommentsAndAssertResult(input, expectedResult); | ||
} | ||
|
||
private void migrateCommentsAndAssertResult(String input, String expectedResult) { | ||
String actualResult = DRLMigrationUtils.migrateStartOfCommentChar(input); | ||
Assert.assertEquals("Start of comment char not correctly migrated (# -> //)!", expectedResult, actualResult); | ||
} | ||
|
||
} |