package com.winterbe.javadoc;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Optional;

/**
 * @author Benjamin Winterberg
 */
public class FileParser {

    public Optional<TypeInfo> parse(File file, String path) throws IOException {
        if (!file.exists()) {
            throw new FileNotFoundException("file does not exist: " + file.getAbsolutePath());
        }

        Document document = Jsoup.parse(file, "UTF-8", "http://download.java.net/jdk8/docs/api/");

        try {
            return getTypeInfo(document, path);
        }
        catch (Exception e) {
            System.err.println("failed to parse file " + file.getAbsolutePath() + ": " + e.getMessage());
            return Optional.empty();
        }
    }

    private Optional<TypeInfo> getTypeInfo(Document document, String path) {
        String title = document.title();
        String typeName = StringUtils.substringBefore(title, " ");

        Element body = document.body();

        String fullType = body
                .select(".header h2")
                .first()
                .text();

        String packageName = body
                .select(".header > .subTitle")
                .last()
                .text();

        TypeInfo typeInfo = new TypeInfo();
        typeInfo.setName(typeName);

        FileType fileType = FileType.ofFullType(fullType);
        typeInfo.setFileType(fileType);

        typeInfo.setPackageName(packageName);
        typeInfo.setPath(path);

        body.select(".contentContainer .description dl dt .simpleTagLabel")
                .stream()
                .filter(el -> el.text().contains("Since"))
                .findFirst()
                .ifPresent(el -> {
                    String version = el.parent().nextElementSibling().text();
                    typeInfo.setVersion(version);
                });

        body.select(".inheritance > li:first-of-type > a")
                .stream()
                .forEach(el -> {
                    String clazz = StringUtils.substringAfterLast(el.text(), ".");
                    String filterExtends = typeInfo.getFilterExtends();
                    filterExtends += " " + clazz;
                    typeInfo.setFilterExtends(StringUtils.lowerCase(filterExtends));
                });

        body.select(".contentContainer > .description > ul.blockList > li.blockList > dl > dt")
                .stream()
                .filter(el ->
                        el.text().contains("All Implemented Interfaces:") ||
                        el.text().contains("All Superinterfaces:"))
                .findFirst()
                .ifPresent(el -> el.nextElementSibling().select("a")
                        .stream()
                        .map(a -> StringUtils.lowerCase(a.text()))
                        .forEach(type -> {
                            String filterExtends = typeInfo.getFilterExtends();
                            filterExtends += " " + type;
                            typeInfo.setFilterExtends(filterExtends);
                        }));

        return Optional.of(typeInfo);
    }
}
@@ -2137,311 +2137,297 @@
    .
-forEach(el -%3E %7B%0A                    String clazz = StringUtils.substringAfterLast(el.text(), %22.%22);%0A                    String filterExtends = typeInfo.getFilterExtends();%0A                    filterExtends += %22 %22 + clazz;%0A                    typeInfo.setFilterExtends(StringUtils.lowerCase(filterExtends)
+map(el -%3E StringUtils.lowerCase(el.text()))%0A                .map(s -%3E StringUtils.substringAfterLast(s, %22.%22))%0A                .forEach(type -%3E %7B%0A                    String filterExtends = typeInfo.getFilterExtends();%0A                    typeInfo.setFilterExtends(filterExtends + %22 %22 + type
 );%0A 
@@ -3069,65 +3069,8 @@
 ();%0A
-                            filterExtends += %22 %22 + type;%0A
     
@@ -3132,16 +3132,29 @@
 rExtends
+ + %22 %22 + type
 );%0A     

Simplify code