📄 padfilter.java
字号:
//
// 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
//
package de.spieleck.app.jacson.filter;
// import org.apache.log4j.Logger;
import de.spieleck.config.ConfigNode;
import de.spieleck.config.SimpleParamMap;
import de.spieleck.config.ConfigVerify.Acceptor;
import de.spieleck.app.jacson.JacsonException;
import de.spieleck.app.jacson.JacsonState;
import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.JacsonRegistry;
import de.spieleck.app.jacson.util.ConfigUtil;
/**
* Filter to pad a chunk. That is the chunk gets
* a prefix and postfix attached.
* @author fsn
* @author brenck (Dirk.Brenckmann -at- gmx.de)
*/
public class PadFilter
extends FilterBase
implements Acceptor
{
/**
* log4j logger - debugging is lame :-)
*/
// private static final Logger L = Logger.getLogger( PadFilter.class.getName());
public final static String EVAL_RUNTIME = "runtime";
public final static String EVAL_INITTIME = "init";
public final static String PRE_EVAL = "preEvaluation";
public final static String POST_EVAL = "postEvaluation";
public final static String PRE_NODE = "pre";
public final static String POST_NODE = "post";
public final static String USEONNULL_NODE = "useOnNullChunks";
/**
* Is the filter to be used on "null"-chunks?
*/
protected boolean useOnNullChunks = false;
/**
* Argument or value used as padding prefix
*/
protected String pre = null;
/**
* Argument or value used as padding postfix
*/
protected String post = null;
/**
* Evaluationtype info of prefix
*/
protected boolean lazyPreEval; // String sPreEvalType;
/**
* Evaluationtype info of postfix
*/
protected boolean lazyPostEval; // String sPostEvalType;
/**
* Default constructor.
* Ensure it's existence.
*/
public PadFilter() { }
/**
* Overwritten init method.
* Called first and only once in initialization process.
*/
public void init(ConfigNode config, JacsonRegistry registry)
throws JacsonConfigException
{
useOnNullChunks = config.getBoolean( USEONNULL_NODE, false );
lazyPreEval = obtainLazyness(config, PRE_EVAL);
pre = obtainXFix(config, PRE_NODE, lazyPreEval);
lazyPostEval = obtainLazyness(config, POST_EVAL);
post = obtainXFix(config, POST_NODE, lazyPostEval);
if ( "".equals(pre) && "".equals(post) )
ConfigUtil.exception("PadFilter needs pre and or post data.", null);
ConfigUtil.verify(config, this);
}
protected static boolean obtainLazyness(ConfigNode config, String name)
{
return config.getString(PRE_EVAL, EVAL_INITTIME).equals(EVAL_RUNTIME);
}
protected static String obtainXFix(ConfigNode config, String name,
boolean lazy)
{
ConfigNode c2 = config.node(name);
if ( c2 == null )
return "";
else if ( lazy )
return c2.getUnexpanded();
else
return c2.getString();
}
/**
* Overwritten accept method.
* Called only once and after the call to init.
* (You can skip this by removing <code>ConfigUtil.verify()</code>
* from <code>init()</code>)
*/
public boolean accept(ConfigNode node)
{
String name = node.getName();
return PRE_NODE.equals(name)
|| POST_NODE.equals(name)
|| PRE_EVAL.equals( name )
|| POST_EVAL.equals( name )
|| USEONNULL_NODE.equals( name );
}
/**
* Overwritten putChunk method.
* Called whenever data is to be filtered. This method MODIFIES the
* incoming passes the
*/
public void putChunk( String chunk )
throws JacsonException
{
if (( chunk == null ) && ( ! useOnNullChunks ))
{
// if ( L.isDebugEnabled())
// {
// L.debug( "Not padding null chunk." );
// }
drain.putChunk(chunk);
}
else
{
// if ( L.isDebugEnabled())
// {
// L.debug( "Padding chunk \"" + chunk + "\"" );
// }
drain.putChunk(tryExpand(pre, lazyPreEval)
+((chunk == null) ? "" : chunk)
+tryExpand(post, lazyPostEval));
}
}
protected String tryExpand(String val, boolean lazy)
{
return tryExpand(val, lazy, getRegState());
}
public static String tryExpand(String val, boolean lazy, JacsonState st)
{
// if ( L.isDebugEnabled())
// L.debug( "Xfix evaluated at runtime: \"" + val + "\"" );
if ( lazy )
return SimpleParamMap.expand(val, st);
else
return val;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -