Issue 507 remove company
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / Validators.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.web.generator.client;
18
19 /**
20  * Helpers methods to check for phone numbers, email addresses, and URL. Other
21  * general purpose check methods should go here as well.
22  * 
23  * @author Yohann Coppel
24  */
25 public final class Validators {
26   public static String filterNumber(String number) {
27     return number.replaceAll("[ \\.,\\-\\(\\)]", "");
28   }
29   
30   public static void validateNumber(String number) throws GeneratorException {    
31     if (!number.matches("\\+?[0-9]+")) {
32       throw new GeneratorException("Phone number must be digits only.");
33     }
34   }
35   
36   public static void validateUrl(String url) throws GeneratorException {
37     if (!isBasicallyValidURI(url)) {
38       throw new GeneratorException("URL is not valid.");
39     }
40   }
41
42   private static boolean isBasicallyValidURI(String uri) {
43     if (uri == null || uri.indexOf(' ') >= 0 || uri.indexOf('\n') >= 0) {
44       return false;
45     }
46     int period = uri.indexOf('.');
47     // Look for period in a domain but followed by at least a two-char TLD
48     if (period >= uri.length() - 2) {
49       return false;
50     }
51     return period >= 0 || uri.indexOf(':') >= 0;
52   }
53   
54   public static void validateEmail(String email) throws GeneratorException {
55     //FIXME: we can have a better check for email here.
56     if (!email.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$")) {
57       throw new GeneratorException("Email is not valid.");
58     }
59   }
60
61 }