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

The following examples show how to use org.openide.filesystems.FileObject#hasExt() . 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 check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: ModuleActions.java    License: Apache License 2.0 6 votes vote down vote up
@CheckForNull TestSources findTestSources(@NonNull Lookup context, boolean allowFolders) {
    TYPE: for (String testType : project.supportedTestTypes()) {
        FileObject testSrcDir = project.getTestSourceDirectory(testType);
        if (testSrcDir != null) {
            FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcDir, null, true);
            if (files != null) {
                for (FileObject file : files) {
                    if (!(file.hasExt("java") || allowFolders && file.isFolder())) {
                        break TYPE;
                    }
                }
                return new TestSources(files, testType, testSrcDir, null);
            }
        }
    }
    return null;
}
 
Example 2
Source Project: netbeans   File: BootClassPathUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
    if (location == StandardLocation.CLASS_OUTPUT) {
        List<JavaFileObject> result = new ArrayList<>();
        FileObject pack = output.getFileObject(packageName.replace('.', '/'));

        if (pack != null) {
            Enumeration<? extends FileObject> c = pack.getChildren(recurse);

            while (c.hasMoreElements()) {
                FileObject file = c.nextElement();
                if (!file.hasExt("class"))
                    continue;
                result.add(new InferableJavaFileObject(file, JavaFileObject.Kind.CLASS));
            }
        }

        return result;
    }
    return super.list(location, packageName, kinds, recurse);
}
 
Example 3
Source Project: netbeans   File: JavaActions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check to see if a (node-like) selection contains one or more Java sources (or folders) inside the root.
 */
static boolean containsSelectedJavaSources(FileObject root, Lookup context) {
    Set<FileObject> selection = new HashSet<FileObject>();
    for (DataObject dob : context.lookupAll(DataObject.class)) {
        selection.add(dob.getPrimaryFile());
    }
    if (selection.isEmpty()) {
        return false;
    }
    for (FileObject f : selection) {
        if (f.isData() && !f.hasExt("java")) { // NOI18N
            return false;
        }
        if (f != root && !FileUtil.isParentOf(root, f)) {
            return false;
        }
    }
    return true;
}
 
Example 4
private void readNonDefaultConfigs(String subDir, boolean createIfNotExists) {
    FileObject configsFO = projectDir.getFileObject(subDir); // NOI18N
    if (configsFO != null) {
        for (FileObject kid : configsFO.getChildren()) {
            if (!kid.hasExt(PROPERTIES_FILE_EXT)) { // NOI18N
                continue;
            }
            Map<String,String> c = getConfig(kid.getName());
            if (c == null && !createIfNotExists) {
                continue;
            }
            EditableProperties cep = null;
            try {
                cep = JFXProjectUtils.readFromFile( kid );
            } catch (IOException ex) {
                // can be ignored
            }
            addToConfig(kid.getName(), cep);
            appParams.extractEntries(cep, kid.getName());
            appManifestEntries.extractEntries(cep, kid.getName());
        }
    }
}
 
Example 5
Source Project: netbeans   File: BootClassPathUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
    if (location == StandardLocation.CLASS_OUTPUT) {
        List<JavaFileObject> result = new ArrayList<>();
        FileObject pack = output.getFileObject(packageName.replace('.', '/'));

        if (pack != null) {
            Enumeration<? extends FileObject> c = pack.getChildren(recurse);

            while (c.hasMoreElements()) {
                FileObject file = c.nextElement();
                if (!file.hasExt("class"))
                    continue;
                result.add(new InferableJavaFileObject(file, JavaFileObject.Kind.CLASS));
            }
        }

        return result;
    }
    return super.list(location, packageName, kinds, recurse);
}
 
Example 6
protected FileObject findPrimaryFile(FileObject fo) {
    if (fo.hasExt("slow")) {
        slowCnt++;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            fail("No failures, please");
        }
        return null;
    }
    
    if (!fo.hasExt("txt")) {
        return null;
    }
    
    assertNull("First invocation", lookup);
    
    FolderLookup l = new FolderLookup(DataFolder.findFolder(button));
    lookup = l.getLookup();
    v = lookup.lookup(JButton.class);
    assertNotNull("The instance computed", v);
    
    return fo;
}
 
Example 7
Source Project: netbeans   File: CreatedModifiedFiles.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws IOException {
    FileObject target = FileUtil.createData(getProject().getProjectDirectory(), path);
    if (tokens == null) {
        copyByteAfterByte(content, target);
    } else {
        copyAndSubstituteTokens(content, target, tokens);
    }
    // #129446: form editor doesn't work sanely unless you do this:
    if (target.hasExt("form")) { // NOI18N
        FileObject java = FileUtil.findBrother(target, "java"); // NOI18N
        if (java != null) {
            java.setAttribute("justCreatedByNewWizard", true); // NOI18N
        }
    } else if (target.hasExt("java") && FileUtil.findBrother(target, "form") != null) { // NOI18N
        target.setAttribute("justCreatedByNewWizard", true); // NOI18N
    }
}
 
