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

The following examples show how to use org.openide.filesystems.FileObject#isValid() . 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: ConfigurationFiles.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<FileInfo> getFiles() {
    FileObject sourceDir = getSourceDirectory();
    if (sourceDir == null) {
        // broken project
        return Collections.emptyList();
    }
    List<FileInfo> files = new ArrayList<>();
    FileObject configDir = sourceDir.getFileObject(CONFIG_DIRECTORY);
    if (configDir != null
            && configDir.isFolder()
            && configDir.isValid()) {
        Enumeration<? extends FileObject> children = configDir.getChildren(true);
        while (children.hasMoreElements()) {
            FileObject child = children.nextElement();
            if (child.isData()
                    && child.isValid()
                    && FileUtils.isPhpFile(child)) {
                files.add(new FileInfo(child));
            }
        }
        Collections.sort(files, FileInfo.COMPARATOR);
    }
    return files;
}
 
Example 2
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void openLocation(CodeProfilingPoint.Location location) {
    File file = FileUtil.normalizeFile(new File(location.getFile()));
    final FileObject fileObject = FileUtil.toFileObject(file);

    if ((fileObject == null) || !fileObject.isValid()) {
        return;
    }

    final int documentOffset = getDocumentOffset(location);

    if (documentOffset == -1) {
        ProfilerDialogs.displayError(Bundle.Utils_CannotOpenSourceMsg());
        return;
    }

    GoToSource.openFile(fileObject, documentOffset);
}
 
Example 3
Source File: Nette2PhpModuleExtender.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Set<FileObject> extend(PhpModule phpModule) throws ExtendingException {
    Set<FileObject> result = new HashSet<>();
    FileObject sourceDirectory = phpModule.getSourceDirectory();
    if (sourceDirectory != null) {
        String projectDirectory = sourceDirectory.getPath();
        FileUtils.copyDirectory(new File(Nette2Options.getInstance().getSandbox()), new File(projectDirectory));
        File netteLibsDirectory = new File(projectDirectory, Constants.NETTE_LIBS_DIR);
        if (isValidNetteLibsDir(netteLibsDirectory) && getPanel().isCopyNetteCheckboxSelected()) {
            FileUtils.copyDirectory(new File(Nette2Options.getInstance().getNetteDirectory()), netteLibsDirectory);
        }
        FileObject bootstrap = FileUtil.toFileObject(new File(projectDirectory, Constants.COMMON_BOOTSTRAP_PATH));
        if (bootstrap != null && !bootstrap.isFolder() && bootstrap.isValid()) {
            result.add(bootstrap);
        }
        FileObject tempDir = sourceDirectory.getFileObject(Constants.NETTE_TEMP_DIR);
        if (tempDir != null) {
            FileUtils.chmod777Recursively(tempDir);
        }
    }
    return result;
}
 
Example 4
Source File: ConfigurationFiles.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<FileInfo> getFiles() {
    FileObject sourceDir = getSourceDirectory();
    if (sourceDir == null) {
        // broken project
        return Collections.emptyList();
    }
    List<FileInfo> files = new ArrayList<>();
    FileObject configDir = sourceDir.getFileObject(CONFIG_DIRECTORY);
    if (configDir != null
            && configDir.isFolder()
            && configDir.isValid()) {
        FileObject[] children = configDir.getChildren();
        for (FileObject child : children) {
            if (child.isData()) {
                files.add(new FileInfo(child));
            }
        }
    }
    Collections.sort(files, FileInfo.COMPARATOR);
    FileObject bootstrap = sourceDir.getFileObject(BOOTSTRAP_FILE);
    if (bootstrap != null) {
        files.add(new FileInfo(bootstrap));
    }
    return files;
}
 
Example 5
Source File: MultiModuleClassPathProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@CheckForNull
private FileObject getDir(@NonNull final String propname) {
    FileObject fo = dirCache.get(propname);
    if (fo == null || !fo.isValid()) {
        final URL u = getURL(propname);
        if (u != null) {
            fo = URLMapper.findFileObject(u);
            if (fo != null) {
                dirCache.put (propname, fo);
            }
        }
    }
    return fo;
}
 
Example 6
Source File: FolderPathLookup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void rebuild() {
    List<PairItem> pairItems = new ArrayList<PairItem>();
    for (FileObject fo : children.getChildren()) {
        if (!fo.isValid()) {
            // Can happen after modules are disabled. Ignore it.
            continue;
        }
        pairItems.add(new PairItem(fo));
    }
    content.setPairs(pairItems);
}
 
Example 7
Source File: PhpCoverageProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isUnderneathSourcesOnlyAndVisible(FileObject fo) {
    return fo != null
            && fo.isValid()
            && CommandUtils.isUnderSources(project, fo)
            && !CommandUtils.isUnderTests(project, fo, false)
            && !CommandUtils.isUnderSelenium(project, fo, false)
            && phpVisibilityQuery.isVisible(fo);
}
 
Example 8
Source File: DataProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
synchronized public static DataProvider getInstance() {
    if (instance == null) {
        instance = new DataProviderImpl();
        currentVersion = DEFAULT_VERSION;
    }
    if (data.isEmpty()) {
        File zipFile = InstalledFileLocator.getDefault().locate(zipFolder + "/" + codeFileNameFromVersion(currentVersion), "org.netbeans.modules.html.ojet", false); //NOI18N
        if (zipFile != null && zipFile.exists()) {
            docRoot = FileUtil.toFileObject(zipFile);
            docRoot = FileUtil.getArchiveRoot(docRoot);
            if (docRoot != null) {
                FileObject folder = docRoot.getFileObject("docs"); // NOI18N
                if (folder != null && folder.isValid()) {
                    for (FileObject child : folder.getChildren()) {
                        String name = child.getName();
                        if (name.startsWith("oj.oj")) {
                            name = name.substring(3);
                            data.put(name, new DataItemImpl.DataItemComponent(name, child.toURL().toString()));
                        } else if (OJETUtils.OJ_MODULE.equals(name)) {
                            moduleData = new DataItemImpl.DataItemModule(child.toURL().toString());
                        }
                    }
                }
            }
        }
    }
    return instance;
}
 
