Search in sources :

Example 1 with ClassPath

use of org.netbeans.api.java.classpath.ClassPath in project enclojure by EricThorsen.

the class SourcePathProviderImpl method setSourceRoots.

/**
     * Sets array of source roots.
     *
     * @param sourceRoots a new array of sourceRoots
     */
public void setSourceRoots(String[] sourceRoots) {
    LOG.log(Level.FINE, "SourcePathProviderImpl.setSourceRoots(" + java.util.Arrays.asList(sourceRoots) + ")");
    Set<String> newRoots = new HashSet<String>(Arrays.asList(sourceRoots));
    ClassPath oldCP = null;
    ClassPath newCP = null;
    synchronized (this) {
        List<FileObject> sourcePath = new ArrayList<FileObject>(Arrays.asList(smartSteppingSourcePath.getRoots()));
        List<FileObject> sourcePathOriginal = new ArrayList<FileObject>(Arrays.asList(originalSourcePath.getRoots()));
        // First check whether there are some new source roots
        Set<String> newOriginalRoots = new HashSet<String>(newRoots);
        for (FileObject fo : sourcePathOriginal) {
            newOriginalRoots.remove(getRoot(fo));
        }
        if (!newOriginalRoots.isEmpty()) {
            for (String root : newOriginalRoots) {
                FileObject fo = getFileObject(root);
                if (fo != null) {
                    sourcePathOriginal.add(fo);
                }
            }
            originalSourcePath = ClassPathSupport.createClassPath(sourcePathOriginal.toArray(new FileObject[0]));
            if (additionalSourceRoots == null) {
                additionalSourceRoots = new HashSet<String>();
            }
            additionalSourceRoots.addAll(newOriginalRoots);
        }
        // Then correct the smart-stepping path
        Set<String> newSteppingRoots = new HashSet<String>(newRoots);
        for (FileObject fo : sourcePath) {
            newSteppingRoots.remove(getRoot(fo));
        }
        Set<FileObject> removedSteppingRoots = new HashSet<FileObject>();
        Set<FileObject> removedOriginalRoots = new HashSet<FileObject>();
        for (FileObject fo : sourcePath) {
            String spr = getRoot(fo);
            if (!newRoots.contains(spr)) {
                removedSteppingRoots.add(fo);
                if (additionalSourceRoots != null && additionalSourceRoots.contains(spr)) {
                    removedOriginalRoots.add(fo);
                    additionalSourceRoots.remove(spr);
                    if (additionalSourceRoots.size() == 0) {
                        additionalSourceRoots = null;
                    }
                }
            }
        }
        if (removedOriginalRoots.size() > 0) {
            sourcePathOriginal.removeAll(removedOriginalRoots);
            originalSourcePath = ClassPathSupport.createClassPath(sourcePathOriginal.toArray(new FileObject[0]));
        }
        if (newSteppingRoots.size() > 0 || removedSteppingRoots.size() > 0) {
            for (String root : newSteppingRoots) {
                FileObject fo = getFileObject(root);
                if (fo != null) {
                    sourcePath.add(fo);
                }
            }
            sourcePath.removeAll(removedSteppingRoots);
            oldCP = smartSteppingSourcePath;
            smartSteppingSourcePath = ClassPathSupport.createClassPath(sourcePath.toArray(new FileObject[0]));
            newCP = smartSteppingSourcePath;
        }
    }
    if (oldCP != null) {
        pcs.firePropertyChange(PROP_SOURCE_ROOTS, oldCP, newCP);
    }
}
Also used : ClassPath(org.netbeans.api.java.classpath.ClassPath) FileObject(org.openide.filesystems.FileObject) HashSet(java.util.HashSet)

Example 2 with ClassPath

use of org.netbeans.api.java.classpath.ClassPath in project enclojure by EricThorsen.

the class SourcePathProviderImpl method getRelativePath.

/**
     * Returns relative path for given url.
     *
     * @param url a url of resource file
     * @param directorySeparator a directory separator character
     * @param includeExtension whether the file extension should be included 
     *        in the result
     *
     * @return relative path
     */
