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

The following are Jave code examples for showing how to use getFileObject() 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.
Example 1
Project: incubator-netbeans   File: J2SEPersistenceProvider.java   Source Code and License Vote up 6 votes
@Override
    public FileObject createLocation() throws IOException {
        FileObject root = getFirstSourceRoot();
        if (root == null) {
            throw new IOException("There are no source roots in the project or the first source root does not exist."); // NOI18N
        }
        FileObject metaInf = root.getFileObject("META-INF"); // NOI18N
        if (metaInf != null) {
            if (!metaInf.isFolder()) {
                throw new IOException("The META-INF directory exists, but is not a folder."); // NOI18N
            }
        } else {
            metaInf = root.createFolder("META-INF"); // NOI18N
        }
//        //
//        FileUtil.addFileChangeListener(puChangeListener, new File(FileUtil.toFile(metaInf), "persistence.xml"));
        //
        return metaInf;
    }
 
Example 2
Project: incubator-netbeans   File: TestFileUtils.java   Source Code and License Vote up 6 votes
/**
 * Create a new data file with specified initial contents.
 * No file events should be fired until the resulting file is complete (see {@link FileObject#createAndOpen}).
 * @param root a root folder which should already exist
 * @param path a /-separated path to the new file within that root
 * @param body the complete contents of the new file (in UTF-8 encoding)
 */
public static FileObject writeFile(FileObject root, String path, String body) throws IOException {
    int slash = path.lastIndexOf('/');
    if (slash != -1) {
        root = FileUtil.createFolder(root, path.substring(0, slash));
        path = path.substring(slash + 1);
    }
    FileObject existing = root.getFileObject(path);
    OutputStream os = existing != null ? existing.getOutputStream() : root.createAndOpen(path);
    try {
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
        pw.print(body);
        pw.flush();
    } finally {
        os.close();
    }
    return root.getFileObject(path);
}
 
Example 3
Project: incubator-netbeans   File: CompletionTestCase.java   Source Code and License Vote up 6 votes
private FileObject getTestFile(File dataDir, String projectName, String testFile, PrintWriter log) throws IOException, InterruptedException {
        File projectFile = new File(dataDir, projectName);
        FileObject project = FileUtil.toFileObject(projectFile);                
        testCase.openDataProjects(projectName);
//        Object prj= ProjectSupport.openProject(projectFile);        
//        if (prj == null)
//            throw new IllegalStateException("Given directory \"" + project + "\" does not contain a project.");
//        
//        log.println("Project found: " + prj);
        
        FileObject test = project.getFileObject("src/" + testFile);
        
        if (test == null)
            throw new IllegalStateException("Given test file does not exist.");
        
        return test;
    }
 
Example 4
Project: incubator-netbeans   File: SingleModulePropertiesTest.java   Source Code and License Vote up 6 votes
public void testGetPlatformVersionedLocation() throws Exception {
    File plafdir = new File(getWorkDir(), "plaf");
    TestFileUtils.writeZipFile(new File(plafdir, "platform/core/core.jar"), "j:unk");
    File harnessdir = new File(getWorkDir(), "harness");
    TestFileUtils.writeZipFile(new File(harnessdir, "modules/org-netbeans-modules-apisupport-harness.jar"), "META-INF/MANIFEST.MF:OpenIDE-Module-Specification-Version: 1.23\n");
    File suitedir = new File(getWorkDir(), "suite");
    SuiteProjectGenerator.createSuiteProject(suitedir, "special", false);
    FileObject suitedirFO = FileUtil.toFileObject(suitedir);
    FileObject plafProps = suitedirFO.getFileObject("nbproject/platform.properties");
    EditableProperties ep = Util.loadProperties(plafProps);
    ep.setProperty("suite.dir", "${basedir}");
    ep.setProperty("nbplatform.special.netbeans.dest.dir", "${suite.dir}/../plaf");
    ep.setProperty("nbplatform.special.harness.dir", "${suite.dir}/../harness");
    ep.setProperty("cluster.path", new String[] {"${nbplatform.active.dir}/platform:", "${nbplatform.special.harness.dir}"});
    Util.storeProperties(plafProps, ep);
    File moduledir = new File(getWorkDir(), "suite/m");
    NbModuleProjectGenerator.createSuiteComponentModule(moduledir, "m", "m", "m/Bundle.properties", null, suitedir, false, false);
    NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(moduledir));
    NbPlatform plaf = SingleModuleProperties.getInstance(p).getActivePlatform();
    assertEquals(plafdir, plaf.getDestDir());
    assertEquals(harnessdir, plaf.getHarnessLocation());
    assertEquals(HarnessVersion.V70, plaf.getHarnessVersion());
}
 
