package org.aguinore;

import java.io.*;

class Downloader {
    private final String pathToFiles;

    Downloader(String pathToFiles) {
        this.pathToFiles = pathToFiles;
    }

    void saveDocs() {
        File baseDir = new File(pathToFiles);
        if (!baseDir.isDirectory()) {
            throw new IllegalArgumentException("Provided path must be a directory");
        }

        File[] files = baseDir.listFiles();
        if (files == null) {
            throw new IllegalArgumentException("Provided path must be a nonempty directory");
        }

        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".html")) {
                parseHtmlFile(file.getAbsolutePath());
            }
        }
    }

    private void parseHtmlFile(String fileName) {
        System.out.println(fileName);
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
               parseHtmlLine(line);
            }
        } catch (FileNotFoundException ex) {
            System.err.println("File not found " + fileName);
        } catch (IOException ex) {
            System.err.println("Something got wrong with file " + fileName + ": ");
            ex.printStackTrace();
        }
    }

    private void parseHtmlLine(String line) {
        String docLink;
        String pattern = "iframe\" src=\"";
        int index = line.indexOf(pattern);
        if (index > 0) {
            int questionMarkIndex = line.indexOf("?");
            if (questionMarkIndex > 0) {
                docLink = line.substring(index + pattern.length(), questionMarkIndex);
                System.out.println(docLink);
            }
        }
    }
}

@@ -33,16 +33,173 @@
 va.io.*;
+%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.StandardCopyOption;
 %0A%0Aclass 
@@ -1862,50 +1862,986 @@
     
-System.out.println(docLink);%0A            %7D
+int startOfFileName = docLink.lastIndexOf(%22/%22) + 1;%0A                String fileName = docLink.substring(startOfFileName);%0A                System.out.println(docLink);%0A                saveDoc(docLink, fileName);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void saveDoc(String docLink, String fileNameTosave) %7B%0A        URL url = null;%0A%0A        try %7B%0A            url = new URL(docLink);%0A            InputStream in = url.openStream();%0A            String placeToSave = pathToFiles + File.separator + Paths.get(fileNameTosave);%0A            System.out.println(%22saving to %22 + placeToSave);%0A            Files.copy(in, Paths.get(placeToSave), StandardCopyOption.REPLACE_EXISTING);%0A            in.close();%0A        %7D catch (MalformedURLException mue) %7B%0A            System.err.println(%22URL not found %22 + docLink);%0A        %7D catch (IOException ioe) %7B%0A            System.err.println(%22Something got wrong with opening stream from url %22 + url.getPath() + %22: %22);%0A            ioe.printStackTrace();
 %0A   

save next to html