Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 21f715263e30c141a5d7b62a3c266f1c0a482702 (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
/*******************************************************************************
 * Copyright (c) 2005, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.core.internal.validation;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.jpa.core.JpaNode;
import org.eclipse.jpt.jpa.core.JpaPreferences;
import org.eclipse.jpt.jpa.core.internal.plugin.JptJpaCorePlugin;
import org.eclipse.wst.validation.internal.core.Message;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

public class DefaultJpaValidationMessages {

	private static String[] DEFAULT_PARMS = new String[0];
	private static TextRange DEFAULT_TEXT_RANGE = TextRange.Empty.instance();

	public static IMessage buildMessage(int defaultSeverity, String messageId, JpaNode targetObject) {
		return buildMessage(defaultSeverity, messageId, targetObject.getResource());
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, IResource targetObject) {
		return buildMessage(defaultSeverity, messageId, DEFAULT_PARMS, targetObject);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, String[] parms, JpaNode targetObject) {
		return buildMessage(defaultSeverity, messageId, parms, targetObject.getResource());
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, String[] parms, IResource targetObject) {
		return buildMessage(defaultSeverity, messageId, parms, targetObject, DEFAULT_TEXT_RANGE);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, JpaNode targetObject, TextRange textRange) {
		return buildMessage(defaultSeverity, messageId, targetObject.getResource(), textRange);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, IResource targetObject, TextRange textRange) {
		return buildMessage(defaultSeverity, messageId, DEFAULT_PARMS, targetObject, textRange);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, String[] parms, JpaNode targetObject, TextRange textRange) {
		return buildMessage(defaultSeverity, messageId, parms, targetObject.getResource(), textRange);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, String[] parms, IResource targetObject, TextRange textRange) {
		return buildMessage(defaultSeverity, messageId, parms, targetObject, textRange, MESSAGE_FACTORY);
	}

	public static IMessage buildMessage(int defaultSeverity, String messageId, String[] parms, IResource targetObject, TextRange textRange, MessageFactory messageFactory) {
		IMessage message = messageFactory.buildMessage(defaultSeverity, messageId, parms, targetObject);
		if (textRange == null) {
			textRange = DEFAULT_TEXT_RANGE;
			// log the exception but allow the message to still be used
			JptJpaCorePlugin.instance().logError(new NullPointerException("Null text range for message ID: " + messageId)); //$NON-NLS-1$
		}
		int lineNumber = textRange.getLineNumber();
		message.setLineNo(lineNumber);
		if (lineNumber == IMessage.LINENO_UNSET) {
			message.setAttribute(IMarker.LOCATION, " "); //$NON-NLS-1$
		}
		message.setOffset(textRange.getOffset());
		message.setLength(textRange.getLength());
		return message;
	}


	private DefaultJpaValidationMessages() {
		super();
		throw new UnsupportedOperationException();
	}


	// ********** message factory **********

	/**
	 * Allow clients to specify a message factory so the message can load the
	 * appropriate resource bundle.
	 */
	public interface MessageFactory {
		IMessage buildMessage(int severity, String messageId, String[] parms, IResource targetObject);
	}

	private static final MessageFactory MESSAGE_FACTORY = new GenericMessageFactory();

	/* CU private */ static class GenericMessageFactory
		implements MessageFactory
	{
		public IMessage buildMessage(int severity, String messageID, String[] parms, IResource targetObject) {
			// check for preference override
			severity = this.getProblemSeverity(targetObject, messageID, severity);
			IMessage message = new Message(JpaValidationMessages.BUNDLE_NAME, severity, messageID, parms, targetObject);
			message.setMarkerId(JpaValidator.MARKER_ID);
			return message;
		}

		private int getProblemSeverity(IResource targetObject, String messageID, int defaultSeverity) {
			String pref = JpaPreferences.getProblemSeverity(targetObject.getProject(), messageID);
			if (pref == null) {
				return defaultSeverity;
			}
			if (pref.equals(JpaPreferences.PROBLEM_ERROR)) {
				return IMessage.HIGH_SEVERITY;
			}
			if (pref.equals(JpaPreferences.PROBLEM_WARNING)) {
				return IMessage.NORMAL_SEVERITY;
			}
			if (pref.equals(JpaPreferences.PROBLEM_INFO)) {
				return IMessage.LOW_SEVERITY;
			}
			return defaultSeverity;
		}

		@Override
		public String toString() {
			return ObjectTools.toString(this);
		}
	}
}

Back to the top