package com.qianwen.core.http; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.jsoup.nodes.Element; import org.jsoup.nodes.TextNode; import org.jsoup.select.Elements; import org.jsoup.select.Selector; import com.qianwen.core.tool.utils.ConvertUtil; import com.qianwen.core.tool.utils.StringUtil; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import org.springframework.core.ResolvableType; import org.springframework.core.convert.TypeDescriptor; /* loaded from: blade-starter-http-9.3.0.0-SNAPSHOT.jar:org/springblade/core/http/CssQueryMethodInterceptor.class */ public class CssQueryMethodInterceptor implements MethodInterceptor { private final Class clazz; private final Element element; public CssQueryMethodInterceptor(final Class clazz, final Element element) { this.clazz = clazz; this.element = element; } @Nullable public Object intercept(Object object, java.lang.reflect.Method method, Object[] args, MethodProxy methodProxy) throws Throwable { String name = method.getName(); if (!StringUtil.startsWithIgnoreCase(name, Method.GET)) { return methodProxy.invokeSuper(object, args); } Field field = this.clazz.getDeclaredField(StringUtil.firstCharToLower(name.substring(3))); CssQuery cssQuery = (CssQuery) field.getAnnotation(CssQuery.class); if (cssQuery == null) { return methodProxy.invokeSuper(object, args); } Class returnType = method.getReturnType(); boolean isColl = Collection.class.isAssignableFrom(returnType); String cssQueryValue = cssQuery.value(); boolean isInner = cssQuery.inner(); if (isInner) { return proxyInner(cssQueryValue, method, returnType, isColl); } Object proxyValue = proxyValue(cssQueryValue, cssQuery, returnType, isColl); if (String.class.isAssignableFrom(returnType)) { return proxyValue; } TypeDescriptor typeDescriptor = new TypeDescriptor(field); return ConvertUtil.convert(proxyValue, typeDescriptor); } @Nullable private Object proxyValue(String cssQueryValue, CssQuery cssQuery, Class returnType, boolean isColl) { if (isColl) { Elements elements = Selector.select(cssQueryValue, this.element); Collection valueList = newColl(returnType); if (elements.isEmpty()) { return valueList; } Iterator it = elements.iterator(); while (it.hasNext()) { Element select = (Element) it.next(); String value = getValue(select, cssQuery); if (value != null) { valueList.add(value); } } return valueList; } Element select2 = Selector.selectFirst(cssQueryValue, this.element); return getValue(select2, cssQuery); } private Object proxyInner(String cssQueryValue, java.lang.reflect.Method method, Class returnType, boolean isColl) { if (isColl) { Elements elements = Selector.select(cssQueryValue, this.element); Collection valueList = newColl(returnType); ResolvableType resolvableType = ResolvableType.forMethodReturnType(method); Class innerType = resolvableType.getGeneric(new int[]{0}).resolve(); if (innerType == null) { throw new IllegalArgumentException("Class " + returnType + " 读取泛型失败。"); } Iterator it = elements.iterator(); while (it.hasNext()) { Element select = (Element) it.next(); valueList.add(DomMapper.readValue(select, innerType)); } return valueList; } Element select2 = Selector.selectFirst(cssQueryValue, this.element); return DomMapper.readValue(select2, returnType); } @Nullable private String getValue(@Nullable Element element, CssQuery cssQuery) { String attrValue; if (element == null) { return null; } String attrName = cssQuery.attr(); if (StringUtil.isBlank(attrName)) { attrValue = element.outerHtml(); } else if ("html".equalsIgnoreCase(attrName)) { attrValue = element.html(); } else if ("text".equalsIgnoreCase(attrName)) { attrValue = getText(element); } else if ("allText".equalsIgnoreCase(attrName)) { attrValue = element.text(); } else { attrValue = element.attr(attrName); } String regex = cssQuery.regex(); if (StringUtil.isBlank(attrValue) || StringUtil.isBlank(regex)) { return attrValue; } return getRegexValue(regex, cssQuery.regexGroup(), attrValue); } @Nullable private String getRegexValue(String regex, int regexGroup, String value) { Matcher matcher = Pattern.compile(regex, 34).matcher(value); if (!matcher.find()) { return null; } if (regexGroup > 0) { return matcher.group(regexGroup); } return matcher.group(); } private String getText(Element element) { return (String) element.childNodes().stream().filter(node -> { return node instanceof TextNode; }).map(node2 -> { return (TextNode) node2; }).map((v0) -> { return v0.text(); }).collect(Collectors.joining()); } private Collection newColl(Class returnType) { return Set.class.isAssignableFrom(returnType) ? new HashSet() : new ArrayList(); } }