KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > AttrClassName


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.io.File JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 /**
30     Class which contains Meta data for all types of attributes which is present in Validation Descriptor
31  * XML File
32  *
33  * Sample
34  * <attribute name=<Name> type="address" />
35  * <attribute name=<Name> type="integer" range="low,high" />
36  * <attribute name=<Name> type="string" max-length="length" />
37     
38     @author Srinivas Krishnan
39     @version 2.0
40 */

41
42 /* Class for attribute type file */
43
44 public class AttrClassName extends AttrType {
45     
46     public AttrClassName(String JavaDoc name, String JavaDoc type, boolean optional) {
47         super(name,type, optional);
48     }
49     
50     public void validate(Object JavaDoc o, ValidationContext valCtx) {
51         _logger.log(Level.CONFIG, "Testing attr: "+valCtx.attrName);
52
53     super.validate(o, valCtx); // call to common validator first
54
if(o == null)
55         return;
56         if(o.equals(""))
57             valCtx.result.failed(valCtx.smh.getLocalString(getClass().getName() + ".nullClassName",
58                                         "Attribute({0}=\"\" : ClassName not Valid", new Object JavaDoc[] {valCtx.attrName}));
59    
60         // Remove the package, extract the identifier alone
61
String JavaDoc className = (String JavaDoc)o;
62         int classStart = className.lastIndexOf(".");
63         if(classStart != -1)
64             className = className.substring(classStart+1);
65         if(!isValidClassName(className)){
66             valCtx.result.failed(valCtx.smh.getLocalString(getClass().getName() + ".invalidClassName",
67                                         "Attribute({0}={1}) : ClassName not Valid", new Object JavaDoc[] {valCtx.attrName, (String JavaDoc)o}));
68         }
69     }
70     
71     public static boolean isValidClassName(String JavaDoc className) {
72         boolean valid = true;
73         for(int i=0;i<className.length();i++) {
74             if(i == 0) {
75                 if(!Character.isJavaIdentifierStart(className.charAt(i)))
76                     valid = false;
77             }
78             if(!Character.isJavaIdentifierPart(className.charAt(i)))
79                 valid = false;
80         }
81         return valid;
82     }
83 }
84
Popular Tags