Example 9
Source File: CopyFile.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void performChange() {
    try {
        FileObject targetFo = FileHandlingFactory.getOrCreateFolder(target);
        FileObject Fo = fo;
        DataObject dob = DataObject.find(Fo);
        newOne = dob.copy(DataFolder.findFolder(targetFo));
        if(newName != null) {
            newOne.rename(newName);
        }
        FileObject[] newFiles = context.lookup(FileObject[].class);
        FileObject newFile = newOne.getPrimaryFile();
        newFile.setAttribute("originalFile", fo.getNameExt()); //NOI18N
        if (newFiles == null) {
            newFiles = new FileObject[]{newFile};
        } else {
            // rather a special case: there can be files from former run of the refactoring,
            // which had been undone. In that case, those files may be invalid and will cause
            // parser errors if processed.
            List<FileObject> stillValidFiles = new ArrayList<>(newFiles.length);
            for (FileObject f : newFiles) {
                if (f.isValid()) {
                    stillValidFiles.add(f);
                }
            }
            newFiles = new FileObject[stillValidFiles.size() + 1];
            stillValidFiles.toArray(newFiles);
            newFiles[newFiles.length - 1] = newFile;
        }
        context.add(newFiles);
        context.add(newFile);
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example 10
Source File: ClassPathProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean checkDirs(List<FileObject> dirs) {
    if (dirs == null) {
        return false;
    }
    for (FileObject fo : dirs) {
        if (!fo.isValid()) {
            return false;
        }
    }
    return true;
}
 
Example 11
Source File: AppClientPersistenceProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private PersistenceScope getPersistenceScope() {
    FileObject persistenceXml = persistenceScope.getPersistenceXml();
    if (persistenceXml != null && persistenceXml.isValid()) {
        return persistenceScope;
    }
    return null;
}
 
Example 12
Source File: Source.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a <code>Source</code> instance for a file. The <code>FileObject</code>
 * passed to this method has to be a valid data file. There is no <code>Source</code>
 * representation for a folder.
 * 
 * @param fileObject The file to get <code>Source</code> for.
 *
 * @return The <code>Source</code> for the given file or <code>null</code>
 *   if the file doesn't exist.
 */
// XXX: this should really be called 'get'
public static Source create (
    FileObject          fileObject
) {
    Parameters.notNull("fileObject", fileObject); //NOI18N
    if (!fileObject.isValid() || !fileObject.isData()) {
        return null;
    }
    
    return _get(fileObject.getMIMEType(), fileObject, Lookup.getDefault());
}
 
Example 13
Source File: JFXProjectUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the project uses Maven build system
 *
 * @param prj the project to check
 * @return true if is Maven project
 */
public static boolean isMavenProject(@NonNull final Project prj) {
    FileObject fo = prj.getProjectDirectory();
    if (fo == null || !fo.isValid()) {
        return false;
    }
    fo = fo.getFileObject("pom.xml"); //NOI18N
    return fo != null;
}
 
Example 14
Source File: AbstractFSTestCase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected FileObject getVersionedFolder() throws IOException {
    if (versionedFolder == null) {
        versionedFolder = createFolder(versionedPath);
        FileObject md = versionedFolder.getFileObject(TestVCS.TEST_VCS_METADATA);
        if(md == null || !md.isValid()) {
            createFolder(versionedPath + "/" + TestVCS.TEST_VCS_METADATA);
        }
        // cleanup the owner cache, this folder just became versioned 
        VersioningManager.getInstance().flushNullOwners(); 
    }
    return versionedFolder;
}
 
Example 15
Source File: WSITEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static FileObject getClientConfigFolder(Project p) {

        FileObject folder = null;

        WsitProvider wsitProvider = p.getLookup().lookup(WsitProvider.class);
        if (wsitProvider != null) {
            folder = wsitProvider.getConfigFilesFolder(true);
        }

        if (folder == null) {
            // proceed with default folder (META-INF) if the provider is not found
            Sources sources = ProjectUtils.getSources(p);
            if (sources == null) return null;
            SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            if ((sourceGroups != null) && (sourceGroups.length > 0)) {
                folder = sourceGroups[0].getRootFolder();
                if (folder != null) {
                    folder = folder.getFileObject("META-INF");
                }
                if ((folder == null) || (!folder.isValid())) {
                    try {
                        folder = sourceGroups[0].getRootFolder().createFolder("META-INF");
                    } catch (IOException ex) {
                        logger.log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        return folder;
    }
 
Example 16
Source File: AnnotationViewDataImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void computeInstances() {
    ArrayList<MarkProviderCreator> newCreators = new ArrayList<MarkProviderCreator>();
    ArrayList<UpToDateStatusProviderFactory> newFactories = new ArrayList<UpToDateStatusProviderFactory>();
    
    for(FileObject f : instanceFiles) {
        if (!f.isValid() || !f.isData()) {
            continue;
        }
        
        try {
            DataObject d = DataObject.find(f);
            InstanceCookie ic = d.getLookup().lookup(InstanceCookie.class);
            if (ic != null) {
                if (MarkProviderCreator.class.isAssignableFrom(ic.instanceClass())) {
                    MarkProviderCreator creator = (MarkProviderCreator) ic.instanceCreate();
                    newCreators.add(creator);
                } else if (UpToDateStatusProviderFactory.class.isAssignableFrom(ic.instanceClass())) {
                    UpToDateStatusProviderFactory factory = (UpToDateStatusProviderFactory) ic.instanceCreate();
                    newFactories.add(factory);
                }
            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, null, e);
        }
    }
    
    this.creators = newCreators;
    this.factories = newFactories;
}
 
Example 17
Source File: MetroConfigLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public MetroConfig loadMetroConfig(Project project) {
    FileObject cfgFolder = getConfigFolder(project, false);
    if ((cfgFolder != null) && (cfgFolder.isValid())) {
        try {
            FileObject cfgFile = cfgFolder.getFileObject(CFG_FILE_NAME);
            return (cfgFile != null) ? loadMetroConfig(cfgFile.getURL()) : null;
        } catch (FileStateInvalidException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return null;
}
 
Example 18
Source File: DBSchemaWizardIterator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Hack which sets the default target to the src/conf or src directory, 
 * whichever exists.
 */
private void setDefaultTarget() {
    FileObject targetFO;
    try {
        DataFolder target = wizardInstance.getTargetFolder();
        targetFO = target.getPrimaryFile();
    } catch (IOException e) {
        targetFO = null;
    }
    
    Project targetProject = Templates.getProject(wizardInstance);
    if (targetProject != null) {
        FileObject projectDir = targetProject.getProjectDirectory();
        if (targetFO == null || targetFO.equals(projectDir)) {
            FileObject newTargetFO = projectDir.getFileObject("src/conf"); // NOI18N
            if (newTargetFO == null || !newTargetFO.isValid()) {
                newTargetFO = projectDir.getFileObject("src/META-INF"); // NOI18N
                // take existence of <projectdir>/src/main as indication
                // of maven style project layout
                FileObject tempFo = projectDir.getFileObject("src/main"); // NOI18N
                if (tempFo != null) {
                    try {
                        newTargetFO = FileUtil.createFolder(tempFo, "resources/META-INF");
                    } catch (IOException ex) {
                        LOG.log(Level.INFO, "Failed to create META-INF folder", ex);
                    }
                }
                if (newTargetFO == null) {
                    if (newTargetFO == null || !newTargetFO.isValid()) {
                        newTargetFO = projectDir.getFileObject("src"); // NOI18N
                        if (newTargetFO == null || !newTargetFO.isValid()) {
                            return;
                        }
                    }
                }
            }

            DataFolder newTarget = DataFolder.findFolder(newTargetFO);
            wizardInstance.setTargetFolder(newTarget);
        }
    }
}
 
Example 19
Source File: PanelSourceFolders.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String checkValidity (final File projectLocation, final File webPages, final File webInf, final File[] sources, final File[] tests) {
       String ploc = projectLocation.getAbsolutePath ();
       
if (projectLocation.equals(webPages))
    return NbBundle.getMessage(PanelSourceFolders.class, "MSG_WebPagesFolderOverlapsProjectFolder"); //NOI18N
    
if (!webPages.exists() || !webPages.isDirectory())
    return NbBundle.getMessage(PanelSourceFolders.class, "MSG_WebPagesFolderDoesNotExist"); //NOI18N
       
if (!webInf.exists() || !webInf.isDirectory())
    return NbBundle.getMessage(PanelSourceFolders.class, "MSG_WebInfFolderDoesNotExist"); //NOI18N

       FileObject webInfFO = FileUtil.toFileObject(webInf);
       FileObject webXml = webInfFO.getFileObject(ProjectWebModule.FILE_DD);
       //#74837 - filesystem is probably not refreshed and file object for non-existing file is found
       //rather setting to null that refreshing filesystem from a performance reason
       if (webXml != null && !webXml.isValid())
           webXml = null;
       Profile j2eeProfile = (Profile) wizardDescriptor.getProperty(ProjectServerWizardPanel.J2EE_LEVEL);
       if (webXml == null && (j2eeProfile == Profile.J2EE_13 || j2eeProfile == Profile.J2EE_14))
           return NbBundle.getMessage(PanelSourceFolders.class, "MSG_FileNotFound", webInf.getPath()); //NOI18N
       
       for (int i=0; i<sources.length;i++) {
           if (!sources[i].isDirectory() || !sources[i].canRead()) {
               return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalSources"), //NOI18N
                       new Object[] {sources[i].getAbsolutePath()});
           }
           String sloc = sources[i].getAbsolutePath ();
           if (ploc.equals (sloc) || ploc.startsWith (sloc + File.separatorChar)) {
               return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); //NOI18N
           }
       }
       for (int i=0; i<tests.length; i++) {
           if (!tests[i].isDirectory() || !tests[i].canRead()) {
               return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalTests"), //NOI18N
                       new Object[] {sources[i].getAbsolutePath()});
           }            
           String tloc = tests[i].getAbsolutePath();
           if (ploc.equals(tloc) || ploc.startsWith(tloc + File.separatorChar)) {
               return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); //NOI18N
           }
       }
       return null;
   }
 
Example 20
Source File: Worker.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean doQuery(
    @NonNull final Pair<String,String> query,
    @NonNull final Request request,
    @NonNull final Worker worker,
    @NonNull Collection<? extends FileObject> roots) throws IOException {
    if (isCancelled()) {
        return false;
    }
    final QuerySupport q = QuerySupport.forRoots(
        FileIndexer.ID,
        FileIndexer.VERSION,
        roots.toArray(new FileObject[roots.size()]));
    final QuerySupport.Query.Factory f = q.getQueryFactory().
            setCamelCaseSeparator(FileSearchAction.CAMEL_CASE_SEPARATOR).
            setCamelCasePart(Utils.isCaseSensitive(Utils.toSearchType(request.getSearchKind())) ?
                    FileSearchAction.CAMEL_CASE_PART_CASE_SENSITIVE :
                    FileSearchAction.CAMEL_CASE_PART_CASE_INSENSITIVE);
    if (isCancelled()) {
        return false;
    }
    final List<FileDescriptor> files = new ArrayList<>();
    final Collection<? extends IndexResult> results = f.field(
        query.first(),
        query.second(),
        request.getSearchKind()).execute();
    for (IndexResult r : results) {
        FileObject file = r.getFile();
        if (file == null || !file.isValid()) {
            // the file has been deleted in the meantime
            continue;
        }
        final Project project = ProjectConvertors.getNonConvertorOwner(file);
        FileDescriptor fd = new FileDescription(
                file,
                r.getRelativePath(),
                project);
        boolean preferred = project != null && request.getCurrentProject() != null ?
                project.getProjectDirectory() == request.getCurrentProject().getProjectDirectory() :
                false;
        FileProviderAccessor.getInstance().setFromCurrentProject(fd, preferred);
        FileProviderAccessor.getInstance().setLineNumber(fd, request.getLine());
        files.add(fd);
        LOG.log(
            Level.FINER,
            "Found: {0}, project={1}, currentProject={2}, preferred={3}",   //NOI18N
            new Object[]{
                file.getPath(),
                project,
                request.getCurrentProject(),
                preferred
            });
    }
    for (FileObject root : roots) {
        request.exclude(root);
    }
    worker.emit(files);
    return true;
}