KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > execution > RegexpOutputRecognizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.ruby.rubyproject.execution;
20
21 import java.util.regex.Matcher JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24 import org.netbeans.modules.ruby.rubyproject.execution.OutputRecognizer.FileLocation;
25 import org.openide.ErrorManager;
26
27
28 /**
29  * A RegexpOutputRecognizer is an OutputRecognizer which knows how to recognize
30  * filenames and linenumbers based on regular expressions. Patterns are compiled once and then
31  * matched for each line.
32  *
33  * @author Tor Norbye
34  */

35 public class RegexpOutputRecognizer extends OutputRecognizer {
36     private final Pattern JavaDoc pattern;
37     private final int fileGroup;
38     private final int lineGroup;
39     private final int columnGroup;
40
41     /** Creates a new instance of RegexpOutputRecognizer */
42     public RegexpOutputRecognizer(String JavaDoc regexp, int fileGroup, int lineGroup, int columnGroup) {
43         pattern = Pattern.compile(regexp);
44         this.fileGroup = fileGroup;
45         this.lineGroup = lineGroup;
46         this.columnGroup = columnGroup;
47     }
48
49     @Override JavaDoc
50     public FileLocation processLine(String JavaDoc line) {
51         Matcher JavaDoc match = pattern.matcher(line);
52
53         if (match.matches()) {
54             String JavaDoc file = null;
55             int lineno = -1;
56             int column = -1;
57
58             if (fileGroup != -1) {
59                 file = match.group(fileGroup);
60             }
61
62             if (lineGroup != -1) {
63                 String JavaDoc linenoStr = match.group(lineGroup);
64
65                 try {
66                     lineno = Integer.parseInt(linenoStr);
67                 } catch (NumberFormatException JavaDoc nfe) {
68                     ErrorManager.getDefault().notify(nfe);
69                     lineno = 0;
70                 }
71             }
72
73             if (columnGroup != -1) {
74                 String JavaDoc columnStr = match.group(columnGroup);
75
76                 try {
77                     column = Integer.parseInt(columnStr);
78                 } catch (NumberFormatException JavaDoc nfe) {
79                     ErrorManager.getDefault().notify(nfe);
80                     column = 0;
81                 }
82             }
83
84             return new FileLocation(file, lineno, column);
85         }
86
87         return null;
88     }
89 }
90
Popular Tags