Java Code Examples for org.openide.filesystems.FileUtil#isArchiveFile()

The following examples show how to use org.openide.filesystems.FileUtil#isArchiveFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JaxRsStackSupportImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isBundled( String classFqn ) {
    try {
        List<URL> urls = getJerseyJars();
        for (URL url : urls) {
            FileObject root = URLMapper.findFileObject(url);
            if (FileUtil.isArchiveFile(root)) {
                root = FileUtil.getArchiveRoot(root);
            }
            String path = classFqn.replace('.', '/') + ".class";
            if (root.getFileObject(path) != null) {
                return true;
            }
        }
        return false;
    }
    catch (FileStateInvalidException e) {
        return false;
    }
}
 
Example 2
Source File: Classpaths.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static Map<String,FileObject> findPackageRootsByName(AntProjectHelper helper, PropertyEvaluator evaluator, List<String> packageRootNames) {
    Map<String,FileObject> roots = new LinkedHashMap<String,FileObject>();
    for (String location : packageRootNames) {
        String locationEval = evaluator.evaluate(location);
        if (locationEval != null) {
            File locationFile = helper.resolveFile(locationEval);
            FileObject locationFileObject = FileUtil.toFileObject(locationFile);
            if (locationFileObject != null) {
                if (FileUtil.isArchiveFile(locationFileObject)) {
                    locationFileObject = FileUtil.getArchiveRoot(locationFileObject);
                }
                roots.put(location, locationFileObject);
            }
        }
    }
    return roots;
}
 
Example 3
Source File: FallbackDefaultJavaPlatform.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static ClassPath sysProp2CP(String propname) {
    String sbcp = System.getProperty(propname);
    if (sbcp == null) {
        return null;
    }
    List<URL> roots = new ArrayList<>();
    StringTokenizer tok = new StringTokenizer(sbcp, File.pathSeparator);
    while (tok.hasMoreTokens()) {
        File f = new File(tok.nextToken());
        if (!f.exists()) {
            continue;
        }
        URL u;
        try {
            File normf = FileUtil.normalizeFile(f);
            u = Utilities.toURI(normf).toURL();
        } catch (MalformedURLException x) {
            throw new AssertionError(x);
        } 
        if (FileUtil.isArchiveFile(u)) {
            u = FileUtil.getArchiveRoot(u);
        }
        roots.add(u);
    }
    return ClassPathSupport.createClassPath(roots.toArray(new URL[roots.size()]));
}
 
Example 4
Source File: ProjectTestBase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
     * Creates boot {@link ClassPath} for platform the test is running on,
     * it uses the sun.boot.class.path property to find out the boot path roots.
     * @return ClassPath
     * @throws java.io.IOException when boot path property contains non valid path
     */
    public static ClassPath createBootClassPath() throws IOException {
        String bootPath = System.getProperty("sun.boot.class.path");
        String[] paths = bootPath.split(File.pathSeparator);
        List<URL> roots = new ArrayList<>(paths.length);
        for (String path : paths) {
            File f = new File(path);
            if (!f.exists()) {
                continue;
            }
            URL url = Utilities.toURI(f).toURL();
            if (FileUtil.isArchiveFile(url)) {
                url = FileUtil.getArchiveRoot(url);
            }
            roots.add(url);
//            System.out.println(url);
        }
//        System.out.println("-----------");
        return ClassPathSupport.createClassPath(roots.toArray(new URL[roots.size()]));
    }
 
Example 5
Source File: WildFlyProperties.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private List<URL> selectJars(FileObject file) {
    if (file == null) {
        return Collections.EMPTY_LIST;
    }
    if (file.isData()) {
        if (file.isValid() && FileUtil.isArchiveFile(file)) {
            URL url = URLMapper.findURL(file, URLMapper.EXTERNAL);
            if (url != null) {
                return Collections.singletonList(FileUtil.getArchiveRoot(url));
            }
        }
        return Collections.EMPTY_LIST;
    }
    List<URL> result = new ArrayList<URL>();
    for (FileObject child : file.getChildren()) {
        result.addAll(selectJars(child));
    }
    return result;
}
 
