⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loopprocessor.java

📁 Web-Harvest是一个Java开源Web数据抽取工具。它能够收集指定的Web页面并从这些页面中提取有用的数据。Web-Harvest主要是运用了像XSLT,XQuery,正则表达式等这些技术来实
💻 JAVA
字号:
/*  Copyright (c) 2006-2007, Vladimir Nikic
    All rights reserved.

    Redistribution and use of this software in source and binary forms,
    with or without modification, are permitted provided that the following
    conditions are met:

    * Redistributions of source code must retain the above
      copyright notice, this list of conditions and the
      following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the
      following disclaimer in the documentation and/or other
      materials provided with the distribution.

    * The name of Web-Harvest may not be used to endorse or promote
      products derived from this software without specific prior
      written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    You can contact Vladimir Nikic by sending e-mail to
    nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
    subject line.
*/
package org.webharvest.runtime.processors;

import org.webharvest.definition.BaseElementDef;
import org.webharvest.definition.LoopDef;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.ScraperContext;
import org.webharvest.runtime.scripting.ScriptEngine;
import org.webharvest.runtime.templaters.BaseTemplater;
import org.webharvest.runtime.variables.*;
import org.webharvest.utils.CommonUtil;
import org.webharvest.utils.Constants;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Loop list processor.
 */
public class LoopProcessor extends BaseProcessor {

    private LoopDef loopDef;

    public LoopProcessor(LoopDef loopDef) {
        super(loopDef);
        this.loopDef = loopDef;
    }

    public IVariable execute(Scraper scraper, ScraperContext context) {
        ScriptEngine scriptEngine = scraper.getScriptEngine();
        String item = BaseTemplater.execute( loopDef.getItem(), scriptEngine);
        String index = BaseTemplater.execute( loopDef.getIndex(), scriptEngine);
        String maxLoopsString = BaseTemplater.execute( loopDef.getMaxloops(), scriptEngine);
        String filter = BaseTemplater.execute( loopDef.getFilter(), scriptEngine);

        double maxLoops = Constants.DEFAULT_MAX_LOOPS;
        if (maxLoopsString != null && !"".equals(maxLoopsString.trim())) {
            maxLoops = Double.parseDouble(maxLoopsString);
        }

        BaseElementDef loopValueDef = loopDef.getLoopValueDef();
        IVariable loopValue = getBodyListContent(loopValueDef, scraper, context);
        debug(loopValueDef, scraper, loopValue);

        List resultList = new ArrayList();

        List list = loopValue != null ? loopValue.toList() : null;
        if (list != null) {
            IVariable itemBeforeLoop = (IVariable) context.get(item);
            IVariable indexBeforeLoop = (IVariable) context.get(index);

            List filteredList = filter != null ? createFilteredList(list, filter) : list;
            Iterator it = filteredList.iterator();

            for (int i = 1; it.hasNext() && i <= maxLoops; i++) {
                IVariable currElement = (IVariable) it.next();

                // define current value of item variable
                if ( item != null && !"".equals(item) ) {
                    context.put(item, currElement);
                }

                // define current value of index variable
                if ( index != null && !"".equals(index) ) {
                    context.put( index, new NodeVariable(String.valueOf(i)) );
                }

                // execute the loop body
                BaseElementDef bodyDef = loopDef.getLoopBodyDef();
                IVariable loopResult = bodyDef != null ? getBodyListContent(bodyDef, scraper, context) : new EmptyVariable();
                debug(bodyDef, scraper, loopResult);
                resultList.addAll( loopResult.toList() );
            }

            // restores previous value of item variable
            if (item != null && itemBeforeLoop != null) {
                context.put(item, itemBeforeLoop);
            }

            // restores previous value of index variable
            if (index != null && indexBeforeLoop != null) {
                context.put(index, indexBeforeLoop);
            }
        }

        return new ListVariable(resultList);
    }

