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

us.codecraft.webmagic.model.ModelPipeline 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.PageModelPipeline;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The extension to Pipeline for page model extractor.
 *
 * @author [email protected] 
* @since 0.2.0 */ class ModelPipeline implements Pipeline { private Map pageModelPipelines = new ConcurrentHashMap(); public ModelPipeline() { } public ModelPipeline put(Class clazz, PageModelPipeline pageModelPipeline) { pageModelPipelines.put(clazz, pageModelPipeline); return this; } @Override public void process(ResultItems resultItems, Task task) { for (Map.Entry classPageModelPipelineEntry : pageModelPipelines.entrySet()) { Object o = resultItems.get(classPageModelPipelineEntry.getKey().getCanonicalName()); if (o != null) { Annotation annotation = classPageModelPipelineEntry.getKey().getAnnotation(ExtractBy.class); if (annotation == null || !((ExtractBy) annotation).multi()) { classPageModelPipelineEntry.getValue().process(o, task); } else { List list = (List) o; for (Object o1 : list) { classPageModelPipelineEntry.getValue().process(o1, task); } } } } } }