KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > TaglibFunctionSignatureIsValid


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.tools.verifier.tests.web;
25
26
27 import com.sun.enterprise.tools.verifier.tests.TagLibTest;
28 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
29 import com.sun.enterprise.tools.verifier.Result;
30 import com.sun.enterprise.tools.verifier.Context;
31 import com.sun.enterprise.tools.verifier.TagLibDescriptor;
32 import com.sun.enterprise.tools.verifier.web.FunctionDescriptor;
33 import com.sun.enterprise.deployment.WebBundleDescriptor;
34
35 /**
36  * The function-signature must be specified using a fully-qualified return type
37  * followed by the method name, followed by the fully-qualified argument types
38  * in parenthesis, separated by commas.
39  *
40  * @author Sudipto Ghosh
41  */

42 public class TaglibFunctionSignatureIsValid extends TagLibTest implements WebCheck {
43     public Result check(WebBundleDescriptor descriptor) {
44         ComponentNameConstructor compName =
45                 getVerifierContext().getComponentNameConstructor();
46         Result result = getInitializedResult();
47         Context context = getVerifierContext();
48         TagLibDescriptor tlds[] = context.getTagLibDescriptors();
49         FunctionDescriptor[] fnDesc = null;
50
51         if (tlds == null) {
52             addGoodDetails(result, compName);
53             result.passed(smh.getLocalString
54                     (getClass().getName() + ".passed",
55                             "No tag lib files are specified"));
56             return result;
57         }
58
59         for (TagLibDescriptor tld : tlds) {
60             if (tld.getSpecVersion().compareTo("2.0") >= 0) {
61                 fnDesc = tld.getFunctionDescriptors();
62                 if (fnDesc != null)
63                     for (FunctionDescriptor fd : fnDesc)
64                         checkSignature(result, fd, tld, compName);
65             }
66         }
67         if (result.getStatus() != Result.FAILED) {
68             addGoodDetails(result, compName);
69             result.passed(smh.getLocalString(getClass()
70                     .getName() +
71                     ".passed", "function-signature element of the tag lib " +
72                     "descriptor are properly defined."));
73         }
74         return result;
75     }
76
77     /**
78      * Checks the validity of the signature string contained in function-signature
79      * object
80      * @param result
81      * @param fnDesc
82      * @param tld
83      * @param compName
84      */

85     private void checkSignature(Result result, FunctionDescriptor fnDesc,
86                                       TagLibDescriptor tld,
87                                       ComponentNameConstructor compName) {
88         String JavaDoc signature = fnDesc.getFunctionSignature();
89         ClassLoader JavaDoc cl = getVerifierContext().getClassLoader();
90         String JavaDoc retType = getRetType(signature);
91         String JavaDoc[] parameter = getParameters(signature);
92         if (checkIfPrimitive(retType) == null && !checkValidRType(retType)) {
93             addErrorDetails(result, compName);
94             result.failed(smh.getLocalString(getClass().getName() +
95                     ".failed",
96                     "ERROR: In the tld [ {0} ] return type is not specified correctly in " +
97                     "this signature [ {1} ]",
98                      new Object JavaDoc[]{tld.getUri(), signature}));
99         }
100         //parameter is a basic type or fully qualified Type
101
if(!checkParamTypeClass(parameter, cl)) {
102             addErrorDetails(result, compName);
103             result.failed(smh.getLocalString(getClass().getName() +
104                     ".failed1",
105                     "ERROR: In the tld [ {0} ] parameters are not specified correctly in " +
106                     "this signature [ {1} ]",
107                      new Object JavaDoc[]{tld.getUri(), signature}));
108         }
109     }
110
111     /**
112      *
113      * @param retType
114      * @return true if the return type is specified correctly, false otherwise
115      */

116     private boolean checkValidRType(String JavaDoc retType) {
117         boolean valid = true;
118         try {
119             Class.forName(retType);
120         } catch (ClassNotFoundException JavaDoc e) {
121              return valid=false;
122         }
123         return valid;
124     }
125
126     /**
127      * return true, if all the parameters specified by par String[] are correctly
128      * specified, false otherwise.
129      * @param par
130      * @param cl
131      * @return
132      */

133     private boolean checkParamTypeClass(String JavaDoc[] par, ClassLoader JavaDoc cl) {
134         for(String JavaDoc s : par) {
135             Class JavaDoc c = checkIfPrimitive(s);
136             if (c == null)
137                 try {
138                     c = Class.forName(s, false, cl);
139                 } catch (ClassNotFoundException JavaDoc e) {
140                     return false;
141                 }
142         }
143         return true;
144     }
145 }
146
Popular Tags