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;
|
}
|
}
|