This project has retired. For details please refer to its Attic page.
NameValidator xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.chemistry.opencmis.inmemory;
21  
22  public class NameValidator {
23  
24      public static final String ERROR_ILLEGAL_ID = "Id contains illegal characters, allowed are 'a'..'z', 'A'..'Z', '0'..'9', '-', '_'";
25      public static final String ERROR_ILLEGAL_NAME = "Name contains illegal characters, allowed are 'a'..'z', 'A'..'Z', '0'..'9', '-', '_', '.', ' '";
26  
27      // Utility class
28      private NameValidator() {
29      }
30  
31      /**
32       * check whether id contains only valid characters Allowed are 'a'..'z',
33       * 'A'..'Z', '0'..'9', '.', '-', ' ', '_';
34       *
35       * @param s
36       *            string to verify
37       */
38      public static boolean isValidId(String s) {
39          if (null == s || s.length() == 0) {
40              return false;
41          }
42  
43          if (s.startsWith("cmis:")) {
44              s = s.substring(5);
45          }
46  
47          for (int i = 0; i < s.length(); i++) {
48              char c = s.charAt(i);
49              if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '-'
50                      || c == '_' || c == ' ')) {
51                  return false;
52              }
53          }
54          return true;
55      }
56  
57      public static boolean isValidLocalName(String s) {
58          return isValidId(s);
59      }
60  
61      /**
62       * check whether id contains only valid characters Allowed are 'a'..'z',
63       * 'A'..'Z', '0'..'9', '.', '-', ' ', '_';
64       *
65       * @param s
66       *            string to verify
67       */
68      public static boolean isValidName(String s) {
69          if (null == s || s.length() == 0) {
70              return false;
71          }
72          for (int i = 0; i < s.length(); i++) {
73              char c = s.charAt(i);
74              if (c == '\\' || c == '/' || c == '\'' || c == '\"' || c == ':' || c == '*' || 
75                      c == '?' ||c == '<' || c == '>' && c == '|')
76                  return false;
77          }
78          return true;
79      }
80  
81      public static boolean isValidNamespace(String s) {
82          return isValidId(s);
83      }
84  
85      public static boolean isValidQueryName(String s) {
86          return isValidId(s);
87      }
88  
89  }