|
|||||||||
| Home >> All >> org >> apache >> oro >> [ text overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
org.apache.oro.text
Class MatchActionProcessor

java.lang.Objectorg.apache.oro.text.MatchActionProcessor
- public final class MatchActionProcessor
- extends java.lang.Object
The MatchActionProcessor class provides AWK-like line by line filtering of a text stream, pattern action pair association, and field splitting based on a registered separator. However, the class can be used with any compatible PatternMatcher/PatternCompiler implementations and need not use the AWK matching classes in org.apache.oro.text.awk. In fact, the default matcher and compiler used by the class are Perl5Matcher and Perl5Compiler from org.apache.oro.text.regex.
To completely understand how to use MatchActionProcessor, you should first look at MatchAction and MatchActionInfo. A MatchActionProcessor is first initialized with the desired PatternCompiler and PatternMatcher instances to use to compile patterns and perform matches. Then, optionally, a field separator may be registered with setFieldSeparator() 55 Finally, as many pattern action pairs as desired are registerd with addAction() 55 before processing the input with processMatches() 55 . Pattern action pairs are processed in the order they were registered.
The look of added actions can closely mirror that of AWK when anonymous classes are used. Here's an example of how you might use MatchActionProcessor to extract only the second column of a semicolon delimited file:
import java.io.*;
import org.apache.oro.text.*;
import org.apache.oro.text.regex.*;
public final class semicolon {
public static final void main(String[] args) {
MatchActionProcessor processor = new MatchActionProcessor();
try {
processor.setFieldSeparator(";");
// Using a null pattern means to perform the action for every line.
processor.addAction(null, new MatchAction() {
public void processMatch(MatchActionInfo info) {
// We assume the second column exists
info.output.println(info.fields.elementAt(1));
}
});
} catch(MalformedPatternException e) {
e.printStackTrace();
System.exit(1);
}
try {
processor.processMatches(System.in, System.out);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
You can redirect the following sample input to stdin to test the code:
1;Trenton;New Jersey 2;Annapolis;Maryland 3;Austin;Texas 4;Richmond;Virginia 5;Harrisburg;Pennsylvania 6;Honolulu;Hawaii 7;Santa Fe;New Mexico
- Since:
- 1.0
- Version:
- @version@
| Field Summary | |
private java.util.Vector |
__actions
|
private org.apache.oro.text.regex.PatternCompiler |
__compiler
|
private MatchAction |
__defaultAction
|
private org.apache.oro.text.regex.Pattern |
__fieldSeparator
|
private org.apache.oro.text.regex.PatternMatcher |
__matcher
|
private java.util.Vector |
__patterns
|
| Constructor Summary | |
MatchActionProcessor()
Default constructor for MatchActionProcessor. |
|
MatchActionProcessor(org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher)
Creates a new MatchActionProcessor instance initialized with the specified pattern compiler and matcher. |
|
| Method Summary | |
void |
addAction(java.lang.String pattern)
Binds a patten to the default action. |
void |
addAction(java.lang.String pattern,
int options)
Binds a patten to the default action, providing options to be used to compile the pattern. |
void |
addAction(java.lang.String pattern,
int options,
MatchAction action)
Registers a pattern action pair, providing options to be used to compile the pattern. |
void |
addAction(java.lang.String pattern,
MatchAction action)
Registers a pattern action pair. |
void |
processMatches(java.io.InputStream input,
java.io.OutputStream output)
This method reads the provided input one line at a time using the platform standart character encoding and for every registered pattern that is contained in the line it executes the associated MatchAction's processMatch() method. |
void |
processMatches(java.io.InputStream input,
java.io.OutputStream output,
java.lang.String encoding)
This method reads the provided input one line at a time and for every registered pattern that is contained in the line it executes the associated MatchAction's processMatch() method. |
void |
processMatches(java.io.Reader input,
java.io.Writer output)
This method reads the provided input one line at a time and for every registered pattern that is contained in the line it executes the associated MatchAction's processMatch() method. |
void |
setFieldSeparator(java.lang.String separator)
Sets the field separator to use when splitting a line into fields. |
void |
setFieldSeparator(java.lang.String separator,
int options)
Sets the field separator to use when splitting a line into fields. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
__fieldSeparator
private org.apache.oro.text.regex.Pattern __fieldSeparator
__compiler
private org.apache.oro.text.regex.PatternCompiler __compiler
__matcher
private org.apache.oro.text.regex.PatternMatcher __matcher
__patterns
private java.util.Vector __patterns
__actions
private java.util.Vector __actions
__defaultAction
private MatchAction __defaultAction
| Constructor Detail |
MatchActionProcessor
public MatchActionProcessor(org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher)
- Creates a new MatchActionProcessor instance initialized with the specified
pattern compiler and matcher. The field separator is set to null by
default, which means that matched lines will not be split into separate
fields unless the field separator is set with
setFieldSeparator() 55 .
MatchActionProcessor
public MatchActionProcessor()
- Default constructor for MatchActionProcessor. Same as calling
MatchActionProcessor(new Perl5Compiler(), new Perl5Matcher());
| Method Detail |
addAction
public void addAction(java.lang.String pattern, int options, MatchAction action) throws org.apache.oro.text.regex.MalformedPatternException
- Registers a pattern action pair, providing options to be used to
compile the pattern. If a pattern is null, the action
is performed for every line of input.
addAction
public void addAction(java.lang.String pattern, int options) throws org.apache.oro.text.regex.MalformedPatternException
- Binds a patten to the default action, providing options to be
used to compile the pattern. The default action is to simply print
the matched line to the output. If a pattern is null, the action
is performed for every line of input.
addAction
public void addAction(java.lang.String pattern) throws org.apache.oro.text.regex.MalformedPatternException
- Binds a patten to the default action. The default action is to simply
print the matched line to the output. If a pattern is null, the action
is performed for every line of input.
addAction
public void addAction(java.lang.String pattern, MatchAction action) throws org.apache.oro.text.regex.MalformedPatternException
- Registers a pattern action pair. If a pattern is null, the action
is performed for every line of input.
setFieldSeparator
public void setFieldSeparator(java.lang.String separator, int options) throws org.apache.oro.text.regex.MalformedPatternException
- Sets the field separator to use when splitting a line into fields.
If the field separator is never set, or set to null, matched input
lines are not split into fields.
setFieldSeparator
public void setFieldSeparator(java.lang.String separator) throws org.apache.oro.text.regex.MalformedPatternException
- Sets the field separator to use when splitting a line into fields.
If the field separator is never set, or set to null, matched input
lines are not split into fields.
processMatches
public void processMatches(java.io.InputStream input, java.io.OutputStream output, java.lang.String encoding) throws java.io.IOException
- This method reads the provided input one line at a time and for
every registered pattern that is contained in the line it executes
the associated MatchAction's processMatch() method. If a field
separator has been defined with
setFieldSeparator() 55 , the
fields member of the MatchActionInfo instance passed to the
processMatch() method is set to a Vector of Strings containing
the split fields of the line. Otherwise the fields member is set
to null. If no match was performed to invoke the action (i.e.,
a null pattern was registered), then the match member is set
to null. Otherwise, the match member will contain the result of
the match.
The input stream, having been exhausted, is closed right before the method terminates and the output stream is flushed.
processMatches
public void processMatches(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
- This method reads the provided input one line at a time using the
platform standart character encoding and for every registered
pattern that is contained in the line it executes the associated
MatchAction's processMatch() method. If a field separator has been
defined with setFieldSeparator() 55 , the
fields member of the MatchActionInfo instance passed to the
processMatch() method is set to a Vector of Strings containing
the split fields of the line. Otherwise the fields member is set
to null. If no match was performed to invoke the action (i.e.,
a null pattern was registered), then the match member is set
to null. Otherwise, the match member will contain the result of
the match.
The input stream, having been exhausted, is closed right before the method terminates and the output stream is flushed.
processMatches
public void processMatches(java.io.Reader input, java.io.Writer output) throws java.io.IOException
- This method reads the provided input one line at a time and for
every registered pattern that is contained in the line it executes
the associated MatchAction's processMatch() method. If a field
separator has been defined with
setFieldSeparator() 55 , the
fields member of the MatchActionInfo instance passed to the
processMatch() method is set to a Vector of Strings containing
the split fields of the line. Otherwise the fields member is set
to null. If no match was performed to invoke the action (i.e.,
a null pattern was registered), then the match member is set
to null. Otherwise, the match member will contain the result of
the match.
The input stream, having been exhausted, is closed right before the method terminates and the output stream is flushed.
|
|||||||||
| Home >> All >> org >> apache >> oro >> [ text overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC
org.apache.oro.text.MatchActionProcessor