Skip to content

Commit

Permalink
[BZ1004869] jcr2vfs: fix comments (# -> //) migration
Browse files Browse the repository at this point in the history
 * 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
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 32 deletions.
@@ -1,8 +1,5 @@
package org.drools.workbench.jcr2vfsmigration.migrater.asset;

import java.util.Map;
import java.util.Scanner;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -11,6 +8,7 @@
import org.drools.guvnor.client.rpc.Module;
import org.drools.guvnor.server.RepositoryAssetService;
import org.drools.repository.AssetItem;
import org.drools.workbench.jcr2vfsmigration.migrater.util.DRLMigrationUtils;
import org.drools.workbench.jcr2vfsmigration.migrater.util.MigrationPathManager;
import org.kie.commons.io.IOService;
import org.kie.commons.java.nio.base.options.CommentedOption;
Expand Down Expand Up @@ -50,26 +48,13 @@ public void migrate( Module jcrModule,
String content = jcrAssetItem.getContent();


//Support for # has been removed from Drools Expert
// Support for # has been removed from Drools Expert
if (AssetFormats.DSL.equals(jcrAssetItem.getFormat())
|| AssetFormats.DSL_TEMPLATE_RULE.equals(jcrAssetItem.getFormat())
|| AssetFormats.RULE_TEMPLATE.equals(jcrAssetItem.getFormat())
|| AssetFormats.DRL.equals(jcrAssetItem.getFormat())
|| AssetFormats.DSL_TEMPLATE_RULE.equals(jcrAssetItem.getFormat())
|| AssetFormats.RULE_TEMPLATE.equals(jcrAssetItem.getFormat())
|| AssetFormats.DRL.equals(jcrAssetItem.getFormat())
|| AssetFormats.FUNCTION.equals(jcrAssetItem.getFormat())) {
StringBuffer sb = new StringBuffer();
Scanner scanner = new Scanner(content);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.startsWith("#")) {
sb.append(line.replaceFirst("#", "//"));
} else {
sb.append(line);
}
}

scanner.close();
content = sb.toString();
content = DRLMigrationUtils.migrateStartOfCommentChar(content);
}

ioService.write( nioPath,
Expand Down
@@ -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();
}

}
@@ -1,23 +1,19 @@
package org.drools.workbench.jcr2vfsmigration;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.drools.workbench.jcr2vfsmigration.vfs.IOServiceFactory;
import org.junit.Ignore;
import org.junit.Test;
import org.kie.commons.java.nio.fs.jgit.JGitFileSystemProvider;

import static org.junit.Assert.*;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static org.junit.Assert.assertNotNull;

public class Jcr2VfsMigrationAppTest {

Expand Down
@@ -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);
}

}

0 comments on commit b633aa7

Please sign in to comment.