1 /*
   2  * Copyright (c) 2013, 2017, 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.xsltc.compiler.Stylesheet;
  24 import com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode;
  25 import java.text.MessageFormat;
  26 import java.util.Locale;
  27 import java.util.ResourceBundle;
  28 import jdk.xml.internal.SecuritySupport;
  29 
  30 /**
  31  * @author Jacek Ambroziak
  32  * @author Santiago Pericas-Geertsen
  33  * @author G. Todd Miller
  34  * @author Erwin Bolwidt <ejb@klomp.org>
  35  * @author Morten Jorgensen
  36  * @LastModified: Sep 2017
  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 IMPORT_PRECEDE_OTHERS_ERR = "IMPORT_PRECEDE_OTHERS_ERR";
  74     public static final String RESULT_TREE_SORT_ERR = "RESULT_TREE_SORT_ERR";
  75     public static final String SYMBOLS_REDEF_ERR = "SYMBOLS_REDEF_ERR";
  76     public static final String XSL_VERSION_ERR = "XSL_VERSION_ERR";
  77     public static final String CIRCULAR_VARIABLE_ERR = "CIRCULAR_VARIABLE_ERR";
  78     public static final String ILLEGAL_BINARY_OP_ERR = "ILLEGAL_BINARY_OP_ERR";
  79     public static final String ILLEGAL_ARG_ERR = "ILLEGAL_ARG_ERR";
  80     public static final String DOCUMENT_ARG_ERR = "DOCUMENT_ARG_ERR";
  81     public static final String MISSING_WHEN_ERR = "MISSING_WHEN_ERR";
  82     public static final String MULTIPLE_OTHERWISE_ERR = "MULTIPLE_OTHERWISE_ERR";
  83     public static final String STRAY_OTHERWISE_ERR = "STRAY_OTHERWISE_ERR";
  84     public static final String STRAY_WHEN_ERR = "STRAY_WHEN_ERR";
  85     public static final String WHEN_ELEMENT_ERR = "WHEN_ELEMENT_ERR";
  86     public static final String UNNAMED_ATTRIBSET_ERR = "UNNAMED_ATTRIBSET_ERR";
  87     public static final String ILLEGAL_CHILD_ERR = "ILLEGAL_CHILD_ERR";
  88     public static final String ILLEGAL_ELEM_NAME_ERR = "ILLEGAL_ELEM_NAME_ERR";
  89     public static final String ILLEGAL_ATTR_NAME_ERR = "ILLEGAL_ATTR_NAME_ERR";
  90     public static final String ILLEGAL_TEXT_NODE_ERR = "ILLEGAL_TEXT_NODE_ERR";
  91     public static final String SAX_PARSER_CONFIG_ERR = "SAX_PARSER_CONFIG_ERR";
  92     public static final String INTERNAL_ERR = "INTERNAL_ERR";
  93     public static final String UNSUPPORTED_XSL_ERR = "UNSUPPORTED_XSL_ERR";
  94     public static final String UNSUPPORTED_EXT_ERR = "UNSUPPORTED_EXT_ERR";
  95     public static final String MISSING_XSLT_URI_ERR = "MISSING_XSLT_URI_ERR";
  96     public static final String MISSING_XSLT_TARGET_ERR = "MISSING_XSLT_TARGET_ERR";
  97     public static final String ACCESSING_XSLT_TARGET_ERR = "ACCESSING_XSLT_TARGET_ERR";
  98     public static final String NOT_IMPLEMENTED_ERR = "NOT_IMPLEMENTED_ERR";
  99     public static final String NOT_STYLESHEET_ERR = "NOT_STYLESHEET_ERR";
 100     public static final String ELEMENT_PARSE_ERR = "ELEMENT_PARSE_ERR";
 101     public static final String KEY_USE_ATTR_ERR = "KEY_USE_ATTR_ERR";
 102     public static final String OUTPUT_VERSION_ERR = "OUTPUT_VERSION_ERR";
 103     public static final String ILLEGAL_RELAT_OP_ERR = "ILLEGAL_RELAT_OP_ERR";
 104     public static final String ATTRIBSET_UNDEF_ERR = "ATTRIBSET_UNDEF_ERR";
 105     public static final String ATTR_VAL_TEMPLATE_ERR = "ATTR_VAL_TEMPLATE_ERR";
 106     public static final String UNKNOWN_SIG_TYPE_ERR = "UNKNOWN_SIG_TYPE_ERR";
 107     public static final String DATA_CONVERSION_ERR = "DATA_CONVERSION_ERR";
 108 
 109     // JAXP/TrAX error messages
 110     public static final String NO_TRANSLET_CLASS_ERR = "NO_TRANSLET_CLASS_ERR";
 111     public static final String NO_MAIN_TRANSLET_ERR = "NO_MAIN_TRANSLET_ERR";
 112     public static final String TRANSLET_CLASS_ERR = "TRANSLET_CLASS_ERR";
 113     public static final String TRANSLET_OBJECT_ERR = "TRANSLET_OBJECT_ERR";
 114     public static final String ERROR_LISTENER_NULL_ERR = "ERROR_LISTENER_NULL_ERR";
 115     public static final String JAXP_UNKNOWN_SOURCE_ERR = "JAXP_UNKNOWN_SOURCE_ERR";
 116     public static final String JAXP_NO_SOURCE_ERR = "JAXP_NO_SOURCE_ERR";
 117     public static final String JAXP_COMPILE_ERR = "JAXP_COMPILE_ERR";
 118     public static final String JAXP_INVALID_ATTR_ERR = "JAXP_INVALID_ATTR_ERR";
 119     public static final String JAXP_INVALID_ATTR_VALUE_ERR = "JAXP_INVALID_ATTR_VALUE_ERR";
 120     public static final String JAXP_SET_RESULT_ERR = "JAXP_SET_RESULT_ERR";
 121     public static final String JAXP_NO_TRANSLET_ERR = "JAXP_NO_TRANSLET_ERR";
 122     public static final String JAXP_NO_HANDLER_ERR = "JAXP_NO_HANDLER_ERR";
 123     public static final String JAXP_NO_RESULT_ERR = "JAXP_NO_RESULT_ERR";
 124     public static final String JAXP_UNKNOWN_PROP_ERR = "JAXP_UNKNOWN_PROP_ERR";
 125     public static final String SAX2DOM_ADAPTER_ERR = "SAX2DOM_ADAPTER_ERR";
 126     public static final String XSLTC_SOURCE_ERR = "XSLTC_SOURCE_ERR";
 127     public static final String ER_RESULT_NULL = "ER_RESULT_NULL";
 128     public static final String JAXP_INVALID_SET_PARAM_VALUE = "JAXP_INVALID_SET_PARAM_VALUE";
 129     public static final String JAXP_SET_FEATURE_NULL_NAME = "JAXP_SET_FEATURE_NULL_NAME";
 130     public static final String JAXP_GET_FEATURE_NULL_NAME = "JAXP_GET_FEATURE_NULL_NAME";
 131     public static final String JAXP_UNSUPPORTED_FEATURE = "JAXP_UNSUPPORTED_FEATURE";
 132     public static final String JAXP_SECUREPROCESSING_FEATURE = "JAXP_SECUREPROCESSING_FEATURE";
 133 
 134     // Command-line error messages
 135     public static final String COMPILE_STDIN_ERR = "COMPILE_STDIN_ERR";
 136     public static final String COMPILE_USAGE_STR = "COMPILE_USAGE_STR";
 137     public static final String TRANSFORM_USAGE_STR = "TRANSFORM_USAGE_STR";
 138 
 139     // Recently added error messages
 140     public static final String STRAY_SORT_ERR = "STRAY_SORT_ERR";
 141     public static final String UNSUPPORTED_ENCODING = "UNSUPPORTED_ENCODING";
 142     public static final String SYNTAX_ERR = "SYNTAX_ERR";
 143     public static final String CONSTRUCTOR_NOT_FOUND = "CONSTRUCTOR_NOT_FOUND";
 144     public static final String NO_JAVA_FUNCT_THIS_REF = "NO_JAVA_FUNCT_THIS_REF";
 145     public static final String TYPE_CHECK_ERR = "TYPE_CHECK_ERR";
 146     public static final String TYPE_CHECK_UNK_LOC_ERR = "TYPE_CHECK_UNK_LOC_ERR";
 147     public static final String ILLEGAL_CMDLINE_OPTION_ERR = "ILLEGAL_CMDLINE_OPTION_ERR";
 148     public static final String CMDLINE_OPT_MISSING_ARG_ERR = "CMDLINE_OPT_MISSING_ARG_ERR";
 149     public static final String WARNING_PLUS_WRAPPED_MSG = "WARNING_PLUS_WRAPPED_MSG";
 150     public static final String WARNING_MSG = "WARNING_MSG";
 151     public static final String FATAL_ERR_PLUS_WRAPPED_MSG = "FATAL_ERR_PLUS_WRAPPED_MSG";
 152     public static final String FATAL_ERR_MSG = "FATAL_ERR_MSG";
 153     public static final String ERROR_PLUS_WRAPPED_MSG = "ERROR_PLUS_WRAPPED_MSG";
 154     public static final String ERROR_MSG = "ERROR_MSG";
 155     public static final String TRANSFORM_WITH_TRANSLET_STR = "TRANSFORM_WITH_TRANSLET_STR";
 156     public static final String TRANSFORM_WITH_JAR_STR = "TRANSFORM_WITH_JAR_STR";
 157     public static final String COULD_NOT_CREATE_TRANS_FACT = "COULD_NOT_CREATE_TRANS_FACT";
 158     public static final String TRANSLET_NAME_JAVA_CONFLICT =
 159                                                  "TRANSLET_NAME_JAVA_CONFLICT";
 160     public static final String INVALID_QNAME_ERR = "INVALID_QNAME_ERR";
 161     public static final String INVALID_NCNAME_ERR = "INVALID_NCNAME_ERR";
 162     public static final String INVALID_METHOD_IN_OUTPUT = "INVALID_METHOD_IN_OUTPUT";
 163 
 164     public static final String OUTLINE_ERR_TRY_CATCH = "OUTLINE_ERR_TRY_CATCH";
 165     public static final String OUTLINE_ERR_UNBALANCED_MARKERS =
 166                                             "OUTLINE_ERR_UNBALANCED_MARKERS";
 167     public static final String OUTLINE_ERR_DELETED_TARGET =
 168                                             "OUTLINE_ERR_DELETED_TARGET";
 169     public static final String OUTLINE_ERR_METHOD_TOO_BIG =
 170                                             "OUTLINE_ERR_METHOD_TOO_BIG";
 171 
 172     public static final String DESERIALIZE_TRANSLET_ERR = "DESERIALIZE_TEMPLATES_ERR";
 173 
 174     // All error messages are localized and are stored in resource bundles.
 175     // This array and the following 4 strings are read from that bundle.
 176     private static ResourceBundle _bundle;
 177 
 178     public final static String ERROR_MESSAGES_KEY   = "ERROR_MESSAGES_KEY";
 179     public final static String COMPILER_ERROR_KEY   = "COMPILER_ERROR_KEY";
 180     public final static String COMPILER_WARNING_KEY = "COMPILER_WARNING_KEY";
 181     public final static String RUNTIME_ERROR_KEY    = "RUNTIME_ERROR_KEY";
 182 
 183     static {
 184         _bundle = SecuritySupport.getResourceBundle(
 185                           "com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMessages",
 186                           Locale.getDefault());
 187     }
 188 
 189     public ErrorMsg(String code) {
 190         _code = code;
 191         _line = 0;
 192     }
 193 
 194     public ErrorMsg(String code, Throwable e) {
 195         _code = code;
 196         _message = e.getMessage();
 197         _line = 0;
 198         _cause = e;
 199     }
 200 
 201     public ErrorMsg(String message, int line) {
 202         _code = null;
 203         _message = message;
 204         _line = line;
 205     }
 206 
 207     public ErrorMsg(String code, int line, Object param) {
 208         _code = code;
 209         _line = line;
 210         _params = new Object[] { param };
 211     }
 212 
 213     public ErrorMsg(String code, Object param) {
 214         this(code);
 215         _params = new Object[1];
 216         _params[0] = param;
 217     }
 218 
 219     public ErrorMsg(String code, Object param1, Object param2) {
 220         this(code);
 221         _params = new Object[2];
 222         _params[0] = param1;
 223         _params[1] = param2;
 224     }
 225 
 226     public ErrorMsg(String code, SyntaxTreeNode node) {
 227         _code = code;
 228         _url  = getFileName(node);
 229         _line = node.getLineNumber();
 230     }
 231 
 232     public ErrorMsg(String code, Object param1, SyntaxTreeNode node) {
 233         _code = code;
 234         _url  = getFileName(node);
 235         _line = node.getLineNumber();
 236         _params = new Object[1];
 237         _params[0] = param1;
 238     }
 239 
 240     public ErrorMsg(String code, Object param1, Object param2,
 241                     SyntaxTreeNode node) {
 242         _code = code;
 243         _url  = getFileName(node);
 244         _line = node.getLineNumber();
 245         _params = new Object[2];
 246         _params[0] = param1;
 247         _params[1] = param2;
 248     }
 249 
 250     public Throwable getCause() {
 251         return _cause;
 252     }
 253 
 254     private String getFileName(SyntaxTreeNode node) {
 255         Stylesheet stylesheet = node.getStylesheet();
 256         if (stylesheet != null)
 257             return stylesheet.getSystemId();
 258         else
 259             return null;
 260     }
 261 
 262     private String formatLine() {
 263         StringBuffer result = new StringBuffer();
 264         if (_url != null) {
 265             result.append(_url);
 266             result.append(": ");
 267         }
 268         if (_line > 0) {
 269             result.append("line ");
 270             result.append(Integer.toString(_line));
 271             result.append(": ");
 272         }
 273         return result.toString();
 274     }
 275 
 276     /**
 277      * This version of toString() uses the _params instance variable
 278      * to format the message. If the <code>_code</code> is negative
 279      * the use _message as the error string.
 280      */
 281     public String toString() {
 282         String suffix = (_params == null) ?
 283             (null != _code ? getErrorMessage() : _message)
 284             : MessageFormat.format(getErrorMessage(), _params);
 285         return formatLine() + suffix;
 286     }
 287 
 288     public String toString(Object obj) {
 289         Object params[] = new Object[1];
 290         params[0] = obj.toString();
 291         String suffix = MessageFormat.format(getErrorMessage(), params);
 292         return formatLine() + suffix;
 293     }
 294 
 295     public String toString(Object obj0, Object obj1) {
 296         Object params[] = new Object[2];
 297         params[0] = obj0.toString();
 298         params[1] = obj1.toString();
 299         String suffix = MessageFormat.format(getErrorMessage(), params);
 300         return formatLine() + suffix;
 301     }
 302 
 303     /**
 304      * Return an ErrorMessages string corresponding to the _code
 305      * This function is temporary until the three special-cased keys
 306      * below are moved into ErrorMessages
 307      *
 308      * @return ErrorMessages string
 309      */
 310     private String getErrorMessage() {
 311       return _bundle.getString(_code);
 312     }
 313 
 314     // If the _isWarningError flag is true, the error is treated as
 315     // a warning by the compiler, but should be reported as an error
 316     // to the ErrorListener. This is a workaround for the TCK failure
 317     // ErrorListener.errorTests.error001.
 318     public void setWarningError(boolean flag) {
 319         _isWarningError = flag;
 320 }
 321 
 322     public boolean isWarningError() {
 323         return _isWarningError;
 324     }
 325 }