📄 jacsonreader.java.v1
字号:
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 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;
/**
* 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());
PipedWriter pWriter = new PipedWriter();
pWriter.connect((PipedReader) in);
rootState = new JacsonState();
ConfigNode 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.
* This actually spawns a Thread which runs Jacson which
* feeds the PipedReader embedded. XXX Note if you want to feed
* more Readers through this class, every request spawns a thread
* and each thread blocks on the synchronization. This can become
* inefficient in such a case the ThreadPool should be used, to
* manage assignments.
*
* @param reader The Reader to read input from
*/
public void setReader(Reader reader)
throws JacsonException, IOException
{
final LineChunkSource lcs = new LineChunkSource();
lcs.setReader(reader);
Thread th = new Thread(new Runnable()
{
public void run()
{
synchronized(jacson)
{
try
{
jacson.run(lcs);
jacson.summary();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
});
th.setDaemon(true);
th.start();
}
}
//
// 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 + -