KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > FileSystemResourceExporter


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 Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.dialogs;
12
13 import java.io.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23
24 /**
25  * Helper class for exporting resources to the file system.
26  */

27 /*package*/class FileSystemResourceExporter {
28     /**
29      * Creates the specified file system directory at <code>destinationPath</code>.
30      * This creates a new file system directory.
31      */

32     public void createFolder(IPath destinationPath) {
33         new File JavaDoc(destinationPath.toOSString()).mkdir();
34     }
35
36     /**
37      * Writes the passed resource to the specified location recursively
38      */

39     public void write(IResource resource, IPath destinationPath)
40             throws CoreException, IOException JavaDoc {
41         if (resource.getType() == IResource.FILE)
42             writeFile((IFile) resource, destinationPath);
43         else
44             writeChildren((IContainer) resource, destinationPath);
45     }
46
47     /**
48      * Exports the passed container's children
49      */

50     protected void writeChildren(IContainer folder, IPath destinationPath)
51             throws CoreException, IOException JavaDoc {
52         if (folder.isAccessible()) {
53             IResource[] children = folder.members();
54             for (int i = 0; i < children.length; i++) {
55                 IResource child = children[i];
56                 writeResource(child, destinationPath.append(child.getName()));
57             }
58         }
59     }
60
61     /**
62      * Writes the passed file resource to the specified destination on the local
63      * file system
64      */

65     protected void writeFile(IFile file, IPath destinationPath)
66             throws IOException JavaDoc, CoreException {
67         FileOutputStream JavaDoc output = null;
68         InputStream JavaDoc contentStream = null;
69
70         try {
71             output = new FileOutputStream JavaDoc(destinationPath.toOSString());
72             contentStream = file.getContents(false);
73             int chunkSize = contentStream.available();
74             byte[] readBuffer = new byte[chunkSize];
75             int n = contentStream.read(readBuffer);
76
77             while (n > 0) {
78                 output.write(readBuffer);
79                 n = contentStream.read(readBuffer);
80             }
81         } finally {
82             if (output != null)
83                 output.close();
84             if (contentStream != null)
85                 contentStream.close();
86         }
87     }
88
89     /**
90      * Writes the passed resource to the specified location recursively
91      */

92     protected void writeResource(IResource resource, IPath destinationPath)
93             throws CoreException, IOException JavaDoc {
94         if (resource.getType() == IResource.FILE)
95             writeFile((IFile) resource, destinationPath);
96         else {
97             createFolder(destinationPath);
98             writeChildren((IContainer) resource, destinationPath);
99         }
100     }
101 }
102
Popular Tags