KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import com.sun.enterprise.deployment.WebBundleDescriptor;
30 import com.sun.enterprise.tools.verifier.Context;
31 import com.sun.enterprise.tools.verifier.Result;
32 import com.sun.enterprise.tools.verifier.TagLibDescriptor;
33 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
34 import com.sun.enterprise.tools.verifier.web.TagDescriptor;
35
36
37 /**
38  * The name of tag must be unique.
39  * @author Sudipto Ghosh
40  */

41
42 public class TagNameIsUnique extends WebTest implements WebCheck {
43     boolean oneFailed=false;
44
45     public Result check(WebBundleDescriptor descriptor) {
46         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
47         Context context = getVerifierContext();
48         Result result = loadWarFile(descriptor);
49
50         TagLibDescriptor tlds[] = context.getTagLibDescriptors();
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         for(TagLibDescriptor tld : tlds) {
59             TagDescriptor[] tagDesc = tld.getTagDescriptors();
60             List JavaDoc<String JavaDoc> name = new ArrayList JavaDoc<String JavaDoc>();
61             for(TagDescriptor td : tagDesc) {
62                 name.add(td.getTagName());
63             }
64             if (name != null) {
65                 String JavaDoc[] names = (String JavaDoc[])name.toArray(new String JavaDoc[0]);
66                 if (!checkForDuplicateNames(result, compName, names, tld)) {
67                     addGoodDetails(result, compName);
68                     result.addGoodDetails(smh.getLocalString
69                             (getClass().getName() + ".passed1",
70                                     "All 'name' elements are defined properly under tag element of [ {0} ]",
71                                     new Object JavaDoc[]{tld.getUri()}));
72                 }
73             }
74         }
75         if(oneFailed){
76             result.setStatus(Result.FAILED);
77         } else {
78             result.setStatus(Result.PASSED);
79         }
80         return result;
81     }
82
83     public boolean checkForDuplicateNames(Result result, ComponentNameConstructor compName, String JavaDoc[] names, TagLibDescriptor tld) {
84         boolean duplicate = false;
85         for(int i=0; i<names.length-1;i++){
86             for (int j=i+1; j<names.length; j++) {
87                 duplicate = names[i].trim().equals(names[j]);
88                 if(duplicate) {
89                     oneFailed=true;
90                     addErrorDetails(result, compName);
91                     result.addErrorDetails(smh.getLocalString
92                             (getClass().getName() + ".failed",
93                                     "The name element value [ {0} ] under tag " +
94                             "element in [ {1} ] is not unique",
95                                     new Object JavaDoc[] {names[i], tld.getUri()}));
96                 }
97             }
98         }
99         return oneFailed;
100     }
101 }
102
Popular Tags