KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > store > BookStoreUtil


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.books.store;
17
18 import java.util.BitSet JavaDoc;
19
20 public class BookStoreUtil {
21     private static BitSet JavaDoc legalFileNameChars;
22     static {
23         legalFileNameChars = new BitSet JavaDoc(256);
24         for (char x = 'a'; x <= 'z'; x++)
25             legalFileNameChars.set(x);
26         for (char x = '0'; x <= '9'; x++)
27             legalFileNameChars.set(x);
28         legalFileNameChars.set('_');
29         legalFileNameChars.set('-');
30         legalFileNameChars.set(' ');
31         legalFileNameChars.set(',');
32     }
33
34     public static String JavaDoc isValidBookInstanceName(String JavaDoc name) {
35         if (name.length() > 255)
36             return "Name too long (maximum 255 characters allowed).";
37
38         for (int i = 0; i < name.length(); i++) {
39             char c = name.charAt(i);
40             if (!legalFileNameChars.get(c))
41                 return "Name contains non-allowed characters: \"" + c + "\".";
42         }
43         return null;
44     }
45
46     public static String JavaDoc fixIllegalFileNameCharacters(String JavaDoc name) {
47         StringBuffer JavaDoc fixedName = new StringBuffer JavaDoc(name.length());
48         for (int i = 0; i < name.length(); i++) {
49             char c = name.charAt(i);
50             if (Character.isWhitespace(c)) {
51                 fixedName.append('_');
52             } else if (legalFileNameChars.get(c)) {
53                 fixedName.append(c);
54             }
55             // other characters are skipped
56
}
57         return fixedName.toString();
58     }
59
60 }
61
Popular Tags