    /**
     * Create filtered list based on specified list and filterStr
     * @param list
     * @param filterStr
     * @return Filtered list
     */
    private List createFilteredList(List list, String filterStr) {
        List result = new ArrayList();
        Set stringSet = new HashSet();

        Filter filter = new Filter(filterStr, list.size());

        Iterator it = list.iterator();
        int index = 1;
        while (it.hasNext()) {
            IVariable curr = (IVariable) it.next();

            if (filter.isInFilter(index)) {
                if (filter.isUnique) {
                    String currStr = curr.toString();
                    if (!stringSet.contains(curr.toString())) {
                        result.add(curr);
                        stringSet.add(currStr);
                    }
                } else {
                    result.add(curr);
                }
            }

            index++;
        }

        return result;
    }

    /**
     * x - starting index in range
     * y - ending index in range
     */
    private static class IntRange extends CommonUtil.IntPair {

        // checks if strins is in form [n][-][m]
        static boolean isValid(String s) {
            Pattern pattern = Pattern.compile("(\\d*)(-?)(\\d*?)");
            Matcher matcher = pattern.matcher(s);
            return matcher.matches();
        }

        private IntRange(int x, int y) {
            super(x, y);
        }

        public IntRange(String s, int size) {
            defineFromString(s, '-', size);
        }

        public boolean isInRange(int index) {
            return index >= x && index <= y;
        }

    }

    /**
     * x - starting index
     * y - index skip - x is first, x+y second, x+2y third, end so on.
     */
    private static class IntSublist extends CommonUtil.IntPair {

        // checks if strins is in form [n][:][m]
        static boolean isValid(String s) {
            Pattern pattern = Pattern.compile("(\\d*)(:?)(\\d*?)");
            Matcher matcher = pattern.matcher(s);
            return matcher.matches();
        }

        private IntSublist(int x, int y) {
            super(x, y);
        }

        public IntSublist(String s, int size) {
            defineFromString(s, ':', size);
        }


        public boolean isInSublist(int index) {
            return (index - x) % y == 0;
        }

    }

   /**
     * Class that represents filter for list filtering. It is created based on filter string.
     * Filter string is comma separated list of filter tokens. Valid filter tokens are:
     *      m - specific integer m
     *      m-n - integers in specified range, if m is ommited it's vaue is 1, if n is
     *            ommited it's value is specified size of list to be filtered
     *      m:n - all integerers starting from m and all subsequent with step n,
     *            m, m+1*n , m+2*n, ...
     *      odd - the same as 1:2
     *      even - the same as 2:2
     *      unique - tells that list must contain unique values (no duplicates)
     */
    private static class Filter {

        private boolean isUnique = false;
        private List filterList;

        private Filter(String filterStr, int size) {
            StringTokenizer tokenizer = new StringTokenizer(filterStr, ",");
            filterList = new ArrayList();
            
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken().trim();

                if ("unique".equals(token)) {
                    isUnique = true;
                } else if ("odd".equals(token)) {
                	filterList.add(new IntSublist(1, 2));
                } else if ("even".equals(token)) {
                	filterList.add( new IntSublist(2, 2));
                } else if (IntRange.isValid(token)) {
                	filterList.add(new IntRange(token, size));
                } else if (IntSublist.isValid(token)) {
                	filterList.add(new IntSublist(token, size));
                }
            }
        }

        /**
         * Checks if specified integer passes the filter
         */
        private boolean isInFilter(int num) {
        	int listSize = filterList.size();
        	
        	if (listSize == 0) {
        		return true;
        	}
        	
            for (int i = 0; i < listSize; i++) {
            	CommonUtil.IntPair curr = (CommonUtil.IntPair) filterList.get(i);
                if ( curr instanceof IntRange && ((IntRange)curr).isInRange(num) ) {
                    return true;
                } else if ( curr instanceof IntSublist && ((IntSublist)curr).isInSublist(num) ) {
                    return true;
                }
            }

            return false;
        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -