KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > ValidationUtil


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.kelp.common;
23 import java.io.File JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 //
26
public class ValidationUtil {
27
28     /**
29      * Hidden constructor
30      */

31     private ValidationUtil() {}
32
33     /**
34      * Test to see if the given package is a valid java package name.
35      *
36      * @param pack
37      * The package name to test
38      *
39      * @return
40      * True if the specified package name is valid.
41      */

42     public static boolean isJavaPackage(String JavaDoc pack) {
43         boolean valid = true;
44         StringTokenizer JavaDoc tokenizer = null;
45
46         if (pack == null) {
47             pack = new String JavaDoc();
48         }
49         pack = pack.trim();
50         if (pack.length() == 0) {
51             valid = false;
52         } else if (pack.charAt(0) == '.') {
53             valid = false;
54         } else if (pack.charAt(pack.length() - 1) == '.') {
55             valid = false;
56         } else {
57             // string not to be resourced
58
final String JavaDoc DELIM = "."; // nores
59
tokenizer = new StringTokenizer JavaDoc(pack, DELIM);
60             while (tokenizer.hasMoreTokens()) {
61                 if (!ValidationUtil.isJavaIdentifier(tokenizer.nextToken())) {
62                     valid = false;
63                     break;
64                 }
65             }
66         }
67         return valid;
68     }
69
70     /**
71      * Test to see if the given indentifier is a valid java identifier.
72      *
73      * @param ident
74      * The identifier to test.
75      *
76      * @return
77      * True if the specified identifier is valid.
78      */

79     public static boolean isJavaIdentifier(String JavaDoc ident) {
80         boolean valid = true;
81
82         for (int i = 0; i < ident.length(); i++) {
83             char ch = ident.charAt(i);
84
85             if (i == 0) {
86                 if (!Character.isJavaIdentifierStart(ch)) {
87                     valid = false;
88                     break;
89                 }
90             } else if (!Character.isJavaIdentifierPart(ch)) {
91                 valid = false;
92                 break;
93             }
94         }
95         return valid;
96     }
97
98     /**
99      * Test to see if a path is a directory.
100      *
101      * @param path
102      * The path to test.
103      *
104      * @return
105      * True if the specified path is a directory.
106      */

107     public static boolean isDirectory(String JavaDoc path) {
108         File JavaDoc file = new File JavaDoc(path);
109
110         return ValidationUtil.isDirectory(file);
111     }
112
113     public static boolean isDirectory(File JavaDoc file) {
114         boolean valid = false;
115
116         if (file != null) {
117             valid = file.isDirectory();
118         }
119         return valid;
120     }
121
122     /**
123      * Test to see if the parent of a given path is a valid directory.
124      *
125      * @param path
126      * The path to test.
127      *
128      * @return
129      * True if the parent of the specified path is a directory.
130      */

131     public static boolean isParentDirectory(String JavaDoc path) {
132         boolean valid = false;
133         File JavaDoc file = new File JavaDoc(path);
134
135         if (file != null && file.getParentFile() != null) {
136             valid = file.getParentFile().isDirectory();
137         }
138         return valid;
139     }
140
141 }
142
Popular Tags