KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > CoreUtility


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.core.util;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.zip.ZipEntry JavaDoc;
20 import java.util.zip.ZipFile JavaDoc;
21
22 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
23
24 import org.eclipse.core.resources.IContainer;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IProjectDescription;
28 import org.eclipse.core.resources.IWorkspaceRoot;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.core.runtime.Platform;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.osgi.service.resolver.BundleDescription;
36 import org.eclipse.pde.core.plugin.IPluginLibrary;
37 import org.eclipse.pde.core.plugin.IPluginModelBase;
38 import org.eclipse.pde.core.plugin.PluginRegistry;
39 import org.eclipse.pde.internal.core.PDECore;
40
41
42 public class CoreUtility {
43     
44     /**
45      * Read a file from an InputStream and write it to the file system.
46      *
47      * @param in InputStream from which to read.
48      * @param file output file to create.
49      * @exception IOException
50      */

51     public static void readFile(InputStream JavaDoc in, File JavaDoc file) throws IOException JavaDoc {
52         FileOutputStream JavaDoc fos = null;
53         try {
54             fos = new FileOutputStream JavaDoc(file);
55
56             byte buffer[] = new byte[1024];
57             int count;
58             while ((count = in.read(buffer, 0, buffer.length)) > 0) {
59                 fos.write(buffer, 0, count);
60             }
61
62             fos.close();
63             fos = null;
64
65             in.close();
66             in = null;
67         } catch (IOException JavaDoc e) {
68             // close open streams
69
if (fos != null) {
70                 try {
71                     fos.close();
72                 } catch (IOException JavaDoc ee) {
73                 }
74             }
75
76             if (in != null) {
77                 try {
78                     in.close();
79                 } catch (IOException JavaDoc ee) {
80                 }
81             }
82
83             throw e;
84         }
85     }
86
87     public static void addNatureToProject(IProject proj, String JavaDoc natureId,
88             IProgressMonitor monitor) throws CoreException {
89         IProjectDescription description = proj.getDescription();
90         String JavaDoc[] prevNatures = description.getNatureIds();
91         String JavaDoc[] newNatures = new String JavaDoc[prevNatures.length + 1];
92         System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
93         newNatures[prevNatures.length] = natureId;
94         description.setNatureIds(newNatures);
95         proj.setDescription(description, monitor);
96     }
97     
98     public static void createFolder(IFolder folder) throws CoreException {
99         if (!folder.exists()) {
100             IContainer parent = folder.getParent();
101             if (parent instanceof IFolder) {
102                 createFolder((IFolder) parent);
103             }
104             folder.create(true, true, null);
105         }
106     }
107
108     public static void createProject(IProject project, IPath location,
109             IProgressMonitor monitor) throws CoreException {
110         if (!Platform.getLocation().equals(location)) {
111             IProjectDescription desc = project.getWorkspace()
112                     .newProjectDescription(project.getName());
113             desc.setLocation(location);
114             project.create(desc, monitor);
115         } else
116             project.create(monitor);
117     }
118     
119     public static String JavaDoc normalize(String JavaDoc text) {
120         if (text == null || text.trim().length() == 0)
121             return ""; //$NON-NLS-1$
122

123         text = text.replaceAll("\\r|\\n|\\f|\\t", " "); //$NON-NLS-1$ //$NON-NLS-2$
124
return text.replaceAll("\\s+", " "); //$NON-NLS-1$ //$NON-NLS-2$
125
}
126
127     public static void deleteContent(File JavaDoc curr) {
128         if (curr.exists()) {
129             if (curr.isDirectory()) {
130                 File JavaDoc[] children = curr.listFiles();
131                 if (children != null) {
132                     for (int i = 0; i < children.length; i++) {
133                         deleteContent(children[i]);
134                     }
135                 }
136             }
137             curr.delete();
138         }
139     }
140     
141     public static boolean guessUnpack(BundleDescription bundle) {
142         if (bundle == null)
143             return true;
144     
145         if (new File JavaDoc(bundle.getLocation()).isFile())
146             return false;
147         
148         IWorkspaceRoot root = PDECore.getWorkspace().getRoot();
149         IContainer container = root.getContainerForLocation(new Path(bundle.getLocation()));
150         if (container == null)
151             return true;
152         
153         if (container instanceof IProject) {
154             try {
155                 if (!((IProject)container).hasNature(JavaCore.NATURE_ID))
156                     return true;
157             } catch (CoreException e) {
158             }
159         }
160         
161         IPluginModelBase model = PluginRegistry.findModel(bundle);
162         if (model == null)
163             return true;
164     
165         IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
166         if (libraries.length == 0)
167             return false;
168     
169         for (int i = 0; i < libraries.length; i++) {
170             if (libraries[i].getName().equals(".")) //$NON-NLS-1$
171
return false;
172         }
173         return true;
174     }
175     
176     public static boolean jarContainsResource(File JavaDoc file, String JavaDoc resource, boolean directory) {
177         ZipFile JavaDoc jarFile = null;
178         try {
179             jarFile = new ZipFile JavaDoc(file, ZipFile.OPEN_READ);
180             ZipEntry JavaDoc resourceEntry = jarFile.getEntry(resource);
181             if (resourceEntry != null)
182                 return directory ? resourceEntry.isDirectory() : true;
183         } catch (IOException JavaDoc e) {
184         } catch (FactoryConfigurationError JavaDoc e) {
185         } finally {
186             try {
187                 if (jarFile != null)
188                     jarFile.close();
189             } catch (IOException JavaDoc e2) {
190             }
191         }
192         return false;
193     }
194     
195     public static void copyFile(IPath originPath, String JavaDoc name, File JavaDoc target) {
196         File JavaDoc source = new File JavaDoc(originPath.toFile(), name);
197         if (source.exists() == false)
198             return;
199         FileInputStream JavaDoc is = null;
200         FileOutputStream JavaDoc os = null;
201         try {
202             is = new FileInputStream JavaDoc(source);
203             os = new FileOutputStream JavaDoc(target);
204             byte[] buf = new byte[1024];
205             long currentLen = 0;
206             int len = is.read(buf);
207             while (len != -1) {
208                 currentLen += len;
209                 os.write(buf, 0, len);
210                 len = is.read(buf);
211             }
212         } catch (IOException JavaDoc e) {
213         } finally {
214             try {
215                 if (is != null)
216                     is.close();
217                 if (os != null)
218                     os.close();
219             } catch (IOException JavaDoc e) {
220             }
221         }
222     }
223     
224     public static org.eclipse.jface.text.Document getTextDocument(File JavaDoc bundleLocation, String JavaDoc path) {
225         ZipFile JavaDoc jarFile = null;
226         InputStream JavaDoc stream = null;
227         try {
228             String JavaDoc extension = new Path(bundleLocation.getName()).getFileExtension();
229             if ("jar".equals(extension) && bundleLocation.isFile()) { //$NON-NLS-1$
230
jarFile = new ZipFile JavaDoc(bundleLocation, ZipFile.OPEN_READ);
231                 ZipEntry JavaDoc manifestEntry = jarFile.getEntry(path);
232                 if (manifestEntry != null) {
233                     stream = jarFile.getInputStream(manifestEntry);
234                 }
235             } else {
236                 File JavaDoc file = new File JavaDoc(bundleLocation, path);
237                 if (file.exists())
238                     stream = new FileInputStream JavaDoc(file);
239             }
240             return getTextDocument(stream);
241         } catch (IOException JavaDoc e) {
242         } finally {
243             try {
244                 if (jarFile != null)
245                     jarFile.close();
246             } catch (IOException JavaDoc e) {
247             }
248         }
249         return null;
250     }
251     
252     public static org.eclipse.jface.text.Document getTextDocument(InputStream JavaDoc in) {
253         ByteArrayOutputStream JavaDoc output = null;
254         String JavaDoc result = null;
255         try {
256             output = new ByteArrayOutputStream JavaDoc();
257
258             byte buffer[] = new byte[1024];
259             int count;
260             while ((count = in.read(buffer, 0, buffer.length)) > 0) {
261                 output.write(buffer, 0, count);
262             }
263
264             result = output.toString("UTF-8"); //$NON-NLS-1$
265
output.close();
266             output = null;
267             in.close();
268             in = null;
269         } catch (IOException JavaDoc e) {
270             // close open streams
271
if (output != null) {
272                 try {
273                     output.close();
274                 } catch (IOException JavaDoc ee) {
275                 }
276             }
277
278             if (in != null) {
279                 try {
280                     in.close();
281                 } catch (IOException JavaDoc ee) {
282                 }
283             }
284         }
285         return result == null ? null : new org.eclipse.jface.text.Document(result);
286     }
287     
288 }
289
Popular Tags