Search string pattern in file


package com.search;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;

public class SearchMain {
	
	/**
	 * F:\\test 
	 * F:\\test2
	 */
	private static final String SRC_LOCATION = "E:\\workspace\\SearchPattern\\src\\srcLocation.txt";

	/**
	 * Service 
	 * TopItems 
	 * SearchResult
	 */
	private static final String TARGET_STR_LOCATION = "E:\\workspace\\SearchPattern\\src\\targetText.txt";
	
	/**
	 * Possible patterns for Data Type "Service"
	 * 
	 * response.get('Service') 
	 * response.get('Service').abc
	 * response.get("Service") 
	 * response.get("Service").abc 
	 * response.Service
	 * response.Service.abc Service
	 */
	private static final Map<String, Set<String>> resultMap = new LinkedHashMap<String, Set<String>>();

	private static final String PATTERN_STR = "(?<=[.\'\"]?)%s(?=[.\'\"]?)";// (?<=[.'"]?)Service(?=[.'"]?)

	public static void main(String[] args) {
		try {
			getSrcDirList(SRC_LOCATION);
			printResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void getSrcDirList(final String srcDirLoc) throws Exception {
		File file = new File(srcDirLoc);
		Scanner scanner = null;
		try {
			scanner = new Scanner(file);
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				System.out.println(line);
				traverseDir(line);
			}
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private static void traverseDir(final String srcDirLoc) throws Exception {
		File[] faFiles = new File(srcDirLoc).listFiles();
		for (File file : faFiles) {
			findinFile(file);
			if (file.isDirectory()) {
				traverseDir(file.getAbsolutePath());
			}
		}
	}

	public static void findinFile(final File srcFile) throws Exception {
		File file = new File(TARGET_STR_LOCATION);
		Scanner scanner = null;
		try {
			scanner = new Scanner(file);
			Set<String> relaventPatternSet = new HashSet<String>();
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();
				System.out.println(line);
				Pattern pattern = parsePattern(line);
				int occurrances = searchOccurrance(srcFile, pattern);
				if (occurrances > 0) {
					relaventPatternSet.add(line);
				}
			}
			if (!relaventPatternSet.isEmpty()) {
				resultMap.put(srcFile.getAbsolutePath(), relaventPatternSet);
			}
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	public static Pattern parsePattern(final String targetStr) throws Exception {
		String formattedPatternStr = String.format(PATTERN_STR, targetStr);
		System.out.println(formattedPatternStr);
		return Pattern.compile(formattedPatternStr);
	}

	public static int searchOccurrance(final File srcFile, Pattern pattern)
			throws Exception {
		String srcFileLoc = srcFile.getAbsolutePath();
		System.out.println(srcFileLoc);
		int tot = 0;
		Scanner scanner = new Scanner(new File(srcFileLoc));
		String pat = null;
		try {
			while ((pat = scanner.findWithinHorizon(pattern, 0)) != null) {
				System.out.println("src text --- >> : " + pat);
				tot++;
			}
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		System.out.println("Results found: " + tot);
		return tot;
	}

	private static void printResult() throws Exception {
		File fpw = new File("F:\\test\\result.txt");
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(fpw);
			for (Map.Entry<String, Set<String>> entry : resultMap.entrySet()) {
				String key = entry.getKey();
				Set<String> value = entry.getValue();
				pw.println("File --------- >> : " + key);
				pw.println();
				for (String pattern : value) {
					pw.println(pattern);
				}
				pw.println();
			}
		} finally {
			if (pw != null) {
				pw.close();
			}
		}
	}
}