1 /* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xalan.internal.xsltc.compiler.util; 22 23 import com.sun.org.apache.xalan.internal.utils.SecuritySupport; 24 import java.text.MessageFormat; 25 import java.util.Locale; 26 import java.util.ResourceBundle; 27 28 import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode; 30 31 /** 32 * @author Jacek Ambroziak 33 * @author Santiago Pericas-Geertsen 34 * @author G. Todd Miller 35 * @author Erwin Bolwidt <ejb@klomp.org> 36 * @author Morten Jorgensen 37 */ 38 public final class ErrorMsg { 39 40 private String _code; 41 private int _line; 42 private String _message = null; 43 private String _url = null; 44 Object[] _params = null; 45 private boolean _isWarningError; 46 47 Throwable _cause; 48 49 // Compiler error messages 50 public static final String MULTIPLE_STYLESHEET_ERR = "MULTIPLE_STYLESHEET_ERR"; 51 public static final String TEMPLATE_REDEF_ERR = "TEMPLATE_REDEF_ERR"; 52 public static final String TEMPLATE_UNDEF_ERR = "TEMPLATE_UNDEF_ERR"; 53 public static final String VARIABLE_REDEF_ERR = "VARIABLE_REDEF_ERR"; 54 public static final String VARIABLE_UNDEF_ERR = "VARIABLE_UNDEF_ERR"; 55 public static final String CLASS_NOT_FOUND_ERR = "CLASS_NOT_FOUND_ERR"; 56 public static final String METHOD_NOT_FOUND_ERR = "METHOD_NOT_FOUND_ERR"; 57 public static final String ARGUMENT_CONVERSION_ERR = "ARGUMENT_CONVERSION_ERR"; 58 public static final String FILE_NOT_FOUND_ERR = "FILE_NOT_FOUND_ERR"; 59 public static final String INVALID_URI_ERR = "INVALID_URI_ERR"; 60 public static final String CATALOG_EXCEPTION = "CATALOG_EXCEPTION"; 61 public static final String FILE_ACCESS_ERR = "FILE_ACCESS_ERR"; 62 public static final String MISSING_ROOT_ERR = "MISSING_ROOT_ERR"; 63 public static final String NAMESPACE_UNDEF_ERR = "NAMESPACE_UNDEF_ERR"; 64 public static final String FUNCTION_RESOLVE_ERR = "FUNCTION_RESOLVE_ERR"; 65 public static final String NEED_LITERAL_ERR = "NEED_LITERAL_ERR"; 66 public static final String XPATH_PARSER_ERR = "XPATH_PARSER_ERR"; 67 public static final String REQUIRED_ATTR_ERR = "REQUIRED_ATTR_ERR"; 68 public static final String ILLEGAL_CHAR_ERR = "ILLEGAL_CHAR_ERR"; 69 public static final String ILLEGAL_PI_ERR = "ILLEGAL_PI_ERR"; 70 public static final String STRAY_ATTRIBUTE_ERR = "STRAY_ATTRIBUTE_ERR"; 71 public static final String ILLEGAL_ATTRIBUTE_ERR = "ILLEGAL_ATTRIBUTE_ERR"; 72 public static final String CIRCULAR_INCLUDE_ERR = "CIRCULAR_INCLUDE_ERR"; 73 public static final String RESULT_TREE_SORT_ERR = "RESULT_TREE_SORT_ERR"; 74 public static final String SYMBOLS_REDEF_ERR = "SYMBOLS_REDEF_ERR"; 75 public static final String XSL_VERSION_ERR = "XSL_VERSION_ERR"; 76 public static final String CIRCULAR_VARIABLE_ERR = "CIRCULAR_VARIABLE_ERR"; 77 public static final String ILLEGAL_BINARY_OP_ERR = "ILLEGAL_BINARY_OP_ERR"; 78 public static final String ILLEGAL_ARG_ERR = "ILLEGAL_ARG_ERR"; 79 public static final String DOCUMENT_ARG_ERR = "DOCUMENT_ARG_ERR"; 80 public static final String MISSING_WHEN_ERR = "MISSING_WHEN_ERR"; 81 public static final String MULTIPLE_OTHERWISE_ERR = "MULTIPLE_OTHERWISE_ERR"; 82 public static final String STRAY_OTHERWISE_ERR = "STRAY_OTHERWISE_ERR"; 83 public static final String STRAY_WHEN_ERR = "STRAY_WHEN_ERR"; 84 public static final String WHEN_ELEMENT_ERR = "WHEN_ELEMENT_ERR"; 85 public static final String UNNAMED_ATTRIBSET_ERR = "UNNAMED_ATTRIBSET_ERR"; 86 public static final String ILLEGAL_CHILD_ERR = "ILLEGAL_CHILD_ERR"; 87 public static final String ILLEGAL_ELEM_NAME_ERR = "ILLEGAL_ELEM_NAME_ERR"; 88 public static final String ILLEGAL_ATTR_NAME_ERR = "ILLEGAL_ATTR_NAME_ERR"; 89 public static final String ILLEGAL_TEXT_NODE_ERR = "ILLEGAL_TEXT_NODE_ERR"; 90 public static final String SAX_PARSER_CONFIG_ERR = "SAX_PARSER_CONFIG_ERR"; 91 public static final String INTERNAL_ERR = "INTERNAL_ERR"; 92 public static final String UNSUPPORTED_XSL_ERR = "UNSUPPORTED_XSL_ERR"; 93 public static final String UNSUPPORTED_EXT_ERR = "UNSUPPORTED_EXT_ERR"; 94 public static final String MISSING_XSLT_URI_ERR = "MISSING_XSLT_URI_ERR"; 95 public static final String MISSING_XSLT_TARGET_ERR = "MISSING_XSLT_TARGET_ERR"; 96 public static final String ACCESSING_XSLT_TARGET_ERR = "ACCESSING_XSLT_TARGET_ERR"; 97 public static final String NOT_IMPLEMENTED_ERR = "NOT_IMPLEMENTED_ERR"; 98 public static final String NOT_STYLESHEET_ERR = "NOT_STYLESHEET_ERR"; 99 public static final String ELEMENT_PARSE_ERR = "ELEMENT_PARSE_ERR"; 100 public static final String KEY_USE_ATTR_ERR = "KEY_USE_ATTR_ERR"; 101 public static final String OUTPUT_VERSION_ERR = "OUTPUT_VERSION_ERR"; 102 public static final String ILLEGAL_RELAT_OP_ERR = "ILLEGAL_RELAT_OP_ERR"; 103 public static final String ATTRIBSET_UNDEF_ERR = "ATTRIBSET_UNDEF_ERR"; 104 public static final String ATTR_VAL_TEMPLATE_ERR = "ATTR_VAL_TEMPLATE_ERR"; 105 public static final String UNKNOWN_SIG_TYPE_ERR = "UNKNOWN_SIG_TYPE_ERR"; 106 public static final String DATA_CONVERSION_ERR = "DATA_CONVERSION_ERR"; 107 108 // JAXP/TrAX error messages 109 public static final String NO_TRANSLET_CLASS_ERR = "NO_TRANSLET_CLASS_ERR"; 110 public static final String NO_MAIN_TRANSLET_ERR = "NO_MAIN_TRANSLET_ERR"; 111 public static final String TRANSLET_CLASS_ERR = "TRANSLET_CLASS_ERR"; 112 public static final String TRANSLET_OBJECT_ERR = "TRANSLET_OBJECT_ERR"; 113 public static final String ERROR_LISTENER_NULL_ERR = "ERROR_LISTENER_NULL_ERR"; 114 public static final String JAXP_UNKNOWN_SOURCE_ERR = "JAXP_UNKNOWN_SOURCE_ERR"; 115 public static final String JAXP_NO_SOURCE_ERR = "JAXP_NO_SOURCE_ERR"; 116 public static final String JAXP_COMPILE_ERR = "JAXP_COMPILE_ERR"; 117 public static final String JAXP_INVALID_ATTR_ERR = "JAXP_INVALID_ATTR_ERR"; 118 public static final String JAXP_INVALID_ATTR_VALUE_ERR = "JAXP_INVALID_ATTR_VALUE_ERR"; 119 public static final String JAXP_SET_RESULT_ERR = "JAXP_SET_RESULT_ERR"; 120 public static final String JAXP_NO_TRANSLET_ERR = "JAXP_NO_TRANSLET_ERR"; 121 public static final String JAXP_NO_HANDLER_ERR = "JAXP_NO_HANDLER_ERR"; 122 public static final String JAXP_NO_RESULT_ERR = "JAXP_NO_RESULT_ERR"; 123 public static final String JAXP_UNKNOWN_PROP_ERR = "JAXP_UNKNOWN_PROP_ERR"; 124 public static final String SAX2DOM_ADAPTER_ERR = "SAX2DOM_ADAPTER_ERR"; 125 public static final String XSLTC_SOURCE_ERR = "XSLTC_SOURCE_ERR"; 126 public static final String ER_RESULT_NULL = "ER_RESULT_NULL"; 127 public static final String JAXP_INVALID_SET_PARAM_VALUE = "JAXP_INVALID_SET_PARAM_VALUE"; 128 public static final String JAXP_SET_FEATURE_NULL_NAME = "JAXP_SET_FEATURE_NULL_NAME"; 129 public static final String JAXP_GET_FEATURE_NULL_NAME = "JAXP_GET_FEATURE_NULL_NAME"; 130 public static final String JAXP_UNSUPPORTED_FEATURE = "JAXP_UNSUPPORTED_FEATURE"; 131 public static final String JAXP_SECUREPROCESSING_FEATURE = "JAXP_SECUREPROCESSING_FEATURE"; 132 133 // Command-line error messages 134 public static final String COMPILE_STDIN_ERR = "COMPILE_STDIN_ERR"; 135 public static final String COMPILE_USAGE_STR = "COMPILE_USAGE_STR"; 136 public static final String TRANSFORM_USAGE_STR = "TRANSFORM_USAGE_STR"; 137 138 // Recently added error messages 139 public static final String STRAY_SORT_ERR = "STRAY_SORT_ERR"; 140 public static final String UNSUPPORTED_ENCODING = "UNSUPPORTED_ENCODING"; 141 public static final String SYNTAX_ERR = "SYNTAX_ERR"; 142 public static final String CONSTRUCTOR_NOT_FOUND = "CONSTRUCTOR_NOT_FOUND"; 143 public static final String NO_JAVA_FUNCT_THIS_REF = "NO_JAVA_FUNCT_THIS_REF"; 144 public static final String TYPE_CHECK_ERR = "TYPE_CHECK_ERR"; 145 public static final String TYPE_CHECK_UNK_LOC_ERR = "TYPE_CHECK_UNK_LOC_ERR"; 146 public static final String ILLEGAL_CMDLINE_OPTION_ERR = "ILLEGAL_CMDLINE_OPTION_ERR"; 147 public static final String CMDLINE_OPT_MISSING_ARG_ERR = "CMDLINE_OPT_MISSING_ARG_ERR"; 148 public static final String WARNING_PLUS_WRAPPED_MSG = "WARNING_PLUS_WRAPPED_MSG"; 149 public static final String WARNING_MSG = "WARNING_MSG"; 150 public static final String FATAL_ERR_PLUS_WRAPPED_MSG = "FATAL_ERR_PLUS_WRAPPED_MSG"; 151 public static final String FATAL_ERR_MSG = "FATAL_ERR_MSG"; 152 public static final String ERROR_PLUS_WRAPPED_MSG = "ERROR_PLUS_WRAPPED_MSG"; 153 public static final String ERROR_MSG = "ERROR_MSG"; 154 public static final String TRANSFORM_WITH_TRANSLET_STR = "TRANSFORM_WITH_TRANSLET_STR"; 155 public static final String TRANSFORM_WITH_JAR_STR = "TRANSFORM_WITH_JAR_STR"; 156 public static final String COULD_NOT_CREATE_TRANS_FACT = "COULD_NOT_CREATE_TRANS_FACT"; 157 public static final String TRANSLET_NAME_JAVA_CONFLICT = 158 "TRANSLET_NAME_JAVA_CONFLICT"; 159 public static final String INVALID_QNAME_ERR = "INVALID_QNAME_ERR"; 160 public static final String INVALID_NCNAME_ERR = "INVALID_NCNAME_ERR"; 161 public static final String INVALID_METHOD_IN_OUTPUT = "INVALID_METHOD_IN_OUTPUT"; 162 163 public static final String OUTLINE_ERR_TRY_CATCH = "OUTLINE_ERR_TRY_CATCH"; 164 public static final String OUTLINE_ERR_UNBALANCED_MARKERS = 165 "OUTLINE_ERR_UNBALANCED_MARKERS"; 166 public static final String OUTLINE_ERR_DELETED_TARGET = 167 "OUTLINE_ERR_DELETED_TARGET"; 168 public static final String OUTLINE_ERR_METHOD_TOO_BIG = 169 "OUTLINE_ERR_METHOD_TOO_BIG"; 170 171 public static final String DESERIALIZE_TRANSLET_ERR = "DESERIALIZE_TEMPLATES_ERR"; 172 173 // All error messages are localized and are stored in resource bundles. 174 // This array and the following 4 strings are read from that bundle. 175 private static ResourceBundle _bundle; 176 177 public final static String ERROR_MESSAGES_KEY = "ERROR_MESSAGES_KEY"; 178 public final static String COMPILER_ERROR_KEY = "COMPILER_ERROR_KEY"; 179 public final static String COMPILER_WARNING_KEY = "COMPILER_WARNING_KEY"; 180 public final static String RUNTIME_ERROR_KEY = "RUNTIME_ERROR_KEY"; 181 182 static { 183 _bundle = SecuritySupport.getResourceBundle( 184 "com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMessages", 185 Locale.getDefault()); 186 } 187 188 public ErrorMsg(String code) { 189 _code = code; 190 _line = 0; 191 } 192 193 public ErrorMsg(String code, Throwable e) { 194 _code = code; 195 _message = e.getMessage(); 196 _line = 0; 197 _cause = e; 198 } 199 200 public ErrorMsg(String message, int line) { 201 _code = null; 202 _message = message; 203 _line = line; 204 } 205 206 public ErrorMsg(String code, int line, Object param) { 207 _code = code; 208 _line = line; 209 _params = new Object[] { param }; 210 } 211 212 public ErrorMsg(String code, Object param) { 213 this(code); 214 _params = new Object[1]; 215 _params[0] = param; 216 } 217 218 public ErrorMsg(String code, Object param1, Object param2) { 219 this(code); 220 _params = new Object[2]; 221 _params[0] = param1; 222 _params[1] = param2; 223 } 224 225 public ErrorMsg(String code, SyntaxTreeNode node) { 226 _code = code; 227 _url = getFileName(node); 228 _line = node.getLineNumber(); 229 } 230 231 public ErrorMsg(String code, Object param1, SyntaxTreeNode node) { 232 _code = code; 233 _url = getFileName(node); 234 _line = node.getLineNumber(); 235 _params = new Object[1]; 236 _params[0] = param1; 237 } 238 239 public ErrorMsg(String code, Object param1, Object param2, 240 SyntaxTreeNode node) { 241 _code = code; 242 _url = getFileName(node); 243 _line = node.getLineNumber(); 244 _params = new Object[2]; 245 _params[0] = param1; 246 _params[1] = param2; 247 } 248 249 public Throwable getCause() { 250 return _cause; 251 } 252 253 private String getFileName(SyntaxTreeNode node) { 254 Stylesheet stylesheet = node.getStylesheet(); 255 if (stylesheet != null) 256 return stylesheet.getSystemId(); 257 else 258 return null; 259 } 260 261 private String formatLine() { 262 StringBuffer result = new StringBuffer(); 263 if (_url != null) { 264 result.append(_url); 265 result.append(": "); 266 } 267 if (_line > 0) { 268 result.append("line "); 269 result.append(Integer.toString(_line)); 270 result.append(": "); 271 } 272 return result.toString(); 273 } 274 275 /** 276 * This version of toString() uses the _params instance variable 277 * to format the message. If the <code>_code</code> is negative 278 * the use _message as the error string. 279 */ 280 public String toString() { 281 String suffix = (_params == null) ? 282 (null != _code ? getErrorMessage() : _message) 283 : MessageFormat.format(getErrorMessage(), _params); 284 return formatLine() + suffix; 285 } 286 287 public String toString(Object obj) { 288 Object params[] = new Object[1]; 289 params[0] = obj.toString(); 290 String suffix = MessageFormat.format(getErrorMessage(), params); 291 return formatLine() + suffix; 292 } 293 294 public String toString(Object obj0, Object obj1) { 295 Object params[] = new Object[2]; 296 params[0] = obj0.toString(); 297 params[1] = obj1.toString(); 298 String suffix = MessageFormat.format(getErrorMessage(), params); 299 return formatLine() + suffix; 300 } 301 302 /** 303 * Return an ErrorMessages string corresponding to the _code 304 * This function is temporary until the three special-cased keys 305 * below are moved into ErrorMessages 306 * 307 * @return ErrorMessages string 308 */ 309 private String getErrorMessage() { 310 return _bundle.getString(_code); 311 } 312 313 // If the _isWarningError flag is true, the error is treated as 314 // a warning by the compiler, but should be reported as an error 315 // to the ErrorListener. This is a workaround for the TCK failure 316 // ErrorListener.errorTests.error001. 317 public void setWarningError(boolean flag) { 318 _isWarningError = flag; 319 } 320 321 public boolean isWarningError() { 322 return _isWarningError; 323 } 324 }