Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. package refactorapp;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import refactorapp.counters.*;
  8.  
  9. public class FileScaner {
  10.  
  11.     private CountResults resultCount;
  12.     private LineReader lineReader;
  13.     private String pattern;
  14.     private File file;
  15.  
  16.     public void scan(String baseDir, String pattern) {
  17.  
  18.        
  19.         this.pattern = pattern;
  20.         File[] dirStack = new File[100];
  21.         int dirStackPtr = 0;
  22.  
  23.         resultCount = new CountResults();
  24.         lineReader = new LineReader();
  25.  
  26.         File dir = new File(baseDir);
  27.         while (dir != null) {
  28.             System.out.println("Scanning " + dir);
  29.             File[] fileList = dir.listFiles();
  30.             for (int i = 0; i < fileList.length; i++) {
  31.                 file = fileList[i];
  32.                 System.out.println("Processing " + file);
  33.                 if (file.isDirectory()) {
  34.                     dirStack[dirStackPtr++] = file;
  35.                     continue;
  36.                 }
  37.  
  38.                 if (file.isFile()) {
  39.                     i = 0;
  40.                     processFile(file, pattern);
  41.                 }
  42.             }
  43.             dir = null;
  44.             if (dirStackPtr > 0) {
  45.                 dir = dirStack[--dirStackPtr];
  46.             }
  47.         }
  48.         getResultCount().print();
  49.     }
  50.  
  51.     private void processFile(File file, String pattern) {
  52.  
  53.         String fileExtension = getFileExtension(file);
  54.         getResultCount().incrementFileCount();
  55.         try {
  56.             if (fileExtension.equals(".java")) {
  57.                 processJavaFile(pattern, file);
  58.             } else if (fileExtension.equals(".sh")) {
  59.                 processShFile(pattern, file);
  60.             } else {
  61.                 processAnyFile(pattern, file);
  62.             }
  63.         } catch (IOException ex) {
  64.             System.out.println("IOException on file " + file.getAbsolutePath() + file.getName());
  65.         }
  66.     }
  67.  
  68.     private String getFileExtension(File file) {
  69.         if (file.getName().contains(".")) {
  70.             return file.getName().substring(file.getName().lastIndexOf("."));
  71.         }
  72.         return "";
  73.  
  74.     }
  75.  
  76.     class BaseLineProcessor implements LineProcessor {
  77.  
  78.         protected CounterChain counterChain;
  79.         protected List<Counter> counterList = new ArrayList<Counter>();
  80.  
  81.         void add(Counter counter) {
  82.             counterList.add(counter);
  83.         }
  84.  
  85.         @Override
  86.         public void processLine(String line) {
  87.             throw new UnsupportedOperationException("Not supported yet.");
  88.         }
  89.  
  90.         class ShFileProcessor extends BaseLineProcessor {
  91.  
  92.             @Override
  93.             public void processLine(String line) {
  94.                 counterChain.processLine(line, resultCount);
  95.             }
  96.  
  97.             ShFileProcessor() {
  98.                
  99.                 add(new LineCounter());
  100.                 add(new ShRemarkCounter());
  101.                 add(new PatternMatchesCounter(pattern, file));
  102.                 add(new WordCounter());
  103.                 add(new CharacterCounter());
  104.                 add(new NonEmptyLinesCounter());
  105.                 counterChain = new CounterChain(counterList);
  106.             }
  107.         }
  108.  
  109.         public LineProcessor returnShFileProcessor() {
  110.             return new ShFileProcessor();
  111.         }
  112.  
  113.         class JavaFileProcessor extends BaseLineProcessor {
  114.  
  115.             @Override
  116.             public void processLine(String line) {
  117.                 counterChain.processLine(line, resultCount);
  118.             }
  119.  
  120.             JavaFileProcessor() {
  121.                
  122.                 add(new LineCounter());
  123.                 add(new JavaRemarkCounter());
  124.                 add(new PatternMatchesCounter(pattern, file));
  125.                 add(new JavaWordsCounter());
  126.                 add(new CharacterCounter());
  127.                 add(new NonEmptyLinesCounter());
  128.                 counterChain = new CounterChain(counterList);
  129.             }
  130.         }
  131.  
  132.         public LineProcessor returnJavaFileProcessor() {
  133.             return new JavaFileProcessor();
  134.         }
  135.  
  136.         public class AnyFileProcessor extends BaseLineProcessor {
  137.  
  138.             @Override
  139.             public void processLine(String line) {
  140.                 counterChain.processLine(line, resultCount);
  141.             }
  142.  
  143.             AnyFileProcessor() {
  144.                
  145.                 add(new LineCounter());
  146.                 add(new PatternMatchesCounter(pattern, file));
  147.                 add(new WordCounter());
  148.                 add(new CharacterCounter());
  149.                 add(new NonEmptyLinesCounter());
  150.                 counterChain = new CounterChain(counterList);
  151.             }
  152.         }
  153.  
  154.         public LineProcessor returnAnyFileProcessor() {
  155.             return new AnyFileProcessor();
  156.         }
  157.     }
  158.  
  159.     private void processAnyFile(String pattern, File file) throws IOException {
  160.         BaseLineProcessor lineProcessor = new BaseLineProcessor();
  161.         lineReader.readLine(file, lineProcessor.returnAnyFileProcessor());
  162.     }
  163.  
  164.     private void processShFile(String pattern, File file) throws IOException {
  165.         BaseLineProcessor lineProcessor = new BaseLineProcessor();
  166.         lineReader.readLine(file, lineProcessor.returnShFileProcessor());
  167.     }
  168.  
  169.     private void processJavaFile(String pattern, File file) throws IOException {
  170.         BaseLineProcessor lineProcessor = new BaseLineProcessor();
  171.         lineReader.readLine(file, lineProcessor.returnJavaFileProcessor());
  172.     }
  173.  
  174.     public static void main(String[] args) {
  175.     }
  176.  
  177.     /**
  178.      * @return the resultCount
  179.      */
  180.     public CountResults getResultCount() {
  181.         return resultCount;
  182.     }
  183.  
  184.     /**
  185.      * @return the pattern
  186.      */
  187.     public String getPattern() {
  188.         return pattern;
  189.     }
  190.  
  191.     /**
  192.      * @return the file
  193.      */
  194.     public File getFile() {
  195.         return file;
  196.     }
  197. }
Add Comment
Please, Sign In to add comment