001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ErrorMsg.java 468649 2006-10-28 07:00:55Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.compiler.util;
023    
024    import java.text.MessageFormat;
025    import java.util.Locale;
026    import java.util.ResourceBundle;
027    
028    import org.apache.xalan.xsltc.compiler.Stylesheet;
029    import org.apache.xalan.xsltc.compiler.SyntaxTreeNode;
030    
031    /**
032     * @author Jacek Ambroziak
033     * @author Santiago Pericas-Geertsen
034     * @author G. Todd Miller
035     * @author Erwin Bolwidt <ejb@klomp.org>
036     * @author Morten Jorgensen
037     */
038    public final class ErrorMsg {
039    
040        private String _code;
041        private int _line;
042        private String _message = null;
043        private String _url = null;
044        Object[] _params = null;
045        private boolean _isWarningError;
046    
047        // Compiler error messages
048        public static final String MULTIPLE_STYLESHEET_ERR = "MULTIPLE_STYLESHEET_ERR";
049        public static final String TEMPLATE_REDEF_ERR = "TEMPLATE_REDEF_ERR";
050        public static final String TEMPLATE_UNDEF_ERR = "TEMPLATE_UNDEF_ERR";
051        public static final String VARIABLE_REDEF_ERR = "VARIABLE_REDEF_ERR";
052        public static final String VARIABLE_UNDEF_ERR = "VARIABLE_UNDEF_ERR";
053        public static final String CLASS_NOT_FOUND_ERR = "CLASS_NOT_FOUND_ERR";
054        public static final String METHOD_NOT_FOUND_ERR = "METHOD_NOT_FOUND_ERR";
055        public static final String ARGUMENT_CONVERSION_ERR = "ARGUMENT_CONVERSION_ERR";
056        public static final String FILE_NOT_FOUND_ERR = "FILE_NOT_FOUND_ERR";
057        public static final String INVALID_URI_ERR = "INVALID_URI_ERR";
058        public static final String FILE_ACCESS_ERR = "FILE_ACCESS_ERR";
059        public static final String MISSING_ROOT_ERR = "MISSING_ROOT_ERR";
060        public static final String NAMESPACE_UNDEF_ERR = "NAMESPACE_UNDEF_ERR";
061        public static final String FUNCTION_RESOLVE_ERR = "FUNCTION_RESOLVE_ERR";
062        public static final String NEED_LITERAL_ERR = "NEED_LITERAL_ERR";
063        public static final String XPATH_PARSER_ERR = "XPATH_PARSER_ERR";
064        public static final String REQUIRED_ATTR_ERR = "REQUIRED_ATTR_ERR";
065        public static final String ILLEGAL_CHAR_ERR = "ILLEGAL_CHAR_ERR";
066        public static final String ILLEGAL_PI_ERR = "ILLEGAL_PI_ERR";
067        public static final String STRAY_ATTRIBUTE_ERR = "STRAY_ATTRIBUTE_ERR";
068        public static final String ILLEGAL_ATTRIBUTE_ERR = "ILLEGAL_ATTRIBUTE_ERR";
069        public static final String CIRCULAR_INCLUDE_ERR = "CIRCULAR_INCLUDE_ERR";
070        public static final String RESULT_TREE_SORT_ERR = "RESULT_TREE_SORT_ERR";
071        public static final String SYMBOLS_REDEF_ERR = "SYMBOLS_REDEF_ERR";
072        public static final String XSL_VERSION_ERR = "XSL_VERSION_ERR";
073        public static final String CIRCULAR_VARIABLE_ERR = "CIRCULAR_VARIABLE_ERR";
074        public static final String ILLEGAL_BINARY_OP_ERR = "ILLEGAL_BINARY_OP_ERR";
075        public static final String ILLEGAL_ARG_ERR = "ILLEGAL_ARG_ERR";
076        public static final String DOCUMENT_ARG_ERR = "DOCUMENT_ARG_ERR";
077        public static final String MISSING_WHEN_ERR = "MISSING_WHEN_ERR";
078        public static final String MULTIPLE_OTHERWISE_ERR = "MULTIPLE_OTHERWISE_ERR";
079        public static final String STRAY_OTHERWISE_ERR = "STRAY_OTHERWISE_ERR";
080        public static final String STRAY_WHEN_ERR = "STRAY_WHEN_ERR";
081        public static final String WHEN_ELEMENT_ERR = "WHEN_ELEMENT_ERR";
082        public static final String UNNAMED_ATTRIBSET_ERR = "UNNAMED_ATTRIBSET_ERR";
083        public static final String ILLEGAL_CHILD_ERR = "ILLEGAL_CHILD_ERR";
084        public static final String ILLEGAL_ELEM_NAME_ERR = "ILLEGAL_ELEM_NAME_ERR";
085        public static final String ILLEGAL_ATTR_NAME_ERR = "ILLEGAL_ATTR_NAME_ERR";
086        public static final String ILLEGAL_TEXT_NODE_ERR = "ILLEGAL_TEXT_NODE_ERR";
087        public static final String SAX_PARSER_CONFIG_ERR = "SAX_PARSER_CONFIG_ERR";
088        public static final String INTERNAL_ERR = "INTERNAL_ERR";
089        public static final String UNSUPPORTED_XSL_ERR = "UNSUPPORTED_XSL_ERR";
090        public static final String UNSUPPORTED_EXT_ERR = "UNSUPPORTED_EXT_ERR";
091        public static final String MISSING_XSLT_URI_ERR = "MISSING_XSLT_URI_ERR";
092        public static final String MISSING_XSLT_TARGET_ERR = "MISSING_XSLT_TARGET_ERR";
093        public static final String NOT_IMPLEMENTED_ERR = "NOT_IMPLEMENTED_ERR";
094        public static final String NOT_STYLESHEET_ERR = "NOT_STYLESHEET_ERR";
095        public static final String ELEMENT_PARSE_ERR = "ELEMENT_PARSE_ERR";
096        public static final String KEY_USE_ATTR_ERR = "KEY_USE_ATTR_ERR";
097        public static final String OUTPUT_VERSION_ERR = "OUTPUT_VERSION_ERR";
098        public static final String ILLEGAL_RELAT_OP_ERR = "ILLEGAL_RELAT_OP_ERR";
099        public static final String ATTRIBSET_UNDEF_ERR = "ATTRIBSET_UNDEF_ERR";
100        public static final String ATTR_VAL_TEMPLATE_ERR = "ATTR_VAL_TEMPLATE_ERR";
101        public static final String UNKNOWN_SIG_TYPE_ERR = "UNKNOWN_SIG_TYPE_ERR";
102        public static final String DATA_CONVERSION_ERR = "DATA_CONVERSION_ERR";
103    
104        // JAXP/TrAX error messages
105        public static final String NO_TRANSLET_CLASS_ERR = "NO_TRANSLET_CLASS_ERR";
106        public static final String NO_MAIN_TRANSLET_ERR = "NO_MAIN_TRANSLET_ERR";
107        public static final String TRANSLET_CLASS_ERR = "TRANSLET_CLASS_ERR";
108        public static final String TRANSLET_OBJECT_ERR = "TRANSLET_OBJECT_ERR";
109        public static final String ERROR_LISTENER_NULL_ERR = "ERROR_LISTENER_NULL_ERR";
110        public static final String JAXP_UNKNOWN_SOURCE_ERR = "JAXP_UNKNOWN_SOURCE_ERR";
111        public static final String JAXP_NO_SOURCE_ERR = "JAXP_NO_SOURCE_ERR";
112        public static final String JAXP_COMPILE_ERR = "JAXP_COMPILE_ERR";
113        public static final String JAXP_INVALID_ATTR_ERR = "JAXP_INVALID_ATTR_ERR";
114        public static final String JAXP_SET_RESULT_ERR = "JAXP_SET_RESULT_ERR";
115        public static final String JAXP_NO_TRANSLET_ERR = "JAXP_NO_TRANSLET_ERR";
116        public static final String JAXP_NO_HANDLER_ERR = "JAXP_NO_HANDLER_ERR";
117        public static final String JAXP_NO_RESULT_ERR = "JAXP_NO_RESULT_ERR";
118        public static final String JAXP_UNKNOWN_PROP_ERR = "JAXP_UNKNOWN_PROP_ERR";
119        public static final String SAX2DOM_ADAPTER_ERR = "SAX2DOM_ADAPTER_ERR";
120        public static final String XSLTC_SOURCE_ERR = "XSLTC_SOURCE_ERR";
121        public static final String ER_RESULT_NULL = "ER_RESULT_NULL";
122        public static final String JAXP_INVALID_SET_PARAM_VALUE = "JAXP_INVALID_SET_PARAM_VALUE";        
123        public static final String JAXP_SET_FEATURE_NULL_NAME = "JAXP_SET_FEATURE_NULL_NAME";
124        public static final String JAXP_GET_FEATURE_NULL_NAME = "JAXP_GET_FEATURE_NULL_NAME";
125        public static final String JAXP_UNSUPPORTED_FEATURE = "JAXP_UNSUPPORTED_FEATURE";
126    
127        // Command-line error messages
128        public static final String COMPILE_STDIN_ERR = "COMPILE_STDIN_ERR";
129        public static final String COMPILE_USAGE_STR = "COMPILE_USAGE_STR";
130        public static final String TRANSFORM_USAGE_STR = "TRANSFORM_USAGE_STR";
131    
132        // Recently added error messages
133        public static final String STRAY_SORT_ERR = "STRAY_SORT_ERR";
134        public static final String UNSUPPORTED_ENCODING = "UNSUPPORTED_ENCODING";
135        public static final String SYNTAX_ERR = "SYNTAX_ERR";
136        public static final String CONSTRUCTOR_NOT_FOUND = "CONSTRUCTOR_NOT_FOUND";
137        public static final String NO_JAVA_FUNCT_THIS_REF = "NO_JAVA_FUNCT_THIS_REF";
138        public static final String TYPE_CHECK_ERR = "TYPE_CHECK_ERR";
139        public static final String TYPE_CHECK_UNK_LOC_ERR = "TYPE_CHECK_UNK_LOC_ERR";
140        public static final String ILLEGAL_CMDLINE_OPTION_ERR = "ILLEGAL_CMDLINE_OPTION_ERR";
141        public static final String CMDLINE_OPT_MISSING_ARG_ERR = "CMDLINE_OPT_MISSING_ARG_ERR";
142        public static final String WARNING_PLUS_WRAPPED_MSG = "WARNING_PLUS_WRAPPED_MSG";
143        public static final String WARNING_MSG = "WARNING_MSG";
144        public static final String FATAL_ERR_PLUS_WRAPPED_MSG = "FATAL_ERR_PLUS_WRAPPED_MSG";
145        public static final String FATAL_ERR_MSG = "FATAL_ERR_MSG";
146        public static final String ERROR_PLUS_WRAPPED_MSG = "ERROR_PLUS_WRAPPED_MSG";
147        public static final String ERROR_MSG = "ERROR_MSG";
148        public static final String TRANSFORM_WITH_TRANSLET_STR = "TRANSFORM_WITH_TRANSLET_STR";
149        public static final String TRANSFORM_WITH_JAR_STR = "TRANSFORM_WITH_JAR_STR";
150        public static final String COULD_NOT_CREATE_TRANS_FACT = "COULD_NOT_CREATE_TRANS_FACT";
151        public static final String TRANSLET_NAME_JAVA_CONFLICT =
152                                                     "TRANSLET_NAME_JAVA_CONFLICT";
153        public static final String INVALID_QNAME_ERR = "INVALID_QNAME_ERR";
154        public static final String INVALID_NCNAME_ERR = "INVALID_NCNAME_ERR";
155        public static final String INVALID_METHOD_IN_OUTPUT = "INVALID_METHOD_IN_OUTPUT";
156    
157        public static final String OUTLINE_ERR_TRY_CATCH = "OUTLINE_ERR_TRY_CATCH";
158        public static final String OUTLINE_ERR_UNBALANCED_MARKERS =
159                                                "OUTLINE_ERR_UNBALANCED_MARKERS";
160        public static final String OUTLINE_ERR_DELETED_TARGET =
161                                                "OUTLINE_ERR_DELETED_TARGET";
162        public static final String OUTLINE_ERR_METHOD_TOO_BIG =
163                                                "OUTLINE_ERR_METHOD_TOO_BIG";
164                                                         
165        // All error messages are localized and are stored in resource bundles.
166        // This array and the following 4 strings are read from that bundle.
167        private static ResourceBundle _bundle;
168        
169        public final static String ERROR_MESSAGES_KEY   = "ERROR_MESSAGES_KEY";
170        public final static String COMPILER_ERROR_KEY   = "COMPILER_ERROR_KEY";
171        public final static String COMPILER_WARNING_KEY = "COMPILER_WARNING_KEY";
172        public final static String RUNTIME_ERROR_KEY    = "RUNTIME_ERROR_KEY";
173    
174        static {
175            _bundle = ResourceBundle.getBundle(
176                              "org.apache.xalan.xsltc.compiler.util.ErrorMessages",
177                              Locale.getDefault());
178        }
179    
180        public ErrorMsg(String code) {
181            _code = code;
182            _line = 0;
183        }
184            
185        public ErrorMsg(Throwable e) {
186            _code = null;
187            _message = e.getMessage();
188            _line = 0;
189        }
190    
191        public ErrorMsg(String message, int line) {
192            _code = null;
193            _message = message;
194            _line = line;
195        }
196    
197        public ErrorMsg(String code, int line, Object param) {
198            _code = code;
199            _line = line;
200            _params = new Object[] { param };
201        }
202    
203        public ErrorMsg(String code, Object param) {
204            this(code);
205            _params = new Object[1];
206            _params[0] = param;
207        }
208    
209        public ErrorMsg(String code, Object param1, Object param2) {
210            this(code);
211            _params = new Object[2];
212            _params[0] = param1;
213            _params[1] = param2;
214        }
215    
216        public ErrorMsg(String code, SyntaxTreeNode node) {
217            _code = code;
218            _url  = getFileName(node);
219            _line = node.getLineNumber();
220        }
221    
222        public ErrorMsg(String code, Object param1, SyntaxTreeNode node) {
223            _code = code;
224            _url  = getFileName(node);
225            _line = node.getLineNumber();
226            _params = new Object[1];
227            _params[0] = param1;
228        }
229    
230        public ErrorMsg(String code, Object param1, Object param2,
231                        SyntaxTreeNode node) {
232            _code = code;
233            _url  = getFileName(node);
234            _line = node.getLineNumber();
235            _params = new Object[2];
236            _params[0] = param1;
237            _params[1] = param2;
238        }
239    
240        private String getFileName(SyntaxTreeNode node) {
241            Stylesheet stylesheet = node.getStylesheet();
242            if (stylesheet != null)
243                return stylesheet.getSystemId();
244            else
245                return null;
246        }
247    
248        private String formatLine() {
249            StringBuffer result = new StringBuffer();
250            if (_url != null) {
251                result.append(_url);
252                result.append(": ");
253            }
254            if (_line > 0) {
255                result.append("line ");
256                result.append(Integer.toString(_line));
257                result.append(": ");
258            }
259            return result.toString();
260        }
261            
262        /**
263         * This version of toString() uses the _params instance variable
264         * to format the message. If the <code>_code</code> is negative
265         * the use _message as the error string.
266         */
267        public String toString() {
268            String suffix = (_params == null) ? 
269                (null != _code ? getErrorMessage() : _message)
270                : MessageFormat.format(getErrorMessage(), _params);
271            return formatLine() + suffix;
272        }
273            
274        public String toString(Object obj) {
275            Object params[] = new Object[1];
276            params[0] = obj.toString();
277            String suffix = MessageFormat.format(getErrorMessage(), params);
278            return formatLine() + suffix;
279        }
280            
281        public String toString(Object obj0, Object obj1) {
282            Object params[] = new Object[2];
283            params[0] = obj0.toString();
284            params[1] = obj1.toString();
285            String suffix = MessageFormat.format(getErrorMessage(), params);
286            return formatLine() + suffix;
287        }
288    
289        /**
290         * Return an ErrorMessages string corresponding to the _code
291         * This function is temporary until the three special-cased keys
292         * below are moved into ErrorMessages
293         * 
294         * @return ErrorMessages string
295         */    
296        private String getErrorMessage() {
297          return _bundle.getString(_code);
298        }
299        
300        // If the _isWarningError flag is true, the error is treated as
301        // a warning by the compiler, but should be reported as an error
302        // to the ErrorListener. This is a workaround for the TCK failure 
303        // ErrorListener.errorTests.error001.
304        public void setWarningError(boolean flag) {
305            _isWarningError = flag;
306    }
307    
308        public boolean isWarningError() {
309            return _isWarningError;
310        }
311    }
312