yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.qianwen.core.http;
 
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jsoup.helper.DataUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import com.qianwen.core.tool.utils.Exceptions;
import org.springframework.cglib.proxy.Enhancer;
 
/* loaded from: blade-starter-http-9.3.0.0-SNAPSHOT.jar:org/springblade/core/http/DomMapper.class */
public class DomMapper {
    public static Document asDocument(ResponseSpec response) {
        return readDocument(response.asString());
    }
 
    public static Document readDocument(InputStream inputStream) {
        try {
            return DataUtil.load(inputStream, StandardCharsets.UTF_8.name(), "");
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static Document readDocument(String html) {
        return Parser.parse(html, "");
    }
 
    public static <T> T readValue(InputStream inputStream, final Class<T> clazz) {
        return (T) readValue((Element) readDocument(inputStream), (Class<Object>) clazz);
    }
 
    public static <T> T readValue(String html, final Class<T> clazz) {
        return (T) readValue((Element) readDocument(html), (Class<Object>) clazz);
    }
 
    public static <T> T readValue(final Element doc, final Class<T> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setUseCache(true);
        enhancer.setCallback(new CssQueryMethodInterceptor(clazz, doc));
        return (T) enhancer.create();
    }
 
    public static <T> List<T> readList(InputStream inputStream, final Class<T> clazz) {
        return readList((Element) readDocument(inputStream), (Class) clazz);
    }
 
    public static <T> List<T> readList(String html, final Class<T> clazz) {
        return readList((Element) readDocument(html), (Class) clazz);
    }
 
    public static <T> List<T> readList(Element doc, Class<T> clazz) {
        CssQuery annotation = (CssQuery) clazz.getAnnotation(CssQuery.class);
        if (annotation == null) {
            throw new IllegalArgumentException("DomMapper readList " + clazz + " mast has annotation @CssQuery.");
        }
        String cssQueryValue = annotation.value();
        Elements elements = doc.select(cssQueryValue);
        ArrayList arrayList = new ArrayList();
        Iterator it = elements.iterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();
            arrayList.add(readValue(element, clazz));
        }
        return arrayList;
    }
}