Convert CompilerOutputParser: convert to kotlin
This commit is contained in:
+105
-111
@@ -14,174 +14,168 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.compilerRunner;
|
package org.jetbrains.kotlin.compilerRunner
|
||||||
|
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil
|
||||||
import com.intellij.util.containers.Stack;
|
import com.intellij.util.containers.Stack
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation;
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
|
import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
import org.xml.sax.Attributes
|
||||||
import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil;
|
import org.xml.sax.InputSource
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.SAXException
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.helpers.DefaultHandler
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
|
||||||
|
|
||||||
import javax.xml.parsers.SAXParser;
|
import javax.xml.parsers.SAXParser
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
import javax.xml.parsers.SAXParserFactory
|
||||||
import java.io.IOException;
|
import java.io.IOException
|
||||||
import java.io.Reader;
|
import java.io.Reader
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.Companion.NO_LOCATION
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil.reportException;
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil.reportException
|
||||||
|
|
||||||
public class CompilerOutputParser {
|
object CompilerOutputParser {
|
||||||
public static void parseCompilerMessagesFromReader(MessageCollector messageCollector, final Reader reader, OutputItemsCollector collector) {
|
fun parseCompilerMessagesFromReader(messageCollector: MessageCollector, reader: Reader, collector: OutputItemsCollector) {
|
||||||
// Sometimes the compiler doesn't output valid XML.
|
// Sometimes the compiler doesn't output valid XML.
|
||||||
// Example: error in command line arguments passed to the compiler.
|
// Example: error in command line arguments passed to the compiler.
|
||||||
// The compiler will print the usage and the SAX parser will break.
|
// The compiler will print the usage and the SAX parser will break.
|
||||||
// In this case, we want to read everything from this stream and report it as an IDE error.
|
// In this case, we want to read everything from this stream and report it as an IDE error.
|
||||||
final StringBuilder stringBuilder = new StringBuilder();
|
val stringBuilder = StringBuilder()
|
||||||
Reader wrappingReader = new Reader() {
|
val wrappingReader = object : Reader() {
|
||||||
@Override
|
@Throws(IOException::class)
|
||||||
public int read(@NotNull char[] cbuf, int off, int len) throws IOException {
|
override fun read(cbuf: CharArray, off: Int, len: Int): Int {
|
||||||
int read = reader.read(cbuf, off, len);
|
val read = reader.read(cbuf, off, len)
|
||||||
stringBuilder.append(cbuf, off, len);
|
stringBuilder.append(cbuf, off, len)
|
||||||
return read;
|
return read
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Throws(IOException::class)
|
||||||
public void close() throws IOException {
|
override fun close() {
|
||||||
// Do nothing:
|
// Do nothing:
|
||||||
// If the SAX parser sees a syntax error, it throws an exception
|
// If the SAX parser sees a syntax error, it throws an exception
|
||||||
// and calls close() on the reader.
|
// and calls close() on the reader.
|
||||||
// We prevent hte reader from being closed here, and close it later,
|
// We prevent hte reader from being closed here, and close it later,
|
||||||
// when all the text is read from it
|
// when all the text is read from it
|
||||||
}
|
}
|
||||||
};
|
|
||||||
try {
|
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
||||||
SAXParser parser = factory.newSAXParser();
|
|
||||||
parser.parse(new InputSource(wrappingReader), new CompilerOutputSAXHandler(messageCollector, collector));
|
|
||||||
}
|
}
|
||||||
catch (Throwable e) {
|
try {
|
||||||
|
val factory = SAXParserFactory.newInstance()
|
||||||
|
val parser = factory.newSAXParser()
|
||||||
|
parser.parse(InputSource(wrappingReader), CompilerOutputSAXHandler(messageCollector, collector))
|
||||||
|
}
|
||||||
|
catch (e: Throwable) {
|
||||||
// Load all the text into the stringBuilder
|
// Load all the text into the stringBuilder
|
||||||
try {
|
try {
|
||||||
// This will not close the reader (see the wrapper above)
|
// This will not close the reader (see the wrapper above)
|
||||||
FileUtil.loadTextAndClose(wrappingReader);
|
FileUtil.loadTextAndClose(wrappingReader)
|
||||||
}
|
}
|
||||||
catch (IOException ioException) {
|
catch (ioException: IOException) {
|
||||||
reportException(messageCollector, ioException);
|
reportException(messageCollector, ioException)
|
||||||
}
|
}
|
||||||
String message = stringBuilder.toString();
|
|
||||||
reportException(messageCollector, new IllegalStateException(message, e));
|
val message = stringBuilder.toString()
|
||||||
messageCollector.report(ERROR, message, NO_LOCATION);
|
reportException(messageCollector, IllegalStateException(message, e))
|
||||||
|
messageCollector.report(ERROR, message, NO_LOCATION)
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
try {
|
||||||
reader.close();
|
reader.close()
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (e: IOException) {
|
||||||
reportException(messageCollector, e);
|
reportException(messageCollector, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CompilerOutputSAXHandler extends DefaultHandler {
|
private class CompilerOutputSAXHandler(private val messageCollector: MessageCollector, private val collector: OutputItemsCollector) : DefaultHandler() {
|
||||||
private static final Map<String, CompilerMessageSeverity> CATEGORIES = new ContainerUtil.ImmutableMapBuilder<String, CompilerMessageSeverity>()
|
|
||||||
.put("error", ERROR)
|
|
||||||
.put("warning", WARNING)
|
|
||||||
.put("logging", LOGGING)
|
|
||||||
.put("output", OUTPUT)
|
|
||||||
.put("exception", EXCEPTION)
|
|
||||||
.put("info", INFO)
|
|
||||||
.put("messages", INFO) // Root XML element
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private final MessageCollector messageCollector;
|
private val message = StringBuilder()
|
||||||
private final OutputItemsCollector collector;
|
private val tags = Stack<String>()
|
||||||
|
private var path: String? = null
|
||||||
|
private var line: Int = 0
|
||||||
|
private var column: Int = 0
|
||||||
|
|
||||||
private final StringBuilder message = new StringBuilder();
|
@Throws(SAXException::class)
|
||||||
private final Stack<String> tags = new Stack<String>();
|
override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) {
|
||||||
private String path;
|
tags.push(qName)
|
||||||
private int line;
|
|
||||||
private int column;
|
|
||||||
|
|
||||||
public CompilerOutputSAXHandler(MessageCollector messageCollector, OutputItemsCollector collector) {
|
message.setLength(0)
|
||||||
this.messageCollector = messageCollector;
|
|
||||||
this.collector = collector;
|
path = attributes.getValue("path")
|
||||||
|
line = safeParseInt(attributes.getValue("line"), -1)
|
||||||
|
column = safeParseInt(attributes.getValue("column"), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Throws(SAXException::class)
|
||||||
public void startElement(@NotNull String uri, @NotNull String localName, @NotNull String qName, @NotNull Attributes attributes)
|
override fun characters(ch: CharArray?, start: Int, length: Int) {
|
||||||
throws SAXException {
|
if (tags.size == 1) {
|
||||||
tags.push(qName);
|
|
||||||
|
|
||||||
message.setLength(0);
|
|
||||||
|
|
||||||
path = attributes.getValue("path");
|
|
||||||
line = safeParseInt(attributes.getValue("line"), -1);
|
|
||||||
column = safeParseInt(attributes.getValue("column"), -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
|
||||||
if (tags.size() == 1) {
|
|
||||||
// We're directly inside the root tag: <MESSAGES>
|
// We're directly inside the root tag: <MESSAGES>
|
||||||
String message = new String(ch, start, length);
|
val message = String(ch!!, start, length)
|
||||||
if (!message.trim().isEmpty()) {
|
if (!message.trim { it <= ' ' }.isEmpty()) {
|
||||||
messageCollector.report(ERROR, "Unhandled compiler output: " + message, NO_LOCATION);
|
messageCollector.report(ERROR, "Unhandled compiler output: " + message, NO_LOCATION)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
message.append(ch, start, length);
|
message.append(ch, start, length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Throws(SAXException::class)
|
||||||
public void endElement(String uri, @NotNull String localName, @NotNull String qName) throws SAXException {
|
override fun endElement(uri: String?, localName: String, qName: String) {
|
||||||
if (tags.size() == 1) {
|
if (tags.size == 1) {
|
||||||
// We're directly inside the root tag: <MESSAGES>
|
// We're directly inside the root tag: <MESSAGES>
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
String qNameLowerCase = qName.toLowerCase();
|
val qNameLowerCase = qName.toLowerCase()
|
||||||
CompilerMessageSeverity category = CATEGORIES.get(qNameLowerCase);
|
var category: CompilerMessageSeverity? = CATEGORIES[qNameLowerCase]
|
||||||
if (category == null) {
|
if (category == null) {
|
||||||
messageCollector.report(ERROR, "Unknown compiler message tag: " + qName, NO_LOCATION);
|
messageCollector.report(ERROR, "Unknown compiler message tag: " + qName, NO_LOCATION)
|
||||||
category = INFO;
|
category = INFO
|
||||||
}
|
}
|
||||||
String text = message.toString();
|
val text = message.toString()
|
||||||
|
|
||||||
if (category == OUTPUT) {
|
if (category == OUTPUT) {
|
||||||
reportToCollector(text);
|
reportToCollector(text)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
messageCollector.report(category, text, CompilerMessageLocation.create(path, line, column, null));
|
messageCollector.report(category, text, CompilerMessageLocation.create(path, line, column, null))
|
||||||
}
|
}
|
||||||
tags.pop();
|
tags.pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reportToCollector(String text) {
|
private fun reportToCollector(text: String) {
|
||||||
OutputMessageUtil.Output output = OutputMessageUtil.parseOutputMessage(text);
|
val output = OutputMessageUtil.parseOutputMessage(text)
|
||||||
if (output != null) {
|
if (output != null) {
|
||||||
collector.add(output.sourceFiles, output.outputFile);
|
collector.add(output.sourceFiles, output.outputFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int safeParseInt(@Nullable String value, int defaultValue) {
|
companion object {
|
||||||
if (value == null) {
|
private val CATEGORIES = ContainerUtil.ImmutableMapBuilder<String, CompilerMessageSeverity>()
|
||||||
return defaultValue;
|
.put("error", ERROR)
|
||||||
}
|
.put("warning", WARNING)
|
||||||
try {
|
.put("logging", LOGGING)
|
||||||
return Integer.parseInt(value.trim());
|
.put("output", OUTPUT)
|
||||||
}
|
.put("exception", EXCEPTION)
|
||||||
catch (NumberFormatException e) {
|
.put("info", INFO)
|
||||||
return defaultValue;
|
.put("messages", INFO) // Root XML element
|
||||||
|
.build()
|
||||||
|
|
||||||
|
private fun safeParseInt(value: String?, defaultValue: Int): Int {
|
||||||
|
if (value == null) {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(value.trim { it <= ' ' })
|
||||||
|
}
|
||||||
|
catch (e: NumberFormatException) {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user