Example 6
Source File: ClassPathExtender.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean addArchiveFiles(final String classPathId, FileObject[] archiveFiles, final String projectXMLElementName) throws IOException {
    for (int i = 0; i < archiveFiles.length; i++) {
        FileObject archiveFile = archiveFiles[i];
        if (FileUtil.isArchiveFile(archiveFile)) {
            archiveFiles[i] = FileUtil.getArchiveRoot(archiveFile);
        }           
    }
    URI[] archiveFileURIs = new URI[archiveFiles.length];
    for (int i = 0; i < archiveFiles.length; i++) {
        archiveFileURIs[i] = archiveFiles[i].toURI();
    }        
    return this.delegate.handleRoots(archiveFileURIs, classPathId, projectXMLElementName, ClassPathModifier.ADD);
}
 
Example 7
Source File: ClasspathPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean accept(File f) {
    if (f.isDirectory())
        return true;            
    try {
        return FileUtil.isArchiveFile(Utilities.toURI(f).toURL());
    } catch (MalformedURLException mue) {
        ErrorManager.getDefault().notify(mue);
        return false;
    }
}
 
Example 8
Source File: BootClassPathImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private PathResourceImplementation createFxCPImpl(JavaPlatform pat) {
    for (FileObject fo : pat.getInstallFolders()) {
        FileObject jdk8 = fo.getFileObject("jre/lib/ext/jfxrt.jar"); // NOI18N
        if (jdk8 == null) {
            FileObject jdk7 = fo.getFileObject("jre/lib/jfxrt.jar"); // NOI18N
            if (jdk7 != null) {
                // jdk7 add the classes on bootclasspath
                if (FileUtil.isArchiveFile(jdk7)) {
                    return ClassPathSupport.createResource(FileUtil.getArchiveRoot(jdk7.toURL()));
                }
            }
        }
    }
    return null;
}
 
Example 9
Source File: JPDAStart.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static URL fileToURL (File file, Project project, boolean reportNonExistingFiles, boolean withSlash) {
    FileObject fileObject = FileUtil.toFileObject (file);
    if (fileObject == null) {
        if (reportNonExistingFiles) {
            String path = file.getAbsolutePath();
            project.log("Have no file for "+path, Project.MSG_WARN);
        }
        return null;
    }
    if (FileUtil.isArchiveFile (fileObject)) {
        fileObject = FileUtil.getArchiveRoot (fileObject);
        if (fileObject == null) {
            project.log("Bad archive "+file.getAbsolutePath(), Project.MSG_WARN);
            /*
            ErrorManager.getDefault().notify(ErrorManager.getDefault().annotate(
                    new NullPointerException("Bad archive "+file.toString()),
                    NbBundle.getMessage(JPDAStart.class, "MSG_WrongArchive", file.getAbsolutePath())));
             */
            return null;
        }
    }
    if (withSlash) {
        return FileUtil.urlForArchiveOrDir(file);
    } else {
        return fileObject.toURL ();
    }
}
 
Example 10
Source File: HintTestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static synchronized List<URL> getBootClassPath() {
    if (bootClassPath == null) {
        try {
            String cp = System.getProperty("sun.boot.class.path");
            List<URL> urls = new ArrayList<URL>();
            String[] paths = cp.split(Pattern.quote(System.getProperty("path.separator")));

            for (String path : paths) {
                File f = new File(path);

                if (!f.canRead())
                    continue;

                FileObject fo = FileUtil.toFileObject(f);

                if (FileUtil.isArchiveFile(fo)) {
                    fo = FileUtil.getArchiveRoot(fo);
                }

                if (fo != null) {
                    urls.add(fo.getURL());
                }
            }

            bootClassPath = urls;
        } catch (FileStateInvalidException e) {
            if (log.isLoggable(Level.SEVERE))
                log.log(Level.SEVERE, e.getMessage(), e);
        }
    }

    return bootClassPath;
}
 
