package im.nll.data.extractor.impl;

import com.google.common.collect.Lists;
import im.nll.data.extractor.ListableExtractor;
import im.nll.data.extractor.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * * 一个使用Jquery选择器的抽取器
 * <p>
 * 关于jquery选择器语法请参考 <a
 * href="http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp"
 * >http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp</a>
 * <p>
 * 因为是基于jsoup实现，可参考<a
 * href="http://jsoup.org/cookbook/extracting-data/selector-syntax"
 * >http://jsoup.org/cookbook/extracting-data/selector-syntax</a>
 *
 * @author <a href="mailto:fivesmallq@gmail.com">fivesmallq</a>
 * @version Revision: 1.0
 * @date 15/12/25 下午9:25
 */
public class SelectorExtractor implements ListableExtractor {
    private final static int TYPE_TEXT = 0;
    private final static int TYPE_HTML = 1;
    private String query;
    /**
     * 元素序号,0代表第一个(默认第一个元素)
     */
    private int eq = 0;
    /**
     * 输出类型 <li>0 只输出文本.</li> <li>1 输出带有html格式.</li><li>
     * 如果想获取其他属性,直接写属性名,比如'href'则输出元素的href属性值</li>
     */
    private int outType = 0;
    private String attr;

    public SelectorExtractor(String query) {
        if (query.endsWith(".html")) {
            this.outType = 1;
            this.query = StringUtils.substringBeforeLast(query, ".html");
        } else {
            this.query = query;
        }
    }

    @Override
    public String extract(String data) {
        Document document = Jsoup.parse(data, "", Parser.xmlParser());
        String result = "";
        switch (outType) {
            case TYPE_TEXT:
                result = document.select(query).eq(eq).text();
                break;
            case TYPE_HTML:
                result = document.select(query).eq(eq).html();
                break;
            default:
                result = document.select(query).eq(eq).attr(attr);
                break;
        }
        return result;
    }

    @Override
    public List<String> extractList(String content) {
        List<String> strings = Lists.newArrayList();
        Document document = Jsoup.parse(content, "", Parser.xmlParser());
        Elements elements = document.select(query);
        for (Element element : elements) {
            switch (outType) {
                case TYPE_TEXT:
                    strings.add(element.text());
                    break;
                case TYPE_HTML:
                    strings.add(element.html());
                    break;
                default:
                    strings.add(element.attr(attr));
                    break;
            }
        }
        return strings;
    }
}

@@ -907,27 +907,30 @@
 inal static 
+Str
 in
-t
+g
  TYPE_TEXT =
@@ -930,17 +930,22 @@
 _TEXT = 
-0
+%22text%22
 ;%0A    pr
@@ -963,19 +963,22 @@
  static 
+Str
 in
-t
+g
  TYPE_HT
@@ -982,18 +982,59 @@
 _HTML = 
-1;
+%22html%22;%0A    /**%0A     * css selector%0A     */
 %0A    pri
@@ -1144,17 +1144,20 @@
 %E5%87%BA%E7%B1%BB%E5%9E%8B %3Cli%3E
-0
+text
  %E5%8F%AA%E8%BE%93%E5%87%BA%E6%96%87%E6%9C%AC.%3C
@@ -1165,17 +1165,20 @@
 li%3E %3Cli%3E
-1
+html
  %E8%BE%93%E5%87%BA%E5%B8%A6%E6%9C%89htm
@@ -1262,19 +1262,22 @@
 private 
+Str
 in
-t
+g
  outType
@@ -1283,34 +1283,14 @@
 e = 
-0;%0A    private String attr
+%22text%22
 ;%0A%0A 
@@ -1399,17 +1399,22 @@
 tType = 
-1
+%22html%22
 ;%0A      
@@ -2037,20 +2037,23 @@
 q).attr(
-attr
+outType
 );%0A     
@@ -2717,20 +2717,23 @@
 nt.attr(
-attr
+outType
 ));%0A    

update param