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

The following are Jave code examples for showing how to use canRead() of the org.openide.filesystems.FileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: JavaFxDefaultJavadocImpl.java   View Source Code Vote up 6 votes
@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 2
Project: incubator-netbeans   File: SourcePathProviderImpl.java   View Source Code Vote up 6 votes
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 3
Project: incubator-netbeans   File: SourcePathProviderImpl.java   View Source Code Vote up 6 votes
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 4
Project: incubator-netbeans   File: SourcePathProviderImpl.java   View Source Code Vote up 6 votes
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 5
Project: incubator-netbeans   File: SourcePathProviderImpl.java   View Source Code Vote up 6 votes
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 6
Project: incubator-netbeans   File: MatchingObject.java   View Source Code Vote up 5 votes
/**
 * 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 7
Project: incubator-netbeans   File: FallbackDefaultJavaPlatform.java   View Source Code Vote up 5 votes
@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
Project: incubator-netbeans   File: SourceMapsScanner.java   View Source Code Vote up 5 votes
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 9
Project: incubator-netbeans   File: GsfHintsManager.java   View Source Code Vote up 5 votes
/** 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 10
Project: incubator-netbeans   File: BiIconEditor.java   View Source Code Vote up 5 votes
/**
 * 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 11
Project: incubator-netbeans   File: StructHandler.java   View Source Code Vote up 5 votes
/**
 * 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 12
Project: incubator-netbeans   File: SourcePathProviderImpl.java   View Source Code Vote up 5 votes
/**
 * 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 13
Project: incubator-netbeans   File: FileElement.java   View Source Code Vote up 5 votes
/** #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 14
Project: incubator-netbeans   File: RulesManager.java   View Source Code Vote up 5 votes
/** 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 15
Project: incubator-netbeans   File: J2SEPlatformDefaultJavadocImpl.java   View Source Code Vote up 4 votes
@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 16
Project: incubator-netbeans   File: RulesManager.java   View Source Code Vote up 4 votes
/** 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 17
Project: incubator-netbeans   File: JavaRefactoringUtils.java   View Source Code Vote up 2 votes
/**
 * 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();
}