📄 matchactionprocessor.java
字号:
* is performed for every line of input. * <p> * @param pattern The pattern to bind to an action. * @exception MalformedPatternException If the pattern cannot be compiled. */ public void addAction(String pattern) throws MalformedPatternException { addAction(pattern, 0); } /** * Registers a pattern action pair. If a pattern is null, the action * is performed for every line of input. * <p> * @param pattern The pattern to bind to an action. * @param action The action to associate with the pattern. * @exception MalformedPatternException If the pattern cannot be compiled. */ public void addAction(String pattern, MatchAction action) throws MalformedPatternException { addAction(pattern, 0, action); } /** * 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. * <p> * @param separator A regular expression defining the field separator. * @param options The options to use when compiling the separator. * @exception MalformedPatternException If the separator cannot be compiled. */ public void setFieldSeparator(String separator, int options) throws MalformedPatternException { if(separator == null) { __fieldSeparator = null; return; } __fieldSeparator = __compiler.compile(separator, options); } /** * 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. * <p> * @param separator A regular expression defining the field separator. * @exception MalformedPatternException If the separator cannot be compiled. */ public void setFieldSeparator(String separator) throws MalformedPatternException { setFieldSeparator(separator, 0); } /** * 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 * {@link #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. * <p> * The input stream, having been exhausted, is closed right before the * method terminates and the output stream is flushed. * <p> * @see MatchActionInfo * @param input The input stream from which to read lines. * @param output Where to send output. * @param encoding The character encoding of the InputStream source. * If you also want to define an output character encoding, * you should use {@link #processMatches(Reader, Writer)} * and specify the encodings when creating the Reader and * Writer sources and sinks. * @exception IOException If an error occurs while reading input * or writing output. */ public void processMatches(InputStream input, OutputStream output, String encoding) throws IOException { processMatches(new InputStreamReader(input, encoding), new OutputStreamWriter(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. If a field separator has been * defined with {@link #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. * * <p> * The input stream, having been exhausted, is closed right before the * method terminates and the output stream is flushed. * <p> * * @see MatchActionInfo * @param input The input stream from which to read lines. * @param output Where to send output. * @exception IOException If an error occurs while reading input * or writing output. */ public void processMatches(InputStream input, OutputStream output) throws IOException { processMatches(new InputStreamReader(input), new OutputStreamWriter(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. If a field * separator has been defined with * {@link #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. * <p> * The input stream, having been exhausted, is closed right before the * method terminates and the output stream is flushed. * <p> * @see MatchActionInfo * @param input The input stream from which to read lines. * @param output Where to send output. * @exception IOException If an error occurs while reading input * or writing output. */ public void processMatches(Reader input, Writer output) throws IOException { int patternCount, current; LineNumberReader reader = new LineNumberReader(input); PrintWriter writer = new PrintWriter(output); MatchActionInfo info = new MatchActionInfo(); Object obj; Pattern pattern; MatchAction action; List fields = new ArrayList(); // Set those things that will not change. info.matcher = __matcher; info.fieldSeparator = __fieldSeparator; info.input = reader; info.output = writer; info.fields = null; patternCount = __patterns.size(); info.lineNumber = 0; while((info.line = reader.readLine()) != null) { info.charLine = info.line.toCharArray(); for(current=0; current < patternCount; current++) { obj = __patterns.elementAt(current); // If a pattern is null, it means to do it for every line. if(obj != null) { pattern = (Pattern)__patterns.elementAt(current); if(__matcher.contains(info.charLine, pattern)) { info.match = __matcher.getMatch(); info.lineNumber = reader.getLineNumber(); info.pattern = pattern; if(__fieldSeparator != null) { fields.clear(); Util.split(fields, __matcher, __fieldSeparator, info.line); info.fields = fields; } else info.fields = null; action = (MatchAction)__actions.elementAt(current); action.processMatch(info); } } else { info.match = null; info.lineNumber = reader.getLineNumber(); if(__fieldSeparator != null) { fields.clear(); Util.split(fields, __matcher, __fieldSeparator, info.line); info.fields = fields; } else info.fields = null; action = (MatchAction)__actions.elementAt(current); action.processMatch(info); } } } // Flush output but don't close, close input since we reached end. writer.flush(); reader.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -