1 11 12 package org.eclipse.pde.internal.ui.editor.validation; 13 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.jdt.core.IJavaProject; 18 import org.eclipse.jdt.core.JavaCore; 19 import org.eclipse.jface.dialogs.IMessageProvider; 20 import org.eclipse.pde.core.plugin.IFragmentModel; 21 import org.eclipse.pde.core.plugin.IPluginModelBase; 22 import org.eclipse.pde.core.plugin.PluginRegistry; 23 import org.eclipse.pde.internal.core.AbstractNLModel; 24 import org.eclipse.pde.internal.core.NLResourceHelper; 25 import org.eclipse.pde.internal.core.PDECore; 26 import org.eclipse.pde.internal.core.builders.CompilerFlags; 27 import org.eclipse.pde.internal.core.util.PDEJavaHelper; 28 import org.eclipse.pde.internal.core.util.VersionUtil; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 30 import org.osgi.framework.InvalidSyntaxException; 31 32 36 public class ControlValidationUtility { 37 38 43 public static boolean validateRequiredField(String value, 44 IValidatorMessageHandler validator, int messageType) { 45 if (value.length() == 0) { 47 validator.addMessage( 48 PDEUIMessages.ControlValidationUtility_errorMsgValueMustBeSpecified, 49 messageType); 50 return false; 51 } 52 return true; 53 } 54 55 63 public static boolean validateTranslatableField(String value, 64 IValidatorMessageHandler validator, 65 IPluginModelBase model, IProject project) { 66 67 int messageType = AbstractControlValidator.getMessageType(project, 69 CompilerFlags.P_NOT_EXTERNALIZED); 70 if (messageType == IMessageProvider.NONE) { 73 return true; 74 } 75 76 if (value.startsWith("%") == false) { validator.addMessage( 79 PDEUIMessages.ControlValidationUtility_errorMsgValueNotExternalized, 80 messageType); 81 return false; 82 } 83 84 if (model instanceof AbstractNLModel) { 86 NLResourceHelper helper = ((AbstractNLModel)model).getNLResourceHelper(); 87 if ((helper == null) || 88 (helper.resourceExists(value) == false)) { 89 validator.addMessage( 90 PDEUIMessages.ControlValidationUtility_errorMsgKeyNotFound, 91 messageType); 92 return false; 93 } 94 } 95 return true; 96 } 97 98 104 public static boolean validateVersionField(String value, 105 IValidatorMessageHandler validator) { 106 IStatus status = VersionUtil.validateVersion(value); 108 if (status.isOK() == false) { 109 validator.addMessage( 110 status.getMessage(), 111 AbstractControlValidator.getMessageType(status)); 112 return false; 113 } 114 return true; 115 } 116 117 123 public static boolean validatePlatformFilterField(String value, 124 IValidatorMessageHandler validator) { 125 try { 127 PDECore.getDefault().getBundleContext().createFilter(value); 128 } catch (InvalidSyntaxException ise) { 129 validator.addMessage( 130 PDEUIMessages.ControlValidationUtility_errorMsgFilterInvalidSyntax, 131 IMessageProvider.ERROR); 132 return false; 133 } 134 135 return true; 136 } 137 138 144 public static boolean validateActivatorField(String value, 145 IValidatorMessageHandler validator, IProject project) { 146 147 int messageType = AbstractControlValidator.getMessageType(project, 149 CompilerFlags.P_UNKNOWN_CLASS); 150 if (messageType == IMessageProvider.NONE) { 153 return true; 154 } 155 156 try { 158 if (project.hasNature(JavaCore.NATURE_ID)) { 159 IJavaProject javaProject = JavaCore.create(project); 160 if (!PDEJavaHelper.isOnClasspath(value, javaProject)) { 162 validator.addMessage( 163 PDEUIMessages.ControlValidationUtility_errorMsgNotOnClasspath, 164 messageType); 165 return false; 166 } 167 } 168 } catch (CoreException ce) { 169 } 171 172 return true; 173 } 174 175 182 public static boolean validateFragmentHostPluginField(String value, 183 IValidatorMessageHandler validator, IProject project) { 184 185 int reqAttMessageType = AbstractControlValidator.getMessageType(project, 188 CompilerFlags.P_NO_REQUIRED_ATT); 189 if ((reqAttMessageType != IMessageProvider.NONE) && 191 validateRequiredField(value, validator, reqAttMessageType) == false) { 192 return false; 193 } 194 int unresImpMessageType = AbstractControlValidator.getMessageType(project, 196 CompilerFlags.P_UNRESOLVED_IMPORTS); 197 if (unresImpMessageType == IMessageProvider.NONE) { 200 return true; 201 } 202 IPluginModelBase hostModel = PluginRegistry.findModel(value); 205 if ((hostModel == null) || 206 (hostModel instanceof IFragmentModel) || 207 (hostModel.isEnabled() == false)) { 208 validator.addMessage( 209 PDEUIMessages.ControlValidationUtility_errorMsgPluginUnresolved, 210 unresImpMessageType); 211 return false; 212 } 213 214 return true; 215 } 216 217 } 218 | Popular Tags |