Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
  2.  
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. /**
  11.  * Created by Robin Dort on 09.01.17.
  12.  */
  13. public class HtmlReader implements HtmlReaderInterface {
  14.  
  15.     /**
  16.      * Klassenvariablen file: File
  17.      *                  fr  : FileReader zum lesen des Files
  18.      *                  input  : Scanner;
  19.      */
  20.     private File file     = null;
  21.     private FileReader fr = null;
  22.     private Scanner input;
  23.  
  24.     /**
  25.      * Klassenkonstanten
  26.      */
  27.     private final static String MSG_FILE_NOT_READABLE = " Das File kann nicht gelesen werden!\n";
  28.     private static final String MSG_FILE_DONT_EXIST   = " Die File Datei existiert nicht!\n";
  29.     private static final String MSG_NO_FILE           = " Bei der Datei handelt es sich nicht um ein File! \n";
  30.     private static final String MSG_PATTERN_NOT_FOUND = " Das zu suchenden Pattern wurde nicht gefunden!\n";
  31.  
  32.     /**
  33.      * Konstruktor dem der Name des Files    übergeben wird und dieses dann einliest
  34.      *
  35.      * @param filename das zu lesende File
  36.      * @throws FileNotReadableException
  37.      * @throws FileDontExistException
  38.      * @throws NotAFileException
  39.      * @throws IOException
  40.      */
  41.     public HtmlReader (String filename) throws FileNotReadableException, FileDontExistException, NotAFileException,
  42.             IOException {
  43.         try {
  44.             this.file = new File(filename); // anlegen des Files
  45.             this.fr = new FileReader(file); // FileReader liest das File ein
  46.  
  47.             if (!file.canRead()) { // wenn das File nicht von FileReader lesbar ist
  48.                 throw new
  49.                         FileNotReadableException(MSG_FILE_NOT_READABLE);
  50.             }
  51.             if (!file.exists()) { // wenn die File Datei nicht existiert
  52.                 throw new
  53.                         FileDontExistException(MSG_FILE_DONT_EXIST);
  54.             }
  55.             if (!file.isFile()) { // wenn es sich nicht um ein File handelt
  56.                 throw new
  57.                         NotAFileException(MSG_NO_FILE);
  58.             }
  59.         }
  60.               catch (FileNotReadableException e) {
  61.                  e.printStackTrace();
  62.             } catch (FileDontExistException e) {
  63.                  e.printStackTrace();
  64.             } catch (NotAFileException e) {
  65.                  e.printStackTrace();
  66.             } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.  
  70.              finally {  // finally Anweisung die nach dem Lesen des Files den FileReader wieder schliesst
  71.             fr.close();
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Methode zum überprüfen ob es sich um eine HTML Datei handelt mithilfe von  Regex
  77.      * @throws PatternNotFoundException
  78.      * @throws IOException
  79.      */
  80.     public void pattern() throws PatternNotFoundException, IOException {
  81.         Pattern p;
  82.         Matcher m;
  83.         Scanner sc = new Scanner(file);//WICHTIG!! Scanner wird mit dem File initialisiert.
  84.  
  85.         String s = "<a[^>]+href=[\\\"']?([^\\\"']+)[\"']?[^>]*>(.+?)<\\/a>";
  86.         String s2 = sc.useDelimiter("\\A").next();
  87.  
  88.         p = Pattern.compile(s); // compile methode zum suchen eines Ausrucks
  89.         m = p.matcher(s2);
  90.  
  91.  
  92.         while (m.find()) {
  93.  
  94.             // wenn der Regex gefunden wurde
  95.             System.out.printf("\nDer zu suchende Regex wurde in der Html Datei gefunden!\nDer " +
  96.                     "zu komplette Link sieht folgendermaßen aus:\n %-20s %20s\n", m.group(1), m.group(2));
  97.  
  98.         }
  99.  
  100.     }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     /**
  107.      * Main Methode zum starten des Programms
  108.      * @param args übergebenes Array der Main Methode
  109.      */
  110.     public static void main (String [] args) {
  111.         try {
  112.             if (args.length == 1) { // java HtmlReader index.html -> Länge args == 1
  113.                  HtmlReader hr = new HtmlReader(args[0]);
  114.                  hr.pattern();
  115.             } else {
  116.                 System.out.print("Usage: Linkscan datei"); // Länge != 1
  117.             }
  118.  
  119.         } catch (Exception e) {
  120.             e.printStackTrace();
  121.         }
  122.  
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement