KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > codegen > ValidationUtil


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

23 package org.enhydra.tool.codegen;
24 //
25
import java.io.File JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * This class instantiates a set of default GeneratorOptions during
30  * initialization. The default options include: appname, d, package
31  * copywrite, copywritefile, nomake, noscript, and nojbuilder.
32  */

33 public class ValidationUtil {
34
35     /**
36      * Hidden constructor
37      */

38     private ValidationUtil() {}
39
40     /**
41      * Test to see if the given package is a valid java package name.
42      *
43      * @param pack
44      * The package name to test
45      *
46      * @return
47      * True if the specified package name is valid.
48      */

49     public static boolean isJavaPackage(String JavaDoc pack) {
50         final String JavaDoc DOT = (new String JavaDoc()) + '.';
51
52         boolean valid = true;
53         StringTokenizer JavaDoc tokenizer = null;
54
55         if (pack.trim().length() == 0) {
56           valid = false;
57         } else if (pack.trim().startsWith(DOT)
58                 || pack.trim().endsWith(DOT)) {
59             valid = false;
60         } else {
61             tokenizer = new StringTokenizer JavaDoc(pack, DOT);
62             while (tokenizer.hasMoreTokens()) {
63                 if (!ValidationUtil.isJavaIdentifier(tokenizer.nextToken())) {
64                     valid = false;
65                     break;
66                 }
67             }
68         }
69         return valid;
70     }
71
72     /**
73      * Test to see if the given indentifier is a valid java identifier.
74      *
75      * @param ident
76      * The identifier to test.
77      *
78      * @return
79      * True if the specified identifier is valid.
80      */

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

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

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