Example 8
Source Project: netbeans   File: FileUtilities.java    License: Apache License 2.0 5 votes vote down vote up
private static void processFolder(FileObject file, NameObtainer nameObtainer, SortedSet<String> result, boolean onlyRoots) {
    Enumeration<? extends FileObject> dataFiles = file.getData(false);
    Enumeration<? extends FileObject> folders = file.getFolders(false);

    if (dataFiles.hasMoreElements()) {
        while (dataFiles.hasMoreElements()) {
            FileObject kid = dataFiles.nextElement();
            if ((kid.hasExt("java") || kid.hasExt("class")) && Utilities.isJavaIdentifier(kid.getName())) {
                // at least one java or class inside directory -> valid package
                String packageName = nameObtainer.getPackageName(file);
                if (packageName == null) {
                    continue;
                }
                result.add(packageName);
                if (onlyRoots) {
                    // don't recurse into subfolders
                    return;
                } else {
                    // recurse inot subfolders
                    break;
                }
            }
        }
    }

    while (folders.hasMoreElements()) {
        processFolder(folders.nextElement(), nameObtainer, result, onlyRoots);
    }
}
 
Example 9
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 10
Source Project: netbeans   File: ShortcutWizard.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isAntScript(FileObject f) {
    try {
        return DataObject.find(f).getLookup().lookup(AntProjectCookie.class) != null;
    } catch (DataObjectNotFoundException x) {
        return f.hasExt("xml"); // NOI18N
    }
}
 
Example 11
@Nonnull
@MustNotContainNull
public static List<FileObject> findAllMindMapsInProject(@Nonnull final Project project, @Nullable final AbstractPlugin plugin) {
  final List<FileObject> result = new ArrayList<FileObject>();

  final Sources sources = ProjectUtils.getSources(project);
  final SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC);

  for (final SourceGroup g : groups) {
    if (plugin != null && plugin.isCanceled()) {
      return result;
    }
    final FileObject gobject = g.getRootFolder();
    if (gobject != null) {
      final Enumeration<? extends FileObject> e = gobject.getChildren(true);
      while (e.hasMoreElements()) {
        if (plugin != null && plugin.isCanceled()) {
          return result;
        }
        final FileObject nxt = e.nextElement();
        if (nxt.isData() && nxt.hasExt("mmd")) {
          result.add(nxt);
        }
      }
    }
  }
  return result;
}
 
Example 12
Source Project: netbeans   File: PropertyChangeTest.java    License: Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (!fo.isFolder()) {
        // here is the common code for the worse behaviour
        if (fo.hasExt("prima")) {
            return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
        }
        
        if (fo.hasExt("seconda")) {
            return FileUtil.findBrother(fo, "prima");
        }
    }
    return null;
}
 
Example 13
Source Project: netbeans   File: SCFTHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    Integer i = queried.get(fo);
    queried.put(fo, i == null ? 1 : i + 1);
    FileObject ret;
    
    if (fo.hasExt("java") || fo.hasExt("form")) {
        ret = org.openide.filesystems.FileUtil.findBrother(fo, "java");
    } else {
        ret = null;
    }
    
    LOG.fine("findPrimaryFile for " + fo + " yeilded " + ret);
    return ret;
}
 
Example 14
private void parseFolder(FileObject fo) {

        Enumeration<? extends FileObject> children = fo.getChildren(false);
        while (children.hasMoreElements()) {
            FileObject nextElement = children.nextElement();
            if (nextElement.hasExt("xml")) {
                parseFile(nextElement);
            }
        }
    }
 
Example 15
Source Project: netbeans   File: BootClassPathUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static ClassPath getModuleBootOnJDK8() throws Exception {
    if (moduleBootOnJDK8 == null) {
        List<FileSystem> roots = new ArrayList<>();

        FileSystem output = FileUtil.createMemoryFileSystem();
        FileObject sink = output.getRoot();

        roots.add(output);

        Set<String> packages = new HashSet<>();
        for (FileObject r : getBootClassPath().getRoots()) {
            FileObject javaDir = r.getFileObject("java");

            if (javaDir == null)
                continue;

            roots.add(r.getFileSystem());

            Enumeration<? extends FileObject> c = javaDir.getChildren(true);

            while (c.hasMoreElements()) {
                FileObject current = c.nextElement();

                if (!current.isData() || !current.hasExt("class")) continue;

                String rel = FileUtil.getRelativePath(r, current.getParent());

                packages.add(rel.replace('/', '.'));
            }
        }

        FileSystem outS = new MultiFileSystem(roots.toArray(new FileSystem[0])) {
            {
                setSystemName("module-boot");
            }
        };

        Repository.getDefault().addFileSystem(outS);

        StringBuilder moduleInfo = new StringBuilder();

        moduleInfo.append("module java.base {\n");

        for (String pack : packages) {
            moduleInfo.append("    exports " + pack + ";\n");
        }

        moduleInfo.append("}\n");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        FileObject javaBase = outS.getRoot();

        try (JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
             JFMImpl fmImpl = new JFMImpl(fm, javaBase, sink)) {
            compiler.getTask(null, fmImpl, null, Arrays.asList("-proc:none"), null,
                             Arrays.asList(new SimpleJavaFileObject(new URI("mem:///module-info.java"), javax.tools.JavaFileObject.Kind.SOURCE) {
                @Override
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                    return moduleInfo.toString();
                }
            })).call();
        }

        javaBase.refresh();
        moduleBootOnJDK8 = ClassPathSupport.createClassPath(javaBase);
    }

    return moduleBootOnJDK8;
}
 
