cli-common module extracted, to be used by the compiler-runner module

This commit is contained in:
Andrey Breslav
2012-11-08 19:14:14 +04:00
parent 3a96559ce2
commit 32745f1202
12 changed files with 17 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
</component>
</module>
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import org.jetbrains.annotations.Nullable;
/**
* @author abreslav
*/
public class CompilerMessageLocation {
public static final CompilerMessageLocation NO_LOCATION = new CompilerMessageLocation(null, -1, -1);
public static CompilerMessageLocation create(@Nullable String path, int line, int column) {
if (path == null) {
return NO_LOCATION;
}
return new CompilerMessageLocation(path, line, column);
}
private final String path;
private final int line;
private final int column;
private CompilerMessageLocation(@Nullable String path, int line, int column) {
this.path = path;
this.line = line;
this.column = column;
}
@Nullable
public String getPath() {
return path;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import java.util.EnumSet;
/**
* @author abreslav
*/
public enum CompilerMessageSeverity {
INFO,
ERROR,
WARNING,
EXCEPTION,
LOGGING;
public static final EnumSet<CompilerMessageSeverity> ERRORS = EnumSet.of(ERROR, EXCEPTION);
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import org.jetbrains.annotations.NotNull;
public interface MessageCollector {
MessageCollector PLAIN_TEXT_TO_SYSTEM_ERR = new MessageCollectorPlainTextToStream(System.err);
void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location);
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import org.jetbrains.annotations.NotNull;
import java.io.PrintStream;
/**
* @author Stepan Koltsov
*/
public class MessageCollectorPlainTextToStream implements MessageCollector {
@NotNull
private final PrintStream stream;
public MessageCollectorPlainTextToStream(@NotNull PrintStream stream) {
this.stream = stream;
}
@Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
stream.println(MessageRenderer.PLAIN.render(severity, message, location));
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/**
* @author Stepan Koltsov
*/
public class MessageCollectorToString implements MessageCollector {
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private final MessageCollector actualCollector = new MessageCollectorPlainTextToStream(new PrintStream(outputStream));
@Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
actualCollector.report(severity, message, location);
}
private static final Charset UTF8 = Charset.forName("utf-8");
@NotNull
public String getString() {
return UTF8.decode(ByteBuffer.wrap(outputStream.toByteArray())).toString();
}
}
@@ -0,0 +1,99 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* @author abreslav
*/
public interface MessageRenderer {
MessageRenderer TAGS = new MessageRenderer() {
@Override
public String renderPreamble() {
return "<MESSAGES>";
}
@Override
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
StringBuilder out = new StringBuilder();
out.append("<").append(severity.toString());
if (location.getPath() != null) {
out.append(" path=\"").append(e(location.getPath())).append("\"");
out.append(" line=\"").append(location.getLine()).append("\"");
out.append(" column=\"").append(location.getColumn()).append("\"");
}
out.append(">");
out.append(e(message));
out.append("</").append(severity.toString()).append(">\n");
return out.toString();
}
private String e(String str) {
return StringUtil.escapeXml(str);
}
@Override
public String renderException(@NotNull Throwable e) {
return render(CompilerMessageSeverity.EXCEPTION, PLAIN.renderException(e), CompilerMessageLocation.NO_LOCATION);
}
@Override
public String renderConclusion() {
return "</MESSAGES>";
}
};
MessageRenderer PLAIN = new MessageRenderer() {
@Override
public String renderPreamble() {
return "";
}
@Override
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
String path = location.getPath();
String position = path == null ? "" : path + ": (" + (location.getLine() + ", " + location.getColumn()) + ") ";
return severity + ": " + position + message;
}
@Override
public String renderException(@NotNull Throwable e) {
StringWriter out = new StringWriter();
//noinspection IOResourceOpenedButNotSafelyClosed
e.printStackTrace(new PrintWriter(out));
return out.toString();
}
@Override
public String renderConclusion() {
return "";
}
};
String renderPreamble();
String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location);
String renderException(@NotNull Throwable e);
String renderConclusion();
}
@@ -0,0 +1,82 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation;
import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.cli.common.messages.MessageRenderer;
import java.io.PrintStream;
import java.util.Collection;
/**
* @author Pavel Talanov
*/
public class PrintingMessageCollector implements MessageCollector {
private boolean verbose;
private final PrintStream errStream;
private final MessageRenderer messageRenderer;
// File path (nullable) -> error message
private final Multimap<String, String> groupedMessages = LinkedHashMultimap.create();
public PrintingMessageCollector(PrintStream errStream,
MessageRenderer messageRenderer,
boolean verbose) {
this.verbose = verbose;
this.errStream = errStream;
this.messageRenderer = messageRenderer;
}
@Override
public void report(@NotNull CompilerMessageSeverity severity,
@NotNull String message,
@NotNull CompilerMessageLocation location) {
String text = messageRenderer.render(severity, message, location);
if (severity == CompilerMessageSeverity.LOGGING) {
if (!verbose) {
return;
}
errStream.println(text);
}
groupedMessages.put(location.getPath(), text);
}
public void printToErrStream() {
if (!groupedMessages.isEmpty()) {
for (String path : groupedMessages.keySet()) {
Collection<String> messageTexts = groupedMessages.get(path);
for (String text : messageTexts) {
errStream.println(text);
}
}
}
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
}