001: /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the Hypersonic SQL Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * This software consists of voluntary contributions made by many individuals
031: * on behalf of the Hypersonic SQL Group.
032: *
033: *
034: * For work added by the HSQL Development Group:
035: *
036: * Copyright (c) 2001-2005, The HSQL Development Group
037: * All rights reserved.
038: *
039: * Redistribution and use in source and binary forms, with or without
040: * modification, are permitted provided that the following conditions are met:
041: *
042: * Redistributions of source code must retain the above copyright notice, this
043: * list of conditions and the following disclaimer.
044: *
045: * Redistributions in binary form must reproduce the above copyright notice,
046: * this list of conditions and the following disclaimer in the documentation
047: * and/or other materials provided with the distribution.
048: *
049: * Neither the name of the HSQL Development Group nor the names of its
050: * contributors may be used to endorse or promote products derived from this
051: * software without specific prior written permission.
052: *
053: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
054: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
055: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
056: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
057: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
058: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
059: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
060: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
061: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
062: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
063: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
064: */
065:
066: package org.hsqldb;
067:
068: import java.io.PrintWriter;
069:
070: import org.hsqldb.lib.HsqlByteArrayOutputStream;
071: import org.hsqldb.resources.BundleHandler;
072:
073: /**
074: * handles creation and reporting of error messages and throwing HsqlException
075: *
076: * Rewritten and extended in successive versions of HSQLDB.
077: *
078: * @author Thomas Mueller (Hypersonic SQL Group)
079: * @version 1.8.0
080: * @since Hypersonic SQL
081: */
082:
083: // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - error reporting
084: // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - setting trace
085: // the system property hsqldb.tracesystemout == true is now used for printing
086: // trace message to System.out
087: // fredt@users 20020305 - patch 1.7.0 - various new messages added
088: // tony_lai@users 20020820 - patch 595073 - Duplicated exception msg
089: // fredt@users 20021230 - patch 488118 by xclay@users - allow multithreading
090: // wsonic@users 20031005 - moved string literal messages to Trace with new methods
091: // nitin chauhan 20031005 - moved concatenated string in asserts and checks to Trace with new methods
092: // fredt@users 20040322 - removed unused code - class is a collection of static methods now
093: // fredt@users 20050524 - use resource bundle for messages
094: /** @todo fredt - 20021022 management of nested throws inside the program in
095: * such a way that it is possible to return exactly the text of the error
096: * thrown at a given level without higher level messages being added and to
097: * preserve the original error code
098: */
099: public class Trace {
100:
101: public static boolean TRACE = false;
102: public static boolean TRACESYSTEMOUT = false;
103: public static final boolean STOP = false;
104: public static final boolean DOASSERT = false;
105:
106: //
107: static String errPropsName = "sql-error-messages";
108: static int bundleHandle = BundleHandler.getBundleHandle(
109: errPropsName, null);
110:
111: //
112: public static final int
113:
114: //
115: DATABASE_ALREADY_IN_USE = 1,
116: CONNECTION_IS_CLOSED = 2,
117: CONNECTION_IS_BROKEN = 3,
118: DATABASE_IS_SHUTDOWN = 4,
119: COLUMN_COUNT_DOES_NOT_MATCH = 5,
120: DIVISION_BY_ZERO = 6,
121: INVALID_ESCAPE = 7,
122: INTEGRITY_CONSTRAINT_VIOLATION = 8,
123: VIOLATION_OF_UNIQUE_INDEX = 9,
124: TRY_TO_INSERT_NULL = 10,
125: UNEXPECTED_TOKEN = 11,
126: UNEXPECTED_END_OF_COMMAND = 12,
127: UNKNOWN_FUNCTION = 13,
128: NEED_AGGREGATE = 14,
129: SUM_OF_NON_NUMERIC = 15,
130: WRONG_DATA_TYPE = 16,
131: CARDINALITY_VIOLATION_NO_SUBCLASS = 17,
132: SERIALIZATION_FAILURE = 18,
133: TRANSFER_CORRUPTED = 19,
134: FUNCTION_NOT_SUPPORTED = 20,
135: TABLE_ALREADY_EXISTS = 21,
136: TABLE_NOT_FOUND = 22,
137: INDEX_ALREADY_EXISTS = 23,
138: SECOND_PRIMARY_KEY = 24,
139: DROP_PRIMARY_KEY = 25,
140: INDEX_NOT_FOUND = 26,
141: COLUMN_ALREADY_EXISTS = 27,
142: COLUMN_NOT_FOUND = 28,
143: FILE_IO_ERROR = 29,
144: WRONG_DATABASE_FILE_VERSION = 30,
145: DATABASE_IS_READONLY = 31,
146: DATA_IS_READONLY = 32,
147: ACCESS_IS_DENIED = 33,
148: INPUTSTREAM_ERROR = 34,
149: NO_DATA_IS_AVAILABLE = 35,
150: USER_ALREADY_EXISTS = 36,
151: USER_NOT_FOUND = 37,
152: ASSERT_FAILED = 38,
153: EXTERNAL_STOP = 39,
154: GENERAL_ERROR = 40,
155: WRONG_OUT_PARAMETER = 41,
156: FUNCTION_NOT_FOUND = 42,
157: TRIGGER_NOT_FOUND = 43,
158: SAVEPOINT_NOT_FOUND = 44,
159: LABEL_REQUIRED = 45,
160: WRONG_DEFAULT_CLAUSE = 46,
161: FOREIGN_KEY_NOT_ALLOWED = 47,
162: UNKNOWN_DATA_SOURCE = 48,
163: BAD_INDEX_CONSTRAINT_NAME = 49,
164: DROP_FK_INDEX = 50,
165: RESULTSET_FORWARD_ONLY = 51,
166: VIEW_ALREADY_EXISTS = 52,
167: VIEW_NOT_FOUND = 53,
168: NOT_USED_54 = 54,
169: NOT_A_TABLE = 55,
170: SYSTEM_INDEX = 56,
171: COLUMN_TYPE_MISMATCH = 57,
172: BAD_ADD_COLUMN_DEFINITION = 58,
173: DROP_SYSTEM_CONSTRAINT = 59,
174: CONSTRAINT_ALREADY_EXISTS = 60,
175: CONSTRAINT_NOT_FOUND = 61,
176: INVALID_JDBC_ARGUMENT = 62,
177: DATABASE_IS_MEMORY_ONLY = 63,
178: OUTER_JOIN_CONDITION = 64,
179: NUMERIC_VALUE_OUT_OF_RANGE = 65,
180: MISSING_SOFTWARE_MODULE = 66,
181: NOT_IN_AGGREGATE_OR_GROUP_BY = 67,
182: INVALID_GROUP_BY = 68,
183: INVALID_HAVING = 69,
184: INVALID_ORDER_BY = 70,
185: INVALID_ORDER_BY_IN_DISTINCT_SELECT = 71,
186: OUT_OF_MEMORY = 72,
187: OPERATION_NOT_SUPPORTED = 73,
188: INVALID_IDENTIFIER = 74,
189: TEXT_TABLE_SOURCE = 75,
190: TEXT_FILE = 76,
191: NOT_USED_77 = 77,
192: ERROR_IN_SCRIPT_FILE = 78,
193: NULL_IN_VALUE_LIST = 79,
194: SOCKET_ERROR = 80,
195: INVALID_CHARACTER_ENCODING = 81,
196: NOT_USED_82 = 82,
197: NOT_USED_83 = 83,
198: NOT_USED_84 = 84,
199: UNEXPECTED_EXCEPTION = 85,
200: NOT_USED_86 = 86,
201: NOT_USED_87 = 87,
202: NOT_USED_88 = 88,
203: NOT_USED_89 = 89,
204: NOT_USED_90 = 90,
205: NOT_USED_91 = 91,
206: NOT_USED_92 = 92,
207: NOT_USED_93 = 93,
208: DATABASE_NOT_EXISTS = 94,
209: INVALID_CONVERSION = 95,
210: ERROR_IN_BINARY_SCRIPT_1 = 96,
211: ERROR_IN_BINARY_SCRIPT_2 = 97,
212: GENERAL_IO_ERROR = 98,
213: EXPRESSION_NOT_SUPPORTED = 99,
214: Constraint_violation = 100,
215: Database_dropTable = 101,
216: ERROR_IN_CONSTRAINT_COLUMN_LIST = 102,
217: TABLE_HAS_NO_PRIMARY_KEY = 103,
218: VIOLATION_OF_UNIQUE_CONSTRAINT = 104,
219: NO_DEFAULT_VALUE_FOR_COLUMN = 105,
220: NOT_A_CONDITION = 106,
221: DatabaseManager_getDatabase = 107,
222: NOT_USED_108 = 108,
223: NOT_USED_109 = 109,
224: NOT_USED_110 = 110,
225: NOT_USED_111 = 111,
226: NOT_USED_112 = 112,
227: DatabaseScriptReader_readDDL = 113,
228: DatabaseScriptReader_readExistingData = 114,
229: Message_Pair = 115,
230: LOAD_SAVE_PROPERTIES = 116,
231: INVALID_TRANSACTION_STATE_NO_SUBCLASS = 117,
232: JDBC_INVALID_BRI_SCOPE = 118,
233: JDBC_NO_RESULT_SET_METADATA = 119,
234: JDBC_NO_RESULT_SET = 120,
235: MISSING_CLOSEBRACKET = 121,
236: ITSNS_OVERWRITE = 122,
237: COLUMN_IS_IN_INDEX = 123,
238: STRING_DATA_TRUNCATION = 124,
239: QUOTED_IDENTIFIER_REQUIRED = 125,
240: STATEMENT_IS_CLOSED = 126,
241: NOT_USED_127 = 127,
242: NOT_USED_128 = 128,
243: DATA_FILE_ERROR = 129,
244: NOT_USED_130 = 130,
245: HsqlDateTime_null_string = 131,
246: NOT_USED_132 = 132,
247: HsqlDateTime_null_date = 133,
248: NOT_USED_134 = 134,
249: HsqlProperties_load = 135,
250: HsqlSocketFactorySecure_verify = 136,
251: HsqlSocketFactorySecure_verify2 = 137,
252: jdbcConnection_nativeSQL = 138,
253: HsqlSocketFactorySecure_verify3 = 139,
254: NOT_USED_140 = 140,
255: NOT_USED_141 = 141,
256: jdbcStatement_executeUpdate = 142,
257: LockFile_checkHeartbeat = 143,
258: LockFile_checkHeartbeat2 = 144,
259: TEXT_STRING_HAS_NEWLINE = 145,
260: Result_Result = 146,
261: SERVER_NO_DATABASE = 147,
262: Server_openServerSocket = 148,
263: Server_openServerSocket2 = 149,
264: TEXT_TABLE_HEADER = 150,
265: NOT_USED_151 = 151,
266: JDBC_PARAMETER_NOT_SET = 152,
267: INVALID_LIMIT = 153,
268: JDBC_STATEMENT_NOT_ROW_COUNT = 154,
269: JDBC_STATEMENT_NOT_RESULTSET = 155,
270: AMBIGUOUS_COLUMN_REFERENCE = 156,
271: CHECK_CONSTRAINT_VIOLATION = 157,
272: JDBC_RESULTSET_IS_CLOSED = 158,
273: SINGLE_COLUMN_EXPECTED = 159,
274: TOKEN_REQUIRED = 160,
275: NOT_USED_161 = 161,
276: NOT_USED_162 = 162,
277: ORDER_LIMIT_REQUIRED = 163,
278: TRIGGER_ALREADY_EXISTS = 164,
279: ASSERT_DIRECT_EXEC_WITH_PARAM = 165,
280: NOT_USED_166 = 166,
281: Expression_compareValues = 167,
282: INVALID_LIMIT_EXPRESSION = 168,
283: INVALID_TOP_EXPRESSION = 169,
284: SQL_CONSTRAINT_REQUIRED = 170,
285: TableWorks_dropConstraint = 171,
286: TEXT_TABLE_SOURCE_FILENAME = 172,
287: TEXT_TABLE_SOURCE_VALUE_MISSING = 173,
288: TEXT_TABLE_SOURCE_SEPARATOR = 174,
289: UNSUPPORTED_PARAM_CLASS = 175,
290: JDBC_NULL_STREAM = 176,
291: INTEGRITY_CONSTRAINT_VIOLATION_NOPARENT = 177,
292: NOT_USED_178 = 178,
293: NOT_USED_179 = 179,
294: QuotedTextDatabaseRowInput_getField = 180,
295: QuotedTextDatabaseRowInput_getField2 = 181,
296: TextDatabaseRowInput_getField = 182,
297: TextDatabaseRowInput_getField2 = 183,
298: TextDatabaseRowInput_getField3 = 184,
299: Parser_ambiguous_between1 = 185,
300: SEQUENCE_REFERENCED_BY_VIEW = 186,
301: NOT_USED_187 = 187,
302: TextCache_openning_file_error = 188,
303: TextCache_closing_file_error = 189,
304: TextCache_purging_file_error = 190,
305: SEQUENCE_NOT_FOUND = 191,
306: SEQUENCE_ALREADY_EXISTS = 192,
307: TABLE_REFERENCED_CONSTRAINT = 193,
308: TABLE_REFERENCED_VIEW = 194,
309: NOT_USED_195 = 195,
310: TEXT_SOURCE_EXISTS = 196,
311: COLUMN_IS_REFERENCED = 197,
312: FUNCTION_CALL_ERROR = 198,
313: TRIGGERED_DATA_CHANGE = 199,
314: INVALID_FUNCTION_ARGUMENT = 200,
315: UNSUPPORTED_INTERNAL_OPERATION = 201,
316: NOT_USED_202 = 202,
317: INVALID_PREPARED_STATEMENT = 203,
318: CREATE_TRIGGER_COMMAND_1 = 204,
319: TRIGGER_FUNCTION_CLASS_NOT_FOUND = 205,
320: NOT_USED_206 = 206,
321: NOT_USED_207 = 207,
322: INVALID_COLLATION_NAME_NO_SUBCLASS = 208,
323: DataFileCache_makeRow = 209,
324: DataFileCache_open = 210,
325: DataFileCache_close = 211,
326: Expression_resolveTypes1 = 212,
327: Expression_resolveTypes2 = 213,
328: Expression_resolveTypes3 = 214,
329: Expression_resolveTypes4 = 215,
330: UNRESOLVED_PARAMETER_TYPE = 216,
331: Expression_resolveTypes6 = 217,
332: Expression_resolveTypes7 = 218,
333: Expression_resolveTypeForLike = 219,
334: NOT_USED_220 = 220,
335: Expression_resolveTypeForIn2 = 221,
336: Session_execute = 222,
337: NOT_USED_223 = 223,
338: NOT_USED_224 = 224,
339: DATA_FILE_IS_FULL = 225,
340: THREE_PART_IDENTIFIER = 226,
341: INVALID_SCHEMA_NAME_NO_SUBCLASS = 227,
342: DEPENDENT_DATABASE_OBJECT_EXISTS = 228,
343: NO_SUCH_ROLE_GRANT = 229,
344: NO_SUCH_ROLE_REVOKE = 230,
345: NONMOD_ACCOUNT = 231,
346: NO_SUCH_GRANTEE = 232,
347: MISSING_SYSAUTH = 233,
348: MISSING_GRANTEE = 234,
349: CHANGE_GRANTEE = 235,
350: NULL_NAME = 236,
351: ILLEGAL_ROLE_NAME = 237,
352: ROLE_ALREADY_EXISTS = 238,
353: NO_SUCH_ROLE = 239,
354: MISSING_ROLEMANAGER = 240,
355: GRANTEE_ALREADY_EXISTS = 241,
356: MISSING_PUBLIC_GRANTEE = 242,
357: NONMOD_GRANTEE = 243,
358: CIRCULAR_GRANT = 244,
359: ALREADY_HAVE_ROLE = 245,
360: DONT_HAVE_ROLE = 246,
361: NOT_USED_247 = 247,
362: RETRIEVE_NEST_ROLE_FAIL = 248,
363: NO_SUCH_RIGHT = 249,
364: IN_SCHEMA_DEFINITION = 250,
365: PRIMARY_KEY_NOT_ALLOWED = 251,
366: COLUMN_IS_IN_CONSTRAINT = 252,
367: COLUMN_SIZE_REQUIRED = 253,
368: INVALID_SIZE_PRECISION = 254,
369: CANNOT_CONNECT_TABLE = 255,
370: LAST_ERROR_HANDLE = 256;
371:
372: //
373: static String MESSAGE_TAG = "$$";
374:
375: //
376:
377: /** Used during tests. */
378: static {
379: try {
380: TRACE = TRACE || Boolean.getBoolean("hsqldb.trace");
381: TRACESYSTEMOUT = TRACESYSTEMOUT
382: || Boolean.getBoolean("hsqldb.tracesystemout");
383: } catch (Exception e) {
384: }
385:
386: if (!"LAST".equals(BundleHandler.getString(bundleHandle, String
387: .valueOf(LAST_ERROR_HANDLE)))) {
388: throw new RuntimeException();
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[] add) {
406:
407: // in case of negative code
408: code = Math.abs(code);
409:
410: String mainErrorMessage = getMessage(code);
411: String 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 sb = new StringBuffer(
423: mainErrorMessage.length() + 32);
424: int lastIndex = 0;
425: int escIndex = mainErrorMessage.length();
426:
427: if (add != null) {
428:
429: // removed test: i < add.length
430: // because if mainErrorMessage is equal to "blabla $$"
431: // then the statement escIndex = mainErrorMessage.length();
432: // is never reached! ???
433: for (int i = 0; i < add.length; i++) {
434: escIndex = mainErrorMessage.indexOf(MESSAGE_TAG,
435: lastIndex);
436:
437: if (escIndex == -1) {
438: break;
439: }
440:
441: sb.append(mainErrorMessage.substring(lastIndex,
442: escIndex));
443: sb.append(add[i] == null ? "null exception message"
444: : add[i].toString());
445:
446: lastIndex = escIndex + MESSAGE_TAG.length();
447: }
448: }
449:
450: escIndex = mainErrorMessage.length();
451:
452: sb.append(mainErrorMessage.substring(lastIndex, escIndex));
453:
454: return new HsqlException(sb.toString(), state, -code);
455: }
456:
457: /**
458: * Compose error message by inserting the strings in the add parameters
459: * in placeholders within the error message. The message string contains
460: * $$ markers for each context variable. Context variables are supplied in
461: * the add parameters.
462: *
463: * @param code main error code
464: * @param add optional parameters
465: *
466: * @return an <code>HsqlException</code>
467: */
468: public static HsqlException error(int code, final Object[] add) {
469: return error(code, 0, add);
470: }
471:
472: public static HsqlException error(int code, int code2, String add) {
473: return error(code, getMessage(code2) + ' ' + add);
474: }
475:
476: public static HsqlException error(int code, int code2) {
477: return error(code, getMessage(code2));
478: }
479:
480: /**
481: * Method declaration
482: *
483: *
484: * @param code
485: * @param add
486: *
487: * @return
488: */
489: public static HsqlException error(int code, Object add) {
490:
491: // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
492: code = Math.abs(code);
493:
494: String s = getMessage(code);
495:
496: if (add != null) {
497: s += ": " + add.toString();
498: }
499:
500: // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
501: return new HsqlException(s.substring(6), s.substring(0, 5),
502: -code);
503:
504: //return getError(s);
505: }
506:
507: /**
508: * Return a new <code>HsqlException</code> according to the result parameter.
509: *
510: * @param result the <code>Result</code> associated with the exception
511: * @return a new <code>HsqlException</code> according to the result parameter
512: */
513: public static HsqlException error(final Result result) {
514: return new HsqlException(result);
515: }
516:
517: /**
518: * Return a new <code>Result</code> of type error.
519: *
520: * @param result the <code>Result</code> associated with the exception
521: * @return a new <code>HsqlException</code> according to the result parameter
522: */
523:
524: // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
525: /**
526: * Constructor for errors
527: *
528: * @param e exception
529: */
530: static Result toResult(HsqlException e) {
531: return new Result(e.getMessage(), e.getSQLState(), e
532: .getErrorCode());
533: }
534:
535: public static RuntimeException runtimeError(int code, Object add) {
536:
537: HsqlException e = error(code, add);
538:
539: return new RuntimeException(e.getMessage());
540: }
541:
542: /**
543: * Returns the error message given the error code.<br/>
544: * Note: this method must be used when throwing exception other
545: * than <code>HsqlException</code>.
546: *
547: * @param errorCode the error code associated to the error message
548: * @return the error message associated with the error code
549: * @see #sDescription
550: */
551: public static String getMessage(final int errorCode) {
552: return getMessage(errorCode, false, null);
553: }
554:
555: /**
556: * Returns the error message given the error code.<br/>
557: * Note: this method must be used when throwing exception other
558: * than <code>HsqlException</code>.
559: *
560: * @param errorCode the error code associated to the error message
561: * @param substitute substitute the $$ tokens using data in the values
562: * @param values value(s) to use to replace the token(s)
563: * @return the error message associated with the error code
564: * @see #sDescription
565: */
566: public static String getMessage(final int errorCode,
567: final boolean substitute, final Object[] values) {
568:
569: if (errorCode < 0) {
570: return "";
571: } else {
572: String key = String.valueOf(errorCode);
573:
574: if (errorCode < 10) {
575: key = "00" + key;
576: } else if (errorCode < 100) {
577: key = "0" + key;
578: }
579:
580: String mainErrorMessage = BundleHandler.getString(
581: bundleHandle, key);
582:
583: if (!substitute) {
584:
585: // return sDescription[errorCode];
586: return mainErrorMessage;
587: } else {
588:
589: // final String mainErrorMessage = sDescription[errorCode];
590: final StringBuffer sb = new StringBuffer(
591: mainErrorMessage.length() + 32);
592: int lastIndex = 0;
593: int escIndex = mainErrorMessage.length();
594:
595: if (values != null) {
596:
597: // removed test: i < add.length
598: // because if mainErrorMessage is equal to "blabla $$"
599: // then the statement escIndex = mainErrorMessage.length();
600: // is never reached! ???
601: for (int i = 0; i < values.length; i++) {
602: escIndex = mainErrorMessage.indexOf(
603: MESSAGE_TAG, lastIndex);
604:
605: if (escIndex == -1) {
606: break;
607: }
608:
609: sb.append(mainErrorMessage.substring(lastIndex,
610: escIndex));
611: sb.append(values[i].toString());
612:
613: lastIndex = escIndex + MESSAGE_TAG.length();
614: }
615: }
616:
617: escIndex = mainErrorMessage.length();
618:
619: sb.append(mainErrorMessage.substring(lastIndex,
620: escIndex));
621:
622: return sb.toString();
623: }
624: }
625: }
626:
627: /**
628: * Method declaration
629: *
630: *
631: * @param code
632: *
633: * @return
634: */
635: public static HsqlException error(int code) {
636: return error(code, null);
637: }
638:
639: /**
640: * Throws exception if condition is false
641: *
642: * @param condition
643: * @param code
644: *
645: * @throws HsqlException
646: */
647: public static void check(boolean condition, int code)
648: throws HsqlException {
649: check(condition, code, null, null, null, null);
650: }
651:
652: /**
653: * Throws exception if condition is false
654: *
655: * @param condition
656: * @param code
657: * @param add
658: *
659: * @throws HsqlException
660: */
661: public static void check(boolean condition, int code, Object add)
662: throws HsqlException {
663:
664: if (!condition) {
665: throw error(code, add);
666: }
667: }
668:
669: /**
670: * Method declaration
671: *
672: *
673: * @param code
674: * @param add
675: *
676: * @throws HsqlException
677: */
678: static void throwerror(int code, Object add) throws HsqlException {
679: throw error(code, add);
680: }
681:
682: /**
683: * Used to print messages to System.out
684: *
685: *
686: * @param message message to print
687: */
688: public static void printSystemOut(String message) {
689:
690: if (TRACESYSTEMOUT) {
691: System.out.println(message);
692: }
693: }
694:
695: /**
696: * Used to print messages to System.out
697: *
698: *
699: * @param message1 message to print
700: * @param message2 message to print
701: */
702: public static void printSystemOut(String message1, long message2) {
703:
704: if (TRACESYSTEMOUT) {
705: System.out.print(message1);
706: System.out.println(message2);
707: }
708: }
709:
710: /**
711: * Returns the stack trace for doAssert()
712: */
713: private static String getStackTrace() {
714:
715: try {
716: Exception e = new Exception();
717:
718: throw e;
719: } catch (Exception e) {
720: HsqlByteArrayOutputStream os = new HsqlByteArrayOutputStream();
721: PrintWriter pw = new PrintWriter(os, true);
722:
723: e.printStackTrace(pw);
724:
725: return os.toString();
726: }
727: }
728:
729: /**
730: * Throws exception if condition is false
731: *
732: * @param condition
733: * @param code
734: * @param add1
735: * @param add2
736: *
737: * @throws HsqlException
738: */
739: static void check(boolean condition, int code, String add1,
740: String add2) throws HsqlException {
741: check(condition, code, add1, add2, null, null);
742: }
743:
744: /**
745: * Throws exception if condition is false
746: *
747: * @param condition
748: * @param code
749: * @param add1
750: * @param add2
751: * @param add3
752: *
753: * @throws HsqlException
754: */
755: static void check(boolean condition, int code, String add1,
756: String add2, String add3) throws HsqlException {
757: check(condition, code, add1, add2, add3, null);
758: }
759:
760: /**
761: * Throws exception if condition is false
762: *
763: * @param condition
764: * @param code
765: * @param add1
766: * @param add2
767: * @param add3
768: * @param add4
769: *
770: * @throws HsqlException
771: */
772: static void check(boolean condition, int code, String add1,
773: String add2, String add3, String add4) throws HsqlException {
774:
775: if (!condition) {
776: String add = "";
777:
778: if (add1 != null) {
779: add += add1;
780: }
781:
782: if (add2 != null) {
783: add += add2;
784: }
785:
786: if (add3 != null) {
787: add += add3;
788: }
789:
790: if (add4 != null) {
791: add += add4;
792: }
793:
794: throw error(code, add.length() > 0 ? add : null);
795: }
796: }
797:
798: /**
799: * Throws exception if assertion fails
800: *
801: * @param condition
802: * @throws HsqlException
803: */
804: static void doAssert(boolean condition) throws HsqlException {
805: doAssert(condition, null);
806: }
807:
808: /**
809: * Throws exception if assertion fails
810: *
811: * @param condition
812: * @param error
813: * @throws HsqlException
814: */
815: static void doAssert(boolean condition, String error)
816: throws HsqlException {
817:
818: if (!condition) {
819: if (error == null) {
820: error = "";
821: }
822:
823: error += getStackTrace();
824:
825: throw error(ASSERT_FAILED, error);
826: }
827: }
828: }
|