📄 fileinsertfilter.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 java.io.*;
import java.util.Map;
import java.util.HashMap;
// import org.apache.log4j.Logger;
import de.spieleck.config.ConfigNode;
import de.spieleck.config.ConfigVerify.Acceptor;
import de.spieleck.app.jacson.JacsonException;
import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.JacsonRegistry;
import de.spieleck.app.jacson.util.ConfigUtil;
/**
* Filter to look up lines from a file.
*
* @author brenck (Dirk.Brenckmann -at- gmx.de )
* @author fsn
*/
public class FileInsertFilter
extends FilterBase
implements Acceptor
{
/**
* log4j logger - debugging is lame :-)
*/
// private static final Logger L = Logger.getLogger( FileInsertFilter.class.getName());
/**
* File cache
*/
private static final Map RANDOMACCESSFILES = new HashMap();
public final static String EVAL_NODE = "evaluation";
public final static String LINE_NODE = "line";
public final static String FILE_NODE = "filename";
public final static String USEONNULL_NODE = "useOnNullChunks";
/**
* Is the filter to be used on "null"-chunks?
*/
protected boolean useOnNullChunks = false;
/**
* Evaluation type
*/
protected boolean lazyEval;
/**
* Line to access
*/
protected String line = null;
/**
* File to access
*/
protected String file = null;
/**
* Default constructor.
* Ensure it's existence.
*/
public FileInsertFilter() { }
/**
* 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 );
lazyEval= PadFilter.obtainLazyness(config, EVAL_NODE);
line = PadFilter.obtainXFix(config, LINE_NODE, lazyEval);
file = PadFilter.obtainXFix(config, FILE_NODE, lazyEval);
ConfigUtil.verify(config, this);
}
/**
* 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 EVAL_NODE.equals( name )
|| LINE_NODE.equals( name )
|| FILE_NODE.equals( name )
|| USEONNULL_NODE.equals( name );
}
/**
* Replace chunk by something found in a file.
*/
public void putChunk( String chunk )
throws JacsonException
{
if (( chunk == null ) && ( ! useOnNullChunks ))
{
// if ( L.isDebugEnabled())
// {
// L.debug( "Not filing null chunk." );
// }
drain.putChunk( chunk );
}
else
{
// if ( L.isDebugEnabled())
// {
// L.debug( "Filing chunk \"" + chunk + "\"" );
// }
String curFile = tryExpand(file, lazyEval);
String curLine = tryExpand(line, lazyEval);
drain.putChunk( getLine( curFile, curLine ));
}
}
public String getLine( String filename, String linenumber ) {
// if ( L.isDebugEnabled()) {
// L.debug( "Trying to read: \"" + filename + "\":\"" + linenumber + "\"" );
// }
File file = null;
if (( filename == null ) || ( filename.trim().length() < 1 )) {
return null;
}
else file = new File( filename );
// if ( L.isDebugEnabled()) {
// L.debug( "File created." );
// }
if (( ! file.exists()) || ( ! file.isFile()) || ( ! file.canRead())) {
// if ( L.isDebugEnabled()) {
// L.debug( "File is not accessable!" );
// }
return null;
}
else linenumber = ((( linenumber == null ) || ( linenumber.trim().length() < 1 )) ? "0" : linenumber );
int line = 0;
try {
line = new Integer( linenumber ).intValue();
}
catch ( Exception e ) { /* not important */ }
// if ( L.isDebugEnabled()) {
// L.debug( "Line number transformed: " + line );
// }
RandomAccessFile rafile = (RandomAccessFile) RANDOMACCESSFILES.get( filename );
if ( rafile == null ) {
try {
rafile = new RandomAccessFile( file, "r" );
RANDOMACCESSFILES.put( filename, rafile );
// if ( L.isDebugEnabled()) {
// L.debug( "Random access file created" );
// }
}
catch ( Exception e ) {
// L.error( e );
return null;
}
}
try {
String ret = null;
rafile.seek( 0 );
for ( int i = 0; ( i < line ); i++ ) {
ret = rafile.readLine();
if ( ret == null ) {
break;
}
}
// if ( L.isDebugEnabled()) {
// L.debug( "Returning random access file line " + line + ": " + ret );
// }
return ret;
}
catch ( Exception e ) {
// L.error( e );
return null;
}
}
/**
* Lazy or non lazy evaluation of value.
* @param val the value to be expanded or not
* @param lazy true means the value needs expanding
*/
protected String tryExpand(String val, boolean lazy)
{
return PadFilter.tryExpand(val, lazy, getRegState());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -