001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.validation;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.JavaCore;
017: import org.eclipse.jface.dialogs.IMessageProvider;
018: import org.eclipse.pde.core.plugin.IFragmentModel;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.core.plugin.PluginRegistry;
021: import org.eclipse.pde.internal.core.AbstractNLModel;
022: import org.eclipse.pde.internal.core.NLResourceHelper;
023: import org.eclipse.pde.internal.core.PDECore;
024: import org.eclipse.pde.internal.core.builders.CompilerFlags;
025: import org.eclipse.pde.internal.core.util.PDEJavaHelper;
026: import org.eclipse.pde.internal.core.util.VersionUtil;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.osgi.framework.InvalidSyntaxException;
029:
030: /**
031: * ControlValidationUtility
032: *
033: */
034: public class ControlValidationUtility {
035:
036: /**
037: * @param text
038: * @param validator
039: * @return
040: */
041: public static boolean validateRequiredField(String value,
042: IValidatorMessageHandler validator, int messageType) {
043: // Check to see if a value was specified
044: if (value.length() == 0) {
045: validator
046: .addMessage(
047: PDEUIMessages.ControlValidationUtility_errorMsgValueMustBeSpecified,
048: messageType);
049: return false;
050: }
051: return true;
052: }
053:
054: /**
055: * @param text
056: * @param validator
057: * @param required
058: * @param model
059: * @param project
060: * @return
061: */
062: public static boolean validateTranslatableField(String value,
063: IValidatorMessageHandler validator, IPluginModelBase model,
064: IProject project) {
065:
066: // Check the compiler flag and translate it into a message type
067: int messageType = AbstractControlValidator.getMessageType(
068: project, CompilerFlags.P_NOT_EXTERNALIZED);
069: // If the message type is none, no validation is required
070: // Same as IGNORE
071: if (messageType == IMessageProvider.NONE) {
072: return true;
073: }
074:
075: // Check to see if the name has been externalized
076: if (value.startsWith("%") == false) { //$NON-NLS-1$
077: validator
078: .addMessage(
079: PDEUIMessages.ControlValidationUtility_errorMsgValueNotExternalized,
080: messageType);
081: return false;
082: }
083:
084: // Check to see if the key is in the plugin's property file
085: if (model instanceof AbstractNLModel) {
086: NLResourceHelper helper = ((AbstractNLModel) model)
087: .getNLResourceHelper();
088: if ((helper == null)
089: || (helper.resourceExists(value) == false)) {
090: validator
091: .addMessage(
092: PDEUIMessages.ControlValidationUtility_errorMsgKeyNotFound,
093: messageType);
094: return false;
095: }
096: }
097: return true;
098: }
099:
100: /**
101: * @param text
102: * @param validator
103: * @param required
104: * @return
105: */
106: public static boolean validateVersionField(String value,
107: IValidatorMessageHandler validator) {
108: // Check for invalid version
109: IStatus status = VersionUtil.validateVersion(value);
110: if (status.isOK() == false) {
111: validator.addMessage(status.getMessage(),
112: AbstractControlValidator.getMessageType(status));
113: return false;
114: }
115: return true;
116: }
117:
118: /**
119: * @param text
120: * @param validator
121: * @param model
122: * @param project
123: */
124: public static boolean validatePlatformFilterField(String value,
125: IValidatorMessageHandler validator) {
126: // Check to see if the platform filter syntax is valid
127: try {
128: PDECore.getDefault().getBundleContext().createFilter(value);
129: } catch (InvalidSyntaxException ise) {
130: validator
131: .addMessage(
132: PDEUIMessages.ControlValidationUtility_errorMsgFilterInvalidSyntax,
133: IMessageProvider.ERROR);
134: return false;
135: }
136:
137: return true;
138: }
139:
140: /**
141: * @param text
142: * @param validator
143: * @param project
144: * @return
145: */
146: public static boolean validateActivatorField(String value,
147: IValidatorMessageHandler validator, IProject project) {
148:
149: // Check the compiler flag and translate it into a message type
150: int messageType = AbstractControlValidator.getMessageType(
151: project, CompilerFlags.P_UNKNOWN_CLASS);
152: // If the message type is none, no validation is required
153: // Same as IGNORE
154: if (messageType == IMessageProvider.NONE) {
155: return true;
156: }
157:
158: // Check to see if the class is on the plug-in classpath
159: try {
160: if (project.hasNature(JavaCore.NATURE_ID)) {
161: IJavaProject javaProject = JavaCore.create(project);
162: // Look for this activator in the project's classpath
163: if (!PDEJavaHelper.isOnClasspath(value, javaProject)) {
164: validator
165: .addMessage(
166: PDEUIMessages.ControlValidationUtility_errorMsgNotOnClasspath,
167: messageType);
168: return false;
169: }
170: }
171: } catch (CoreException ce) {
172: // Ignore
173: }
174:
175: return true;
176: }
177:
178: /**
179: * @param value
180: * @param validator
181: * @param model
182: * @param project
183: * @return
184: */
185: public static boolean validateFragmentHostPluginField(String value,
186: IValidatorMessageHandler validator, IProject project) {
187:
188: // Check the compiler flag and translate it into a message type
189: // If the message type is none, it is the same as IGNORE
190: int reqAttMessageType = AbstractControlValidator
191: .getMessageType(project,
192: CompilerFlags.P_NO_REQUIRED_ATT);
193: // Check to see if the host plug-in was defined
194: if ((reqAttMessageType != IMessageProvider.NONE)
195: && validateRequiredField(value, validator,
196: reqAttMessageType) == false) {
197: return false;
198: }
199: // Check the compiler flag and translate it into a message type
200: int unresImpMessageType = AbstractControlValidator
201: .getMessageType(project,
202: CompilerFlags.P_UNRESOLVED_IMPORTS);
203: // If the message type is none, no validation is required
204: // Same as IGNORE
205: if (unresImpMessageType == IMessageProvider.NONE) {
206: return true;
207: }
208: // Check to see if the host plugin is defined, enabled and not a
209: // fragment itself
210: IPluginModelBase hostModel = PluginRegistry.findModel(value);
211: if ((hostModel == null)
212: || (hostModel instanceof IFragmentModel)
213: || (hostModel.isEnabled() == false)) {
214: validator
215: .addMessage(
216: PDEUIMessages.ControlValidationUtility_errorMsgPluginUnresolved,
217: unresImpMessageType);
218: return false;
219: }
220:
221: return true;
222: }
223:
224: }
|