Example 11
Source File: RepositoryForBinaryQueryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private URL[] getJavadocJarRoot(File file) {
    try {
        if (file.exists()) {
            FileObject fo = FileUtil.toFileObject(file);
            if (!FileUtil.isArchiveFile(fo)) {
                //#124175  ignore any jar files that are not jar files (like when downloaded file is actually an error html page).
                Logger.getLogger(RepositoryForBinaryQueryImpl.class.getName()).log(Level.INFO, "javadoc in repository is not really a JAR: {0}", file);
                return new URL[0];
            }
            //try detecting the source path root, in case the source jar has the sources not in root.
            Date date = (Date) fo.getAttribute(ATTR_STAMP);
            String path = (String) fo.getAttribute(ATTR_PATH);
            if (date == null || fo.lastModified().after(date)) {
                path = checkPath(FileUtil.getArchiveRoot(fo), fo);
            }

            URL[] url;
            if (path != null) {
                url = new URL[1];
                URL root = FileUtil.getArchiveRoot(Utilities.toURI(file).toURL());
                if (!path.endsWith("/")) { //NOI18N
                    path = path + "/"; //NOI18N
                }
                url[0] = new URL(root, path);
            } else {
                 url = new URL[1];
                url[0] = FileUtil.getArchiveRoot(Utilities.toURI(file).toURL());
            }
            return url;
        }
    } catch (MalformedURLException exc) {
        ErrorManager.getDefault().notify(exc);
    }
    return new URL[0];
}
 
Example 12
Source File: RepositoryForBinaryQueryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized URL[] getRoots() {
    URL[] toRet;
    checkCurrentProject();
    Project prj = currentProject;
    if (prj != null) {
        toRet = new URL[0];
    } else {
        URL root = FileUtil.isArchiveFile(binary) ? FileUtil.getArchiveRoot(binary) : binary;
        File[] f = SourceJavadocByHash.find(root, true);
        if (f != null) {
            List<URL> accum = new ArrayList<URL>();
            for (File ff : f) {
                URL[] url = getJavadocJarRoot(ff);
                if (url != null) {
                    accum.addAll(Arrays.asList(url));
                }
            }
            toRet = accum.toArray(new URL[0]);
        } else if (javadocJarFile != null && javadocJarFile.exists()) {
            toRet = getJavadocJarRoot(javadocJarFile);
        } else if (fallbackJavadocJarFile != null && fallbackJavadocJarFile.exists()) {
            toRet = getJavadocJarRoot(fallbackJavadocJarFile);
        } else {
            toRet = checkShadedMultiJars();
        }
    }
    if (!Arrays.equals(cached, toRet)) {
        //how to figure otherwise that something changed, possibly multiple people hold the result instance
        // and one asks the roots, later we get event from outside, but then the cached value already updated..
        RP.post(new Runnable() {
            @Override
            public void run() {
                support.fireChange();
            }
        });
    }
    cached = toRet;
    return toRet;
}
 
Example 13
Source File: Hk2PluginProperties.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param file
 * @return
 * @throws java.net.MalformedURLException
 */
public static URL fileToUrl(File file) throws MalformedURLException {
    File nfile = FileUtil.normalizeFile(file);
    URL url = nfile.toURI().toURL();
    if (FileUtil.isArchiveFile(url)) {
        url = FileUtil.getArchiveRoot(url);
    }
    return url;
}
 
Example 14
Source File: Hk2LibraryProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<URL> translateArchiveUrls(List<URL> urls) {
    List<URL> result = new ArrayList<>(urls.size());
    for (URL u : urls) {
        if (FileUtil.isArchiveFile(u)) {
            result.add(FileUtil.getArchiveRoot(u));
        } else {
            result.add(u);
        }
    }
    return result;
}
 
