1 19 20 package org.netbeans.modules.java.j2seproject.classpath; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import org.netbeans.api.project.ant.AntArtifact; 31 import org.netbeans.api.project.libraries.Library; 32 import org.netbeans.api.project.libraries.LibraryManager; 33 import org.netbeans.api.queries.CollocationQuery; 34 import org.netbeans.spi.project.support.ant.AntProjectHelper; 35 import org.netbeans.spi.project.support.ant.EditableProperties; 36 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 37 import org.netbeans.spi.project.support.ant.PropertyUtils; 38 import org.netbeans.spi.project.support.ant.ReferenceHelper; 39 import org.openide.filesystems.FileUtil; 40 41 45 public class ClassPathSupport { 46 47 private PropertyEvaluator evaluator; 48 private ReferenceHelper referenceHelper; 49 private AntProjectHelper antProjectHelper; 50 private Set <String > wellKnownPaths; 51 private String libraryPrefix; 52 private String librarySuffix; 53 private String antArtifactPrefix; 54 55 56 public ClassPathSupport( PropertyEvaluator evaluator, 57 ReferenceHelper referenceHelper, 58 AntProjectHelper antProjectHelper, 59 String [] wellKnownPaths, 60 String libraryPrefix, 61 String librarySuffix, 62 String antArtifactPrefix ) { 63 this.evaluator = evaluator; 64 this.referenceHelper = referenceHelper; 65 this.antProjectHelper = antProjectHelper; 66 this.wellKnownPaths = wellKnownPaths == null ? null : new HashSet <String >(Arrays.asList(wellKnownPaths)); 67 this.libraryPrefix = libraryPrefix; 68 this.librarySuffix = librarySuffix; 69 this.antArtifactPrefix = antArtifactPrefix; 70 } 71 72 74 public Iterator <Item> itemsIterator( String propertyValue ) { 75 return itemsList( propertyValue ).iterator(); 77 } 78 79 public List <Item> itemsList( String propertyValue ) { 80 81 String pe[] = PropertyUtils.tokenizePath( propertyValue == null ? "": propertyValue ); List <Item> items = new ArrayList <Item>( pe.length ); 83 for( int i = 0; i < pe.length; i++ ) { 84 Item item; 85 86 if ( isWellKnownPath( pe[i] ) ) { 88 item = Item.create( pe[i] ); 90 } 91 else if ( isLibrary( pe[i] ) ) { 92 String libraryName = pe[i].substring( libraryPrefix.length(), pe[i].lastIndexOf('.') ); Library library = LibraryManager.getDefault().getLibrary( libraryName ); 95 if ( library == null ) { 96 item = Item.createBroken( Item.TYPE_LIBRARY, pe[i] ); 97 } 98 else { 99 item = Item.create( library, pe[i] ); 100 } 101 } 102 else if ( isAntArtifact( pe[i] ) ) { 103 Object [] ret = referenceHelper.findArtifactAndLocation(pe[i]); 105 if ( ret[0] == null || ret[1] == null ) { 106 item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i] ); 107 } 108 else { 109 AntArtifact artifact = (AntArtifact)ret[0]; 111 URI uri = (URI )ret[1]; 112 File usedFile = antProjectHelper.resolveFile(evaluator.evaluate(pe[i])); 113 File artifactFile = new File (artifact.getScriptLocation().toURI().resolve(uri).normalize()); 114 if (usedFile.equals(artifactFile)) { 115 item = Item.create( artifact, uri, pe[i] ); 116 } 117 else { 118 item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i] ); 119 } 120 } 121 } else { 122 String eval = evaluator.evaluate( pe[i] ); 124 File f = null; 125 if (eval != null) { 126 f = antProjectHelper.resolveFile( eval ); 127 } 128 129 if ( f == null || !f.exists() ) { 130 item = Item.createBroken( Item.TYPE_JAR, pe[i] ); 131 } 132 else { 133 item = Item.create( f, pe[i] ); 134 } 135 } 136 137 items.add( item ); 138 139 } 140 141 return items; 142 143 } 144 145 148 public String [] encodeToStrings(Iterator <Item> classpath) { 149 150 ArrayList result = new ArrayList (); 151 152 while( classpath.hasNext() ) { 153 154 Item item = classpath.next(); 155 String reference = null; 156 157 switch( item.getType() ) { 158 159 case Item.TYPE_JAR: 160 reference = item.getReference(); 161 if ( item.isBroken() ) { 162 break; 163 } 164 if (reference == null) { 165 File file = item.getFile(); 167 reference = referenceHelper.createForeignFileReference(file, null); 169 } 170 break; 171 case Item.TYPE_LIBRARY: 172 reference = item.getReference(); 173 if ( item.isBroken() ) { 174 break; 175 } 176 Library library = item.getLibrary(); 177 if (reference == null) { 178 if ( library == null ) { 179 break; 180 } 181 reference = getLibraryReference( item ); 182 } 183 break; 184 case Item.TYPE_ARTIFACT: 185 reference = item.getReference(); 186 if ( item.isBroken() ) { 187 break; 188 } 189 AntArtifact artifact = (AntArtifact)item.getArtifact(); 190 if ( reference == null) { 191 if ( artifact == null ) { 192 break; 193 } 194 reference = referenceHelper.addReference( item.getArtifact(), item.getArtifactURI()); 195 } 196 break; 197 case Item.TYPE_CLASSPATH: 198 reference = item.getReference(); 199 break; 200 } 201 202 if ( reference != null ) { 203 result.add( reference ); 204 } 205 206 } 207 208 String [] items = new String [ result.size() ]; 209 for( int i = 0; i < result.size(); i++) { 210 if ( i < result.size() - 1 ) { 211 items[i] = result.get( i ) + ":"; 212 } 213 else { 214 items[i] = (String )result.get( i ); } 216 } 217 218 return items; 219 } 220 221 public String getLibraryReference( Item item ) { 222 if ( item.getType() != Item.TYPE_LIBRARY ) { 223 throw new IllegalArgumentException ( "Item must be of type LIBRARY" ); 224 } 225 return libraryPrefix + item.getLibrary().getName() + librarySuffix; 226 } 227 228 230 private boolean isWellKnownPath( String property ) { 231 return wellKnownPaths == null ? false : wellKnownPaths.contains( property ); 232 } 233 234 private boolean isAntArtifact( String property ) { 235 return antArtifactPrefix == null ? false : property.startsWith( antArtifactPrefix ); 236 } 237 238 private boolean isLibrary( String property ) { 239 if ( libraryPrefix != null && property.startsWith( libraryPrefix ) ) { 240 return librarySuffix == null ? true : property.endsWith( librarySuffix ); 241 } 242 else { 243 return false; 244 } 245 246 } 247 248 250 252 public static class Item { 253 254 public static final int TYPE_JAR = 0; 256 public static final int TYPE_LIBRARY = 1; 257 public static final int TYPE_ARTIFACT = 2; 258 public static final int TYPE_CLASSPATH = 3; 259 260 private static final String BROKEN = "BrokenReference"; 263 private Object object; 264 private URI artifactURI; 265 private int type; 266 private String property; 267 268 private Item( int type, Object object, String property ) { 269 this.type = type; 270 this.object = object; 271 this.property = property; 272 } 273 274 private Item( int type, Object object, URI artifactURI, String property ) { 275 this( type, object, property ); 276 this.artifactURI = artifactURI; 277 } 278 279 281 282 public static Item create( Library library, String property ) { 283 if ( library == null ) { 284 throw new IllegalArgumentException ( "library must not be null" ); } 286 return new Item( TYPE_LIBRARY, library, property ); 287 } 288 289 public static Item create( AntArtifact artifact, URI artifactURI, String property ) { 290 if ( artifactURI == null ) { 291 throw new IllegalArgumentException ( "artifactURI must not be null" ); } 293 if ( artifact == null ) { 294 throw new IllegalArgumentException ( "artifact must not be null" ); } 296 return new Item( TYPE_ARTIFACT, artifact, artifactURI, property ); 297 } 298 299 public static Item create( File file, String property ) { 300 if ( file == null ) { 301 throw new IllegalArgumentException ( "file must not be null" ); } 303 return new Item( TYPE_JAR, file, property ); 304 } 305 306 public static Item create( String property ) { 307 if ( property == null ) { 308 throw new IllegalArgumentException ( "property must not be null" ); } 310 return new Item ( TYPE_CLASSPATH, null, property ); 311 } 312 313 public static Item createBroken( int type, String property ) { 314 if ( property == null ) { 315 throw new IllegalArgumentException ( "property must not be null in broken items" ); } 317 return new Item( type, BROKEN, property ); 318 } 319 320 321 323 public int getType() { 324 return type; 325 } 326 327 public Library getLibrary() { 328 if ( getType() != TYPE_LIBRARY ) { 329 throw new IllegalArgumentException ( "Item is not of required type - LIBRARY" ); } 331 assert object instanceof Library : 332 "Invalid object type: "+object.getClass().getName()+" instance: "+object.toString()+" expected type: Library"; return (Library)object; 334 } 335 336 public File getFile() { 337 if ( getType() != TYPE_JAR ) { 338 throw new IllegalArgumentException ( "Item is not of required type - JAR" ); } 340 return (File )object; 341 } 342 343 public AntArtifact getArtifact() { 344 if ( getType() != TYPE_ARTIFACT ) { 345 throw new IllegalArgumentException ( "Item is not of required type - ARTIFACT" ); } 347 return (AntArtifact)object; 348 } 349 350 public URI getArtifactURI() { 351 if ( getType() != TYPE_ARTIFACT ) { 352 throw new IllegalArgumentException ( "Item is not of required type - ARTIFACT" ); } 354 return artifactURI; 355 } 356 357 358 public String getReference() { 359 return property; 360 } 361 362 public boolean isBroken() { 363 return object == BROKEN; 364 } 365 366 public int hashCode() { 367 368 int hash = getType(); 369 370 if ( object == BROKEN ) { 371 return BROKEN.hashCode(); 372 } 373 374 switch ( getType() ) { 375 case TYPE_ARTIFACT: 376 hash += getArtifact().getType().hashCode(); 377 hash += getArtifact().getScriptLocation().hashCode(); 378 hash += getArtifactURI().hashCode(); 379 break; 380 case TYPE_CLASSPATH: 381 hash += property.hashCode(); 382 break; 383 default: 384 hash += object.hashCode(); 385 } 386 387 return hash; 388 } 389 390 public boolean equals( Object itemObject ) { 391 392 if ( !( itemObject instanceof Item ) ) { 393 return false; 394 } 395 396 Item item = (Item)itemObject; 397 398 if ( getType() != item.getType() ) { 399 return false; 400 } 401 402 if ( isBroken() != item.isBroken() ) { 403 return false; 404 } 405 406 if ( isBroken() ) { 407 return getReference().equals( item.getReference() ); 408 } 409 410 switch ( getType() ) { 411 case TYPE_ARTIFACT: 412 if ( getArtifact().getType() != item.getArtifact().getType() ) { 413 return false; 414 } 415 416 if ( !getArtifact().getScriptLocation().equals( item.getArtifact().getScriptLocation() ) ) { 417 return false; 418 } 419 420 if ( !getArtifactURI().equals( item.getArtifactURI() ) ) { 421 return false; 422 } 423 return true; 424 case TYPE_CLASSPATH: 425 return property.equals( item.property ); 426 default: 427 return object.equals( item.object ); 428 } 429 430 } 431 432 } 433 434 440 public static boolean relativizeLibraryClassPath (final EditableProperties ep, final AntProjectHelper aph, final String libCpProperty) { 441 String value = PropertyUtils.getGlobalProperties().getProperty(libCpProperty); 442 if (value == null) { 444 return false; 445 } 446 String [] paths = PropertyUtils.tokenizePath(value); 447 StringBuffer sb = new StringBuffer (); 448 File projectDir = FileUtil.toFile(aph.getProjectDirectory()); 449 for (int i=0; i<paths.length; i++) { 450 File f = aph.resolveFile(paths[i]); 451 if (CollocationQuery.areCollocated(f, projectDir)) { 452 sb.append(PropertyUtils.relativizeFile(projectDir, f)); 453 } else { 454 return false; 455 } 456 if (i+1<paths.length) { 457 sb.append(File.pathSeparatorChar); 458 } 459 } 460 if (sb.length() == 0) { 461 return false; 462 } 463 ep.setProperty(libCpProperty, sb.toString()); 464 ep.setComment(libCpProperty, new String []{ 465 "# Property "+libCpProperty+" is set here just to make sharing of project simpler.", 467 "# The library definition has always preference over this property."}, false); 468 return true; 469 } 470 471 476 public static String getAntPropertyName( String property ) { 477 if ( property != null && 478 property.startsWith( "${" ) && property.endsWith( "}" ) ) { return property.substring( 2, property.length() - 1 ); 481 } 482 else { 483 return property; 484 } 485 } 486 487 } 488 | Popular Tags |