📄 jacsonblock.java
字号:
package de.spieleck.app.jacson;
import java.util.List;
import java.util.LinkedList;
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.util.ConfigUtil;
import de.spieleck.app.jacson.report.CascadableReport;
/**
* Special Evaluator to actually perform whole standard processing.
* <p>This is the major building block of Jacson.
* <P> The standard behaviour is implemented as a {@link JacsonEvaluator}
* itself, to allow the imbedding of blocks into other blocks.
* <P> This plugin takes the incoming chunk. Applies all filters
* and then passes the chunk to the underlying evaluators.
* @author fsn
*/
public class JacsonBlock
implements JacsonEvaluator, JacsonNames, Acceptor
{
/** Filtering the chunk stream */
protected List filters;
/** The first filter in the chain or the chunk Dispatcher */
private JacsonChunkDrain myDrain;
/** Evaluator to actually treat the chuncks. */
protected List evaluators;
/** special {@link JacsonChunkDrain} to dispatch chunk to Evaluators. */
protected JacsonChunkDrain chunkDispatch;
/** Additional text to augment output */
protected String label = null;
/** Encapsulate state, */
protected JacsonState myState = new JacsonState();
/** Prepare for handling Reports */
private CascadableReport reports = new CascadableReport();
public JacsonBlock()
{
setChunkDispatch(new ChunkDispatcher());
}
public void init(ConfigNode config, JacsonRegistry registry)
throws JacsonConfigException
{
label = registry.scanForString(JS_OBJ_LABEL, config);
obtainFilters(config, registry.find(config, JS_OBJ_FILTER), registry);
obtainEvaluators(config, registry.find(config, JS_OBJ_EVAL), registry);
obtainReports(registry.find(config, JS_OBJ_REPORT), registry);
boolean blockReport = config.getBoolean(BLOCKREPORT, false);
reports.setBlock(blockReport);
//
if ( evaluators.size() == 0
&& filters.size() == 0 )
ConfigUtil.exception("Block without eval or filter!", config);
ConfigUtil.verify(config, this);
registerReport(null);
registerState(null);
}
public boolean accept(ConfigNode node)
{
String name = node.getName();
return name.equals(JS_OBJ_LABEL) // XXX only once
|| name.equals(JS_OBJ_FILTER)
|| name.equals(JS_OBJ_EVAL)
|| name.equals(JS_OBJ_REPORT)
|| name.equals(BLOCKREPORT)
|| JacsonRegistry.acceptId(node);
}
// XXX This contains an inlined Factory.
protected void obtainFilters(ConfigNode config,
Iterator iter, JacsonRegistry registry)
throws JacsonConfigException
{
filters = new LinkedList();
while ( iter.hasNext() )
{
ConfigNode filterConfig = (ConfigNode) iter.next();
// String clazz = filterConfig.getString();
String clazz = registry.scanForValue(filterConfig);
JacsonFilter filter = (JacsonFilter)FastClassForName.newInstance(
clazz, JS_PCK_FILTER, JacsonFilter.class);
if ( filter == null )
ConfigUtil.exception("No Filter: "+clazz, config);
filter.init(filterConfig, registry);
addFilter(filter);
}
}
/** Obtain a list of reports */
protected void obtainReports(Iterator iter, JacsonRegistry registry)
throws JacsonConfigException
{
while ( iter.hasNext() )
{
ConfigNode reportConfig = (ConfigNode) iter.next();
JacsonReport jr = createReport(reportConfig, null);
addReport(jr);
}
}
/** Helper clazz to create an report */
public static JacsonReport createReport(ConfigNode cfg, String reportClazz)
throws JacsonConfigException
{
if ( reportClazz == null )
reportClazz = cfg.getString();
if ( reportClazz == null )
reportClazz = "XMLReport";
JacsonReport jr = (JacsonReport) FastClassForName.newInstance(
reportClazz, JS_PCK_REPORT, JacsonReport.class);
if ( jr == null )
{
ConfigUtil.exception("No JacsonReport "+reportClazz, cfg);
}
jr.init(cfg);
return jr;
}
/** Obtain the list of (sub-)Evaluators */
protected void obtainEvaluators(ConfigNode config,
Iterator iter, JacsonRegistry registry)
throws JacsonConfigException
{
evaluators = new LinkedList();
while ( iter.hasNext() )
{
ConfigNode evalConfig = (ConfigNode) iter.next();
// String clazz = evalConfig.getString();
String clazz = registry.scanForValue(evalConfig);
JacsonEvaluator eval;
if ( clazz == null )
eval = new JacsonBlock();
else
eval = (JacsonEvaluator) FastClassForName
.newInstance(clazz, "de.spieleck.app.jacson.eval.",
JacsonEvaluator.class);
if ( eval == null )
ConfigUtil.exception("No Evaluator: "+clazz, config);
eval.init(evalConfig, registry);
addEvaluator(eval);
}
}
public void addFilter(JacsonFilter filter)
{
filters.add(filter);
// That was easy, now we rebuild the filter processing chain:
Iterator it = filters.iterator();
JacsonFilter filter2, filter1 = (JacsonFilter) it.next();
myDrain = filter1;
while ( it.hasNext() )
{
filter2 = (JacsonFilter) it.next();
filter1.setDrain(filter2);
filter1 = filter2;
}
filter1.setDrain(chunkDispatch);
}
public void addEvaluator(JacsonEvaluator plug)
{
evaluators.add(plug);
}
public void addReport(JacsonReport rep)
{
reports.addReport(rep);
}
public void clearReports()
{
reports.clear();
}
public void registerReport(JacsonReport report)
{
if ( report != null )
reports.setSuperReport(report);
registerReport(evaluators.iterator(), reports);
registerReport(filters.iterator(), reports);
}
public static void registerReport(Iterator it, JacsonReport report)
{
while ( it.hasNext() )
{
Object o = it.next();
if ( o instanceof JacsonReporting )
((JacsonReporting)o).registerReport(report);
}
}
public void registerState(JacsonState state)
{
if ( state != null )
myState.setParent(state); // envelope state, to protect data
registerState(evaluators.iterator(), myState);
registerState(filters.iterator(), myState);
reports.registerState(myState);
}
public static void registerState(Iterator it, JacsonState state)
{
while ( it.hasNext() )
{
Object o = it.next();
if ( o instanceof JacsonStately )
((JacsonStately)o).registerState(state);
}
}
/** Expose the report to the public */
public JacsonPrintReport getReport()
{
return reports;
}
public void putChunk(String chunk)
throws JacsonException
{
getMyDrain().putChunk(chunk);
}
public JacsonChunkDrain getMyDrain()
{
return myDrain;
}
protected void setChunkDispatch(JacsonChunkDrain d)
{
chunkDispatch = d;
myDrain = d;
}
protected String getLabel()
{
return label;
}
public void summary()
{
JacsonReport jr = getReport();
jr.begin(JS_REP_BLOCK);
if ( getLabel() != null )
jr.report(JS_REP_LABEL, getLabel() );
jr.begin(JS_REP_FILTER);
summary(filters.iterator());
jr.end();
jr.begin(JS_REP_EVAL);
summary(evaluators.iterator());
jr.end();
jr.end();
}
public static void summary(Iterator it)
{
while ( it.hasNext() )
{
Object o = it.next();
if ( o instanceof JacsonReporting )
((JacsonReporting)o).summary();
}
}
/** Helper also used elsewhere */
public void dispatchChunk(String chunk)
throws JacsonException
{
Iterator it = evaluators.iterator();
while ( it.hasNext() )
{
JacsonEvaluator eval = (JacsonEvaluator) it.next();
eval.putChunk(chunk);
}
}
/** special {@link JacsonChunkDrain} to dispatch chunk to Evaluators. */
private class ChunkDispatcher
implements JacsonChunkDrain
{
public ChunkDispatcher() { }
public void putChunk(String chunk)
throws JacsonException
{
dispatchChunk(chunk);
}
/** dummy implementation, NEVER EVER CALLED!!! */
public void summary() { }
/** dummy implementation, NEVER EVER CALLED!!! */
public void registerReport(JacsonReport jr) { }
/** dummy implementation, NEVER EVER CALLED!!! */
public void registerState(JacsonState jr) { }
}
}
//
// 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 + -