001 /* 002 * Copyright 2010-2013 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.jet.cli.common.messages; 018 019 import com.intellij.openapi.util.text.StringUtil; 020 import org.jetbrains.annotations.NotNull; 021 022 import java.io.PrintWriter; 023 import java.io.StringWriter; 024 025 public interface MessageRenderer { 026 027 MessageRenderer TAGS = new MessageRenderer() { 028 @Override 029 public String renderPreamble() { 030 return "<MESSAGES>"; 031 } 032 033 @Override 034 public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) { 035 StringBuilder out = new StringBuilder(); 036 out.append("<").append(severity.toString()); 037 if (location.getPath() != null) { 038 out.append(" path=\"").append(e(location.getPath())).append("\""); 039 out.append(" line=\"").append(location.getLine()).append("\""); 040 out.append(" column=\"").append(location.getColumn()).append("\""); 041 } 042 out.append(">"); 043 044 out.append(e(message)); 045 046 out.append("</").append(severity.toString()).append(">\n"); 047 return out.toString(); 048 } 049 050 private String e(String str) { 051 return StringUtil.escapeXml(str); 052 } 053 054 @Override 055 public String renderException(@NotNull Throwable e) { 056 return render(CompilerMessageSeverity.EXCEPTION, PLAIN.renderException(e), CompilerMessageLocation.NO_LOCATION); 057 } 058 059 @Override 060 public String renderConclusion() { 061 return "</MESSAGES>"; 062 } 063 }; 064 065 MessageRenderer PLAIN = new MessageRenderer() { 066 @Override 067 public String renderPreamble() { 068 return ""; 069 } 070 071 @Override 072 public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) { 073 String path = location.getPath(); 074 String position = path == null ? "" : path + ": (" + (location.getLine() + ", " + location.getColumn()) + ") "; 075 return severity + ": " + position + message; 076 } 077 078 @Override 079 public String renderException(@NotNull Throwable e) { 080 StringWriter out = new StringWriter(); 081 //noinspection IOResourceOpenedButNotSafelyClosed 082 e.printStackTrace(new PrintWriter(out)); 083 return out.toString(); 084 } 085 086 @Override 087 public String renderConclusion() { 088 return ""; 089 } 090 }; 091 092 String renderPreamble(); 093 String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location); 094 String renderException(@NotNull Throwable e); 095 String renderConclusion(); 096 }