fr.whimtrip.ext.jwhthtmltopojo.impl.AcceptIfValidAttrRegexCheck Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimtrip-ext-htmltopojo Show documentation
Show all versions of whimtrip-ext-htmltopojo Show documentation
Fully featured highly pluggable and customizable Java html to pojo reflection converter
package fr.whimtrip.ext.jwhthtmltopojo.impl;
import fr.whimtrip.core.util.exception.ObjectCreationException;
import fr.whimtrip.ext.jwhthtmltopojo.HtmlToPojoUtils;
import fr.whimtrip.ext.jwhthtmltopojo.annotation.AttrRegexCheck;
import fr.whimtrip.ext.jwhthtmltopojo.annotation.Selector;
import fr.whimtrip.ext.jwhthtmltopojo.intrf.AcceptIfResolver;
import org.jsoup.nodes.Element;
import java.lang.reflect.Field;
import java.util.regex.Pattern;
/**
* Part of project jwht-htmltopojo
*
* Example implementation of an {@link AcceptIfResolver}.
*
*
* This one will extract one attribute of the given HTML Element
* {@code {@link Element}} and check if it matches with the
* submitted regex.
*
*
*
* You have to provide an {@link AttrRegexCheck} annotation on top of
* the corresponding field in order for this deserializer to work properly.
*
*
* @author Louis-wht
* @since 1.0.0
*/
public class AcceptIfValidAttrRegexCheck implements AcceptIfResolver {
private AttrRegexCheck attrRegexCheck;
/**
* {@inheritDoc}
*/
@Override
public void init(Field field, Object parentObject, Selector selector) throws ObjectCreationException {
attrRegexCheck = field.getAnnotation(AttrRegexCheck.class);
if(attrRegexCheck == null)
throw new ObjectCreationException(field, this.getClass(), AttrRegexCheck.class);
}
/**
* {@inheritDoc}
*/
@Override
public boolean accept(Element element, Object parentObject) {
String attr = HtmlToPojoUtils.extractRawValue(element, "", attrRegexCheck.attr());
return Pattern
.compile(attrRegexCheck.value())
.matcher(attr)
.find();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy