KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > Trace


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb;
68
69 import java.io.PrintWriter JavaDoc;
70
71 import org.hsqldb.lib.HsqlByteArrayOutputStream;
72 import org.hsqldb.resources.BundleHandler;
73
74 /**
75  * handles creation and reporting of error messages and throwing HsqlException
76  *
77  * Rewritten and extended in successive versions of HSQLDB.
78  *
79  * @author Thomas Mueller (Hypersonic SQL Group)
80  * @version 1.8.0
81  * @since Hypersonic SQL
82  */

83
84 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - error reporting
85
// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - setting trace
86
// the system property hsqldb.tracesystemout == true is now used for printing
87
// trace message to System.out
88
// fredt@users 20020305 - patch 1.7.0 - various new messages added
89
// tony_lai@users 20020820 - patch 595073 - Duplicated exception msg
90
// fredt@users 20021230 - patch 488118 by xclay@users - allow multithreading
91
// wsonic@users 20031005 - moved string literal messages to Trace with new methods
92
// nitin chauhan 20031005 - moved concatenated string in asserts and checks to Trace with new methods
93
// fredt@users 20040322 - removed unused code - class is a collection of static methods now
94
// fredt@users 20050524 - use resource bundle for messages
95

96 /** @todo fredt - 20021022 management of nested throws inside the program in
97  * such a way that it is possible to return exactly the text of the error
98  * thrown at a given level without higher level messages being added and to
99  * preserve the original error code
100  */

