Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. /**
  2.  *
  3.  *  @author Dębski Piotr S16213
  4.  *
  5.  */
  6.  
  7. package zad2;
  8.  
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.nio.file.Files;
  12. import java.nio.file.Paths;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15.  
  16. public class Finder {
  17.    
  18.     public int count = 0;
  19.     public int countString = 0;
  20.     public String source;
  21.     Pattern p = Pattern.compile("if\\s*\\(");
  22.     Pattern p1 = Pattern.compile("\\/\\*(.|[\\r\\n])*?\\*\\/"); // wyszukuje wszystkie komentarze multiline
  23.     Pattern p3 = Pattern.compile("//(\\s|\\w|\\n|\\r)+"); //wyszukuje wszystkie komentarze online
  24.     Pattern p4 = Pattern.compile("\"(.)+\""); // wyszukuje wszystkie Stringi
  25.    
  26.     Pattern p2;
  27.     String content;
  28.  
  29.     public Finder(String source) throws IOException{
  30.         File f = new File(source);
  31.         if(f.exists() && !f.isDirectory()){
  32.             this.source = source;
  33.             content = new String(Files.readAllBytes(Paths.get(this.source)));
  34.         } else{
  35.             System.out.println("Error! File not found");
  36.         }
  37.     }
  38.    
  39.     public int getIfCount() throws IOException{
  40.        
  41.         content = content.replaceAll("\\/\\*(.|[\\r\\n])*?\\*\\/", "");
  42.         content = content.replaceAll("//(\\s|\\w|\\n|\\r)+", "");
  43.         content = content.replaceAll("\"(.)+\"", "");
  44.         Matcher m = p.matcher(content);
  45.        
  46.         while(m.find()){
  47.             count++;
  48.         }
  49.         return count;
  50.     }
  51.    
  52.     public int getStringCount(String s){
  53.         p2 = Pattern.compile(s);
  54.         Matcher m = p2.matcher(content);
  55.        
  56.         while(m.find()){
  57.             countString++;
  58.         }
  59.         return countString;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement