KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > binary > AbstractCodeCreationOperation


1 /*******************************************************************************
2  * Copyright (c) 2006 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.jdt.internal.corext.refactoring.binary;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.net.URI JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.filesystem.EFS;
21 import org.eclipse.core.filesystem.IFileStore;
22
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.core.runtime.OperationCanceledException;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.core.runtime.SubProgressMonitor;
32
33 import org.eclipse.core.resources.IWorkspaceRunnable;
34
35 import org.eclipse.jdt.core.IClassFile;
36 import org.eclipse.jdt.core.IPackageFragment;
37
38
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40
41 /**
42  * Partial implementation of a code creation operation.
43  *
44  * @since 3.2
45  */

46 public abstract class AbstractCodeCreationOperation implements IWorkspaceRunnable {
47
48     /** The URI where to output the stubs */
49     protected final URI JavaDoc fOutputURI;
50
51     /** The list of packages to create stubs for */
52     protected final List JavaDoc fPackages;
53
54     /**
55      * Creates a new abstract code creation operation.
56      *
57      * @param uri
58      * the URI where to output the code
59      * @param packages
60      * the list of packages to create code for
61      */

62     protected AbstractCodeCreationOperation(final URI JavaDoc uri, final List JavaDoc packages) {
63         Assert.isNotNull(uri);
64         Assert.isNotNull(packages);
65         fOutputURI= uri;
66         fPackages= packages;
67     }
68
69     /**
70      * Creates a new compilation unit with the given contents.
71      *
72      * @param store
73      * the file store
74      * @param name
75      * the name of the compilation unit
76      * @param content
77      * the content of the compilation unit
78      * @param monitor
79      * the progress monitor to use
80      * @throws CoreException
81      * if an error occurs while creating the compilation unit
82      */

83     protected void createCompilationUnit(final IFileStore store, final String JavaDoc name, final String JavaDoc content, final IProgressMonitor monitor) throws CoreException {
84         OutputStream JavaDoc stream= null;
85         try {
86             stream= new BufferedOutputStream JavaDoc(store.getChild(name).openOutputStream(EFS.NONE, new SubProgressMonitor(monitor, 1)));
87             try {
88                 stream.write(content.getBytes());
89             } catch (IOException JavaDoc exception) {
90                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, exception.getLocalizedMessage(), exception));
91             }
92         } finally {
93             if (stream != null) {
94                 try {
95                     stream.close();
96                 } catch (IOException JavaDoc exception) {
97                     // Do nothing
98
}
99             }
100         }
101     }
102
103     /**
104      * Creates a package fragment with the given name.
105      *
106      * @param store
107      * the file store
108      * @param name
109      * the name of the package
110      * @param monitor
111      * the progress monitor to use
112      * @throws CoreException
113      * if an error occurs while creating the package fragment
114      */

115     protected void createPackageFragment(final IFileStore store, final String JavaDoc name, final IProgressMonitor monitor) throws CoreException {
116         store.mkdir(EFS.NONE, monitor);
117     }
118
119     /**
120      * Returns the operation label.
121      *
122      * @return the operation label
123      */

124     protected abstract String JavaDoc getOperationLabel();
125
126     /**
127      * Runs the stub generation on the specified class file.
128      *
129      * @param file
130      * the class file
131      * @param parent
132      * the parent store
133      * @param monitor
134      * the progress monitor to use
135      * @throws CoreException
136      * if an error occurs
137      */

138     protected abstract void run(IClassFile file, IFileStore parent, IProgressMonitor monitor) throws CoreException;
139
140     /**
141      * {@inheritDoc}
142      */

143     public void run(IProgressMonitor monitor) throws CoreException {
144         if (monitor == null)
145             monitor= new NullProgressMonitor();
146         monitor.beginTask(getOperationLabel(), 100 * fPackages.size());
147         try {
148             final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(128);
149             for (final Iterator JavaDoc iterator= fPackages.iterator(); iterator.hasNext();) {
150                 final IPackageFragment fragment= (IPackageFragment) iterator.next();
151                 final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100);
152                 final IClassFile[] files= fragment.getClassFiles();
153                 final int size= files.length;
154                 subMonitor.beginTask(getOperationLabel(), size * 50);
155                 final String JavaDoc name= fragment.getElementName();
156                 IFileStore store= EFS.getStore(fOutputURI);
157                 if (!"".equals(name)) { //$NON-NLS-1$
158
final String JavaDoc pack= name;
159                     buffer.setLength(0);
160                     buffer.append(name);
161                     final int length= buffer.length();
162                     for (int index= 0; index < length; index++) {
163                         if (buffer.charAt(index) == '.')
164                             buffer.setCharAt(index, '/');
165                     }
166                     store= store.getChild(new Path(buffer.toString()));
167                     if (!pack.startsWith(".")) //$NON-NLS-1$
168
createPackageFragment(store, pack, new SubProgressMonitor(subMonitor, 10));
169                 } else
170                     createPackageFragment(store, "", new SubProgressMonitor(subMonitor, 10)); //$NON-NLS-1$
171
final IProgressMonitor subsubMonitor= new SubProgressMonitor(subMonitor, 30);
172                 try {
173                     subsubMonitor.beginTask(getOperationLabel(), size * 100);
174                     for (int index= 0; index < size; index++) {
175                         if (subMonitor.isCanceled())
176                             throw new OperationCanceledException();
177                         run(files[index], store, new SubProgressMonitor(subsubMonitor, 100));
178                     }
179                 } finally {
180                     subsubMonitor.done();
181                 }
182             }
183         } finally {
184             monitor.done();
185         }
186     }
187 }
188
Popular Tags