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