1 19 20 package org.netbeans.api.java.project.classpath; 21 22 import java.io.Externalizable ; 23 import java.io.IOException ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.project.FileOwnerQuery; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.ant.AntArtifact; 33 import org.netbeans.api.project.libraries.Library; 34 import org.netbeans.modules.java.project.classpath.ProjectClassPathModifierAccessor; 35 import org.netbeans.spi.java.project.classpath.ProjectClassPathModifierImplementation; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.URLMapper; 39 40 47 public class ProjectClassPathModifier { 48 49 50 51 private ProjectClassPathModifier () { 52 53 } 54 55 67 @SuppressWarnings ("deprecation") public static boolean addLibraries (final Library[] libraries, final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 69 final Extensible extensible = findExtensible (projectArtifact, classPathType); 70 if (extensible != null) { 71 if (extensible.pcmi != null) { 72 assert extensible.sg != null; 73 assert extensible.classPathType != null; 74 return ProjectClassPathModifierAccessor.INSTANCE.addLibraries (libraries, extensible.pcmi, extensible.sg, extensible.classPathType); 75 } 76 else if (extensible.pcpe != null) { 77 boolean result = false; 78 for (int i=0; i< libraries.length; i++) { 79 result |= extensible.pcpe.addLibrary (libraries[i]); 80 } 81 return result; 82 } 83 } 84 throw new UnsupportedOperationException (); 85 } 86 87 88 100 public static boolean removeLibraries (final Library[] libraries, final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 101 final Extensible extensible = findExtensible (projectArtifact, classPathType); 102 if (extensible != null && extensible.pcmi != null) { 103 assert extensible.sg != null; 104 assert extensible.classPathType != null; 105 return ProjectClassPathModifierAccessor.INSTANCE.removeLibraries (libraries, extensible.pcmi, extensible.sg, extensible.classPathType); 106 } 107 throw new UnsupportedOperationException (); 108 } 109 110 122 @SuppressWarnings ("deprecation") public static boolean addRoots (final URL [] classPathRoots, final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 124 final Extensible extensible = findExtensible(projectArtifact, classPathType); 125 if (extensible != null) { 126 if (extensible.pcmi != null) { 127 assert extensible.sg != null; 128 assert extensible.classPathType != null; 129 return ProjectClassPathModifierAccessor.INSTANCE.addRoots (classPathRoots, extensible.pcmi, extensible.sg, extensible.classPathType); 130 } 131 else if (extensible.pcpe != null) { 132 boolean result = false; 133 for (int i=0; i< classPathRoots.length; i++) { 134 URL urlToAdd = classPathRoots[i]; 135 if ("jar".equals(urlToAdd.getProtocol())) { 136 urlToAdd = FileUtil.getArchiveFile (urlToAdd); 137 } 138 final FileObject fo = URLMapper.findFileObject(urlToAdd); 139 if (fo == null) { 140 throw new UnsupportedOperationException ("Adding of a non existent root is not supported by project."); } 142 result |= extensible.pcpe.addArchiveFile (fo); 143 } 144 return result; 145 } 146 } 147 throw new UnsupportedOperationException (); 148 } 149 150 162 public static boolean removeRoots (final URL [] classPathRoots, final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 163 final Extensible extensible = findExtensible (projectArtifact, classPathType); 164 if (extensible != null && extensible.pcmi != null) { 165 assert extensible.sg != null; 166 assert extensible.classPathType != null; 167 return ProjectClassPathModifierAccessor.INSTANCE.removeRoots (classPathRoots, extensible.pcmi, extensible.sg, extensible.classPathType); 168 } 169 throw new UnsupportedOperationException (); 170 } 171 172 187 @SuppressWarnings ("deprecation") public static boolean addAntArtifacts (final AntArtifact[] artifacts, final URI [] artifactElements, 189 final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 190 final Extensible extensible = findExtensible (projectArtifact, classPathType); 191 if (extensible != null) { 192 assert artifacts.length == artifactElements.length; 193 if (extensible.pcmi != null) { 194 assert extensible.sg != null; 195 assert extensible.classPathType != null; 196 return ProjectClassPathModifierAccessor.INSTANCE.addAntArtifacts (artifacts, artifactElements, extensible.pcmi, extensible.sg, extensible.classPathType); 197 } 198 else if (extensible.pcpe != null) { 199 boolean result = false; 200 for (int i=0; i< artifacts.length; i++) { 201 result |= extensible.pcpe.addAntArtifact (artifacts[i], artifactElements[i]); 202 } 203 return result; 204 } 205 } 206 throw new UnsupportedOperationException (); 207 } 208 209 224 public static boolean removeAntArtifacts (final AntArtifact[] artifacts, final URI [] artifactElements, 225 final FileObject projectArtifact, final String classPathType) throws IOException , UnsupportedOperationException { 226 final Extensible extensible = findExtensible (projectArtifact, classPathType); 227 if (extensible != null && extensible.pcmi != null) { 228 assert extensible.sg != null; 229 assert extensible.classPathType != null; 230 return ProjectClassPathModifierAccessor.INSTANCE.removeAntArtifacts (artifacts, artifactElements, extensible.pcmi, extensible.sg, extensible.classPathType); 231 } 232 throw new UnsupportedOperationException (); 233 } 234 235 236 249 @SuppressWarnings ("deprecation") private static Extensible findExtensible (final FileObject fo, final String classPathType) { 251 assert fo != null; 252 assert classPathType != null; 253 final Project project = FileOwnerQuery.getOwner(fo); 254 if (project == null) { 255 return null; 256 } 257 final ProjectClassPathModifierImplementation pm = project.getLookup().lookup(ProjectClassPathModifierImplementation.class); 258 if (pm != null) { 259 final SourceGroup[] sgs = ProjectClassPathModifierAccessor.INSTANCE.getExtensibleSourceGroups(pm); 260 assert sgs != null : "Class: " + pm.getClass() + " returned null as source groups."; for (SourceGroup sg : sgs) { 262 if ((fo == sg.getRootFolder() || FileUtil.isParentOf(sg.getRootFolder(),fo)) && sg.contains(fo)) { 263 final String [] types = ProjectClassPathModifierAccessor.INSTANCE.getExtensibleClassPathTypes(pm,sg); 264 assert types != null : "Class: " + pm.getClass() + " returned null as classpath types."; for (String type : types) { 266 if (classPathType.equals(type)) { 267 return new Extensible (pm, sg, type); 268 } 269 } 270 } 271 } 272 } 273 else if (classPathType.equals(ClassPath.COMPILE)) { 274 final org.netbeans.spi.java.project.classpath.ProjectClassPathExtender pe = 275 project.getLookup().lookup(org.netbeans.spi.java.project.classpath.ProjectClassPathExtender.class); 276 if (pe != null) { 277 return new Extensible (pe); 278 } 279 } 280 return null; 281 } 282 283 284 289 private static final class Extensible { 290 291 private final String classPathType; 292 private final SourceGroup sg; 293 private final ProjectClassPathModifierImplementation pcmi; 294 @SuppressWarnings ("deprecation") private final org.netbeans.spi.java.project.classpath.ProjectClassPathExtender pcpe; 296 297 298 private Extensible (final ProjectClassPathModifierImplementation pcmi , final SourceGroup sg, final String classPathType) { 299 assert pcmi != null; 300 assert sg != null; 301 assert classPathType != null; 302 this.pcmi = pcmi; 303 this.sg = sg; 304 this.classPathType = classPathType; 305 this.pcpe = null; 306 } 307 308 @SuppressWarnings ("deprecation") private Extensible (final org.netbeans.spi.java.project.classpath.ProjectClassPathExtender pcpe) { 310 assert pcpe != null; 311 this.pcpe = pcpe; 312 this.pcmi = null; 313 this.sg = null; 314 this.classPathType = ClassPath.COMPILE; 315 } 316 } 317 318 } 319 | Popular Tags |