1 31 32 package org.opencms.main; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.i18n.CmsMessageContainer; 36 import org.opencms.util.CmsStringUtil; 37 38 import java.io.PrintWriter ; 39 import java.io.StringWriter ; 40 import java.util.Locale ; 41 42 54 public class CmsException extends Exception implements I_CmsThrowable { 55 56 57 private static final long serialVersionUID = -1372556209321406104L; 58 59 60 protected CmsMessageContainer m_message; 61 62 67 public CmsException(CmsMessageContainer message) { 68 69 super(message.getKey()); 70 m_message = message; 71 } 72 73 79 public CmsException(CmsMessageContainer message, Throwable cause) { 80 81 super(message.getKey(), cause); 82 m_message = message; 83 } 84 85 94 public static String getFormattedErrorstack(Throwable t) { 95 96 String stacktrace = CmsException.getStackTraceAsString(t); 97 if (CmsStringUtil.isEmpty(stacktrace)) { 98 return ""; 99 } else { 100 stacktrace = CmsStringUtil.escapeJavaScript(stacktrace); 101 stacktrace = CmsEncoder.escapeXml(stacktrace); 102 StringBuffer result = new StringBuffer (256); 103 result.append("<html><body style='background-color: Window; overflow: scroll;'><pre>"); 104 result.append(stacktrace); 105 result.append("</pre></body></html>"); 106 return result.toString(); 107 } 108 } 109 110 119 public static String getStackTraceAsString(Throwable e) { 120 121 StringWriter stringWriter = new StringWriter (); 122 e.printStackTrace(new PrintWriter (stringWriter)); 123 return stringWriter.toString(); 124 } 125 126 134 public CmsException createException(CmsMessageContainer container, Throwable cause) { 135 136 return new CmsException(container, cause); 137 } 138 139 142 public String getLocalizedMessage() { 143 144 if (m_message == null) { 145 return super.getLocalizedMessage(); 146 } 147 return m_message.key(); 148 } 149 150 153 public String getLocalizedMessage(Locale locale) { 154 155 return m_message.key(locale); 156 } 157 158 161 public String getMessage() { 162 163 return getLocalizedMessage(); 164 } 165 166 169 public CmsMessageContainer getMessageContainer() { 170 171 return m_message; 172 } 173 } | Popular Tags |