package com.github.vedenin.thirdpartylib.htmlparser;

import com.github.vedenin.thirdpartylib.annotations.Atom;
import com.github.vedenin.thirdpartylib.annotations.BoilerPlate;
import com.github.vedenin.thirdpartylib.annotations.Contract;
import com.github.vedenin.thirdpartylib.annotations.Reference;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vvedenin on 12/16/2016.
 */
@Atom
@Contract("Provide information about HTML pages")
@Reference({ElementAtom.class})
public class DocumentAtom {
    private final Document document;

    @Contract("Should returns elements according this CSS Query")
    public List<ElementAtom> select(String cssQuery) {
        Elements elements = document.select(cssQuery);
        List<ElementAtom> result = new ArrayList<>(elements.size());
        for(Element element: elements) {
            result.add(ElementAtom.getProxy(element));
        }
        return result;
    }

    @Contract("Should returns text from html without any tags")
    public String getText() {
        return document.text();
    }


    // -------------- Just boilerplate code for Atom -----------------
    @BoilerPlate
    private DocumentAtom(Document document) {
        this.document = document;
    }

    @BoilerPlate
    public static DocumentAtom getAtom(Document document) {
        return new DocumentAtom(document);
    }

}

@@ -339,68 +339,29 @@
 ort 
-org.jsoup.select.Elements;%0Aimport org.jsoup.nodes.Elemen
+java.util.ArrayLis
 t;%0A
-%0A
 impo
@@ -373,34 +373,38 @@
 va.util.
-Array
 List;%0A
+%0A%0A
 import 
+static 
 java.uti
@@ -405,20 +405,35 @@
 va.util.
-Li
 st
+ream.Collectors.*
 ;%0A%0A/**%0A 
@@ -752,63 +752,8 @@
 ) %7B%0A
-        Elements elements = document.select(cssQuery);%0A
     
@@ -803,89 +803,71 @@
 t%3C%3E(
-elements.size());%0A        for(Element ele
+);%0A        result.addAll(docu
 ment
-: elements) %7B%0A            result.add
+.select(cssQuery).stream().map
 (Ele
@@ -878,17 +878,18 @@
 Atom
-.
+::
 getProxy
 (ele
@@ -888,29 +888,29 @@
 roxy
-(element));%0A        %7D
+).collect(toList()));
 %0A   

Create atom-proxy for third party libs