Example 15
Source File: LibraryUtil.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public static String getLibrary(ClassPath classPath, String resource) {
    // Find as resource as a class
    String classNameAsPath = resource.replace('.', '/') + ".class";
    FileObject classResource = classPath.findResource(classNameAsPath);
    if (classResource == null) {
        // Find as resource as a package
        classNameAsPath = resource.replace('.', '/');
        classResource = classPath.findResource(classNameAsPath);

    }
    if (classResource != null) {
        FileObject archiveFile = FileUtil.getArchiveFile(classResource);
        if (archiveFile != null && FileUtil.isArchiveFile(archiveFile)) {
            File toFile = FileUtil.toFile(archiveFile);
            try {
                JarFile jf = new JarFile(toFile);
                Manifest manifest = jf.getManifest();
                Attributes mainAttributes = manifest.getMainAttributes();

                // Find library version by Bundle information (Eclipse/OSGi)
                if (mainAttributes.containsKey(BUNDLE_NAME) && mainAttributes.containsKey(BUNDLE_VERSION)) {
                    if (!mainAttributes.getValue(BUNDLE_NAME).isEmpty()) {
                        return mainAttributes.getValue(BUNDLE_NAME);
                    }
                }

                // If unsuccessful, try by default Manifest Headers
                if (mainAttributes.containsKey(Name.IMPLEMENTATION_TITLE) && mainAttributes.containsKey(Name.IMPLEMENTATION_VERSION)) {
                    if (!mainAttributes.getValue(Name.IMPLEMENTATION_TITLE).isEmpty()) {
                        return mainAttributes.getValue(Name.IMPLEMENTATION_TITLE);
                    }
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

    }
    return null;
}
 
Example 16
Source File: Hk2PluginProperties.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param file
 * @return
 * @throws java.net.MalformedURLException
 */
public static URL fileToUrl(File file) throws MalformedURLException {
    File nfile = FileUtil.normalizeFile(file);
    URL url = nfile.toURI().toURL();
    if (FileUtil.isArchiveFile(url)) {
        url = FileUtil.getArchiveRoot(url);
    }
    return url;
}
 
Example 17
Source File: PreferredCCParser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static JavaSource getJavaSource(SourcePathProvider sp) {
    String[] roots = sp.getOriginalSourceRoots();
    List<FileObject> sourcePathFiles = new ArrayList<FileObject>();
    for (String root : roots) {
        FileObject fo = FileUtil.toFileObject (new java.io.File(root));
        if (fo != null && FileUtil.isArchiveFile (fo)) {
            fo = FileUtil.getArchiveRoot (fo);
        }
        sourcePathFiles.add(fo);
    }
    ClassPath bootPath = ClassPathSupport.createClassPath(new FileObject[] {});
    ClassPath classPath = ClassPathSupport.createClassPath(new FileObject[] {});
    ClassPath sourcePath = ClassPathSupport.createClassPath(sourcePathFiles.toArray(new FileObject[] {}));
    return JavaSource.create(ClasspathInfo.create(bootPath, classPath, sourcePath), new FileObject[] {});
}
 
Example 18
Source File: Hk2LibraryProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Set {@see LibraryImplementation} content for given library name.
 * <p/>
 * @param lib         Target {@see LibraryImplementation}.
 * @param namePattern Library name pattern to search for it in
 *                    <code>PayaraLibrary</code> list.
 * @param libraryName Library name in returned Library instance.
 */
private void setLibraryImplementationContent(LibraryImplementation lib,
        Pattern namePattern, String libraryName) {
    ConfigBuilder cb = ConfigBuilderProvider.getBuilder(server);
    List<PayaraLibrary> pfLibs = cb.getLibraries(server.getVersion());
    for (PayaraLibrary pfLib : pfLibs) {
        if (namePattern.matcher(pfLib.getLibraryID()).matches()) {
            List<String> javadocLookups = pfLib.getJavadocLookups();
            lib.setName(libraryName);
            // Build class path
            List<URL> cp = new ArrayList<>();
            for (URL url : pfLib.getClasspath()) {
                if (FileUtil.isArchiveFile(url)) {
                    cp.add(FileUtil.getArchiveRoot(url));
                } else {
                    cp.add(url);
                }
            }
            // Build java docs
            List<URL> javadoc = new ArrayList<>();
            if (javadocLookups != null) {
                for (String lookup : javadocLookups) {
                    try {
                        File j2eeDoc = InstalledFileLocator
                                .getDefault().locate(lookup,
                                JAVAEE_DOC_CODE_BASE, false);
                        if (j2eeDoc != null) {
                            javadoc.add(fileToUrl(j2eeDoc));
                        }
                    } catch (MalformedURLException e) {
                        ErrorManager.getDefault()
                                .notify(ErrorManager.INFORMATIONAL, e);
                    }
                }
            }
            lib.setContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH,
                    cp);
            lib.setContent(J2eeLibraryTypeProvider.VOLUME_TYPE_JAVADOC,
                    javadoc);
        }
    }
}
 
Example 19
Source File: Util.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Get JRE extension JARs/ZIPs.
 * @param extPath a native-format path for e.g. jre/lib/ext
 * @return a native-format classpath for extension JARs and ZIPs found in it
 */
public static String getExtensions (String extPath) {
    if (extPath == null) {
        return null;
    }
    final StringBuilder sb = new StringBuilder();
    final StringTokenizer tk = new StringTokenizer (extPath, File.pathSeparator);
    while (tk.hasMoreTokens()) {
        File extFolder = FileUtil.normalizeFile(new File(tk.nextToken()));
        File[] files = extFolder.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (!f.exists()) {
                    //May happen, eg. broken link, it is safe to ignore it
                    //since it is an extension directory, but log it.
                    LOG.log(
                            Level.WARNING,
                            NbBundle.getMessage(Util.class,"MSG_BrokenExtension"),
                            new Object[] {
                                f.getName(),
                                extFolder.getAbsolutePath()
                            });
                    continue;
                }
                if (Utilities.isMac() && "._.DS_Store".equals(f.getName())) {  //NOI18N
                    //Ignore Apple temporary ._.DS_Store files in the lib/ext folder
                    continue;
                }
                FileObject fo = FileUtil.toFileObject(f);
                if (fo == null) {
                    LOG.log(
                            Level.WARNING,
                            "Cannot create FileObject for file: {0} exists: {1}", //NOI18N
                            new Object[]{
                                f.getAbsolutePath(),
                                f.exists()
                            });
                    continue;
                }
                if (!FileUtil.isArchiveFile(fo)) {
                    // #42961: Mac OS X has e.g. libmlib_jai.jnilib.
                    continue;
                }
                sb.append(File.pathSeparator);
                sb.append(files[i].getAbsolutePath());
            }
        }
    }
    if (sb.length() == 0) {
        return null;
    }
    return sb.substring(File.pathSeparator.length());
}
 
