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

📄 selectionfilterfilter.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.app.jacson.filter;

import java.util.Iterator;

import de.spieleck.config.ConfigNode;
import de.spieleck.config.ConfigVerify.Acceptor;

import de.spieleck.util.FastClassForName;

import de.spieleck.app.jacson.JacsonException;
import de.spieleck.app.jacson.JacsonChunkDrain;
import de.spieleck.app.jacson.JacsonBlock;
import de.spieleck.app.jacson.JacsonFilter;
import de.spieleck.app.jacson.JacsonNames;
import de.spieleck.app.jacson.JacsonSelect;
import de.spieleck.app.jacson.PluginBase;
import de.spieleck.app.jacson.JacsonRegistry;
import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.SelectionResult;

/**
 * A very complex filter that applies a {@link JacsonSelect}
 * to a chunk, can apply filters on the matches and then remerges
 * the chunk.
 * @author fsn
 */
public class SelectionFilterFilter
    extends JacsonBlock
    implements JacsonFilter, JacsonNames, Acceptor
{
    public final static String BLOCK_NODE = "block";

    protected JacsonChunkDrain drain;

    protected JacsonSelect select;

    protected boolean block = false;

    public SelectionFilterFilter()
    {
          setChunkDispatch(new NewChunkDispatcher());
    }

    public void setDrain(JacsonChunkDrain drain)
    {
        this.drain = drain;
    }

    public void init(ConfigNode node, JacsonRegistry registry)
        throws JacsonConfigException
    {
        select = getSelect(node, registry);
        block = node.getBoolean(BLOCK_NODE, false);
        super.init(node, registry);
    }

    public boolean accept(ConfigNode node)
    {
        String name = node.getName();
        return name.equals(BLOCK_NODE)
            || acceptSelect(node)
            || super.accept(node); 
    }

    public static boolean acceptSelect(ConfigNode node)
    {
        String name = node.getName();
        return name.equals(JS_OBJ_SELECT);
    }

    public static JacsonSelect getSelect(ConfigNode node, 
                                              JacsonRegistry registry)
        throws JacsonConfigException
    {
        Iterator it = registry.find(node, JS_OBJ_SELECT);
        if ( !it.hasNext() )
            throw new JacsonConfigException("Need a "+JS_OBJ_SELECT+" child.");
        ConfigNode selConfig = (ConfigNode) it.next();
        if ( it.hasNext() )
            throw new JacsonConfigException("Need exactly one selection child.");
        String clazz = registry.scanForValue(selConfig);
        JacsonSelect select = (JacsonSelect) FastClassForName.newInstance(
                                clazz, JS_PCK_SELECT, JacsonSelect.class);
        if ( select == null )
            throw new JacsonConfigException("Cannot create selection "+clazz+".");
        select.init(selConfig, registry);
        return select;
    }

    public String filterResult;

    public void putChunk(String chunk)
        throws JacsonException
    {
        if ( chunk == null )
            drain.putChunk(null);
        else
        {    
            select.setChunk(chunk);
            SelectionResult sr = select.getNextSelection();
            if ( sr == null && block )
                return;
            StringBuffer res = new StringBuffer(chunk.length());
            int lastEnd = 0;
            while ( sr != null )
            {   
                int start = sr.getStart();
                if ( start > lastEnd )
                    res.append(chunk.substring(lastEnd, start));
                lastEnd = sr.getEnd();
                String newChunk = sr.getChunk();
                // Handling blocks in the substream
                filterResult = null;
                getMyDrain().putChunk(newChunk);
                if ( filterResult == null && block )
                    return;
                if ( filterResult != null )
                    res.append(filterResult);
                sr = select.getNextSelection();
            }
            if ( lastEnd < chunk.length() )
                res.append(chunk.substring(lastEnd));
            drain.putChunk(res.toString());
        }
    }

    /** repeats most of  {@link de.spieleck.app.jacson.JacsonBlock.ChunkDispatcher} */
    protected class NewChunkDispatcher
        extends PluginBase
        implements JacsonChunkDrain
    {
        public NewChunkDispatcher() { };

        public void putChunk(String chunk)
            throws JacsonException
        {
            // this is external global Variable, how ugly!
            filterResult = chunk;
            dispatchChunk(chunk);
        }
    }
}

//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

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