1 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 36 public class DDUtils { 37 38 public static String [] getUrlPatterns(WebApp webApp, Servlet servlet) { 39 if (servlet.getServletName()==null) return new String []{}; 40 ServletMapping[] mapping = webApp.getServletMapping(); 41 java.util.List maps = new java.util.ArrayList (); 42 for (int i=0;i<mapping.length;i++) { 43 if (servlet.getServletName().equals(mapping[i].getServletName())) { 44 String urlPattern = mapping[i].getUrlPattern(); 45 if (urlPattern!=null) maps.add(urlPattern); 46 } 47 } 48 String [] urlPatterns = new String [maps.size()]; 49 maps.toArray(urlPatterns); 50 return urlPatterns; 51 } 52 53 public static String [] getUrlPatterns(WebApp webApp, Filter filter) { 54 if (filter.getFilterName()==null) return new String []{}; 55 FilterMapping[] mapping = webApp.getFilterMapping(); 56 java.util.List maps = new java.util.ArrayList (); 57 for (int i=0;i<mapping.length;i++) { 58 if (filter.getFilterName().equals(mapping[i].getFilterName())) { 59 String urlPattern = mapping[i].getUrlPattern(); 60 if (urlPattern!=null) maps.add(urlPattern); 61 else { 62 String servletName = mapping[i].getServletName(); 63 if (servletName!=null) maps.add(servletName); 64 } 65 } 66 } 67 String [] urlPatterns = new String [maps.size()]; 68 maps.toArray(urlPatterns); 69 return urlPatterns; 70 } 71 72 public static String [] getStringArray(String text) { 73 java.util.StringTokenizer tok = new java.util.StringTokenizer (text,","); 74 java.util.Set set = new java.util.HashSet (); 75 while (tok.hasMoreTokens()) { 76 String token = tok.nextToken().trim(); 77 if (token.length()>0) set.add(token); 78 } 79 String [] stringArray = new String [set.size()]; 80 set.toArray(stringArray); 81 return stringArray; 82 } 83 84 public static boolean isServletMapping(WebApp webApp, String urlPattern) { 85 return webApp.findBeanByName("ServletMapping","UrlPattern",urlPattern)!=null; 86 } 87 88 public static boolean isServletMapping(WebApp webApp, Servlet servlet, String urlPattern) { 89 ServletMapping[] maps = webApp.getServletMapping(); 90 String 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 urlPatternList(String [] urlPatterns) { 102 if (urlPatterns==null) return ""; 103 StringBuffer buf = new StringBuffer (); 104 for (int i=0;i<urlPatterns.length;i++) { 105 if (i>0) buf.append(", "); buf.append(urlPatterns[i]); 107 } 108 return buf.toString(); 109 } 110 111 public static void addServletMappings(WebApp webApp, Servlet servlet, String [] urlPatterns) { 112 String servletName = servlet.getServletName(); 113 try { 114 for (int i=0;i<urlPatterns.length;i++) { 115 ServletMapping map = (ServletMapping)webApp.createBean("ServletMapping"); map.setServletName(servletName); 117 map.setUrlPattern(urlPatterns[i]); 118 webApp.addServletMapping(map); 119 } 120 } catch (ClassNotFoundException ex){} 121 } 122 123 public static void setServletMappings(WebApp webApp, Servlet servlet, String [] urlPatterns) { 124 String servletName = servlet.getServletName(); 125 java.util.List oldMaps = getServletMappingList(webApp,servlet); 126 java.util.List newPatterns = new java.util.ArrayList (); 127 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 for (int i=0;i<min;i++) { 143 ServletMapping oldMap = (ServletMapping)oldMaps.get(i); 144 oldMap.setUrlPattern((String )newPatterns.get(i)); 145 } 146 try { 148 for (int i=min;i<newPatterns.size();i++) { 149 ServletMapping map = (ServletMapping)webApp.createBean("ServletMapping"); map.setServletName(servletName); 151 map.setUrlPattern((String )newPatterns.get(i)); 152 webApp.addServletMapping(map); 153 } 154 } catch (ClassNotFoundException ex){} 155 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 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 maps = getFilterMappingList(webApp,filter); 170 FilterMapping[] newMappings = new FilterMapping[maps.size()]; 171 maps.toArray(newMappings); 172 return newMappings; 173 } 174 175 178 public static FilterMapping[] getFilterMappings(WebApp webApp, Servlet servlet) { 179 java.util.List maps = new java.util.ArrayList (); 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 getServletMappingList(WebApp webApp, Servlet servlet) { 194 String servletName = servlet.getServletName(); 195 java.util.List maps = new java.util.ArrayList (); 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 getFilterMappingList(WebApp webApp, Filter filter) { 207 String filterName = filter.getFilterName(); 208 java.util.List maps = new java.util.ArrayList (); 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 className) { 220 if (className==null || className.length()==0) return; 221 try { 222 SourceGroup[] sourceGroups = getJavaSourceGroups(dObj); 223 String 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"); 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 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 fileName) { 243 if (fileName==null || fileName.length()==0) return; 244 FileObject docBase = null; 245 try { 246 docBase = getDocumentBase(dObj); 247 } catch (java.io.IOException 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 tok) { 267 FileObject docBase = null; 268 try { 269 docBase = getDocumentBase(dObj); 270 } catch (java.io.IOException ex) {return;} 271 if (!tok.hasMoreTokens()) return; 272 boolean found=false; 273 if (docBase!=null) 274 while (tok.hasMoreTokens()) { 275 String 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 { 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 { 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 { 312 WebModule wm = WebModule.getWebModule(dObj.getPrimaryFile()); 313 if (wm==null) return null; 314 return wm.getDocumentBase(); 315 } 316 317 public static String getResourcePath(SourceGroup[] groups, FileObject fo) { 318 return getResourcePath(groups, fo, '.', false); 319 } 320 321 public static String getResourcePath(SourceGroup[] groups, FileObject fo, char separator) { 322 return getResourcePath(groups, fo, separator, false); 323 } 324 325 public static String 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 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 346 public static void removeServletMappings(WebApp webApp, String 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 358 public static java.util.Stack removeFilterMappings(WebApp webApp, String filterName) { 359 java.util.Stack deletedRows = new java.util.Stack (); 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 (i)); 366 } 367 } 368 return deletedRows; 369 } 370 371 373 public static void removeFilterMappingsForServlet(WebApp webApp, String servletName) { 374 java.util.Stack deletedRows = new java.util.Stack (); 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 addItem(String text, String newItem, boolean asFirst) { 385 String [] stringArray = getStringArray(text); 386 java.util.List list = new java.util.ArrayList (); 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 getAsString(java.util.List list) { 402 StringBuffer buf = new StringBuffer (); 403 for (int i=0;i<list.size();i++) { 404 if (i>0) buf.append(", "); buf.append((String )list.get(i)); 406 } 407 return buf.toString(); 408 } 409 410 public static String [] getServletNames(WebApp webApp) { 411 Servlet[] allServlets = webApp.getServlet(); 412 java.util.List list = new java.util.ArrayList (); 413 for (int i=0;i<allServlets.length;i++) { 414 String servletName = allServlets[i].getServletName(); 415 if (servletName!=null && !list.contains(allServlets[i])) list.add(servletName); 416 } 417 String [] names = new String [list.size()]; 418 list.toArray(names); 419 return names; 420 } 421 422 public static String [] getFilterNames(WebApp webApp) { 423 Filter[] filters = webApp.getFilter(); 424 java.util.List list = new java.util.ArrayList (); 425 for (int i=0;i<filters.length;i++) { 426 String filterName = filters[i].getFilterName(); 427 if (filterName!=null && !list.contains(filters[i])) list.add(filterName); 428 } 429 String [] names = new String [list.size()]; 430 list.toArray(names); 431 return names; 432 } 433 434 public static String checkServletMappig(String uri) { 435 if (!uri.matches("[\\*/].*")) { 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(".*\\*.*\\*.*")) { return NbBundle.getMessage(DDUtils.class,"MSG_TwoAsterisks"); 441 } else if (uri.matches("..*\\*..*")) { return NbBundle.getMessage(DDUtils.class,"MSG_AsteriskInTheMiddle"); 443 } 444 return null; 445 } 446 447 } 448 | Popular Tags |