KickJava   Java API By Example, From Geeks To Geeks.

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


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.j2ee.dd.api.web.*;
23 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
24 import org.netbeans.modules.web.api.webmodule.WebModule;
25 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
26 import org.netbeans.api.project.*;
27 import org.netbeans.api.java.project.JavaProjectConstants;
28 import org.openide.loaders.DataObject;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.util.NbBundle;
32
33 /**
34  * @author mkuchtiak
35  */

36 public class DDUtils {
37     
38     public static String JavaDoc[] getUrlPatterns(WebApp webApp, Servlet servlet) {
39         if (servlet.getServletName()==null) return new String JavaDoc[]{};
40         ServletMapping[] mapping = webApp.getServletMapping();
41         java.util.List JavaDoc maps = new java.util.ArrayList JavaDoc();
42         for (int i=0;i<mapping.length;i++) {
43             if (servlet.getServletName().equals(mapping[i].getServletName())) {
44                 String JavaDoc urlPattern = mapping[i].getUrlPattern();
45                 if (urlPattern!=null) maps.add(urlPattern);
46             }
47         }
48         String JavaDoc[] urlPatterns = new String JavaDoc[maps.size()];
49         maps.toArray(urlPatterns);
50         return urlPatterns;
51     }
52     
53     public static String JavaDoc[] getUrlPatterns(WebApp webApp, Filter filter) {
54         if (filter.getFilterName()==null) return new String JavaDoc[]{};
55         FilterMapping[] mapping = webApp.getFilterMapping();
56         java.util.List JavaDoc maps = new java.util.ArrayList JavaDoc();
57         for (int i=0;i<mapping.length;i++) {
58             if (filter.getFilterName().equals(mapping[i].getFilterName())) {
59                 String JavaDoc urlPattern = mapping[i].getUrlPattern();
60                 if (urlPattern!=null) maps.add(urlPattern);
61                 else {
62                     String JavaDoc servletName = mapping[i].getServletName();
63                     if (servletName!=null) maps.add(servletName);
64                 }
65             }
66         }
67         String JavaDoc[] urlPatterns = new String JavaDoc[maps.size()];
68         maps.toArray(urlPatterns);
69         return urlPatterns;
70     }
71     
72     public static String JavaDoc[] getStringArray(String JavaDoc text) {
73         java.util.StringTokenizer JavaDoc tok = new java.util.StringTokenizer JavaDoc(text,",");
74         java.util.Set JavaDoc set = new java.util.HashSet JavaDoc();
75         while (tok.hasMoreTokens()) {
76             String JavaDoc token = tok.nextToken().trim();
77             if (token.length()>0) set.add(token);
78         }
79         String JavaDoc[] stringArray = new String JavaDoc[set.size()];
80         set.toArray(stringArray);
81         return stringArray;
82     }
83     
84     public static boolean isServletMapping(WebApp webApp, String JavaDoc urlPattern) {
85         return webApp.findBeanByName("ServletMapping","UrlPattern",urlPattern)!=null;
86     }
87     
88     public static boolean isServletMapping(WebApp webApp, Servlet servlet, String JavaDoc urlPattern) {
89         ServletMapping[] maps = webApp.getServletMapping();
90         String JavaDoc servletName = servlet.getServletName();
91         if (servletName!=null) {
92             for (int i=0;i<maps.length;i++) {
93                 if (urlPattern.equals(maps[i].getUrlPattern()) && !servletName.equals(maps[i].getServletName()) ) {
94                     return true;
95                 }
96             }
97         }
98         return false;
99     }
100     
101     public static String JavaDoc urlPatternList(String JavaDoc[] urlPatterns) {
102         if (urlPatterns==null) return "";
103         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
104         for (int i=0;i<urlPatterns.length;i++) {
105             if (i>0) buf.append(", "); //NOI18N
106
buf.append(urlPatterns[i]);
107         }
108         return buf.toString();
109     }
110     
111     public static void addServletMappings(WebApp webApp, Servlet servlet, String JavaDoc[] urlPatterns) {
112         String JavaDoc servletName = servlet.getServletName();
113         try {
114             for (int i=0;i<urlPatterns.length;i++) {
115                 ServletMapping map = (ServletMapping)webApp.createBean("ServletMapping"); //NOI18
116
map.setServletName(servletName);
117                 map.setUrlPattern(urlPatterns[i]);
118                 webApp.addServletMapping(map);
119             }
120         } catch (ClassNotFoundException JavaDoc ex){}
121     }
122     
123     public static void setServletMappings(WebApp webApp, Servlet servlet, String JavaDoc[] urlPatterns) {
124         String JavaDoc servletName = servlet.getServletName();
125         java.util.List JavaDoc oldMaps = getServletMappingList(webApp,servlet);
126         java.util.List JavaDoc newPatterns = new java.util.ArrayList JavaDoc();
127         // looking for old mappings
128
for (int i=0;i<urlPatterns.length;i++) {
129             boolean found =false;
130             for (int j=0;j<oldMaps.size();j++) {
131                 ServletMapping oldMap = (ServletMapping)oldMaps.get(j);
132                 if (urlPatterns[i].equals(oldMap.getUrlPattern())) {
133                     oldMaps.remove(oldMap);
134                     found=true;
135                     break;
136                 }
137             }
138             if (!found) newPatterns.add(urlPatterns[i]);
139         }
140         int min = java.lang.Math.min(oldMaps.size(),newPatterns.size());
141         // replace old mappings
142
for (int i=0;i<min;i++) {
143             ServletMapping oldMap = (ServletMapping)oldMaps.get(i);
144             oldMap.setUrlPattern((String JavaDoc)newPatterns.get(i));
145         }
146         // add new mappings
147
try {
148             for (int i=min;i<newPatterns.size();i++) {
149                 ServletMapping map = (ServletMapping)webApp.createBean("ServletMapping"); //NOI18
150
map.setServletName(servletName);
151                 map.setUrlPattern((String JavaDoc)newPatterns.get(i));
152                 webApp.addServletMapping(map);
153             }
154         } catch (ClassNotFoundException JavaDoc ex){}
155         // removing old mappings
156
for (int i=min;i<oldMaps.size();i++) {
157             webApp.removeServletMapping((ServletMapping)oldMaps.get(i));
158         }
159     }
160     
161     public static ServletMapping[] getServletMappings(WebApp webApp, Servlet servlet) {
162         java.util.List JavaDoc maps = getServletMappingList(webApp,servlet);
163         ServletMapping[] newMappings = new ServletMapping[maps.size()];
164         maps.toArray(newMappings);
165         return newMappings;
166     }
167     
168     public static FilterMapping[] getFilterMappings(WebApp webApp, Filter filter) {
169         java.util.List JavaDoc maps = getFilterMappingList(webApp,filter);
170         FilterMapping[] newMappings = new FilterMapping[maps.size()];
171         maps.toArray(newMappings);
172         return newMappings;
173     }
174     
175     /**
176      * @return filter mappings that refer to given <code>servlet</code>.
177      */

178     public static FilterMapping[] getFilterMappings(WebApp webApp, Servlet servlet) {
179         java.util.List JavaDoc maps = new java.util.ArrayList JavaDoc();
180         if (servlet == null){
181             return new FilterMapping[0];
182         }
183         FilterMapping[] mapping = webApp.getFilterMapping();
184         for (int i=0;i<mapping.length;i++) {
185             FilterMapping fm = mapping[i];
186             if (fm.getServletName() != null && fm.getServletName().equals(servlet.getServletName())){
187                 maps.add(fm);
188             }
189         }
190         return (FilterMapping[]) maps.toArray(new FilterMapping[maps.size()]);
191     }
192     
193     private static java.util.List JavaDoc getServletMappingList(WebApp webApp, Servlet servlet) {
194         String JavaDoc servletName = servlet.getServletName();
195         java.util.List JavaDoc maps = new java.util.ArrayList JavaDoc();
196         if (servletName==null) return maps;
197         ServletMapping[] mapping = webApp.getServletMapping();
198         for (int i=0;i<mapping.length;i++) {
199             if (servlet.getServletName().equals(mapping[i].getServletName())) {
200                 maps.add(mapping[i]);
201             }
202         }
203         return maps;
204     }
205     
206     private static java.util.List JavaDoc getFilterMappingList(WebApp webApp, Filter filter) {
207         String JavaDoc filterName = filter.getFilterName();
208         java.util.List JavaDoc maps = new java.util.ArrayList JavaDoc();
209         if (filterName==null) return maps;
210         FilterMapping[] mapping = webApp.getFilterMapping();
211         for (int i=0;i<mapping.length;i++) {
212             if (filter.getFilterName().equals(mapping[i].getFilterName())) {
213                 maps.add(mapping[i]);
214             }
215         }
216         return maps;
217     }
218     
219     public static void openEditorFor(DDDataObject dObj, String JavaDoc className) {
220         if (className==null || className.length()==0) return;
221         try {
222             SourceGroup[] sourceGroups = getJavaSourceGroups(dObj);
223             String JavaDoc resource = className.trim().replace('.','/');
224             for (int i=0;i<sourceGroups.length;i++) {
225                 FileObject fo = sourceGroups[i].getRootFolder();
226                 FileObject target = fo.getFileObject(resource+".java"); //NOI18N
227
if (target!=null) {
228                     DataObject javaDo = DataObject.find(target);
229                     org.openide.cookies.OpenCookie cookie =
230                             (org.openide.cookies.OpenCookie)javaDo.getCookie(org.openide.cookies.OpenCookie.class);
231                     if (cookie !=null) {
232                         cookie.open();
233                         return;
234                     }
235                 }
236             }
237         } catch (java.io.IOException JavaDoc ex) {}
238         org.openide.DialogDisplayer.getDefault().notify(new org.openide.NotifyDescriptor.Message(
239                 org.openide.util.NbBundle.getMessage(DDUtils.class,"MSG_sourceNotFound")));
240     }
241     
242     public static void openEditorForSingleFile(DDDataObject dObj, String JavaDoc fileName) {
243         if (fileName==null || fileName.length()==0) return;
244         FileObject docBase = null;
245         try {
246             docBase = getDocumentBase(dObj);
247         } catch (java.io.IOException JavaDoc ex) {return;}
248         if (docBase!=null) {
249             FileObject target = docBase.getFileObject(fileName.trim());
250             if (target!=null) {
251                 try {
252                     DataObject javaDo = DataObject.find(target);
253                     org.openide.cookies.OpenCookie cookie =
254                             (org.openide.cookies.OpenCookie)javaDo.getCookie(org.openide.cookies.OpenCookie.class);
255                     if (cookie !=null) {
256                         cookie.open();
257                         return;
258                     }
259                 } catch (org.openide.loaders.DataObjectNotFoundException ex) {}
260             }
261         }
262         org.openide.DialogDisplayer.getDefault().notify(new org.openide.NotifyDescriptor.Message(
263                 org.openide.util.NbBundle.getMessage(DDUtils.class,"MSG_sourceNotFound")));
264     }
265     
266     public static void openEditorForFiles(DDDataObject dObj, java.util.StringTokenizer JavaDoc tok) {
267         FileObject docBase = null;
268         try {
269             docBase = getDocumentBase(dObj);
270         } catch (java.io.IOException JavaDoc ex) {return;}
271         if (!tok.hasMoreTokens()) return;
272         boolean found=false;
273         if (docBase!=null)
274             while (tok.hasMoreTokens()) {
275             String JavaDoc resource = tok.nextToken().trim();
276             if (resource.length()>0) {
277                 FileObject target = docBase.getFileObject(resource);
278                 if (target!=null) {
279                     try {
280                         DataObject javaDo = DataObject.find(target);
281                         org.openide.cookies.OpenCookie cookie =
282                                 (org.openide.cookies.OpenCookie)javaDo.getCookie(org.openide.cookies.OpenCookie.class);
283                         if (cookie !=null) {
284                             cookie.open();
285                             found=true;
286                         }
287                     } catch (org.openide.loaders.DataObjectNotFoundException ex) {}
288                 }
289             }
290             }
291         if (!found) {
292             org.openide.DialogDisplayer.getDefault().notify(new org.openide.NotifyDescriptor.Message(
293                     org.openide.util.NbBundle.getMessage(DDUtils.class,"MSG_sourcesNotFound")));
294         }
295     }
296     
297     public static SourceGroup[] getJavaSourceGroups(DDDataObject dObj) throws java.io.IOException JavaDoc {
298         Project proj = FileOwnerQuery.getOwner(dObj.getPrimaryFile());
299         if (proj==null) return new SourceGroup[]{};
300         Sources sources = ProjectUtils.getSources(proj);
301         return sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
302     }
303     
304     public static SourceGroup[] getDocBaseGroups(DDDataObject dObj) throws java.io.IOException JavaDoc {
305         Project proj = FileOwnerQuery.getOwner(dObj.getPrimaryFile());
306         if (proj==null) return new SourceGroup[]{};
307         Sources sources = ProjectUtils.getSources(proj);
308         return sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
309     }
310     
311     public static FileObject getDocumentBase(DDDataObject dObj) throws java.io.IOException JavaDoc {
312         WebModule wm = WebModule.getWebModule(dObj.getPrimaryFile());
313         if (wm==null) return null;
314         return wm.getDocumentBase();
315     }
316     
317     public static String JavaDoc getResourcePath(SourceGroup[] groups, FileObject fo) {
318         return getResourcePath(groups, fo, '.', false);
319     }
320     
321     public static String JavaDoc getResourcePath(SourceGroup[] groups, FileObject fo, char separator) {
322         return getResourcePath(groups, fo, separator, false);
323     }
324     
325     public static String JavaDoc getResourcePath(SourceGroup[] groups, FileObject fo, char separator, boolean withExt) {
326         for (int i=0;i<groups.length;i++) {
327             FileObject root = groups[i].getRootFolder();
328             if (FileUtil.isParentOf(root,fo)) {
329                 String JavaDoc relativePath = FileUtil.getRelativePath(root,fo);
330                 if (relativePath!=null) {
331                     if (separator!='/') relativePath = relativePath.replace('/',separator);
332                     if (!withExt) {
333                         int index = relativePath.lastIndexOf((int)'.');
334                         if (index>0) relativePath = relativePath.substring(0,index);
335                     }
336                     return relativePath;
337                 } else {
338                     return "";
339                 }
340             }
341         }
342         return "";
343     }
344     /** removes all filter mappings for given servlet name
345      */

346     public static void removeServletMappings(WebApp webApp, String JavaDoc servletName) {
347         if (servletName==null) return;
348         ServletMapping[] oldMaps = webApp.getServletMapping();
349         for (int i=0;i<oldMaps.length;i++) {
350             if (servletName.equals(oldMaps[i].getServletName())) {
351                 webApp.removeServletMapping(oldMaps[i]);
352             }
353         }
354     }
355     /** removes all filter mappings for given filter name
356      * @return Stack of deleted rows
357      */

358     public static java.util.Stack JavaDoc removeFilterMappings(WebApp webApp, String JavaDoc filterName) {
359         java.util.Stack JavaDoc deletedRows = new java.util.Stack JavaDoc();
360         if (filterName==null) return deletedRows;
361         FilterMapping[] oldMaps = webApp.getFilterMapping();
362         for (int i=0;i<oldMaps.length;i++) {
363             if (filterName.equals(oldMaps[i].getFilterName())) {
364                 webApp.removeFilterMapping(oldMaps[i]);
365                 deletedRows.push(new Integer JavaDoc(i));
366             }
367         }
368         return deletedRows;
369     }
370     
371     /** removes all filter mappings for given servlet name
372      */

373     public static void removeFilterMappingsForServlet(WebApp webApp, String JavaDoc servletName) {
374         java.util.Stack JavaDoc deletedRows = new java.util.Stack JavaDoc();
375         if (servletName==null) return;
376         FilterMapping[] oldMaps = webApp.getFilterMapping();
377         for (int i=0;i<oldMaps.length;i++) {
378             if (servletName.equals(oldMaps[i].getServletName())) {
379                 webApp.removeFilterMapping(oldMaps[i]);
380             }
381         }
382     }
383     
384     public static String JavaDoc addItem(String JavaDoc text, String JavaDoc newItem, boolean asFirst) {
385         String JavaDoc[] stringArray = getStringArray(text);
386         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
387         if (asFirst) {
388             list.add(newItem);
389             for (int i=0;i<stringArray.length;i++) {
390                 if (!newItem.equals(stringArray[i])) list.add(stringArray[i]);
391             }
392         } else {
393             for (int i=0;i<stringArray.length;i++) {
394                 if (!newItem.equals(stringArray[i])) list.add(stringArray[i]);
395             }
396             list.add(newItem);
397         }
398         return getAsString(list);
399     }
400     
401     private static String JavaDoc getAsString(java.util.List JavaDoc list) {
402         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
403         for (int i=0;i<list.size();i++) {
404             if (i>0) buf.append(", "); //NOI18N
405
buf.append((String JavaDoc)list.get(i));
406         }
407         return buf.toString();
408     }
409     
410     public static String JavaDoc[] getServletNames(WebApp webApp) {
411         Servlet[] allServlets = webApp.getServlet();
412         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
413         for (int i=0;i<allServlets.length;i++) {
414             String JavaDoc servletName = allServlets[i].getServletName();
415             if (servletName!=null && !list.contains(allServlets[i])) list.add(servletName);
416         }
417         String JavaDoc[] names = new String JavaDoc[list.size()];
418         list.toArray(names);
419         return names;
420     }
421     
422     public static String JavaDoc[] getFilterNames(WebApp webApp) {
423         Filter[] filters = webApp.getFilter();
424         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
425         for (int i=0;i<filters.length;i++) {
426             String JavaDoc filterName = filters[i].getFilterName();
427             if (filterName!=null && !list.contains(filters[i])) list.add(filterName);
428         }
429         String JavaDoc[] names = new String JavaDoc[list.size()];
430         list.toArray(names);
431         return names;
432     }
433     
434     public static String JavaDoc checkServletMappig(String JavaDoc uri) {
435         if (!uri.matches("[\\*/].*")) { //NOI18N
436
return NbBundle.getMessage(DDUtils.class,"MSG_WrongUriStart");
437         } else if (uri.length()>1 && uri.endsWith("/")) {
438             return NbBundle.getMessage(DDUtils.class,"MSG_WrongUriEnd");
439         } else if (uri.matches(".*\\*.*\\*.*")) { //NOI18N
440
return NbBundle.getMessage(DDUtils.class,"MSG_TwoAsterisks");
441         } else if (uri.matches("..*\\*..*")) { //NOI18N
442
return NbBundle.getMessage(DDUtils.class,"MSG_AsteriskInTheMiddle");
443         }
444         return null;
445     }
446     
447 }
448
Popular Tags