public class LogFilter extends java.lang.Object implements Filter, java.io.Serializable
The current implementation supports replacing the file extension. The reason for supporting this is from first hand experience porting an existing website to Tomcat + JSP. Later on we may want to provide the ability to replace the whole filename. If the need materializes, we can add it later.
Example of how to use it is provided in the main method. An example is provided below.
testf = new LogFilter();
String[] incl = { "hello.html", "index.html", "/index.jsp" };
String[] theFiles = {
"/test/hello.jsp", "/test/one/hello.html", "hello.jsp", "hello.htm", "/test/open.jsp",
"/test/open.html", "/index.jsp", "/index.jhtml", "newindex.jsp", "oldindex.jsp", "oldindex1.jsp",
"oldindex2.jsp", "oldindex3.jsp", "oldindex4.jsp", "oldindex5.jsp", "oldindex6.jsp", "/test/index.htm"
};
testf.excludeFiles(incl);
System.out.println(" ------------ exclude test -------------");
for (String theFile : theFiles) {
boolean fl = testf.isFiltered(theFile);
String line = testf.filter(theFile);
if (line != null) {
System.out.println("the file: " + line);
}
}
As a general note. Both isFiltered and filter() have to be called. Calling
either one will not produce the desired result. isFiltered(string) will tell
you if a string should be filtered. The second step is to filter the string,
which will return null if it is filtered and replace any part of the string
that should be replaced.Modifier and Type | Field and Description |
---|---|
protected boolean |
CHANGEEXT |
protected java.lang.String[] |
EXCFILE |
protected java.util.ArrayList<org.apache.oro.text.regex.Pattern> |
EXCPATTERNS |
protected java.lang.String[] |
EXCPTRN |
protected boolean |
FILEFILTER |
protected java.lang.String[] |
INCFILE |
protected java.util.ArrayList<org.apache.oro.text.regex.Pattern> |
INCPATTERNS |
protected java.lang.String[] |
INCPTRN |
protected java.lang.String |
NEWEXT |
protected java.lang.String |
NEWFILE |
protected java.lang.String |
OLDEXT |
protected boolean |
PTRNFILTER |
protected boolean |
USEFILE |
Constructor and Description |
---|
LogFilter() |
Modifier and Type | Method and Description |
---|---|
org.apache.oro.text.regex.Pattern |
createPattern(java.lang.String pattern)
create a new pattern object from the string.
|
boolean |
excFile(java.lang.String text)
Method implements the logic for filtering file name exclusion.
|
void |
excludeFiles(java.lang.String[] filenames)
Give the filter a list of files to exclude
|
void |
excludePattern(java.lang.String[] regexp)
Give the filter a set of regular expressions to filter with for
exclusion.
|
protected boolean |
excPattern(java.lang.String text)
The method assumes by default the text is not excluded.
|
java.lang.String |
filter(java.lang.String text)
The current implementation checks the boolean if the text should be used
or not. isFilter( string) has to be called first.
|
protected boolean |
filterFile(java.lang.String file)
Filter the file.
|
protected boolean |
filterPattern(java.lang.String text)
The current implementation assumes the user has checked the regular
expressions so that they don't cancel each other.
|
boolean |
incFile(java.lang.String text)
Method implements the logic for filtering file name inclusion.
|
void |
includeFiles(java.lang.String[] filenames)
Give the filter a list of files to include
|
void |
includePattern(java.lang.String[] regexp)
Give the filter a set of regular expressions to filter with for
inclusion.
|
protected boolean |
incPattern(java.lang.String text)
By default, the method assumes the entry is not included, unless it
matches.
|
boolean |
isFiltered(java.lang.String path,
TestElement el)
In the case of log filtering the important thing is whether the log entry
should be used.
|
boolean |
replaceExtension(java.lang.String text)
Method uses indexOf to replace the old extension with the new extension.
|
void |
reset()
Tell the filter when the parsing has reached the end of the log file and
is about to begin again.
|
void |
setReplaceExtension(java.lang.String oldext,
java.lang.String newext)
The method will replace the file extension with the new one.
|
protected boolean CHANGEEXT
protected java.lang.String OLDEXT
protected java.lang.String NEWEXT
protected java.lang.String[] INCFILE
protected java.lang.String[] EXCFILE
protected boolean FILEFILTER
protected boolean USEFILE
protected java.lang.String[] INCPTRN
protected java.lang.String[] EXCPTRN
protected boolean PTRNFILTER
protected java.util.ArrayList<org.apache.oro.text.regex.Pattern> EXCPATTERNS
protected java.util.ArrayList<org.apache.oro.text.regex.Pattern> INCPATTERNS
protected java.lang.String NEWFILE
public void setReplaceExtension(java.lang.String oldext, java.lang.String newext)
setReplaceExtension
in interface Filter
oldext
- old extensionnewext
- new extensionFilter.setReplaceExtension(java.lang.String,
java.lang.String)
public void includeFiles(java.lang.String[] filenames)
includeFiles
in interface Filter
filenames
- list of files to includeFilter.includeFiles(java.lang.String[])
public void excludeFiles(java.lang.String[] filenames)
excludeFiles
in interface Filter
filenames
- list of files to excludeFilter.excludeFiles(java.lang.String[])
public void includePattern(java.lang.String[] regexp)
includePattern
in interface Filter
regexp
- list of regular expressionsFilter.includePattern(String[])
public void excludePattern(java.lang.String[] regexp)
excludePattern
in interface Filter
regexp
- list of regular expressionsFilter.excludePattern(String[])
public boolean isFiltered(java.lang.String path, TestElement el)
isFiltered
in interface Filter
path
- path to be testedel
- TestElement
in which the line would be addedtrue
if entry should be excludedFilter.isFiltered(String, TestElement)
protected boolean filterFile(java.lang.String file)
file
- the file to filterpublic boolean incFile(java.lang.String text)
text
- name of the file to tested (must not be null
)public boolean excFile(java.lang.String text)
text
- name of the file to be tested (must not be null)protected boolean filterPattern(java.lang.String text)
text
- text to be checkedprotected boolean incPattern(java.lang.String text)
text
- text to be checkedtrue
if text is includedprotected boolean excPattern(java.lang.String text)
text
- text to be checkedtrue
if text is excludedpublic boolean replaceExtension(java.lang.String text)
text
- name of the file in which the extension should be replaced
(must not be null)true
if the extension could be replaced,
false
otherwisepublic java.lang.String filter(java.lang.String text)
filter
in interface Filter
text
- log line to be filteredFilter.filter(java.lang.String)
public org.apache.oro.text.regex.Pattern createPattern(java.lang.String pattern)
pattern
- string representation of the perl5 compatible regex patternnull
if no pattern could be
compiledCopyright © 1998-2019 Apache Software Foundation. All Rights Reserved.