1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.validators;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import org.eclipse.jface.dialogs.IInputValidator;
20 * Regular expression validator
22 * @author Toni Kalajainen
24 public class RegexpValidator implements IInputValidator {
27 //^([a-zA-Z0-9\\*]([a-zA-Z0-9\\-\\*]{0,61}[a-zA-Z0-9\\*])?\\.)*[a-zA-Z0-9\\*]([a-zA-Z0-9\\-\\*]{0,61}[a-zA-Z0-9\\*])?$
30 //public static final String URL_VALIDATOR_STRING = "^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\\?\\/.=]+$";
31 //public static RegexpValidator URL_VALIDATOR = new RegexpValidator(URL_VALIDATOR_STRING, "Invalid URL");
33 private final Pattern pattern;
34 private final String errorMsg;
36 public RegexpValidator(String regexp, String errorMsg) {
37 pattern = Pattern.compile(regexp);
38 this.errorMsg = errorMsg;
41 public RegexpValidator(Pattern pattern, String errorMsg) {
42 this.pattern = pattern;
43 this.errorMsg = errorMsg;
46 public String isValid(String newText) {
47 Matcher m = pattern.matcher(newText);
48 if (m.matches()) return null;