org.aliuge.crawler.extractor.selector.IntegerElementCssSelector.java Source code

Java tutorial

Introduction

Here is the source code for org.aliuge.crawler.extractor.selector.IntegerElementCssSelector.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aliuge.crawler.extractor.selector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aliuge.crawler.exception.ExtractException;
import org.aliuge.crawler.extractor.selector.action.IntegerSelectorAction;
import org.aliuge.crawler.extractor.selector.action.SelectorAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.select.Elements;

import com.google.common.base.CharMatcher;
import com.google.common.collect.Lists;

/**
 * @author chenxinwen
 * @date 2014813
 * @desc ???null
 */
public class IntegerElementCssSelector extends AbstractElementCssSelector<Integer> {
    private Log log = LogFactory.getLog(IntegerElementCssSelector.class);
    private Integer content;
    @SuppressWarnings("unchecked")
    private List<SelectorAction> actions = Lists.newArrayList();

    public IntegerElementCssSelector() {
        super();
    }

    public IntegerElementCssSelector(String name, String value, String attr, boolean isRequired, int index,
            String regex) {
        super(name, value, attr, isRequired, index, regex);
    }

    @Override
    public Integer getContent() throws ExtractException {
        Elements elements = null;
        try {
            // content???document2+??
            if (null != content && !newDoc) {
                return content;
            }
            if (null != document) {
                elements = super.document.select(value);
                if (elements.isEmpty())
                    return null;
                String temp;
                switch ($Attr) {
                case text:
                    temp = CharMatcher.DIGIT.retainFrom(getExtractText(elements));
                    break;
                default:
                    temp = CharMatcher.DIGIT.retainFrom(getExtractAttr(elements, attr));
                    break;
                }

                if (StringUtils.isNotBlank(temp)) {
                    Integer integer = Integer.parseInt(temp);
                    this.content = integer;
                    newDoc = false;
                    return content;
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            log.error(elements.toString());
            throw new ExtractException("????:" + e.getMessage());
        }
        return null;
    }

    /**
     * content?
     */
    @Override
    public Map<String, Integer> getContentMap() throws ExtractException {
        if (newDoc) {
            this.content = getContent();
        }
        if (null == this.content)
            return null;
        Map<String, Integer> m = new HashMap<String, Integer>(1);
        m.put(name, this.content);
        return m;
    }

    public void setContent(Integer content) {
        this.content = content;
    }

    @Override
    public void addAction(SelectorAction<Integer> action) {
        this.actions.add((IntegerSelectorAction) action);
    }

}