101 public class Trace {
102
103     public static boolean TRACE = false;
104     public static boolean TRACESYSTEMOUT = false;
105     public static final boolean STOP = false;
106     public static final boolean DOASSERT = false;
107
108     //
109
static String JavaDoc errPropsName = "sql-error-messages";
110     static int bundleHandle = BundleHandler.getBundleHandle(errPropsName,
111         null);
112
113     //
114
public static final int
115
116     //
117
DATABASE_ALREADY_IN_USE = 1,
118     CONNECTION_IS_CLOSED = 2,
119     CONNECTION_IS_BROKEN = 3,
120     DATABASE_IS_SHUTDOWN = 4,
121     COLUMN_COUNT_DOES_NOT_MATCH = 5,
122     DIVISION_BY_ZERO = 6,
123     INVALID_ESCAPE = 7,
124     INTEGRITY_CONSTRAINT_VIOLATION = 8,
125     VIOLATION_OF_UNIQUE_INDEX = 9,
126     TRY_TO_INSERT_NULL = 10,
127     UNEXPECTED_TOKEN = 11,
128     UNEXPECTED_END_OF_COMMAND = 12,
129     UNKNOWN_FUNCTION = 13,
130     NEED_AGGREGATE = 14,
131     SUM_OF_NON_NUMERIC = 15,
132     WRONG_DATA_TYPE = 16,
133     CARDINALITY_VIOLATION_NO_SUBCLASS = 17,
134     SERIALIZATION_FAILURE = 18,
135     TRANSFER_CORRUPTED = 19,
136     FUNCTION_NOT_SUPPORTED = 20,
137     TABLE_ALREADY_EXISTS = 21,
138     TABLE_NOT_FOUND = 22,
139     INDEX_ALREADY_EXISTS = 23,
140     SECOND_PRIMARY_KEY = 24,
141     DROP_PRIMARY_KEY = 25,
142     INDEX_NOT_FOUND = 26,
143     COLUMN_ALREADY_EXISTS = 27,
144     COLUMN_NOT_FOUND = 28,
145     FILE_IO_ERROR = 29,
146     WRONG_DATABASE_FILE_VERSION = 30,
147     DATABASE_IS_READONLY = 31,
148     DATA_IS_READONLY = 32,
149     ACCESS_IS_DENIED = 33,
150     INPUTSTREAM_ERROR = 34,
151     NO_DATA_IS_AVAILABLE = 35,
152     USER_ALREADY_EXISTS = 36,
153     USER_NOT_FOUND = 37,
154     ASSERT_FAILED = 38,
155     EXTERNAL_STOP = 39,
156     GENERAL_ERROR = 40,
157     WRONG_OUT_PARAMETER = 41,
158     FUNCTION_NOT_FOUND = 42,
159     TRIGGER_NOT_FOUND = 43,
160     SAVEPOINT_NOT_FOUND = 44,
161     LABEL_REQUIRED = 45,
162     WRONG_DEFAULT_CLAUSE = 46,
163     FOREIGN_KEY_NOT_ALLOWED = 47,
164     UNKNOWN_DATA_SOURCE = 48,
165     BAD_INDEX_CONSTRAINT_NAME = 49,
166     DROP_FK_INDEX = 50,
167     RESULTSET_FORWARD_ONLY = 51,
168     VIEW_ALREADY_EXISTS = 52,
169     VIEW_NOT_FOUND = 53,
170     NOT_USED_54 = 54,
171     NOT_A_TABLE = 55,
172     SYSTEM_INDEX = 56,
173     COLUMN_TYPE_MISMATCH = 57,
174     BAD_ADD_COLUMN_DEFINITION = 58,
175     DROP_SYSTEM_CONSTRAINT = 59,
176     CONSTRAINT_ALREADY_EXISTS = 60,
177     CONSTRAINT_NOT_FOUND = 61,
178     INVALID_JDBC_ARGUMENT = 62,
179     DATABASE_IS_MEMORY_ONLY = 63,
180     OUTER_JOIN_CONDITION = 64,
181     NUMERIC_VALUE_OUT_OF_RANGE = 65,
182     MISSING_SOFTWARE_MODULE = 66,
183     NOT_IN_AGGREGATE_OR_GROUP_BY = 67,
184     INVALID_GROUP_BY = 68,
185     INVALID_HAVING = 69,
186     INVALID_ORDER_BY = 70,
187     INVALID_ORDER_BY_IN_DISTINCT_SELECT = 71,
188     OUT_OF_MEMORY = 72,
189     OPERATION_NOT_SUPPORTED = 73,
190     INVALID_IDENTIFIER = 74,
191     TEXT_TABLE_SOURCE = 75,
192     TEXT_FILE = 76,
193     NOT_USED_77 = 77,
194     ERROR_IN_SCRIPT_FILE = 78,
195     NULL_IN_VALUE_LIST = 79,
196     SOCKET_ERROR = 80,
197     INVALID_CHARACTER_ENCODING = 81,
198     NOT_USED_82 = 82,
199     NOT_USED_83 = 83,
200     NOT_USED_84 = 84,
201     UNEXPECTED_EXCEPTION = 85,
202     NOT_USED_86 = 86,
203     NOT_USED_87 = 87,
204     NOT_USED_88 = 88,
205     NOT_USED_89 = 89,
206     NOT_USED_90 = 90,
207     NOT_USED_91 = 91,
208     NOT_USED_92 = 92,
209     NOT_USED_93 = 93,
210     DATABASE_NOT_EXISTS = 94,
211     INVALID_CONVERSION = 95,
212     ERROR_IN_BINARY_SCRIPT_1 = 96,
213     ERROR_IN_BINARY_SCRIPT_2 = 97,
214     GENERAL_IO_ERROR = 98,
215     EXPRESSION_NOT_SUPPORTED = 99,
216     Constraint_violation = 100,
217     Database_dropTable = 101,
218     ERROR_IN_CONSTRAINT_COLUMN_LIST = 102,
219     TABLE_HAS_NO_PRIMARY_KEY = 103,
220     VIOLATION_OF_UNIQUE_CONSTRAINT = 104,
221     NO_DEFAULT_VALUE_FOR_COLUMN = 105,
222     NOT_A_CONDITION = 106,
223     DatabaseManager_getDatabase = 107,
224     NOT_USED_108 = 108,
225     NOT_USED_109 = 109,
226     NOT_USED_110 = 110,
227     NOT_USED_111 = 111,
228     NOT_USED_112 = 112,
229     DatabaseScriptReader_readDDL = 113,
230     DatabaseScriptReader_readExistingData = 114,
231     Message_Pair = 115,
232     LOAD_SAVE_PROPERTIES = 116,
233     INVALID_TRANSACTION_STATE_NO_SUBCLASS = 117,
234     JDBC_INVALID_BRI_SCOPE = 118,
235     JDBC_NO_RESULT_SET_METADATA = 119,
236     JDBC_NO_RESULT_SET = 120,
237     MISSING_CLOSEBRACKET = 121,
238     ITSNS_OVERWRITE = 122,
239     COLUMN_IS_IN_INDEX = 123,
240     STRING_DATA_TRUNCATION = 124,
241     QUOTED_IDENTIFIER_REQUIRED = 125,
242     STATEMENT_IS_CLOSED = 126,
243     NOT_USED_127 = 127,
244     NOT_USED_128 = 128,
245     DATA_FILE_ERROR = 129,
246     NOT_USED_130 = 130,
247     HsqlDateTime_null_string = 131,
248     NOT_USED_132 = 132,
249     HsqlDateTime_null_date = 133,
250     NOT_USED_134 = 134,
251     HsqlProperties_load = 135,
252     HsqlSocketFactorySecure_verify = 136,
253     HsqlSocketFactorySecure_verify2 = 137,
254     jdbcConnection_nativeSQL = 138,
255     HsqlSocketFactorySecure_verify3 = 139,
256     NOT_USED_140 = 140,
257     NOT_USED_141 = 141,
258     jdbcStatement_executeUpdate = 142,
259     LockFile_checkHeartbeat = 143,
260     LockFile_checkHeartbeat2 = 144,
261     TEXT_STRING_HAS_NEWLINE = 145,
262     Result_Result = 146,
263     SERVER_NO_DATABASE = 147,
264     Server_openServerSocket = 148,
265     Server_openServerSocket2 = 149,
266     TEXT_TABLE_HEADER = 150,
267     NOT_USED_151 = 151,
268     JDBC_PARAMETER_NOT_SET = 152,
269     INVALID_LIMIT = 153,
270     JDBC_STATEMENT_NOT_ROW_COUNT = 154,
271     JDBC_STATEMENT_NOT_RESULTSET = 155,
272     AMBIGUOUS_COLUMN_REFERENCE = 156,
273     CHECK_CONSTRAINT_VIOLATION = 157,
274     JDBC_RESULTSET_IS_CLOSED = 158,
275     SINGLE_COLUMN_EXPECTED = 159,
276     TOKEN_REQUIRED = 160,
277     NOT_USED_161 = 161,
278     NOT_USED_162 = 162,
279     ORDER_LIMIT_REQUIRED = 163,
280     TRIGGER_ALREADY_EXISTS = 164,
281     ASSERT_DIRECT_EXEC_WITH_PARAM = 165,
282     NOT_USED_166 = 166,
283     Expression_compareValues = 167,
284     INVALID_LIMIT_EXPRESSION = 168,
285     INVALID_TOP_EXPRESSION = 169,
286     SQL_CONSTRAINT_REQUIRED = 170,
287     TableWorks_dropConstraint = 171,
288     TEXT_TABLE_SOURCE_FILENAME = 172,
289     TEXT_TABLE_SOURCE_VALUE_MISSING = 173,
290     TEXT_TABLE_SOURCE_SEPARATOR = 174,
291     UNSUPPORTED_PARAM_CLASS = 175,
292     JDBC_NULL_STREAM = 176,
293     INTEGRITY_CONSTRAINT_VIOLATION_NOPARENT = 177,
294     NOT_USED_178 = 178,
295     NOT_USED_179 = 179,
296     QuotedTextDatabaseRowInput_getField = 180,
297     QuotedTextDatabaseRowInput_getField2 = 181,
298     TextDatabaseRowInput_getField = 182,
299     TextDatabaseRowInput_getField2 = 183,
300     TextDatabaseRowInput_getField3 = 184,
301     Parser_ambiguous_between1 = 185,
302     SEQUENCE_REFERENCED_BY_VIEW = 186,
303     NOT_USED_187 = 187,
304     TextCache_openning_file_error = 188,
305     TextCache_closing_file_error = 189,
306     TextCache_purging_file_error = 190,
307     SEQUENCE_NOT_FOUND = 191,
308     SEQUENCE_ALREADY_EXISTS = 192,
309     TABLE_REFERENCED_CONSTRAINT = 193,
310     TABLE_REFERENCED_VIEW = 194,
311     NOT_USED_195 = 195,
312     TEXT_SOURCE_EXISTS = 196,
313     COLUMN_IS_REFERENCED = 197,
314     FUNCTION_CALL_ERROR = 198,
315     TRIGGERED_DATA_CHANGE = 199,
316     INVALID_FUNCTION_ARGUMENT = 200,
317     UNSUPPORTED_INTERNAL_OPERATION = 201,
318     NOT_USED_202 = 202,
319     INVALID_PREPARED_STATEMENT = 203,
320     CREATE_TRIGGER_COMMAND_1 = 204,
321     TRIGGER_FUNCTION_CLASS_NOT_FOUND = 205,
322     NOT_USED_206 = 206,
323     NOT_USED_207 = 207,
324     INVALID_COLLATION_NAME_NO_SUBCLASS = 208,
325     DataFileCache_makeRow = 209,
326     DataFileCache_open = 210,
327     DataFileCache_close = 211,
328     Expression_resolveTypes1 = 212,
329     Expression_resolveTypes2 = 213,
330     Expression_resolveTypes3 = 214,
331     Expression_resolveTypes4 = 215,
332     UNRESOLVED_PARAMETER_TYPE = 216,
333     Expression_resolveTypes6 = 217,
334     Expression_resolveTypes7 = 218,
335     Expression_resolveTypeForLike = 219,
336     NOT_USED_220 = 220,
337     Expression_resolveTypeForIn2 = 221,
338     Session_execute = 222,
339     NOT_USED_223 = 223,
340     NOT_USED_224 = 224,
341     DATA_FILE_IS_FULL = 225,
342     THREE_PART_IDENTIFIER = 226,
343     INVALID_SCHEMA_NAME_NO_SUBCLASS = 227,
344     DEPENDENT_DATABASE_OBJECT_EXISTS = 228,
345     NO_SUCH_ROLE_GRANT = 229,
346     NO_SUCH_ROLE_REVOKE = 230,
347     NONMOD_ACCOUNT = 231,
348     NO_SUCH_GRANTEE = 232,
349     MISSING_SYSAUTH = 233,
350     MISSING_GRANTEE = 234,
351     CHANGE_GRANTEE = 235,
352     NULL_NAME = 236,
353     ILLEGAL_ROLE_NAME = 237,
354     ROLE_ALREADY_EXISTS = 238,
355     NO_SUCH_ROLE = 239,
356     MISSING_ROLEMANAGER = 240,
357     GRANTEE_ALREADY_EXISTS = 241,
358     MISSING_PUBLIC_GRANTEE = 242,
359     NONMOD_GRANTEE = 243,
360     CIRCULAR_GRANT = 244,
361     ALREADY_HAVE_ROLE = 245,
362     DONT_HAVE_ROLE = 246,
363     NOT_USED_247 = 247,
364     RETRIEVE_NEST_ROLE_FAIL = 248,
365     NO_SUCH_RIGHT = 249,
366     IN_SCHEMA_DEFINITION = 250,
367     PRIMARY_KEY_NOT_ALLOWED = 251,
368     COLUMN_IS_IN_CONSTRAINT = 252,
369     COLUMN_SIZE_REQUIRED = 253,
370     INVALID_SIZE_PRECISION = 254,
371     LAST_ERROR_HANDLE = 255;
372
373     //
374
static String JavaDoc MESSAGE_TAG = "$$";
375
376     //
377

378     /** Used during tests. */
379     static {
380         try {
381             TRACE = TRACE || Boolean.getBoolean("hsqldb.trace");
382             TRACESYSTEMOUT = TRACESYSTEMOUT
383                              || Boolean.getBoolean("hsqldb.tracesystemout");
384         } catch (Exception JavaDoc e) {}
385
386         if (!"LAST".equals(BundleHandler.getString(bundleHandle,
387                 String.valueOf(LAST_ERROR_HANDLE)))) {
388             throw new RuntimeException JavaDoc();
389         }
390     }
391
392     /**
393      * Compose error message by inserting the strings in the add parameters
394      * in placeholders within the error message. The message string contains
395      * $$ markers for each context variable. Context variables are supplied in
396      * the add parameters.
397      *
398      * @param code main error code
399      * @param subCode sub error code (if 0 => no subMessage!)
400      * @param add optional parameters
401      *
402      * @return an <code>HsqlException</code>
403      */

404     public static HsqlException error(int code, int subCode,
405                                       final Object JavaDoc[] add) {
406
407         // in case of negative code
408
code = Math.abs(code);
409
410         String JavaDoc mainErrorMessage = getMessage(code);
411         String JavaDoc state = "S1000";
412
413         if (mainErrorMessage.length() >= 5) {
414             state = mainErrorMessage.substring(0, 5);
415             mainErrorMessage = mainErrorMessage.substring(6);
416         }
417
418         if (subCode != 0) {
419             mainErrorMessage += getMessage(Math.abs(subCode));
420         }
421
422         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(mainErrorMessage.length() + 32);
423         int lastIndex = 0;
424         int escIndex = mainErrorMessage.length();
425
426         if (add != null) {
427
428             // removed test: i < add.length
429
// because if mainErrorMessage is equal to "blabla $$"
430
// then the statement escIndex = mainErrorMessage.length();
431
// is never reached! ???
432
for (int i = 0; i < add.length; i++) {
433                 escIndex = mainErrorMessage.indexOf(MESSAGE_TAG, lastIndex);
434
435                 if (escIndex == -1) {
436                     break;
437                 }
438
439                 sb.append(mainErrorMessage.substring(lastIndex, escIndex));
440                 sb.append(add[i] == null ? "null exception message"
441                                          : add[i].toString());
442
443                 lastIndex = escIndex + MESSAGE_TAG.length();
444             }
445         }
446
447         escIndex = mainErrorMessage.length();
448
449         sb.append(mainErrorMessage.substring(lastIndex, escIndex));
450
451         return new HsqlException(sb.toString(), state, -code);
452     }
453
454     /**
455      * Compose error message by inserting the strings in the add parameters
456      * in placeholders within the error message. The message string contains
457      * $$ markers for each context variable. Context variables are supplied in
458      * the add parameters.
459      *
460      * @param code main error code
461      * @param add optional parameters
462      *
463      * @return an <code>HsqlException</code>
464      */

465     public static HsqlException error(int code, final Object JavaDoc[] add) {
466         return error(code, 0, add);
467     }
468
469     public static HsqlException error(int code, int code2, String JavaDoc add) {
470         return error(code, getMessage(code2) + ' ' + add);
471     }
472
473     public static HsqlException error(int code, int code2) {
474         return error(code, getMessage(code2));
475     }
476
477     /**
478      * Method declaration
479      *
480      *
481      * @param code
482      * @param add
483      *
484      * @return
485      */

486     public static HsqlException error(int code, Object JavaDoc add) {
487
488 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
489
code = Math.abs(code);
490
491         String JavaDoc s = getMessage(code);
492
493         if (add != null) {
494             s += ": " + add.toString();
495         }
496
497 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
498
return new HsqlException(s.substring(6), s.substring(0, 5), -code);
499
500         //return getError(s);
501
}
502
503     /**
504      * Return a new <code>HsqlException</code> according to the result parameter.
505      *
506      * @param result the <code>Result</code> associated with the exception
507      * @return a new <code>HsqlException</code> according to the result parameter
508      */

509     public static HsqlException error(final Result result) {
510         return new HsqlException(result);
511     }
512
513     /**
514      * Return a new <code>Result</code> of type error.
515      *
516      * @param result the <code>Result</code> associated with the exception
517      * @return a new <code>HsqlException</code> according to the result parameter
518      */

519
520 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
521

522     /**
523      * Constructor for errors
524      *
525      * @param e exception
526      */

527     static Result toResult(HsqlException e) {
528         return new Result(e.getMessage(), e.getSQLState(), e.getErrorCode());
529     }
530
531     public static RuntimeException JavaDoc runtimeError(int code, Object JavaDoc add) {
532
533         HsqlException e = error(code, add);
534
535         return new RuntimeException JavaDoc(e.getMessage());
536     }
537
538     /**
539      * Returns the error message given the error code.<br/>
540      * Note: this method must be used when throwing exception other
541      * than <code>HsqlException</code>.
542      *
543      * @param errorCode the error code associated to the error message
544      * @return the error message associated with the error code
545      * @see #sDescription
546      */

547     public static String JavaDoc getMessage(final int errorCode) {
548         return getMessage(errorCode, false, null);
549     }
550
551     /**
552      * Returns the error message given the error code.<br/>
553      * Note: this method must be used when throwing exception other
554      * than <code>HsqlException</code>.
555      *
556      * @param errorCode the error code associated to the error message
557      * @param substitute substitute the $$ tokens using data in the values
558      * @param values value(s) to use to replace the token(s)
559      * @return the error message associated with the error code
560      * @see #sDescription
561      */

562     public static String JavaDoc getMessage(final int errorCode,
563                                     final boolean substitute,
564                                     final Object JavaDoc[] values) {
565
566         if (errorCode < 0) {
567             return "";
568         } else {
569             String JavaDoc key = String.valueOf(errorCode);
570
571             if (errorCode < 10) {
572                 key = "00" + key;
573             } else if (errorCode < 100) {
574                 key = "0" + key;
575             }
576
577             String JavaDoc mainErrorMessage = BundleHandler.getString(bundleHandle,
578                 key);
579
580             if (!substitute) {
581
582 // return sDescription[errorCode];
583
return mainErrorMessage;
584             } else {
585
586 // final String mainErrorMessage = sDescription[errorCode];
587
final StringBuffer JavaDoc sb =
588                     new StringBuffer JavaDoc(mainErrorMessage.length() + 32);
589                 int lastIndex = 0;
590                 int escIndex = mainErrorMessage.length();
591
592                 if (values != null) {
593
594                     // removed test: i < add.length
595
// because if mainErrorMessage is equal to "blabla $$"
596
// then the statement escIndex = mainErrorMessage.length();
597
// is never reached! ???
598
for (int i = 0; i < values.length; i++) {
599                         escIndex = mainErrorMessage.indexOf(MESSAGE_TAG,
600                                                             lastIndex);
601
602                         if (escIndex == -1) {
603                             break;
604                         }
605
606                         sb.append(mainErrorMessage.substring(lastIndex,
607                                                              escIndex));
608                         sb.append(values[i].toString());
609
610                         lastIndex = escIndex + MESSAGE_TAG.length();
611                     }
612                 }
613
614                 escIndex = mainErrorMessage.length();
615
616                 sb.append(mainErrorMessage.substring(lastIndex, escIndex));
617
618                 return sb.toString();
619             }
620         }
621     }
622
623     /**
624      * Method declaration
625      *
626      *
627      * @param code
628      *
629      * @return
630      */

631     public static HsqlException error(int code) {
632         return error(code, null);
633     }
634
635     /**
636      * Throws exception if condition is false
637      *
638      * @param condition
639      * @param code
640      *
641      * @throws HsqlException
642      */

643     public static void check(boolean condition,
644                              int code) throws HsqlException {
645         check(condition, code, null, null, null, null);
646     }
647
648     /**
649      * Throws exception if condition is false
650      *
651      * @param condition
652      * @param code
653      * @param add
654      *
655      * @throws HsqlException
656      */

657     public static void check(boolean condition, int code,
658                              Object JavaDoc add) throws HsqlException {
659
660         if (!condition) {
661             throw error(code, add);
662         }
663     }
664
665     /**
666      * Method declaration
667      *
668      *
669      * @param code
670      * @param add
671      *
672      * @throws HsqlException
673      */

674     static void throwerror(int code, Object JavaDoc add) throws HsqlException {
675         throw error(code, add);
676     }
677
678     /**
679      * Used to print messages to System.out
680      *
681      *
682      * @param message message to print
683      */

684     public static void printSystemOut(String JavaDoc message) {
685
686         if (TRACESYSTEMOUT) {
687             System.out.println(message);
688         }
689     }
690
691     /**
692      * Used to print messages to System.out
693      *
694      *
695      * @param message1 message to print
696      * @param message2 message to print
697      */

698     public static void printSystemOut(String JavaDoc message1, long message2) {
699
700         if (TRACESYSTEMOUT) {
701             System.out.print(message1);
702             System.out.println(message2);
703         }
704     }
705
706     /**
707      * Returns the stack trace for doAssert()
708      */

709     private static String JavaDoc getStackTrace() {
710
711         try {
712             Exception JavaDoc e = new Exception JavaDoc();
713
714             throw e;
715         } catch (Exception JavaDoc e) {
716             HsqlByteArrayOutputStream os = new HsqlByteArrayOutputStream();
717             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os, true);
718
719             e.printStackTrace(pw);
720
721             return os.toString();
722         }
723     }
724
725     /**
726      * Throws exception if condition is false
727      *
728      * @param condition
729      * @param code
730      * @param add1
731      * @param add2
732      *
733      * @throws HsqlException
734      */

