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

The following are Jave code examples for showing how to use createAndOpen() 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: DefaultMatcherTest.java   View Source Code Vote up 6 votes
private void createAndCheckTextContent(String encoding) {
    FileObject root = FileUtil.createMemoryFileSystem().getRoot();
    try {
        OutputStream os = root.createAndOpen("file");
        try {
            OutputStreamWriter osw = new OutputStreamWriter(os,
                    encoding);
            try {
                osw.append("Test Text");
                osw.flush();
            } finally {
                osw.close();
            }
        } finally {
            os.close();
        }
        assertTrue("File with encoding " + encoding
                + " was detected as binary file",
                DefaultMatcher.hasTextContent(root.getFileObject("file")));
    } catch (UnsupportedEncodingException eee) {
        LOG.log(Level.INFO, "Unknown encoding {0}", encoding);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 2
Project: incubator-netbeans   File: TestFileUtils.java   View Source Code 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: TestFileUtils.java   View Source Code Vote up 6 votes
/**
 * Create a new ZIP file.
 * 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 ZIP file within that root
 * @param entries a list of entries in the form of "filename:UTF8-contents"; parent dirs created automatically
 * @return the newly created ZIP file (use {@link FileUtil#getArchiveRoot} if you want the root entry)
 * @throws IOException for the usual reasons
 */
public static FileObject writeZipFile(FileObject root, String path, String... entries) 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 {
        org.openide.util.test.TestFileUtils.writeZipFile(os, entries);
    } finally {
        os.close();
    }
    return root.getFileObject(path);
}
 
Example 4
Project: incubator-netbeans   File: CopyPathToClipboardActionTest.java   View Source Code Vote up 6 votes
/**
 * Creates ZIP file archive.zip that contains three files, a.txt, b.txt and
 * c.txt.
 */
private void createZipFile(FileObject parentFolder) throws IOException {
    OutputStream outStream = parentFolder.createAndOpen("archive.zip");
    ZipOutputStream zipStream = new ZipOutputStream(outStream);
    zipStream.putNextEntry(new ZipEntry("a.txt"));
    zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
    zipStream.closeEntry();
    zipStream.putNextEntry(new ZipEntry("b.txt"));
    zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
    zipStream.closeEntry();
    zipStream.putNextEntry(new ZipEntry("c.txt"));
    zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
    zipStream.closeEntry();
    zipStream.close();
    outStream.close();
}
 
Example 5
Project: incubator-netbeans   File: DefaultMatcherTest.java   View Source Code Vote up 5 votes
public void testHasTextContentWithBinaryContent() {
    FileObject root = FileUtil.createMemoryFileSystem().getRoot();
    try {
        OutputStream os = root.createAndOpen("file");
        try {
            os.write(new byte[]{90, 98, 88, 97, 94, 0, 1, 2, 4, 5, 4, 6});
        } finally {
            os.close();
        }
        assertFalse("Binary file was detected as textual file",
                DefaultMatcher.hasTextContent(root.getFileObject("file")));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 6
Project: incubator-netbeans   File: CompletionContextTest.java   View Source Code Vote up 5 votes
private void writeSourceFile() throws Exception {
    File f = new File(getWorkDir(), "source.fxml");
    FileObject dirFo = FileUtil.toFileObject(getWorkDir());
    OutputStream ostm = dirFo.createAndOpen("source.fxml");
    OutputStreamWriter wr = new OutputStreamWriter(ostm);
    wr.write(text);
    wr.flush();
    wr.close();
    
    sourceFO = dirFo.getFileObject("source.fxml");
}
 
Example 7
Project: incubator-netbeans   File: DataShadowTranslateTest.java   View Source Code Vote up 5 votes
/**
 * Checks that file with just regular characters in name is translated OK
 * @throws Exception 
 */
public void testRegularURI() throws Exception {
    
    FileObject fo = FileUtil.getConfigRoot();
    
    FileObject origDir = fo.createFolder("origFolder");
    FileObject newFile = origDir.createData("regularFileName.txt");
    
    final FileObject d = fo.createFolder("subfolder");
    OutputStream ostm = d.createAndOpen("regularShadowURI.shadow");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ostm));
    
    URI uri = newFile.toURI();
    String urlString = newFile.toURI().toString();
    bw.write(urlString + ".old");
    bw.newLine();
    bw.newLine();
    bw.close();
    
    FileObject fob = d.getFileObject("regularShadowURI.shadow");
    DataObject dd = DataObject.find(fob);
    
    assertTrue("Shadow must be translated, not broken", dd instanceof DataShadow);
    
    DataShadow ds = (DataShadow)dd;
    assertEquals("Shadow's original must be on the translated location", newFile, ds.getOriginal().getPrimaryFile());
}
 
Example 8
Project: incubator-netbeans   File: DataShadowTranslateTest.java   View Source Code Vote up 5 votes
/**
 * Checks translation on Shadows, which use FS name + path, not URI
 * @throws Exception 
 */
public void testFSNameAndPath() throws Exception {
    FileObject fo = FileUtil.getConfigRoot();
    
    FileObject origDir = fo.createFolder("origFolder3");
    
    // create empty real file with special and non-ASCII chars
    FileObject newFile = origDir.createData("moved-here.txt");
    
    // createa a fake file, just to get its URI right:
    FileObject fake = fo.createData("dead-file-location.old");
    
    final FileObject d = fo.createFolder("subfolder3");
    OutputStream ostm = d.createAndOpen("regularShadowURI.shadow");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ostm));
    
    bw.write(fake.getPath());
    bw.newLine();
    bw.write(fake.getFileSystem().getSystemName());
    bw.newLine();
    
    fake.delete();
    
    bw.close();
    
    FileObject fob = d.getFileObject("regularShadowURI.shadow");
    DataObject dd = DataObject.find(fob);
    
    assertTrue("Shadow must be translated, not broken", dd instanceof DataShadow);
    
    DataShadow ds = (DataShadow)dd;
    assertEquals("Shadow's original must be on the translated location", newFile, ds.getOriginal().getPrimaryFile());
}
 
