Commit bc11fa6b authored by Daniel Honsel's avatar Daniel Honsel
Browse files

Implement validation rule checkNoTabs.

parent a93cb71a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ public class FileParser implements Callable<Resource> {
		Resource res;
		int lines = 0;
		try {
			lines = T3Q.countLines(file.getPath());
			lines = T3Q.countLines(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
+63 −18
Original line number Diff line number Diff line
package de.ugoe.cs.swe.T3Q;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.cli.CommandLine;
@@ -16,6 +21,8 @@ import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import de.ugoe.cs.swe.TTCN3Configuration.QualityCheckProfile;
import de.ugoe.cs.swe.common.ConfigTools;
@@ -24,6 +31,7 @@ import de.ugoe.cs.swe.common.T3QOptionsHandler;
import de.ugoe.cs.swe.common.exceptions.TerminationException;
import de.ugoe.cs.swe.common.logging.LoggingInterface;
import de.ugoe.cs.swe.common.logging.LoggingInterface.LogLevel;
import de.ugoe.cs.swe.scoping.TTCN3GlobalScopeProvider;

public class T3Q {
	private static String versionNumber = "v2.0.0b17";
@@ -331,24 +339,61 @@ public class T3Q {
		return buildStamp;
	}
	
	public static int countLines(String filename) throws IOException {
		InputStream is = new BufferedInputStream(new FileInputStream(filename));
		try {
			byte[] c = new byte[1024];
			int count = 0;
			int readChars = 0;
			boolean empty = true;
			while ((readChars = is.read(c)) != -1) {
				empty = false;
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\n') {
						++count;
	private static List<String> readFile(String filename) throws FileNotFoundException, IOException {	
		List<String> file = Lists.newArrayList();
		FileInputStream stream = new FileInputStream(filename);
		//InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
		InputStreamReader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader);
		String line = (buffer.readLine());

		while (line != null) {
			file.add(line);
			line = (buffer.readLine());
		}

		buffer.close();
		reader.close();
		stream.close();
		
		return file;
	}	
	
	private static void findTabs(String filename, List<String> file) {
		if (!activeProfile.isCheckNoTabs()) {
			return;
		}
			return (count == 0 && !empty) ? 1 : count;
		} finally {
			is.close();
		
		for (int i = 0; i < file.size(); i++) {
			String line = file.get(i);
			for (int j = 0; j < line.length(); j++) {
				if (line.charAt(j) == '\t') {
					String[] position = {Integer.toString(i), Integer.toString(j)};
					if (TTCN3GlobalScopeProvider.FOUND_TABS.containsKey(filename)) {
						TTCN3GlobalScopeProvider.FOUND_TABS.get(filename).add(position);
					} else {
						List<String[]> list = Lists.newArrayList();
						list.add(position);
						TTCN3GlobalScopeProvider.FOUND_TABS.put(filename, list);
					}
				}
			}
		}
	}
	
	/**
	 * This method counts the lines of a file. Furthermore, it executes methods to find tabs
	 * in the current file. Found tabs will be stored in TTCN3GlobalScopeProvider.FOUND_TABS.
	 */
	public static int countLines(File data) throws IOException {
		List<String> file = readFile(data.getPath());
		// This only works with unique file names, but this assumption should be fulfilled
		// otherwise the path should be hashed
		findTabs(data.getName(), file);
		if (file != null) {
			return file.size();
		} else {
			return 0;
		}
	}
}
+3 −0
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@ public class TTCN3GlobalScopeProvider extends AbstractGlobalScopeProvider {
	public static final List<Resource> RESOURCES = Collections.synchronizedList(Lists.<Resource> newArrayList());
	public static final Map<String, TTCN3Module> NAMED_MODULES = Collections.synchronizedMap(Maps.<String, TTCN3Module> newHashMap());
	
	// array[0] = line, array[1] = column
	public static final Map<String, List<String[]>> FOUND_TABS = Collections.synchronizedMap(Maps.<String, List<String[]>> newHashMap());		


	@Override
	public IScope getScope(Resource resource, final EReference reference, Predicate<IEObjectDescription> filter) {
+27 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import static de.ugoe.cs.swe.scoping.TTCN3GlobalScopeProvider.*

import static extension de.ugoe.cs.swe.common.TTCN3ReferenceHelper.*
import static extension de.ugoe.cs.swe.common.TTCN3ScopeHelper.*
import static extension org.eclipse.emf.ecore.util.EcoreUtil.*
import static extension org.eclipse.xtext.EcoreUtil2.*

class CheckDefinitionComeFirstParameter {
@@ -1346,6 +1347,32 @@ class CodeStyleValidator extends AbstractDeclarativeValidator {
		}
	}
	
	@Check
	def checkNoTabs(TTCN3Module module) {
		if (!activeProfile.checkNoTabs) {
			return
		}
		
		var String[] segments = module.URI.path.split("\\\\")

		// This only works with unique file names, but this assumption should be fulfilled
		// otherwise the path should be hashed
		if (TTCN3GlobalScopeProvider.FOUND_TABS.containsKey(segments.last)) {
			for (String[] s : TTCN3GlobalScopeProvider.FOUND_TABS.get(segments.last)) {
				statistics.incrementCountStyle
				val message = "Line contains a tab character! (at column " + s.get(1) + ")"
				warning(
					message,
					null,
					MessageClass.STYLE.toString,
					s.get(0),
					s.get(0),
					MiscTools.getMethodName()
				);
			}				
		}		
	}	
	
	override register(EValidatorRegistrar registrar) {
		// not needed for classes used as ComposedCheck
	}