Example 5
Project: incubator-netbeans   File: SettingsType.java   Source Code and License Vote up 6 votes
private void addUsersFiles(FileObject mimeFolder, String profileId, boolean fullScan, Map<String, List<Object []>> files) {
    if (profileId == null) {
        FileObject settingHome = mimeFolder.getFileObject(settingTypeId);
        if (settingHome != null && settingHome.isFolder()) {
            if (isUsingProfiles) {
                FileObject [] profileHomes = settingHome.getChildren();
                for(FileObject f : profileHomes) {
                    if (f.isFolder()) {
                        String id = f.getNameExt();
                        addFiles(f, fullScan, files, id, f, false, false);
                    }
                }
            } else {
                addFiles(settingHome, fullScan, files, null, null, false, false);
            }
        }
    } else {
        FileObject folder = mimeFolder.getFileObject(settingTypeId + "/" + profileId); //NOI18N
        if (folder != null && folder.isFolder()) {
            addFiles(folder, fullScan, files, profileId, folder, false, false);
        }
    }
}
 
Example 6
Project: incubator-netbeans   File: JpaControllerSetupPanelVisual.java   Source Code and License Vote up 6 votes
void store(WizardDescriptor settings) {
//        settings.putProperty(WizardProperties.JSF_FOLDER, jsfFolder.getText());
        String pkg = getPackage();
        settings.putProperty(WizardProperties.JPA_CONTROLLER_PACKAGE, pkg);
//        settings.putProperty(WizardProperties.AJAXIFY_JSF_CRUD, Boolean.valueOf(ajaxifyCheckbox.isSelected()));
        try {
            FileObject fo = getLocationValue().getRootFolder();
            String pkgSlashes = pkg.replace('.', '/');
            FileObject targetFolder = fo.getFileObject(pkgSlashes);
            if (targetFolder == null) {
                targetFolder = FileUtil.createFolder(fo, pkgSlashes);
            }
            Templates.setTargetFolder(settings, targetFolder);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
 
Example 7
Project: incubator-netbeans   File: CopyPathToClipboardActionTest.java   Source Code and License Vote up 5 votes
@Override
public void setUp() throws IOException, PropertyVetoException {

    // create projects
    projects.add(createProject("projectDir1"));
    projects.add(createProject("projectDir2"));

    List l = new LinkedList<DataObject>();
    clearWorkDir();
    FileObject root = FileUtil.toFileObject(getWorkDir());
    FileObject test1 = root.createFolder("test1");
    FileObject test2 = root.createFolder("test2");
    FileObject test1a = test1.createFolder("A");
    FileObject test1aTestClassJava = test1a.createData("TestClass.java");
    FileObject test2DataTxt = test2.createData("data.txt");
    createZipFile(test2);
    DataObject shadowFile = createShadowFile(test2DataTxt);

    FileObject archiveZip = test2.getFileObject("archive.zip");
    assertTrue(FileUtil.isArchiveFile(archiveZip));
    FileObject archiveRoot = FileUtil.getArchiveRoot(archiveZip);
    assertNotNull(archiveRoot);
    FileObject test2ArchiveZipBTxt = archiveRoot.getFileObject("b.txt");

    // test1/A/TestClass.java
    l.add(DataObject.find(test1aTestClassJava));
    // test2/data.txt
    l.add(DataObject.find(test2DataTxt));
    // test2/archive.zip:b.txt
    l.add(DataObject.find(test2ArchiveZipBTxt));
    // /testDataShadows/testShadowFile -> test2/data.txt
    l.add(shadowFile);
    dataObjects = l;
    List<Lookup.Provider> context = new LinkedList<Lookup.Provider>();
    context.addAll(projects);
    context.addAll(dataObjects);
    action = new CopyPathToClipboardAction(context);
}
 
Example 8
Project: incubator-netbeans   File: CopyClassesRefactoringPlugin.java   Source Code and License Vote up 5 votes
@Override
public Problem fastCheckParameters() {
    URL target = refactoring.getTarget().lookup(URL.class);
    try {
        target.toURI();
    } catch (URISyntaxException ex) {
        return createProblem(null, true, NbBundle.getMessage(CopyClassesRefactoringPlugin.class, "ERR_InvalidPackage", RefactoringUtils.getPackageName(target)));
    }
    FileObject fo = target != null ? URLMapper.findFileObject(target) : null;
    if (fo == null) {
        return createProblem(null, true, NbBundle.getMessage(CopyClassesRefactoringPlugin.class, "ERR_TargetFolderNotSet"));
    }
    if (!JavaRefactoringUtils.isOnSourceClasspath(fo)) {
        return createProblem(null, true, NbBundle.getMessage(CopyClassesRefactoringPlugin.class, "ERR_TargetFolderNotJavaPackage"));
    }
    String targetPackageName = RefactoringUtils.getPackageName(target);
    if (!RefactoringUtils.isValidPackageName(targetPackageName)) {
        String msg = new MessageFormat(NbBundle.getMessage(CopyClassesRefactoringPlugin.class, "ERR_InvalidPackage")).format(
                new Object[]{targetPackageName});
        return createProblem(null, true, msg);
    }
    Collection<? extends FileObject> sources = refactoring.getRefactoringSource().lookupAll(FileObject.class);
    Problem p = null;
    for (FileObject fileObject : sources) {
        if (fo.getFileObject(fileObject.getName(), fileObject.getExt()) != null) {
            p = createProblem(p, false, NbBundle.getMessage(CopyClassesRefactoringPlugin.class, "ERR_ClassesToCopyClashes"));
            break;
        }
    }
    return p;
}
 
Example 9
Project: incubator-netbeans   File: SingleModulePropertiesTest.java   Source Code and License Vote up 5 votes
public void testSimulateLocalizedBundlePackageRefactoring() throws Exception {
    NbModuleProject p = generateStandaloneModule("module1");
    SingleModuleProperties props = loadProperties(p);
    assertEquals("display name from ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name from LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
    
    // rename package
    FileObject pDir = p.getProjectDirectory();
    FileObject pkg = pDir.getFileObject("src/org/example/module1");
    FileLock lock = pkg.lock();
    pkg.rename(lock, "module1Renamed", null);
    lock.releaseLock();
    System.gc();    // no more random
    FileObject manifestFO = pDir.getFileObject("manifest.mf");

    // change manifest
    EditableManifest mf = Util.loadManifest(manifestFO);
    mf.setAttribute(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, "org/example/module1Renamed/resources/Bundle.properties", null);
    Util.storeManifest(manifestFO, mf);
    simulatePropertiesOpening(props, p);
    
    // make sure that properties are not damaged
    assertEquals("display name was refreshed in ProjectInformation", "Testing Module",
            ProjectUtils.getInformation(p).getDisplayName());
    assertEquals("display name was refreshed in LocalizedBundleInfo", "Testing Module",
            props.getBundleInfo().getDisplayName());
}
 
Example 10
Project: incubator-netbeans   File: JFXProjectUtils.java   Source Code and License Vote up 5 votes
/**
 * Adds FX specific build script jfx-impl.xml to project build system
 * @param p
 * @param dirFO
 * @param type
 * @throws IOException 
 */
static void createJfxExtension(Project p, FileObject dirFO, WizardType type) throws IOException {
    FileObject templateFO = FileUtil.getConfigFile("Templates/JFX/jfx-impl.xml"); // NOI18N
    if (templateFO != null) {
        FileObject nbprojectFO = dirFO.getFileObject("nbproject"); // NOI18N
        FileObject jfxBuildFile = FileUtil.copyFile(templateFO, nbprojectFO, "jfx-impl"); // NOI18N
        if (type == JavaFXProjectWizardIterator.WizardType.SWING) {
            FileObject templatesFO = nbprojectFO.getFileObject("templates"); // NOI18N
            if (templatesFO == null) {
                templatesFO = nbprojectFO.createFolder("templates"); // NOI18N
            }
            FileObject swingTemplateFO1 = FileUtil.getConfigFile("Templates/JFX/FXSwingTemplate.html"); // NOI18N
            if (swingTemplateFO1 != null) {
                FileUtil.copyFile(swingTemplateFO1, templatesFO, "FXSwingTemplate"); // NOI18N
            }
            FileObject swingTemplateFO2 = FileUtil.getConfigFile("Templates/JFX/FXSwingTemplateApplet.jnlp"); // NOI18N
            if (swingTemplateFO1 != null) {
                FileUtil.copyFile(swingTemplateFO2, templatesFO, "FXSwingTemplateApplet"); // NOI18N
            }
            FileObject swingTemplateFO3 = FileUtil.getConfigFile("Templates/JFX/FXSwingTemplateApplication.jnlp"); // NOI18N
            if (swingTemplateFO1 != null) {
                FileUtil.copyFile(swingTemplateFO3, templatesFO, "FXSwingTemplateApplication"); // NOI18N
            }
        }
        JFXProjectUtils.addExtension(p);
    }
}
 
Example 11
Project: incubator-netbeans   File: JUnitUtils.java   Source Code and License Vote up 5 votes
/** */
static FileObject getPackageFolder(
        FileObject root,
        String pkgName) throws IOException {
    String relativePathName = pkgName.replace('.', '/');
    FileObject folder = root.getFileObject(relativePathName);
    if (folder == null) {
        folder = FileUtil.createFolder(root, relativePathName);
    }
    return folder;
}
 
Example 12
Project: incubator-netbeans   File: SuiteOperationsTest.java   Source Code and License Vote up 5 votes
public void testDeleteOfNonEmptySuite() throws Exception {
    SuiteProject suite = generateSuite("suite");
    FileObject prjDir = suite.getProjectDirectory();
    prjDir.createFolder("branding");
    NbModuleProject module1 = TestBase.generateSuiteComponent(suite, "module1");
    NbModuleProject module2 = TestBase.generateSuiteComponent(suite, "module2");
    assertEquals("module1 is suite component", NbModuleType.SUITE_COMPONENT, module1.getModuleType());
    assertEquals("module2 is suite component", NbModuleType.SUITE_COMPONENT, module2.getModuleType());
    module1.open();
    module2.open();
    
    suite.open();
    SuiteActions ap = (SuiteActions) suite.getLookup().lookup(ActionProvider.class);
    assertNotNull("have an action provider", ap);
    assertTrue("delete action is enabled", ap.isActionEnabled(ActionProvider.COMMAND_DELETE, null));
    
    
    // build project
    ap.invokeActionImpl(ActionProvider.COMMAND_BUILD, suite.getLookup()).waitFinished();
    prjDir.refresh();
    assertNotNull("suite was built", prjDir.getFileObject("build"));
    
    FileObject[] expectedMetadataFiles = new FileObject[] {
        prjDir.getFileObject(GeneratedFilesHelper.BUILD_XML_PATH),
        prjDir.getFileObject("nbproject"),
    };
    assertEquals("correct metadata files", Arrays.asList(expectedMetadataFiles), ProjectOperations.getMetadataFiles(suite));
    FileObject[] expectedDataFiles = new FileObject[] {
        prjDir.getFileObject("branding"),
    };
    assertEquals("correct data files", Arrays.asList(expectedDataFiles), ProjectOperations.getDataFiles(suite));
    
    // It is hard to simulate exact scenario invoked by user. Let's test at least something.
    ProjectOperations.notifyDeleting(suite);
    prjDir.getFileSystem().refresh(true);
    assertNull(prjDir.getFileObject("build"));
    
    assertEquals("module1 became standalone module", NbModuleType.STANDALONE, module1.getModuleType());
    assertEquals("module2 became standalone module", NbModuleType.STANDALONE, module2.getModuleType());
}
 
Example 13
Project: incubator-netbeans   File: ProjectUtilities.java   Source Code and License Vote up 5 votes
public static URL copyAppletHTML(Project project, PropertyEvaluator props, FileObject profiledClassFile, String value) {
    try {
        String buildDirProp = props.getProperty("build.dir"); //NOI18N

        FileObject buildFolder = getOrCreateBuildFolder(project, buildDirProp);

        FileObject htmlFile;
        htmlFile = profiledClassFile.getParent().getFileObject(profiledClassFile.getName(), "html"); //NOI18N

        if (htmlFile == null) {
            htmlFile = profiledClassFile.getParent().getFileObject(profiledClassFile.getName(), "HTML"); //NOI18N
        }

        if (htmlFile == null) {
            return null;
        }

        FileObject existingFile = buildFolder.getFileObject(htmlFile.getName(), htmlFile.getExt());

        if (existingFile != null) {
            existingFile.delete();
        }

        htmlFile.copy(buildFolder, profiledClassFile.getName(), value).getURL();

        return htmlFile.getURL();
    } catch (IOException e) {
        ErrorManager.getDefault()
                    .annotate(e, Bundle.ProjectUtilities_FailedCopyAppletFileMsg(e.getMessage()));
        ErrorManager.getDefault().notify(ErrorManager.ERROR, e);

        return null;
    }
}
 
Example 14
Project: incubator-netbeans   File: TestUtil.java   Source Code and License Vote up 5 votes
@Override
    public ProjectManager.Result isProject2(FileObject dir) {
        FileObject testproject = dir.getFileObject("testproject");
        if(testproject != null && testproject.isFolder() && testproject.getFileObject("broken") != null) {
            return new ProjectManager.Result(TEST_PROJECT_ICON);
}
        if(testproject != null && testproject.isFolder()) {
            return new ProjectManager.Result(
                        dir.getName(), 
                        TEST_PROJECT_TYPE, 
                        TEST_PROJECT_ICON);
        }
        return null;
    }
 
Example 15
Project: incubator-netbeans   File: TestLocatorImpl.java   Source Code and License Vote up 5 votes
static @CheckForNull FileObject findOpposite(FileObject rule, boolean toTest) {
    ClassPath cp = ClassPath.getClassPath(rule, ClassPath.SOURCE);
    String resourceName = cp != null ? cp.getResourceName(rule) : null;

    if (resourceName == null) {
        Logger.getLogger(TestLocatorImpl.class.getName()).log(Level.FINE, "cp==null or rule file cannot be found on its own source cp");
        return null;
    }

    String testFileName = resourceName.substring(0, resourceName.lastIndexOf('.')) + (toTest ? ".test" : ".hint");

    FileObject testFile = cp.findResource(testFileName);

    if (testFile == null) {
        URL[] sr;

        if (toTest) {
            sr = UnitTestForSourceQuery.findUnitTests(cp.findOwnerRoot(rule));
        } else {
            sr = UnitTestForSourceQuery.findSources(cp.findOwnerRoot(rule));
        }
        
        for (URL testRoot : sr) {
            FileObject testRootFO = URLMapper.findFileObject(testRoot);

            if (testRootFO != null) {
                testFile = testRootFO.getFileObject(testFileName);

                if (testFile != null) {
                    break;
                }
            }
        }
    }

    return testFile;
}
 
Example 16
Project: incubator-netbeans   File: LayersBridge.java   Source Code and License Vote up 5 votes
@Override
public void deleteProfile (String profile) {
    FileObject root = FileUtil.getConfigFile(KEYMAPS_FOLDER);
    if (root == null) return;
    root = root.getFileObject (profile);
    if (root == null) return;
    try {
        root.delete ();
    } catch (IOException ex) {
        ErrorManager.getDefault ().notify (ex);
    }
}
 
Example 17
Project: incubator-netbeans   File: SettingsType.java   Source Code and License Vote up 5 votes
@Override
protected FileObject getLegacyMimeFolder(FileObject baseFolder, String mimeType) {
    if (mimeType == null || mimeType.length() == 0) {
        return baseFolder.getFileObject(Utils.TEXT_BASE_MIME_TYPE);
    } else {
        return super.getMimeFolder(baseFolder, mimeType);
    }
}
 
Example 18
Project: incubator-netbeans   File: StatFilesTest.java   Source Code and License Vote up 5 votes
public void testGetCachedFileObject() throws IOException {
    FileObject fobj = getFileObject(testFile);
    FileObject parent = fobj.getParent();
    parent = parent.createFolder("parent");
    FileObject child = parent.createData("child");
    monitor.reset();
    FileObject ch = parent.getFileObject("child");
    monitor.getResults().assertResult(0, StatFiles.ALL);
    //second time
    monitor.reset();
    ch = parent.getFileObject("child");
    monitor.getResults().assertResult(0, StatFiles.ALL);
}
 
Example 19
Project: incubator-netbeans   File: ProjectTemplateAttributesProviderTest.java   Source Code and License Vote up 4 votes
public boolean isProject(FileObject projectDirectory) {
    return projectDirectory.getFileObject("nbproject") != null;
}
 
Example 20
Project: incubator-netbeans   File: TestProject.java   Source Code and License Vote up 4 votes
@Override
public boolean isProject(FileObject projectDirectory) {
    return projectDirectory.getFileObject(PROJECT_MARKER) != null;
}