Skip to content
Snippets Groups Projects
Commit 97ead8b0 authored by felmer's avatar felmer
Browse files

LMS-663 JavaCodeNormalizer with test introduced.

SVN: 9359
parent b61616a3
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2008 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
/**
* Normalizes multiline Java code. All lines are trimmed. All block comments and single line
* comments are removed. Consecutive lines are put into one line separated by a space character.
* Empty lines and comment lines starting a new line in the normalized output.
*
* @author Franz-Josef Elmer
*/
public class JavaCodeNormalizer
{
private static interface IState
{
public IState next(PrintWriter writer, String trimmedLine);
}
private static final class Code implements IState
{
static final IState FIRST_LINE = new Code(true);
static final IState LINE = new Code(false);
private final boolean firstLine;
Code(boolean firstLine)
{
this.firstLine = firstLine;
}
public IState next(PrintWriter writer, String trimmedLine)
{
if (trimmedLine.length() == 0 || trimmedLine.startsWith("//"))
{
return FIRST_LINE;
} else if (trimmedLine.startsWith("/*"))
{
return Comment.INSTANCE;
}
if (firstLine)
{
writer.println();
} else
{
writer.print(' ');
}
writer.print(removeInlineComment(trimmedLine));
return LINE;
}
private String removeInlineComment(String trimmedLine)
{
int indexOfInlineComment = trimmedLine.indexOf("//");
if (indexOfInlineComment < 0)
{
return trimmedLine;
}
return trimmedLine.substring(0, indexOfInlineComment).trim();
}
}
private static final class Comment implements IState
{
static final IState INSTANCE = new Comment();
public IState next(PrintWriter writer, String trimmedLine)
{
return trimmedLine.endsWith("*/") ? Code.FIRST_LINE : this;
}
}
private JavaCodeNormalizer()
{
}
/**
* Returns the specified Java code in a normalized way.
*/
public static String normalizeJavaCode(String javaCode)
{
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
BufferedReader reader = new BufferedReader(new StringReader(javaCode));
IState state = Code.FIRST_LINE;
String line;
try
{
while ((line = reader.readLine()) != null)
{
state = state.next(writer, line.trim());
}
return stringWriter.toString().trim();
} catch (IOException ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} finally
{
IOUtils.closeQuietly(reader);
}
}
}
/*
* Copyright 2008 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.test;
import static ch.systemsx.cisd.common.utilities.OSUtilities.LINE_SEPARATOR;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
/**
*
*
* @author Franz-Josef Elmer
*/
public class JavaCodeNormalizerTest extends AssertJUnit
{
private static final String EXAMPLE =
"/*\n"
+ " * Copyright 2008 ETH Zuerich, CISD\r"
+ " */\n"
+ "\n"
+ "package ch.systemsx.cisd.openbis.plugin.generic.shared;\n"
+ "\n"
+ "import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;\n"
+ "\n"
+ "public interface IGenericServer\n"
+ "{\n"
+ " /**\n"
+ " * blabla\n"
+ " */\n"
+ " @Transactional\n"
+ " public ExperimentPE get(int a,\n"
+ " // TODO\n"
+ " @Guard // checks ID\n"
+ " Id id);\n";
private static final String NORMALIZED_EXAMPLE =
"package ch.systemsx.cisd.openbis.plugin.generic.shared;" + LINE_SEPARATOR
+ "import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;" + LINE_SEPARATOR
+ "public interface IGenericServer {" + LINE_SEPARATOR
+ "@Transactional public ExperimentPE get(int a," + LINE_SEPARATOR
+ "@Guard Id id);";
@Test
public void test()
{
assertEquals(NORMALIZED_EXAMPLE, JavaCodeNormalizer.normalizeJavaCode(EXAMPLE));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment