Java Code Examples for org.openide.filesystems.FileObject#canRead()

The following examples show how to use org.openide.filesystems.FileObject#canRead() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: netbeans   File: SourcePathProviderImpl.java    License: Apache License 2.0 6 votes vote down vote up
private ClassPath getAdditionalClassPath(File baseDir) {
    try {
        String root = BaseUtilities.toURI(baseDir).toURL().toExternalForm();
        Properties sourcesProperties = Properties.getDefault ().getProperties ("debugger").getProperties ("sources");
        List<String> additionalSourceRoots = (List<String>) sourcesProperties.
                getProperties("additional_source_roots").
                getMap("project", Collections.emptyMap()).
                get(root);
        if (additionalSourceRoots == null || additionalSourceRoots.isEmpty()) {
            return null;
        }
        List<FileObject> additionalSourcePath = new ArrayList<FileObject>(additionalSourceRoots.size());
        for (String ar : additionalSourceRoots) {
            FileObject fo = getFileObject(ar);
            if (fo != null && fo.canRead()) {
                additionalSourcePath.add(fo);
            }
        }
        this.additionalSourceRoots = new LinkedHashSet<String>(additionalSourceRoots);
        return createClassPath(
                additionalSourcePath.toArray(new FileObject[0]));
    } catch (MalformedURLException ex) {
        Exceptions.printStackTrace(ex);
        return null;
    }
}
 
Example 2
Source Project: netbeans   File: SourcePathProviderImpl.java    License: Apache License 2.0 6 votes vote down vote up
private List<FileObject> getAdditionalRemoteClassPath() {
    Properties sourcesProperties = Properties.getDefault ().getProperties ("debugger").getProperties ("sources");
    List<String> additionalSourceRoots = (List<String>) sourcesProperties.
            getProperties("additional_source_roots").
            getCollection("src_roots", Collections.emptyList());
    if (additionalSourceRoots == null || additionalSourceRoots.isEmpty()) {
        return null;
    }
    List<FileObject> additionalSourcePath = new ArrayList<FileObject>(additionalSourceRoots.size());
    for (String ar : additionalSourceRoots) {
        FileObject fo = getFileObject(ar);
        if (fo != null && fo.canRead()) {
            additionalSourcePath.add(fo);
        }
    }
    this.additionalSourceRoots = new LinkedHashSet<String>(additionalSourceRoots);
    return additionalSourcePath;
    //return ClassPathSupport.createClassPath(
    //        additionalSourcePath.toArray(new FileObject[0]));
}
 
Example 3
Source Project: netbeans   File: SourcePathProviderImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static ClassPath createClassPath(FileObject[] froots) {
    List<PathResourceImplementation> pris = new ArrayList<PathResourceImplementation> ();
    for (FileObject fo : froots) {
        if (fo != null && fo.canRead()) {
            try {
                URL url = fo.toURL();
                pris.add(ClassPathSupport.createResource(url));
            } catch (IllegalArgumentException iaex) {
                // Can be thrown from ClassPathSupport.createResource()
                // Ignore - bad source root
                //logger.log(Level.INFO, "Invalid source root = "+fo, iaex);
                logger.warning(iaex.getLocalizedMessage());
            }
        }
    }
    return ClassPathSupport.createClassPath(pris);
}
 
Example 4
Source Project: netbeans   File: SourcePathProviderImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static ClassPath createClassPath(URL[] urls) {
    List<PathResourceImplementation> pris = new ArrayList<PathResourceImplementation> ();
    for (URL url : urls) {
        FileObject fo = URLMapper.findFileObject(url);
        if (fo != null && fo.canRead()) {
            try {
                pris.add(ClassPathSupport.createResource(url));
            } catch (IllegalArgumentException iaex) {
                // Can be thrown from ClassPathSupport.createResource()
                // Ignore - bad source root
                //logger.log(Level.INFO, "Invalid source root = "+fo, iaex);
                logger.warning(iaex.getLocalizedMessage());
            }
        }
    }
    return ClassPathSupport.createClassPath(pris);
}
 
Example 5
@NonNull
Collection<? extends URI> accept(@NonNull final FileObject fo) {
    if (fo.canRead() && fo.isData()) {
        final String nameExt = fo.getNameExt();
        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();
}
 
Example 6
Source Project: netbeans   File: GrailsPluginSupport.java    License: Apache License 2.0 6 votes vote down vote up
private List<GrailsPlugin> loadInstalledPlugins10() {
    List<GrailsPlugin> plugins = new ArrayList<GrailsPlugin>();
    try {
        FileObject pluginsDir = project.getProjectDirectory().getFileObject("plugins"); //NOI18N
        if (pluginsDir != null && pluginsDir.isFolder()) {
            pluginsDir.refresh();
            for (FileObject child : pluginsDir.getChildren()) {
                if (child.isFolder()) {
                    FileObject descriptor = child.getFileObject("plugin.xml"); //NOI18N
                    if (descriptor != null && descriptor.canRead()) {
                        plugins.add(getPluginFromInputStream(descriptor.getInputStream(), null));
                    }
                }
            }
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
    Collections.sort(plugins);
    return plugins;
}
 
Example 7
@NonNull
private static FileObject[] getSources(@NonNull final Collection<? extends FileObject> installFolders) {
    if (installFolders.isEmpty()) {
        return new FileObject[0];
    }
    final FileObject installFolder = installFolders.iterator().next();
    if (installFolder == null || !installFolder.isValid()) {
        return new FileObject[0];
    }
    FileObject src = installFolder.getFileObject("src.zip");    //NOI18N
    if (src == null) {
        src = installFolder.getFileObject("src.jar");    //NOI18N
    }
    if (src == null || !src.canRead()) {
        return new FileObject[0];
    }
    FileObject root = FileUtil.getArchiveRoot(src);
    if (root == null) {
        return new FileObject[0];
    }
    if (Utilities.getOperatingSystem() == Utilities.OS_MAC) {
        FileObject reloc = root.getFileObject("src");   //NOI18N
        if (reloc != null) {
            root = reloc;
        }
    }
    return new FileObject[]{root};
}
 
Example 8
Source Project: netbeans   File: BiIconEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * translates resource path defined in {@link java.beans.BeanInfo}'s subclass
 * that complies with {@link Class#getResource(java.lang.String) Class.getResource} format
 * to format complying with {@link ClassPath#getResourceName(org.openide.filesystems.FileObject) ClassPath.getResourceName}
 * @param resourcePath absolute path or path relative to package of BeanInfo's subclass
 * @param beanInfo BeanInfo's subclass
 * @return path as URL
 * @throws FileStateInvalidException invalid FileObject
 * @throws FileNotFoundException resource cannot be found
 */
private static URL resolveIconPath(String resourcePath, FileObject beanInfo)
        throws FileStateInvalidException, FileNotFoundException {
    ClassPath cp = ClassPath.getClassPath(beanInfo, ClassPath.SOURCE);
    String path = resourcePath.charAt(0) != '/'
            ? '/' + cp.getResourceName(beanInfo.getParent()) + '/' + resourcePath
            : resourcePath;
    FileObject res = cp.findResource(path);
    if (res != null && res.canRead() && res.isData()) {
        return res.getURL();
    } else {
        throw new FileNotFoundException(path);
    }
}
 
Example 9
Source Project: netbeans   File: SourcePathProviderImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the source root (if any) for given url.
 *
 * @param url a url of resource file
 *
 * @return the source root or <code>null</code> when no source root was found.
 */
@Override
public synchronized String getSourceRoot(String url) {
    FileObject fo;
    try {
        fo = URLMapper.findFileObject(new java.net.URL(url));
    } catch (java.net.MalformedURLException ex) {
        fo = null;
    }
    FileObject[] roots = null;
    if (fo != null && fo.canRead()) {
        ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
        if (cp != null) {
            roots = cp.getRoots();
        }
    }
    if (roots == null) {
        roots = originalSourcePath.getRoots();
    }
    for (FileObject fileObject : roots) {
        String rootURL = fileObject.toURL().toString();
        if (url.startsWith(rootURL)) {
            String root = getRoot(fileObject);
            if (root != null) {
                return root;
            }
        }
    }
    return null; // not found
}
 
Example 10
Source Project: netbeans   File: RulesManager.java    License: Apache License 2.0 5 votes vote down vote up
/** Read rules from system filesystem */
private static List<Pair<POMErrorFixBase, FileObject>> readRules( FileObject folder ) {
    List<Pair<POMErrorFixBase,FileObject>> rules = new LinkedList<Pair<POMErrorFixBase,FileObject>>();
    
    if (folder == null) {
        return rules;
    }

    Queue<FileObject> q = new LinkedList<FileObject>();
    
    q.offer(folder);
    
    while(!q.isEmpty()) {
        FileObject o = q.poll();
        
        if (o.isFolder()) {
            q.addAll(Arrays.asList(o.getChildren()));
            continue;
        }
        
        if (!o.isData()) {
            continue;
        }
        
        String name = o.getNameExt().toLowerCase();

        if ( o.canRead() ) {
            POMErrorFixBase r = null;
            if ( name.endsWith( INSTANCE_EXT ) ) {
                r = instantiateRule(o);
            }
            if ( r != null ) {
                rules.add( Pair.<POMErrorFixBase,FileObject>of( r, o ) );
            }
        }
    }
    return rules;
}
 
Example 11
Source Project: netbeans   File: MatchingObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check validity status of this item.
 * 
 * @return  an invalidity status of this item if it is invalid,
 *          or {@code null} if this item is valid
 * @author  Tim Boudreau
 * @author  Marian Petras
 */
private InvalidityStatus getFreshInvalidityStatus() {
    log(FINER, "getInvalidityStatus()");                            //NOI18N
    FileObject f = getFileObject();
    if (!f.isValid()) {
        log(FINEST, " - DELETED");            
        return InvalidityStatus.DELETED;
    }
    if (f.isFolder()) {
        log(FINEST, " - BECAME_DIR");            
        return InvalidityStatus.BECAME_DIR;
    }
    
    long stamp = f.lastModified().getTime();
    if ((!refreshed && stamp > resultModel.getStartTime())
            || (refreshed && stamp > timestamp)) {
        log(SEVERE, "file's timestamp changed since start of the search");
        if (LOG.isLoggable(FINEST)) {
            final java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTimeInMillis(stamp);
            log(FINEST, " - file stamp:           " + stamp + " (" + cal.getTime() + ')');
            cal.setTimeInMillis(resultModel.getStartTime());
            log(FINEST, " - result model created: " + resultModel.getStartTime() + " (" + cal.getTime() + ')');
        }            
        return InvalidityStatus.CHANGED;
    }
    
    if (f.getSize() > Integer.MAX_VALUE) {            
        return InvalidityStatus.TOO_BIG;
    }
    
    if (!f.canRead()) {           
        return InvalidityStatus.CANT_READ;
    }
    
    return null;
}
 
Example 12
Source Project: netbeans   File: FileElement.java    License: Apache License 2.0 5 votes vote down vote up
/** #26521, 114976 - ignore not readable and windows' locked files. */
private static void handleIOException(FileObject fo, IOException ioe) throws IOException {
    if (fo.canRead()) {
        if (!BaseUtilities.isWindows() || !(ioe instanceof FileNotFoundException) || !fo.isValid() || !fo.getName().toLowerCase().contains("ntuser")) {//NOI18N
            throw ioe;
        }
    }
}
 
Example 13
Source Project: netbeans   File: GotoBaseAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnabled() {
    if (isValid(Utilities.getFocusedComponent()) == false) {
        return false;
    }

    FileObject fileObject = findTargetFO();
    if (fileObject != null && fileObject.canRead()) {
        return true;
    }
    return false;
}
 
Example 14
Source Project: netbeans   File: SourceMapsScanner.java    License: Apache License 2.0 5 votes vote down vote up
private void registerIfSourceMap(FileObject fo) {
    if (!fo.isValid() || !fo.isData() ||
        !fo.getExt().equalsIgnoreCase(SRC_MAP_EXT) ||
        !fo.canRead()) {

        return ;
    }
    LOG.log(Level.FINE, "  found source map (?) {0}", fo);
    SourceMap sm;
    try {
        sm = SourceMap.parse(fo.asText("UTF-8"));
    } catch (IOException | IllegalArgumentException ex) {
        return ;
    }
    FileObject compiledFO;
    String compiledFile = sm.getFile();
    if (compiledFile == null) {
        compiledFO = findCompiledFile(fo);
    } else {
        compiledFO = fo.getParent().getFileObject(compiledFile);
    }
    LOG.log(Level.FINE, "  have source map for generated file {0}", compiledFO);
    if (compiledFO != null) {
        synchronized (sourceMaps) {
            sourceMaps.put(fo, sm);
        }
        smt.registerTranslation(compiledFO, sm);
    }
}
 
Example 15
Source Project: netbeans   File: GsfHintsManager.java    License: Apache License 2.0 5 votes vote down vote up
/** Read rules from system filesystem */
    private static List<Pair<Rule,FileObject>> readRules( FileObject folder ) {

        List<Pair<Rule,FileObject>> rules = new LinkedList<Pair<Rule,FileObject>>();
        
        if (folder == null) {
            return rules;
        }

        //HashMap<FileObject,DefaultMutableTreeNode> dir2node = new HashMap<FileObject,DefaultMutableTreeNode>();

//        // XXX Probably not he best order
//        Enumeration e = folder.getData( true );
        
        Enumeration<FileObject> e = Collections.enumeration(getSortedDataRecursively(folder));
        while( e.hasMoreElements() ) {
            FileObject o = e.nextElement();
            String name = o.getNameExt().toLowerCase();

            if ( o.canRead() ) {
                Rule r = null;
                if ( name.endsWith( INSTANCE_EXT ) ) {
                    r = instantiateRule(o);
                }
                if ( r != null ) {
                    rules.add( Pair.<Rule,FileObject>of( r, o ) );
                }
            }
        }
        Collections.sort(rules, new Comparator<Pair<Rule,FileObject>>() {
            @Override
            public int compare(Pair<Rule,FileObject> p1, Pair<Rule,FileObject> p2) {
                return p1.first().getDisplayName().compareTo(p2.first().getDisplayName());
            }
        });
        return rules;
    }
 
Example 16
Source Project: netbeans   File: StructHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Reparses file. 
 *
 * @param  fire  true if should fire changes
 */
private PropertiesStructure reparseNowBlocking(boolean fire) {
    PropertiesStructure propStructure = null;
    synchronized (this) {
        if (!parsingAllowed) {
            return null;
        }

        FileObject fo = propFileEntry.getFile();
        if(!fo.canRead()) {
            // whatever happend - the file does not exist
            // so don't even try to parse it
            // XXX may be a HACK. see issue #63321. This is supposed to be
            // rewriten after 6.0.
            return null;
        }
        PropertiesParser parser = new PropertiesParser(propFileEntry);

        try {
            parserWRef = new WeakReference<PropertiesParser>(parser);

            parser.initParser();
            propStructure = parser.parseFile();

        } catch (IOException ioe) {
            // di do prdele
        } finally {
            parser.clean();
        }
    }
    updatePropertiesStructure(propStructure, fire);
    return propStructure;
}
 
Example 17
@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();
}
 
Example 18
Source Project: netbeans   File: RulesManager.java    License: Apache License 2.0 4 votes vote down vote up
/** Read rules from system filesystem */
private List<Pair<Rule,FileObject>> readRules( FileObject folder ) {
    List<Pair<Rule,FileObject>> rules = new LinkedList<Pair<Rule,FileObject>>();
    
    if (folder == null) {
        return rules;
    }

    Queue<FileObject> q = new LinkedList<FileObject>();
    
    q.offer(folder);
    
    while(!q.isEmpty()) {
        FileObject o = q.poll();
        
        o.removeFileChangeListener(this);
        o.addFileChangeListener(this);
        
        if (o.isFolder()) {
            q.addAll(Arrays.asList(o.getChildren()));
            continue;
        }
        
        if (!o.isData()) {
            continue;
        }
        
        String name = o.getNameExt().toLowerCase();

        if ( o.canRead() ) {
            Rule r = null;
            if ( name.endsWith( INSTANCE_EXT ) ) {
                r = instantiateRule(o);
            }
            if ( r != null ) {
                rules.add( new Pair<Rule,FileObject>( r, o ) );
            }
        }
    }
    return rules;
}
 
Example 19
Source Project: netbeans   File: JavaRefactoringUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * returns true if file's mime type is text/x-java and file is on know source path
 * @param file 
 * @return 
 */
@SuppressWarnings("deprecation")
public static boolean isRefactorable(FileObject file) {
    return RefactoringUtils.isRefactorable(file) && file.canWrite() && file.canRead();
}