Skip to content

Commit b381de3

Browse files
fjalvingholamy
authored andcommitted
Add the response file parser.
1 parent 1e3835f commit b381de3

File tree

2 files changed

+331
-131
lines changed

2 files changed

+331
-131
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.codehaus.plexus.compiler.eclipse;
2+
3+
import org.codehaus.plexus.compiler.CompilerMessage;
4+
import org.codehaus.plexus.compiler.CompilerMessage.Kind;
5+
6+
import javax.xml.stream.XMLInputFactory;
7+
import javax.xml.stream.XMLStreamConstants;
8+
import javax.xml.stream.XMLStreamReader;
9+
import java.io.BufferedReader;
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStreamReader;
14+
import java.io.Reader;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
/**
19+
* @author <a href="mailto:[email protected]">Frits Jalvingh</a>
20+
* Created on 31-3-18.
21+
*/
22+
public class EcjResponseParser {
23+
/*--------------------------------------------------------------*/
24+
/* CODING: Decode ECJ -log format results. */
25+
/*--------------------------------------------------------------*/
26+
/**
27+
* Scan the specified response file for compilation messages.
28+
*/
29+
public List<CompilerMessage> parse(File xmltf, boolean warningsAsErrors) throws Exception {
30+
//if(xmltf.length() < 80)
31+
// return;
32+
33+
List<CompilerMessage> list = new ArrayList<>();
34+
XMLInputFactory xmlif = getStreamFactory();
35+
try(Reader src = new BufferedReader(new InputStreamReader(new FileInputStream(xmltf), "utf-8"))) {
36+
XMLStreamReader xsr = xmlif.createXMLStreamReader(src);
37+
38+
// scan for "source" elements, skip all else.
39+
while(xsr.hasNext()) {
40+
int type = xsr.next();
41+
if(type == XMLStreamConstants.START_ELEMENT && "source".equals(xsr.getLocalName())) {
42+
decodeSourceElement(list, xsr, warningsAsErrors);
43+
}
44+
}
45+
}
46+
return list;
47+
}
48+
49+
private void decodeSourceElement(List<CompilerMessage> list, XMLStreamReader xsr, boolean warningsAsErrors) throws Exception {
50+
String filename = xsr.getAttributeValue(null, "path");
51+
52+
//-- Got a file- call handler
53+
File path = new File(filename).getCanonicalFile();
54+
while(xsr.hasNext()) {
55+
int type = xsr.nextTag();
56+
if(type == XMLStreamConstants.START_ELEMENT) {
57+
if("problems".equals(xsr.getLocalName())) {
58+
decodeProblems(list, path.toString(), xsr, warningsAsErrors);
59+
} else
60+
ignoreTillEnd(xsr);
61+
62+
} else if(type == XMLStreamConstants.END_ELEMENT) {
63+
return;
64+
}
65+
}
66+
}
67+
68+
/**
69+
* Locate "problem" nodes.
70+
*/
71+
private void decodeProblems(List<CompilerMessage> list, String sourcePath, XMLStreamReader xsr, boolean warningsAsErrors) throws Exception {
72+
while(xsr.hasNext()) {
73+
int type = xsr.nextTag();
74+
if(type == XMLStreamConstants.START_ELEMENT) {
75+
if("problem".equals(xsr.getLocalName())) {
76+
decodeProblem(list, sourcePath, xsr, warningsAsErrors);
77+
} else
78+
ignoreTillEnd(xsr);
79+
80+
} else if(type == XMLStreamConstants.END_ELEMENT) {
81+
return;
82+
}
83+
}
84+
}
85+
86+
87+
private void decodeProblem(List<CompilerMessage> list, String sourcePath, XMLStreamReader xsr, boolean warningsAsErrors) throws Exception {
88+
String id = xsr.getAttributeValue(null, "optionKey"); // Key for the problem
89+
int startline = getInt(xsr, "line");
90+
int column = getInt(xsr, "charStart");
91+
int endCol = getInt(xsr, "charEnd");
92+
String sev = xsr.getAttributeValue(null, "severity");
93+
String message = "Unknown message?";
94+
95+
//-- Go watch for "message"
96+
while(xsr.hasNext()) {
97+
int type = xsr.nextTag();
98+
if(type == XMLStreamConstants.START_ELEMENT) {
99+
if("message".equals(xsr.getLocalName())) {
100+
message = xsr.getAttributeValue(null, "value");
101+
}
102+
ignoreTillEnd(xsr);
103+
104+
} else if(type == XMLStreamConstants.END_ELEMENT) {
105+
break;
106+
}
107+
}
108+
109+
Kind msgtype;
110+
if("warning".equalsIgnoreCase(sev))
111+
msgtype = warningsAsErrors ? Kind.ERROR : Kind.WARNING;
112+
else if("error".equalsIgnoreCase(sev))
113+
msgtype = Kind.ERROR;
114+
else if("info".equalsIgnoreCase(sev))
115+
msgtype = Kind.NOTE;
116+
else {
117+
msgtype = Kind.OTHER;
118+
}
119+
120+
CompilerMessage cm = new CompilerMessage(sourcePath, msgtype, startline, column, startline, endCol, message);
121+
list.add(cm);
122+
}
123+
124+
private static void ignoreTillEnd(XMLStreamReader xsr) throws Exception {
125+
int depth = 1;
126+
while(xsr.hasNext()) {
127+
int type = xsr.next();
128+
if(type == XMLStreamConstants.START_ELEMENT) {
129+
depth++;
130+
} else if(type == XMLStreamConstants.END_ELEMENT) {
131+
depth--;
132+
if(depth == 0)
133+
return;
134+
}
135+
}
136+
}
137+
138+
private static int getInt(XMLStreamReader xsr, String name) throws IOException {
139+
String v = xsr.getAttributeValue(null, name);
140+
if(null == v)
141+
return -1;
142+
try {
143+
return Integer.parseInt(v.trim());
144+
} catch(Exception x) {
145+
throw new IOException("Illegal integer value '" + v + "' in attribute " + name);
146+
}
147+
}
148+
149+
static private XMLInputFactory getStreamFactory() {
150+
XMLInputFactory xmlif = XMLInputFactory.newInstance();
151+
xmlif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
152+
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
153+
xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
154+
return xmlif;
155+
}
156+
157+
}

0 commit comments

Comments
 (0)