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   * and errors from mono & csc on windows which has column num also
12   * <p/>
13   * ex error = "src\\test\\csharp\\Hierarchy\\Logger.cs(98,4): warning CS0618: 'NUnit.Framework.Assertion' is obsolete: 'Use Assert class instead'";
14   *
15   * @author <a href="mailto:gdodinet@karmicsoft.com">Gilles Dodinet</a>
16   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
17   * @author <a href="mailto:matthew.pocock@ncl.ac.uk">Matthew Pocock</a>
18   * @author <a href="mailto:chris.stevenson@gmail.com">Chris Stevenson</a>
19   */
20  public class DefaultCSharpCompilerParser
21  {
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  
32      public static CompilerMessage parseLine( String line )
33      {
34          CompilerMessage ce = null;
35  
36          if ( isOutputWithNoColumnNumber( line ) )
37          {
38              ce = parseLineWithNoColumnNumber( line );
39          }
40          else
41          {
42              ce = parseLineWithColumnNumberAndLineNumber( line );
43          }
44  
45          return ce;
46      }
47  
48      private static boolean isOutputWithNoColumnNumber( String line )
49      {
50  
51          int i = line.indexOf( MAGIC_LINE_MARKER );
52  
53          if ( i == -1 )
54          {
55              return true;
56          }
57  
58          String chunk1 = line.substring( i + MAGIC_LINE_MARKER.length() );
59  
60          int j = chunk1.indexOf( MAGIC_LINE_MARKER_2 );
61  
62          String chunk2 = chunk1.substring( 0, j );
63  
64          return ( chunk2.indexOf( "," ) == -1 );
65      }
66  
67      private static CompilerMessage parseLineWithNoColumnNumber( String line )
68      {
69  
70          String file = null;
71          boolean error = true;
72          int startline = -1;
73          int startcolumn = -1;
74          int endline = -1;
75          int endcolumn = -1;
76          String message;
77  
78          if ( line.startsWith( ERROR_PREFIX ) )
79          {
80              message = line.substring( ERROR_PREFIX.length() );
81          }
82          else if ( line.startsWith( COMPILATION_PREFIX ) )
83          {
84              // ignore
85  
86              return null;
87          }
88          else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
89          {
90              int i = line.indexOf( MAGIC_LINE_MARKER );
91  
92              int j = line.indexOf( ' ', i );
93  
94              file = line.substring( 0, i + 3 );
95  
96              String num = line.substring( i + MAGIC_LINE_MARKER.length(), j - 1 );
97  
98              startline = Integer.parseInt( num );
99  
100             endline = startline;
101 
102             message = line.substring( j + 1 + ERROR_PREFIX.length() );
103 
104             error = line.indexOf( ") error" ) != -1;
105         }
106         else
107         {
108             System.err.println( "Unknown output: " + line );
109 
110             return null;
111         }
112 
113         return new CompilerMessage( file, error, startline, startcolumn, endline, endcolumn, message );
114 
115     }
116 
117     private static CompilerMessage parseLineWithColumnNumberAndLineNumber( String line )
118     {
119 
120         String file = null;
121         boolean error = true;
122         int startline = -1;
123         int startcolumn = -1;
124         int endline = -1;
125         int endcolumn = -1;
126         String message;
127 
128         if ( line.startsWith( ERROR_PREFIX ) )
129         {
130             message = line.substring( ERROR_PREFIX.length() );
131         }
132         else if ( line.startsWith( COMPILATION_PREFIX ) )
133         {
134             return null;
135         }
136         else if ( line.indexOf( MAGIC_LINE_MARKER ) != -1 )
137         {
138             int i = line.indexOf( MAGIC_LINE_MARKER );
139 
140             int j = line.indexOf( ' ', i );
141 
142             file = line.substring( 0, i + 3 );
143 
144             String linecol = line.substring( i + MAGIC_LINE_MARKER.length(), j - 2 );
145 
146             String linenum = null;
147             String colnum = null;
148 
149             if ( linecol.indexOf( "," ) > -1 && linecol.split( "," ).length == 2 )
150             {
151                 linenum = linecol.split( "," )[0];
152                 colnum = linecol.split( "," )[1];
153             }
154             else if ( linecol.split( "," ).length == 1 )
155             {
156                 linenum = linecol.split( "," )[0];
157                 colnum = "-1";
158             }
159             else
160             {
161                 linenum = linecol.trim();
162                 colnum = "-1";
163             }
164 
165             startline = StringUtils.isEmpty( linenum ) ? -1 : Integer.parseInt( linenum );
166 
167             startcolumn = StringUtils.isEmpty( colnum ) ? -1 : Integer.parseInt( colnum );
168 
169             endline = startline;
170 
171             endcolumn = startcolumn;
172 
173             message = line.substring( j + 1 + ERROR_PREFIX.length() );
174 
175             error = line.indexOf( "): error" ) != -1;
176         }
177         else
178         {
179             System.err.println( "Unknown output: " + line );
180 
181             return null;
182         }
183 
184         return new CompilerMessage( file, error, startline, startcolumn, endline, endcolumn, message );
185     }
186 
187 }