]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/validators/RegexpValidator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / validators / RegexpValidator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.validators;
13
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.eclipse.jface.dialogs.IInputValidator;
18
19 /**
20  * Regular expression validator
21  * 
22  * @author Toni Kalajainen
23  */
24 public class RegexpValidator implements IInputValidator {
25
26     // Hostname mask
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\\*])?$
28     
29     // Untested
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");
32     
33     private final Pattern pattern;
34     private final String errorMsg;
35     
36     public RegexpValidator(String regexp, String errorMsg) {
37         pattern = Pattern.compile(regexp);
38         this.errorMsg = errorMsg;
39     }
40     
41     public RegexpValidator(Pattern pattern, String errorMsg) {
42         this.pattern = pattern;
43         this.errorMsg = errorMsg;
44     }
45
46     public String isValid(String newText) {
47         Matcher m = pattern.matcher(newText);
48         if (m.matches()) return null;
49         return errorMsg;
50     }
51
52 }