All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.codecraft.webmagic.model.PageModelCollectorPipeline Maven / Gradle / Ivy

The newest version!
package us.codecraft.webmagic.model;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.pipeline.CollectorPageModelPipeline;
import us.codecraft.webmagic.pipeline.CollectorPipeline;

import java.lang.annotation.Annotation;
import java.util.List;

/**
 * @author [email protected]
 * @since 0.4.0
 */
class PageModelCollectorPipeline implements CollectorPipeline {

    private final CollectorPageModelPipeline classPipeline = new CollectorPageModelPipeline();

    private final Class clazz;

    PageModelCollectorPipeline(Class clazz) {
        this.clazz = clazz;
    }

    @Override
    public List getCollected() {
        return classPipeline.getCollected();
    }

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        Object o = resultItems.get(clazz.getCanonicalName());
        if (o != null) {
            Annotation annotation = clazz.getAnnotation(ExtractBy.class);
            if (annotation == null || !((ExtractBy) annotation).multi()) {
                classPipeline.process((T) o, task);
            } else {
                List list = (List) o;
                for (Object o1 : list) {
                   classPipeline.process((T) o1, task);
                }
            }
        }
    }
}