KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > FileSystemExporter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.wizards.datatransfer;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18
19 /**
20  * Helper class for exporting resources to the file system.
21  */

22 /*package*/ class FileSystemExporter {
23 /**
24  * Creates the specified file system directory at <code>destinationPath</code>.
25  * This creates a new file system directory.
26  */

27 public void createFolder(IPath destinationPath) {
28     new File(destinationPath.toOSString()).mkdir();
29 }
30 /**
31  * Writes the passed resource to the specified location recursively
32  */

33 public void write(IResource resource,IPath destinationPath) throws CoreException, IOException {
34     if (resource.getType() == IResource.FILE)
35         writeFile((IFile)resource,destinationPath);
36     else
37         writeChildren((IContainer)resource,destinationPath);
38 }
39 /**
40  * Exports the passed container's children
41  */

42 protected void writeChildren(IContainer folder, IPath destinationPath) throws CoreException, IOException {
43     if (folder.isAccessible()) {
44         IResource[] children = folder.members();
45         for (int i = 0; i<children.length; i++) {
46             IResource child = children[i];
47             writeResource(
48                 child,
49                 destinationPath.append(child.getName()));
50         }
51     }
52 }
53 /**
54  * Writes the passed file resource to the specified destination on the local
55  * file system
56  */

57 protected void writeFile(IFile file, IPath destinationPath) throws IOException, CoreException {
58     FileOutputStream output = null;
59     InputStream contentStream = null;
60
61     try {
62         contentStream = file.getContents(false);
63         output = new FileOutputStream(destinationPath.toOSString());
64         int chunkSize = contentStream.available();
65         byte[] readBuffer = new byte[chunkSize];
66         int n = contentStream.read(readBuffer);
67         
68         while (n > 0) {
69             output.write(readBuffer);
70             n = contentStream.read(readBuffer);
71         }
72     } finally {
73         if (output != null)
74             output.close();
75         if (contentStream != null)
76             contentStream.close();
77     }
78 }
79 /**
80  * Writes the passed resource to the specified location recursively
81  */

82 protected void writeResource(IResource resource,IPath destinationPath) throws CoreException, IOException {
83     if (resource.getType() == IResource.FILE)
84         writeFile((IFile)resource,destinationPath);
85     else {
86         createFolder(destinationPath);
87         writeChildren((IContainer)resource,destinationPath);
88     }
89 }
90 }
91
Popular Tags