View Javadoc
1   package org.codehaus.plexus.compiler.csharp;
2   
3   import org.codehaus.plexus.compiler.CompilerMessage;
4   import org.codehaus.plexus.util.StringUtils;
5   
6   /**
7    * Handles output from both mono with only the line number
8    * <p>
9    * ex error = "/home/trygvis/dev/com.myrealbox/trunk/mcs/nunit20/core/./TestRunnerThread.cs(29) error CS0246: Cannot find type 'NameValueCollection'"
10   * </p>
11   * <p>
12   * and errors from mono &amp; csc on windows which has column num also
13   * </p>
14   * ex error = "src\\test\\csharp\\Hierarchy\\Logger.cs(98,4): warning CS0618: 'NUnit.Framework.Assertion' is obsolete: 'Use Assert class instead'";
15   *
16   * @author <a href="mailto:gdodinet@karmicsoft.com">Gilles Dodinet</a>
17   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
18   * @author <a href="mailto:matthew.pocock@ncl.ac.uk">Matthew Pocock</a>
19   * @author <a href="mailto:chris.stevenson@gmail.com">Chris Stevenson</a>
20   */
21  public class DefaultCSharpCompilerParser {
22  
23      private static String ERROR_PREFIX = "error ";
24  
25      private static String COMPILATION_PREFIX = "Compilation ";
26  
27      private static String MAGIC_LINE_MARKER = ".cs(";
28  
29      private static String MAGIC_LINE_MARKER_2 = ")";
30  
31      public static CompilerMessage parseLine(String line) {
32          CompilerMessage ce = null;
33  
34          if (isOutputWithNoColumnNumber(line)) {
35              ce = parseLineWithNoColumnNumber(line);
36          } else {
37              ce = parseLineWithColumnNumberAndLineNumber(line);
38          }
39  
40          return ce;
41      }
42  
43      private static boolean isOutputWithNoColumnNumber(String line) {
44  
45          int i = line.indexOf(MAGIC_LINE_MARKER);
46  
47          if (i == -1) {
48              return true;
49          }
50  
51          String chunk1 = line.substring(i + MAGIC_LINE_MARKER.length());
52  
53          int j = chunk1.indexOf(MAGIC_LINE_MARKER_2);
54  
55          String chunk2 = chunk1.substring(0, j);
56  
57          return !chunk2.contains(",");
58      }
59  
60      private static CompilerMessage parseLineWithNoColumnNumber(String line) {
61  
62          String file = null;
63          boolean error = true;
64          int startline = -1;
65          int startcolumn = -1;
66          int endline = -1;
67          int endcolumn = -1;
68          String message;
69  
70          if (line.startsWith(ERROR_PREFIX)) {
71              message = line.substring(ERROR_PREFIX.length());
72          } else if (line.startsWith(COMPILATION_PREFIX)) {
73              // ignore
74  
75              return null;
76          } else if (line.contains(MAGIC_LINE_MARKER)) {
77              int i = line.indexOf(MAGIC_LINE_MARKER);
78  
79              int j = line.indexOf(' ', i);
80  
81              file = line.substring(0, i + 3);
82  
83              String num = line.substring(i + MAGIC_LINE_MARKER.length(), j - 1);
84  
85              startline = Integer.parseInt(num);
86  
87              endline = startline;
88  
89              message = line.substring(j + 1 + ERROR_PREFIX.length());
90  
91              error = line.contains(") error");
92          } else {
93              System.err.println("Unknown output: " + line);
94  
95              return null;
96          }
97  
98          return new CompilerMessage(file, error, startline, startcolumn, endline, endcolumn, message);
99      }
100 
101     private static CompilerMessage parseLineWithColumnNumberAndLineNumber(String line) {
102 
103         String file = null;
104         boolean error = true;
105         int startline = -1;
106         int startcolumn = -1;
107         int endline = -1;
108         int endcolumn = -1;
109         String message;
110 
111         if (line.startsWith(ERROR_PREFIX)) {
112             message = line.substring(ERROR_PREFIX.length());
113         } else if (line.startsWith(COMPILATION_PREFIX)) {
114             return null;
115         } else if (line.contains(MAGIC_LINE_MARKER)) {
116             int i = line.indexOf(MAGIC_LINE_MARKER);
117 
118             int j = line.indexOf(' ', i);
119 
120             file = line.substring(0, i + 3);
121 
122             String linecol = line.substring(i + MAGIC_LINE_MARKER.length(), j - 2);
123 
124             String linenum = null;
125             String colnum = null;
126 
127             if (linecol.contains(",") && linecol.split(",").length == 2) {
128                 linenum = linecol.split(",")[0];
129                 colnum = linecol.split(",")[1];
130             } else if (linecol.split(",").length == 1) {
131                 linenum = linecol.split(",")[0];
132                 colnum = "-1";
133             } else {
134                 linenum = linecol.trim();
135                 colnum = "-1";
136             }
137 
138             startline = StringUtils.isEmpty(linenum) ? -1 : Integer.parseInt(linenum);
139 
140             startcolumn = StringUtils.isEmpty(colnum) ? -1 : Integer.parseInt(colnum);
141 
142             endline = startline;
143 
144             endcolumn = startcolumn;
145 
146             message = line.substring(j + 1 + ERROR_PREFIX.length());
147 
148             error = line.contains("): error");
149         } else {
150             System.err.println("Unknown output: " + line);
151 
152             return null;
153         }
154 
155         return new CompilerMessage(file, error, startline, startcolumn, endline, endcolumn, message);
156     }
157 }