Example 9
Project: incubator-netbeans   File: DataShadowTranslateTest.java   View Source Code Vote up 2 votes
/**
 * Checks that DataShadows to regular (non-SFS) files are not translated
 * even if a translation is defined for their path
 * 
 * @throws Exception 
 */
public void testNonSFSUriNotAffected() throws Exception {
    File wd = getWorkDir();
    
    clearWorkDir();
    
    FileObject origDir = FileUtil.toFileObject(wd);
    
    FileObject dirWithSpace = origDir.createFolder("Space Dir");
    FileObject newFile = dirWithSpace.createData("testFile.txt");
    

    File subDir = new File(wd, "translate");
    
    subDir.mkdirs();
    
    File metaTranslate = new File(subDir, "META-INF/netbeans");
    metaTranslate.mkdirs();
    
    String workPath = newFile.toURI().getRawPath();
    FileWriter wr = new FileWriter(new File(metaTranslate, "translate.names"));
    BufferedWriter bw = new BufferedWriter(wr);
    
    bw.write(workPath.substring(1) + "/testFile.txt=" + workPath.substring(1) + "/moved/testFile.txt");
    bw.close();
    
    
    FileObject fo = FileUtil.toFileObject(wd);
    
    ClassLoader orig = Lookup.getDefault().lookup(ClassLoader.class);
    
    ClassLoader my = new URLClassLoader(new URL[] {
        subDir.toURL()
    }, orig);
    
    MockLookup.setInstances(my);
    
    
    FileObject cfgRoot = FileUtil.getConfigRoot();
    
    
    OutputStream ostm = cfgRoot.createAndOpen("nonSFSFile.shadow");
    
    bw = new BufferedWriter(new OutputStreamWriter(ostm));
    
    bw.write(newFile.toURI().toString());
    bw.newLine();
    bw.newLine();
    
    newFile.delete();
    
    bw.close();
    
    FileObject fob = cfgRoot.getFileObject("nonSFSFile.shadow");
    DataObject dd = DataObject.find(fob);
    
    assertFalse("Shadow must be still broken", dd instanceof DataShadow);
}