Example 16
Source Project: netbeans   File: ActionUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Convenience method to find a file selection in a selection (context).
 * All files must exist on disk (according to {@link FileUtil#toFile}).
 * If a constraining directory is supplied, they must also be contained in it.
 * If a constraining file suffix is supplied, the base names of the files
 * must end with that suffix.
 * The return value is null if there are no matching files; or if the strict
 * parameter is true and some of the files in the selection did not match
 * the constraints (disk files, directory, and/or suffix).
 * <p class="nonnormative">
 * Typically {@link org.openide.loaders.DataNode}s will form a node selection
 * which will be placed in the context. This method does <em>not</em> directly
 * look for nodes in the selection; but generally the lookups of the nodes in
 * a node selection are spliced into the context as well, so the {@link FileObject}s
 * should be available. A corollary of not checking nodes directly is that any
 * nodes in the context which do not correspond to files at all (i.e. do not have
 * {@link FileObject} in their lookup) are ignored, even with the strict parameter on;
 * and that multiple nodes in the context with the same associated file are treated
 * as a single entry.
 * </p>
 * @param context a selection as provided to e.g. <code>ActionProvider.isActionEnabled(...)</code>
 * @param dir a constraining parent directory, or null to not check for a parent directory
 * @param suffix a file suffix (e.g. <samp>.java</samp>) to constrain files by,
 *               or null to not check suffixes
 * @param strict if true, all files in the selection have to be accepted
 * @return a nonempty selection of disk files, or null
 * @see <a href="@[email protected]/org/netbeans/spi/project/ActionProvider.html#isActionEnabled(java.lang.String,%20org.openide.util.Lookup)"><code>ActionProvider.isActionEnabled(...)</code></a>
 */
public static FileObject[] findSelectedFiles(Lookup context, FileObject dir, String suffix, boolean strict) {
    if (dir != null && !dir.isFolder()) {
        throw new IllegalArgumentException("Not a folder: " + dir); // NOI18N
    }
    if (suffix != null && suffix.indexOf('/') != -1) {
        throw new IllegalArgumentException("Cannot includes slashes in suffix: " + suffix); // NOI18N
    }
    Collection<? extends FileObject> candidates = context.lookupAll(FileObject.class);
    if (candidates.isEmpty()) { // should not be for DataNode selections, but for compatibility
        Collection<? extends DataObject> compatibilityCandidates = context.lookupAll(DataObject.class);
        if (compatibilityCandidates.isEmpty()) {
            return null; // shortcut - just not a file selection at all
        }
        List<FileObject> _candidates = new ArrayList<FileObject>();
        for (DataObject d : compatibilityCandidates) {
            _candidates.add(d.getPrimaryFile());
        }
        candidates = _candidates;
    }
    Collection<FileObject> files = new LinkedHashSet<FileObject>(); // #50644: remove dupes
    for (FileObject f : candidates) {
        if (f.hasExt("form")) {
            continue; // #206309
        }
        boolean matches = FileUtil.toFile(f) != null;
        if (dir != null) {
            matches &= (FileUtil.isParentOf(dir, f) || dir == f);
        }
        if (suffix != null) {
            matches &= f.getNameExt().endsWith(suffix);
        }
        // Generally only files from one project will make sense.
        // Currently the action UI infrastructure (PlaceHolderAction)
        // checks for that itself. Should there be another check here?
        if (matches) {
            files.add(f);
        } else if (strict) {
            return null;
        }
    }
    if (files.isEmpty()) {
        return null;
    }
    return files.toArray(new FileObject[files.size()]);
}
 
Example 17
Source Project: netbeans   File: CreateFromTemplateTest.java    License: Apache License 2.0 4 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo) {
    if (fo.hasExt("prima")) {
        return fo;
    }
    return null;
}
 
Example 18
@Override
public boolean isFileObjectSupported(FileObject file) {
    return file.hasExt("java"); // NOI18N
}
 
Example 19
Source Project: netbeans   File: BrowserSupport.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean ignoreChangeDefaultImpl(FileObject fo) {
    // #217284 - ignore changes in CSS
    return fo.hasExt("css");
}
 
Example 20
protected FileObject findPrimaryFile(FileObject fo) {
    if (fo.hasExt("txt") || fo.equals(instanceFile)) {
        return fo;
    }
    return null;
}