public String getRelativePath(String url, char directorySeparator, boolean includeExtension) {
    // 1) url -> FileObject
    FileObject fo = null;
    URL theUrl = null;
    if (verbose) {
        System.out.println("SPPI: getRelativePath " + url);
    }
    try {
        LOG.log(Level.ALL, " SourcePathProviderImpl::getRelativePath - " + url);
        fo = URLMapper.findFileObject(theUrl = new URL(url));
        if (verbose) {
            System.out.println("SPPI:   fo " + fo);
        }
    } catch (MalformedURLException e) {
        //e.printStackTrace ();
        return null;
    }
    String relativePath = smartSteppingSourcePath.getResourceName(fo, directorySeparator, includeExtension);
    if (relativePath == null) {
        // fallback to FileObject's class path
        ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
        if (cp == null) {
            cp = ClassPath.getClassPath(fo, ClassPath.COMPILE);
        }
        if (cp == null) {
            //ET - If the url of the full path exists, return that...
            if (fo != null && theUrl != null) {
                File f = new File(theUrl.getPath());
                if (f.exists())
                    return f.getAbsolutePath();
            }
            return null;
        }
        relativePath = cp.getResourceName(fo, directorySeparator, includeExtension);
    }
    if (relativePath == null) {
        LOG.log(Level.ALL, " SourcePathProviderImpl::getRelativePath - NO GO! " + url);
    }
    return relativePath;
}
Also used : ClassPath(org.netbeans.api.java.classpath.ClassPath) MalformedURLException(java.net.MalformedURLException) FileObject(org.openide.filesystems.FileObject) File(java.io.File) URL(java.net.URL)

Example 3 with ClassPath

use of org.netbeans.api.java.classpath.ClassPath in project enclojure by EricThorsen.

the class SourcePathProviderImpl method getAllURLs.

/**
     * Translates a relative path to all possible URLs.
     * Uses GlobalPathRegistry if global == true.
     *
     * @param relativePath a relative path (java/lang/Thread.java)
     * @param global true if global path should be used
     * @return url
     */
public String[] getAllURLs(String relativePath, boolean global) {
    if (verbose) {
        System.out.println("SPPI: getURL " + relativePath + " global " + global);
    }
    List<FileObject> fos;
    relativePath = normalize(relativePath);
    if (originalSourcePath == null) {
        fos = new ArrayList<FileObject>();
        for (ClassPath cp : GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE)) {
            fos.addAll(cp.findAllResources(relativePath));
        }
    } else {
        synchronized (this) {
            if (!global) {
                fos = smartSteppingSourcePath.findAllResources(relativePath);
                if (verbose) {
                    System.out.println("SPPI:   fos " + fos);
                }
            } else {
                fos = originalSourcePath.findAllResources(relativePath);
                if (verbose) {
                    System.out.println("SPPI:   fos " + fos);
                }
            }
        }
    }
    List<String> urls = new ArrayList<String>(fos.size());
    for (FileObject fo : fos) {
        try {
            urls.add(fo.getURL().toString());
        } catch (FileStateInvalidException e) {
            if (verbose) {
                System.out.println("SPPI:   FileStateInvalidException for " + fo);
            // skip it
            }
        }
    }
    return urls.toArray(new String[0]);
}
Also used : ClassPath(org.netbeans.api.java.classpath.ClassPath) FileStateInvalidException(org.openide.filesystems.FileStateInvalidException) FileObject(org.openide.filesystems.FileObject)

Example 4 with ClassPath

use of org.netbeans.api.java.classpath.ClassPath in project enclojure by EricThorsen.

the class Utils method getResourceNameFromFullPath.

public static String getResourceNameFromFullPath(String fullPath) {
    try {
        java.io.File fPath = null;
        if (fullPath.startsWith("file:/")) {
            fPath = new java.io.File(new java.net.URL(fullPath).toURI());
        } else {
            fPath = new java.io.File(fullPath);
        }
        FileObject fo = FileUtil.toFileObject(fPath);
        Object[] pcks = getClasspathForSource().toArray();
        for (int i = 0; i < pcks.length; i++) {
            ClassPath p = (ClassPath) pcks[i];
            if (p.contains(fo)) {
                return p.getResourceName(fo);
            }
        }
    } catch (MalformedURLException ex) {
        Exceptions.printStackTrace(ex);
    } catch (URISyntaxException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}
Also used : ClassPath(org.netbeans.api.java.classpath.ClassPath) java.net(java.net) File(java.io.File)

Aggregations

ClassPath (org.netbeans.api.java.classpath.ClassPath)4 FileObject (org.openide.filesystems.FileObject)3 File (java.io.File)2 java.net (java.net)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 FileStateInvalidException (org.openide.filesystems.FileStateInvalidException)1