📄 jacson.java
字号:
package de.spieleck.app.jacson;
import java.io.IOException;
import java.util.Date;
import org.xml.sax.InputSource;
import de.spieleck.config.Config;
import de.spieleck.config.ConfigNode;
import de.spieleck.app.jacson.source.LineChunkSource;
/**
* The master Object to be used when <i>Jacson</i> is embedded into
* other programs and also a <code>main()</code> driver to enable
* <i>Jacson</i> on the commandline.
* @author fsn
*/
public class Jacson
extends JacsonBlock
{
protected JacsonState params;
protected Jacson()
{
super();
}
public Jacson(JacsonState params)
{
this();
setParams(params);
}
public Jacson(ConfigNode config, JacsonState params)
throws JacsonConfigException
{
this(params);
JacsonRegistry registry = new JacsonRegistry(config);
init(config, registry);
}
public void setParams(JacsonState params)
{
this.params = params;
}
public void run(JacsonChunkSource chunker)
throws IOException, JacsonException
{
long t1 = System.currentTimeMillis();
JacsonReport jr = (JacsonReport) getReport();
JacsonState state = new JacsonState(params);
registerState(state);
chunker.registerReport(jr);
chunker.registerState(state);
String id = chunker.message();
if ( id != null )
state.putGlobal(JS_STATE_ID, id);
String chunk;
do
{
chunk = chunker.nextChunk();
state.incChunkNo();
putChunk(chunk);
}
while ( chunk != null );
//
long dt = System.currentTimeMillis() - t1;
jr.report(JS_REP_TIME, ""+dt);
chunker.summary();
}
public void summary()
{
JacsonReport jr = (JacsonReport) getReport();
jr.begin(JS_REP_SUMMARY);
jr.report(JS_REP_TSTAMP, (new Date()).toString());
long t1 = System.currentTimeMillis();
super.summary();
long dt = System.currentTimeMillis() - t1;
jr.report(JS_REP_TIME, ""+dt);
jr.end();
}
protected static void exit(Class clazz, Throwable e)
{
System.err.println(clazz.getName()+": "+e);
System.exit(1);
}
/**
* Obtain Jacson parameters identified by "=".
*/
public static int paramArgs(String[] args, JacsonState rootState)
{
int argsStart = 1;
// Parameters containing "="
while ( argsStart < args.length && args[argsStart].indexOf('=') > 0 )
{
int pos = args[argsStart].indexOf('=');
rootState.set(args[argsStart].substring(0,pos),
args[argsStart].substring(pos+1) );
argsStart++;
}
return argsStart;
}
public static ConfigNode obtainConfig(String name, JacsonState rootState)
throws JacsonConfigException
{
ConfigNode config = null;
try
{
InputSource is = new InputSource(name);
config = Config.parse(is, rootState);
}
catch ( Exception e )
{
throw new JacsonConfigException("Cannot parse config!", e);
}
config = config.node(JS_MAIN_CFG);
if ( config == null )
throw new JacsonConfigException("missing "+JS_MAIN_CFG+" section in config.");
return config;
}
public static void main(String[] args)
{
JacsonState rootState = new JacsonState();
int argsStart = paramArgs(args, rootState);
try
{
ConfigNode config = obtainConfig(args[0], rootState);
Jacson stat = new Jacson(config, rootState);
JacsonReport jr = stat.getReport();
//
jr.begin(JS_REP_PROC);
long t1 = System.currentTimeMillis();
for(int i = argsStart; i < args.length; i++)
{
try
{
JacsonChunkSource ch = new LineChunkSource(args[i], true);
stat.run(ch);
}
catch ( IOException e )
{
e.printStackTrace(System.out);
}
}
long dt = System.currentTimeMillis() - t1;
jr.report(JS_REP_TIME, ""+dt);
jr.end();
stat.summary();
jr.finish();
}
catch (JacsonConfigException e )
{
exit(Jacson.class, e);
}
catch (JacsonException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//
// 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 + -