Search in sources :

Example 1 with Resource

use of org.drools.compiler.compiler.io.Resource in project drools by kiegroup.

the class AbstractKnowledgeTest method writeFs.

public void writeFs(MemoryFileSystem mfs, Folder f, java.io.File file1) {
    for (Resource rs : f.getMembers()) {
        if (rs instanceof Folder) {
            java.io.File file2 = new java.io.File(file1, ((Folder) rs).getName());
            file2.mkdir();
            writeFs(mfs, (Folder) rs, file2);
        } else {
            byte[] bytes = mfs.getFileContents((MemoryFile) rs);
            try {
                IoUtils.write(new java.io.File(file1, ((File) rs).getName()), bytes);
            } catch (IOException e) {
                fail("Unable to write project to file system\n" + e.getMessage());
            }
        }
    }
}
Also used : Resource(org.drools.compiler.compiler.io.Resource) IOException(java.io.IOException) Folder(org.drools.compiler.compiler.io.Folder) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) File(org.drools.compiler.compiler.io.File)

Example 2 with Resource

use of org.drools.compiler.compiler.io.Resource in project drools by kiegroup.

the class MemoryFileSystem method writeAsFs.

public void writeAsFs(Folder f, java.io.File file1) {
    for (Resource rs : f.getMembers()) {
        if (rs instanceof Folder) {
            java.io.File file2 = new java.io.File(file1, ((Folder) rs).getName());
            file2.mkdir();
            writeAsFs((Folder) rs, file2);
        } else {
            byte[] bytes = getFileContents((MemoryFile) rs);
            try {
                IoUtils.write(new java.io.File(file1, ((File) rs).getName()), bytes);
            } catch (IOException e) {
                throw new RuntimeException("Unable to write project to file system\n", e);
            }
        }
    }
}
Also used : Resource(org.drools.compiler.compiler.io.Resource) IOException(java.io.IOException) Folder(org.drools.compiler.compiler.io.Folder) ZipFile(java.util.zip.ZipFile) File(org.drools.compiler.compiler.io.File)

Example 3 with Resource

use of org.drools.compiler.compiler.io.Resource in project drools by kiegroup.

the class MemoryFileSystem method createFolder.

public void createFolder(MemoryFolder folder) {
    // create current, if it does not exist.
    if (!existsFolder(folder)) {
        // create parent if it does not exist
        if (!existsFolder((MemoryFolder) folder.getParent())) {
            createFolder((MemoryFolder) folder.getParent());
        }
        folders.put(folder.getPath().toPortableString(), new HashSet<Resource>());
        Folder parent = folder.getParent();
        folders.get(parent.getPath().toPortableString()).add(folder);
    }
}
Also used : Resource(org.drools.compiler.compiler.io.Resource) Folder(org.drools.compiler.compiler.io.Folder)

Example 4 with Resource

use of org.drools.compiler.compiler.io.Resource in project drools by kiegroup.

the class MemoryFileSystem method remove.

public void remove(Set<Resource> members) {
    for (Iterator<Resource> it = members.iterator(); it.hasNext(); ) {
        Resource res = it.next();
        if (res instanceof Folder) {
            remove(folders.get(res.getPath().toPortableString()));
        } else {
            String fileName = res.getPath().toPortableString();
            fileContents.remove(fileName);
            if (modifiedFilesSinceLastMark != null) {
                modifiedFilesSinceLastMark.add(fileName);
            }
        }
        it.remove();
    }
}
Also used : Resource(org.drools.compiler.compiler.io.Resource) Folder(org.drools.compiler.compiler.io.Folder)

Example 5 with Resource

use of org.drools.compiler.compiler.io.Resource in project drools by kiegroup.

the class MemoryFileSystem method writeJarEntries.

private void writeJarEntries(Folder f, ZipOutputStream out) throws IOException {
    for (Resource rs : f.getMembers()) {
        String rname = rs.getPath().toPortableString();
        if (rs instanceof Folder) {
            // a folder name must end with / according to ZIP spec
            rname = rname.endsWith("/") ? rname : rname + "/";
            ZipEntry entry = new ZipEntry(rname);
            out.putNextEntry(entry);
            writeJarEntries((Folder) rs, out);
        } else {
            ZipEntry entry = new ZipEntry(rname);
            out.putNextEntry(entry);
            byte[] contents = getFileContents((MemoryFile) rs);
            if (contents == null) {
                IOException e = new IOException("No content found for: " + rs);
                log.error(e.getMessage(), e);
                throw e;
            }
            out.write(contents);
            out.closeEntry();
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Resource(org.drools.compiler.compiler.io.Resource) IOException(java.io.IOException) Folder(org.drools.compiler.compiler.io.Folder)

Aggregations

Folder (org.drools.compiler.compiler.io.Folder)6 Resource (org.drools.compiler.compiler.io.Resource)6 IOException (java.io.IOException)4 File (org.drools.compiler.compiler.io.File)3 ZipFile (java.util.zip.ZipFile)2 ZipEntry (java.util.zip.ZipEntry)1 MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)1