Java Doc for MatchActionProcessor.java in  » Development » Jakarta-ORO » org » apache » oro » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Jakarta ORO » org.apache.oro.text 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.oro.text.MatchActionProcessor

MatchActionProcessor
final public class MatchActionProcessor (Code)
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 MatchActionProcessor.setFieldSeparator setFieldSeparator() Finally, as many pattern action pairs as desired are registerd with MatchActionProcessor.addAction addAction() before processing the input with MatchActionProcessor.processMatches processMatches() . 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
 

version:
   @version@
since:
   1.0
See Also:   MatchAction
See Also:   MatchActionInfo



Constructor Summary
public  MatchActionProcessor(PatternCompiler compiler, PatternMatcher matcher)
     Creates a new MatchActionProcessor instance initialized with the specified pattern compiler and matcher.
public  MatchActionProcessor()
     Default constructor for MatchActionProcessor.

Method Summary
public  voidaddAction(String pattern, int options, MatchAction action)
     Registers a pattern action pair, providing options to be used to compile the pattern.
public  voidaddAction(String pattern, int options)
     Binds a patten to the default action, providing options to be used to compile the pattern.
public  voidaddAction(String pattern)
     Binds a patten to the default action.
public  voidaddAction(String pattern, MatchAction action)
     Registers a pattern action pair.
public  voidprocessMatches(InputStream input, OutputStream output, 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.
public  voidprocessMatches(InputStream input, 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.
public  voidprocessMatches(Reader input, 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.
public  voidsetFieldSeparator(String separator, int options)
     Sets the field separator to use when splitting a line into fields.
public  voidsetFieldSeparator(String separator)
     Sets the field separator to use when splitting a line into fields.


Constructor Detail
MatchActionProcessor
public MatchActionProcessor(PatternCompiler compiler, PatternMatcher matcher)(Code)
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 MatchActionProcessor.setFieldSeparator setFieldSeparator() .


Parameters:
  compiler - The PatternCompiler to use to compile registeredpatterns.
Parameters:
  matcher - The PatternMatcher to use when searching for matches.




MatchActionProcessor
public MatchActionProcessor()(Code)
Default constructor for MatchActionProcessor. Same as calling
MatchActionProcessor(new Perl5Compiler(), new Perl5Matcher());




Method Detail
addAction
public void addAction(String pattern, int options, MatchAction action) throws MalformedPatternException(Code)
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.


Parameters:
  pattern - The pattern to bind to an action.
Parameters:
  options - The compilation options to use for the pattern.
Parameters:
  action - The action to associate with the pattern.
exception:
  MalformedPatternException - If the pattern cannot be compiled.




addAction
public void addAction(String pattern, int options) throws MalformedPatternException(Code)
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.


Parameters:
  pattern - The pattern to bind to an action.
Parameters:
  options - The compilation options to use for the pattern.
exception:
  MalformedPatternException - If the pattern cannot be compiled.




addAction
public void addAction(String pattern) throws MalformedPatternException(Code)
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.


Parameters:
  pattern - The pattern to bind to an action.
exception:
  MalformedPatternException - If the pattern cannot be compiled.




addAction
public void addAction(String pattern, MatchAction action) throws MalformedPatternException(Code)
Registers a pattern action pair. If a pattern is null, the action is performed for every line of input.


Parameters:
  pattern - The pattern to bind to an action.
Parameters:
  action - The action to associate with the pattern.
exception:
  MalformedPatternException - If the pattern cannot be compiled.




processMatches
public void processMatches(InputStream input, OutputStream output, String encoding) throws IOException(Code)
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 MatchActionProcessor.setFieldSeparator setFieldSeparator() , 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.


See Also:   MatchActionInfo
Parameters:
  input - The input stream from which to read lines.
Parameters:
  output - Where to send output.
Parameters:
  encoding - The character encoding of the InputStream source.If you also want to define an output character encoding,you should use MatchActionProcessor.processMatches(Reader,Writer)and specify the encodings when creating the Reader andWriter sources and sinks.
exception:
  IOException - If an error occurs while reading inputor writing output.




processMatches
public void processMatches(InputStream input, OutputStream output) throws IOException(Code)
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 MatchActionProcessor.setFieldSeparator setFieldSeparator() , 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.


See Also:   MatchActionInfo
Parameters:
  input - The input stream from which to read lines.
Parameters:
  output - Where to send output.
exception:
  IOException - If an error occurs while reading inputor writing output.




processMatches
public void processMatches(Reader input, Writer output) throws IOException(Code)
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 MatchActionProcessor.setFieldSeparator setFieldSeparator() , 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.


See Also:   MatchActionInfo
Parameters:
  input - The input stream from which to read lines.
Parameters:
  output - Where to send output.
exception:
  IOException - If an error occurs while reading inputor writing output.




setFieldSeparator
public void setFieldSeparator(String separator, int options) throws MalformedPatternException(Code)
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.


Parameters:
  separator - A regular expression defining the field separator.
Parameters:
  options - The options to use when compiling the separator.
exception:
  MalformedPatternException - If the separator cannot be compiled.




setFieldSeparator
public void setFieldSeparator(String separator) throws MalformedPatternException(Code)
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.


Parameters:
  separator - A regular expression defining the field separator.
exception:
  MalformedPatternException - If the separator cannot be compiled.




Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.