KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > SectionValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
21
22 import org.netbeans.modules.xml.multiview.Error;
23 import org.netbeans.modules.j2ee.dd.api.web.*;
24 import org.openide.util.NbBundle;
25
26 /** SectionValidator.java
27  *
28  * Created on February 9, 2005, 4:57 PM
29  * @author mkuchtiak
30  */

31 public class SectionValidator {
32
33     static Error JavaDoc validateServlets(WebApp webApp) {
34         Servlet[] servlets = webApp.getServlet();
35         Error JavaDoc error=null;
36         for (int i=0;i<servlets.length;i++) {
37             // valiadation for missing servlet-name
38
String JavaDoc name = servlets[i].getServletName();
39             if (name==null || name.length()==0) {
40                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(servlets[i],"ServletName"); //NOI18N
41
error = new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "Servlet Name", loc);
42                 break;
43             }
44             // valiadation for missing servlet-class/jsp-file
45
name = servlets[i].getServletClass();
46             if (name==null) name = servlets[i].getJspFile();
47             if (name==null || name.length()==0) {
48                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(servlets[i],"ServletClass"); //NOI18N
49
error = new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "Servlet Class", loc);
50                 break;
51             }
52             // validation for duplicite url-patterns
53
String JavaDoc[] urlPatterns = DDUtils.getUrlPatterns(webApp,servlets[i]);
54             for (int j=0;j<urlPatterns.length;j++) {
55                 String JavaDoc errMessage = DDUtils.checkServletMappig(urlPatterns[j]);
56                 if (errMessage!=null) {
57                     Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(servlets[i],"ServletMapping"); //NOI18N
58
error = new Error JavaDoc(Error.ERROR_MESSAGE, errMessage , loc);
59                     break;
60                 }
61                 if (DDUtils.isServletMapping(webApp, servlets[i], urlPatterns[j])) {
62                     Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(servlets[i],"ServletMapping"); //NOI18N
63
error = new Error JavaDoc(Error.DUPLICATE_VALUE_MESSAGE, urlPatterns[j] , loc);
64                     break;
65                 }
66             }
67         }
68         return error;
69     }
70     
71     static Error JavaDoc validateFilters(WebApp webApp) {
72         Filter[] filters = webApp.getFilter();
73         Error JavaDoc error=null;
74         for (int i=0;i<filters.length;i++) {
75             // valiadation for missing filter-name
76
String JavaDoc name = filters[i].getFilterName();
77             if (name==null || name.length()==0) {
78                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(filters[i],"FilterName"); //NOI18N
79
error = new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "Filter Name", loc);
80                 break;
81             }
82             // valiadation for missing filter-class
83
name = filters[i].getFilterClass();
84             if (name==null || name.length()==0) {
85                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(filters[i],"FilterClass"); //NOI18N
86
error = new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "Filter Class", loc);
87                 break;
88             }
89         }
90         return error;
91     }
92     
93     static Error JavaDoc validatePages(WebApp webApp) {
94         Error JavaDoc error=null;
95         JspConfig jspConfig=null;
96         JspPropertyGroup[] groups=null;
97         try {
98             jspConfig = webApp.getSingleJspConfig();
99         } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {
100             return null;
101         }
102         if (jspConfig==null) {
103             return null;
104         } else groups = jspConfig.getJspPropertyGroup();
105         if (groups==null) return null;
106         
107         // validation for missing url-patern(s)
108
for (int i=0;i<groups.length;i++) {
109             if (groups[i].sizeUrlPattern()==0) {
110                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(groups[i],"url_patterns"); //NOI18N
111
error = new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "URL Pattern", loc);
112                 break;
113             }
114         }
115         return error;
116     }
117     
118     static String JavaDoc validateNewServlet(WebApp webApp, String JavaDoc servletName, String JavaDoc servletClass, String JavaDoc jspFile, String JavaDoc urlPatterns) {
119         if (servletName.length()==0) {
120             return NbBundle.getMessage(SectionValidator.class,"TXT_EmptyServletName");
121         } else {
122             Servlet[] servlets = webApp.getServlet();
123             boolean exists=false;
124             for (int i=0;i<servlets.length;i++) {
125                 if (servletName.equals(servlets[i].getServletName())) {
126                     exists=true;
127                     break;
128                 }
129             }
130             if (exists) {
131                 return NbBundle.getMessage(SectionValidator.class,"TXT_ServletNameExists",servletName);
132             }
133         }
134         if (servletClass.length()==0 && jspFile.length()==0) {
135             return NbBundle.getMessage(SectionValidator.class,"TXT_Servlet_BothMissing");
136         }
137         if (servletClass.length()>0 && jspFile.length()>0) {
138             return NbBundle.getMessage(SectionValidator.class,"TXT_Servlet_BothSpecified");
139         }
140         String JavaDoc[] patterns = DDUtils.getStringArray(urlPatterns);
141         if (patterns.length>0) {
142             for (int i=0;i<patterns.length;i++) {
143                 String JavaDoc errMessage = DDUtils.checkServletMappig(patterns[i]);
144                 if (errMessage!=null) return errMessage;
145                 if (DDUtils.isServletMapping(webApp,patterns[i])) {
146                     return NbBundle.getMessage(SectionValidator.class,"TXT_UrlPatternExists",patterns[i]);
147                 }
148             }
149         }
150         return null;
151     }
152     
153     static String JavaDoc validateNewFilter(WebApp webApp, String JavaDoc filterName, String JavaDoc filterClass) {
154         if (filterName.length()==0) {
155             return NbBundle.getMessage(SectionValidator.class,"TXT_EmptyFilterName");
156         } else {
157             Filter[] filters = webApp.getFilter();
158             boolean exists=false;
159             for (int i=0;i<filters.length;i++) {
160                 if (filterName.equals(filters[i].getFilterName())) {
161                     exists=true;
162                     break;
163                 }
164             }
165             if (exists) {
166                 return NbBundle.getMessage(SectionValidator.class,"TXT_FilterNameExists",filterName);
167             }
168         }
169         if (filterClass.length()==0) {
170             return NbBundle.getMessage(SectionValidator.class,"TXT_EmptyFilterClass");
171         }
172         return null;
173     }
174     
175     static String JavaDoc validateNewJspPropertyGroup(String JavaDoc urlPatterns) {
176         String JavaDoc[] patterns = DDUtils.getStringArray(urlPatterns);
177         if (patterns.length==0) {
178             return NbBundle.getMessage(SectionValidator.class,"TXT_EmptyUrls");
179         }
180         return null;
181     }
182 }
183
Popular Tags