735     static void check(boolean condition, int code, String JavaDoc add1,
736                       String JavaDoc add2) throws HsqlException {
737         check(condition, code, add1, add2, null, null);
738     }
739
740     /**
741      * Throws exception if condition is false
742      *
743      * @param condition
744      * @param code
745      * @param add1
746      * @param add2
747      * @param add3
748      *
749      * @throws HsqlException
750      */

751     static void check(boolean condition, int code, String JavaDoc add1, String JavaDoc add2,
752                       String JavaDoc add3) throws HsqlException {
753         check(condition, code, add1, add2, add3, null);
754     }
755
756     /**
757      * Throws exception if condition is false
758      *
759      * @param condition
760      * @param code
761      * @param add1
762      * @param add2
763      * @param add3
764      * @param add4
765      *
766      * @throws HsqlException
767      */

768     static void check(boolean condition, int code, String JavaDoc add1, String JavaDoc add2,
769                       String JavaDoc add3, String JavaDoc add4) throws HsqlException {
770
771         if (!condition) {
772             String JavaDoc add = "";
773
774             if (add1 != null) {
775                 add += add1;
776             }
777
778             if (add2 != null) {
779                 add += add2;
780             }
781
782             if (add3 != null) {
783                 add += add3;
784             }
785
786             if (add4 != null) {
787                 add += add4;
788             }
789
790             throw error(code, add.length() > 0 ? add
791                                                : null);
792         }
793     }
794
795     /**
796      * Throws exception if assertion fails
797      *
798      * @param condition
799      * @throws HsqlException
800      */

801     static void doAssert(boolean condition) throws HsqlException {
802         doAssert(condition, null);
803     }
804
805     /**
806      * Throws exception if assertion fails
807      *
808      * @param condition
809      * @param error
810      * @throws HsqlException
811      */

812     static void doAssert(boolean condition,
813                          String JavaDoc error) throws HsqlException {
814
815         if (!condition) {
816             if (error == null) {
817                 error = "";
818             }
819
820             error += getStackTrace();
821
822             throw error(ASSERT_FAILED, error);
823         }
824     }
825 }
826
Popular Tags