Example 20
Source File: J2SEPlatformDefaultJavadocImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NonNull
Collection<? extends URI> accept(@NonNull FileObject fo) {
    if (fo.canRead()) {
        if (fo.isFolder()) {
            if ("docs".equals(fo.getName())) {  //NOI18N
                return Collections.singleton(fo.toURI());
            }
        } else if (fo.isData()) {
            final String nameExt = fo.getNameExt();
            final String vendorPath = VENDOR_DOCS.get(nameExt);
            if (vendorPath != null) {
                if (FileUtil.isArchiveFile(fo)) {
                    try {
                        return Collections.singleton(
                            new URL (FileUtil.getArchiveRoot(fo.toURL()).toExternalForm() + vendorPath).toURI());
                    } catch (MalformedURLException | URISyntaxException e) {
                        LOG.log(
                            Level.INFO,
                            "Invalid Javadoc URI for file : {0}, reason: {1}",
                            new Object[]{
                                FileUtil.getFileDisplayName(fo),
                                e.getMessage()
                        });
                        //pass
                    }
                }
            } else if (DOCS_FILE_PATTERN.matcher(nameExt).matches() && !JAVAFX_FILE_PATTERN.matcher(nameExt).matches()) {
                final FileObject root = FileUtil.getArchiveRoot(fo);
                if (root != null) {
                    final List<URI> roots = new ArrayList<>(DOCS_PATHS.size());
                    for (String path : DOCS_PATHS) {
                        final FileObject docRoot = root.getFileObject(path);
                        if (docRoot != null) {
                            roots.add(docRoot.toURI());
                        }
                    }
                    return Collections.unmodifiableCollection(roots);
                }
            }
        }
    }
    return Collections.emptySet();
}