Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: e5fb20aeec7e54eab79a0dec30c78f1d530d2026 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.validation.internal.core;


import java.util.Locale;

import org.eclipse.wst.validation.internal.provisional.core.IMessage;

/**
 * <p>
 * This exception is the only exception which should be thrown by IValidators. The message in this
 * exception must be suitable for showing to the user. All ValidationExceptions will have their
 * message extracted when they're caught, and the message will be shown to the user.
 * 
 * @plannedfor 1.0
 * </p>
 */
public class ValidationException extends Exception {
	private Throwable _lowLevelException = null;
	private IMessage _message = null;
	private ClassLoader _loader = null;
	private final static long serialVersionUID = -3387516993124229949L;


	
	/**
	 * Constructs a new exception with a given message string. <br>
	 * <br>
	 * 
	 * @param message
	 *            IMessage, which is Locale-independent, which contains the message to be shown to
	 *            be shown to the user.
	 */
	public ValidationException(IMessage message) {
		this(message, null);
	}

	/**
	 * Constructs a new exception with a given message string, and low-level exception. <br>
	 * <br>
	 * 
	 * @param message
	 *            IMessage Locale-independent message to be shown to the user.
	 * @param exception
	 *            relevant low-level exception, or <code>null</code> if none. <br>
	 *            &nbsp;&nbsp;&nbsp For example, when a method fails because of a network
	 *            communications &nbsp;&nbsp;&nbsp problem, this would be the
	 *            <code>java.io.IOException</code> &nbsp;&nbsp;&nbsp describing the exact nature
	 *            of the problem.
	 */
	public ValidationException(IMessage message, Throwable exception) {
		super();
		_message = message;
		_lowLevelException = exception;
	}

	/**
	 * @return the low-level exception associated with this ValidationException.
	 */
	public Throwable getAssociatedException() {
		return _lowLevelException;
	}

	/**
	 * @return the IMessage to be shown to the user, or null if this exception should be handled
	 * internally.
	 */
	public IMessage getAssociatedMessage() {
		return _message;
	}

	/**
	 * @return if the IValidator which threw this exception was loaded by a different ClassLoader than the
	 * framework, this method returns the ClassLoader of the IValidator.
	 */
	public ClassLoader getClassLoader() {
		return _loader;
	}

	/**
	 * @return the error message string of this <code>Throwable</code> object if it was
	 *         {@link java.lang.Throwable#Throwable(String) created}with an error message string;
	 *         or <code>null</code> if it was {@link java.lang.Throwable#Throwable() created}with
	 *         no error message.
	 *  
	 */
	public String getMessage() {
		return _message.getText(getClassLoader());
	}

	/**
	 * @param locale
	 * 			The locale of which to get the message.
	 * @return the error message string of this <code>Throwable</code> object if it was
	 *         {@link java.lang.Throwable#Throwable(String) created}with an error message string;
	 *         or <code>null</code> if it was {@link java.lang.Throwable#Throwable() created}with
	 *         no error message.
	 */
	public String getMessage(Locale locale) {
		return _message.getText(locale, getClassLoader());
	}

	/**
	 * <p>
	 * If the IValidator which threw this exception was loaded by a different ClassLoader than the
	 * framework, this method should set the ClassLoader to be the ClassLoader of the IValidator.
	 * </p>
	 * @param loader
	 *  		ClassLoader of the validator
	 */
	public void setClassLoader(ClassLoader loader) {
		_loader = loader;
	}

	/**
	 * <p>
	 * Returns a short description of this throwable object. If this <code>Throwable</code> object
	 * was {@link java.lang.Throwable#Throwable(String) created}with an error message string, then
	 * the result is the concatenation of three strings:
	 * <ul>
	 * <li>The name of the actual class of this object
	 * <li>": " (a colon and a space)
	 * <li>The result of the {@link #getMessage}method for this object
	 * </ul>
	 * If this <code>Throwable</code> object was {@link java.lang.Throwable#Throwable() created}
	 * with no error message string, then the name of the actual class of this object is returned.
	 * </p>
	 * 
	 * @return a string representation of this <code>Throwable</code>.
	 */
	public String toString() {
		return toString(Locale.getDefault());
	}

	/**
	 * <p>
	 * Returns a short description of this throwable object. If this <code>Throwable</code> object
	 * was {@link java.lang.Throwable#Throwable(String) created}with an error message string, then
	 * the result is the concatenation of three strings:
	 * <ul>
	 * <li>The name of the actual class of this object
	 * <li>": " (a colon and a space)
	 * <li>The result of the {@link #getMessage}method for this object
	 * </ul>
	 * If this <code>Throwable</code> object was {@link java.lang.Throwable#Throwable() created}
	 * with no error message string, then the name of the actual class of this object is returned.
	 * </p>
	 * 
	 * @return a string representation of this <code>Throwable</code>.
	 */
	public String toString(Locale locale) {
		String s = getClass().getName();
		String message = getMessage(locale);
		return (message != null) ? (s + ": " + message) : s; //$NON-NLS-1$
	}
}

Back to the top