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

📄 jacsonreader.v2

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

import java.io.Reader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PipedReader;
import java.io.PrintWriter;
import java.util.LinkedList;

import de.spieleck.config.ConfigNode;

import de.spieleck.app.jacson.source.LineChunkSource;

/**
 * Wrap a Jacson into a {@link java.io.FilterReader}.
 *
 * @author fsn
 */
public class JacsonReader
    extends FilterReader
{
    /** Embedded worker Jacson */
    protected Jacson jacson;

    /** The state used by this Jacson */
    protected JacsonState rootState;

    /** Configuration used by the Jacson */
    protected ConfigNode config;

    /** The Writer to pipe Jacson-Input into */
    protected PipedWriter pWriter;

    /** The queue of Inputs */
    protected LinkedList readerQueue = null;

    /** Work thread to resolve the queue */
    protected Thread worker;

    /** Did we do something. */
    protected boolean started = false; 
   
    /** 
     * Create a new Reader with a prescribed configuration.
     *
     * @param configName Name of the configuration file to be parsed.
     */
    public JacsonReader(String configName)
        throws JacsonConfigException, IOException
    { 
        super(new PipedReader());
        pWriter = new PipedWriter();
        pWriter.connect((PipedReader) in);
        rootState = new JacsonState();
        config = Jacson.obtainConfig(configName, rootState);
        jacson = new Jacson(config, rootState);
        jacson.getReport().setPrintWriter(new PrintWriter(pWriter));
    }

    /**
     * Set a Reader to run through the Jacson job.
     *
     * @param reader The Reader to read input from
     */
    public void setReader(Reader reader)
        throws JacsonException, IOException
    {
        if ( readerQueue == null )
        {
            synchronized(this)
            {
                if ( readerQueue == null )
                {
                    readerQueue = new LinkedList();
                    worker = new Thread(new WorkRun(), "JacsonReaderThread");
                    worker.setDaemon(true);
                    worker.start();
                }
            }
        }
        synchronized(readerQueue)
        {
            readerQueue.add(reader);
            readerQueue.notify();
        }
    }

    /**
     * Filter a simple reader.
     * @param reader The reader to read.
     */
    protected void filter(Reader reader)
    {
        if ( reader != null )
        {
            LineChunkSource lcs = new LineChunkSource();
            lcs.setReader(reader);
            try
            {
                jacson.run(lcs);
                jacson.summary();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

    /** 
     * Internal runner class to work with queued readers.
     */
    protected class WorkRun
        implements Runnable
    {
        public void run()
        {
            try
            {
                while ( worker != null || !started )
                {
                    Reader re = null;
                    synchronized(readerQueue)
                    {
                        if ( readerQueue.isEmpty() )
                            readerQueue.wait();
                        re = (Reader) readerQueue.removeFirst();
                    }
                    started = true;
                    filter(re);
                }
            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

    public void finish()
    {
        worker = null;
        synchronized(readerQueue)
        {
            readerQueue.notifyAll();
        }
    }

    public void close()
    {
        finish();
        try
        {
            pWriter.close();
        }
        catch ( IOException ignore ) { }
        try
        {
            super.close();
        }
        catch ( IOException ignore ) { }
    }
}
//
//    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 + -