001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.util;
011:
012: import java.io.ByteArrayOutputStream;
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.util.zip.ZipEntry;
019: import java.util.zip.ZipFile;
020:
021: import javax.xml.parsers.FactoryConfigurationError;
022:
023: import org.eclipse.core.resources.IContainer;
024: import org.eclipse.core.resources.IFolder;
025: import org.eclipse.core.resources.IProject;
026: import org.eclipse.core.resources.IProjectDescription;
027: import org.eclipse.core.resources.IWorkspaceRoot;
028: import org.eclipse.core.runtime.CoreException;
029: import org.eclipse.core.runtime.IPath;
030: import org.eclipse.core.runtime.IProgressMonitor;
031: import org.eclipse.core.runtime.Path;
032: import org.eclipse.core.runtime.Platform;
033: import org.eclipse.jdt.core.JavaCore;
034: import org.eclipse.osgi.service.resolver.BundleDescription;
035: import org.eclipse.pde.core.plugin.IPluginLibrary;
036: import org.eclipse.pde.core.plugin.IPluginModelBase;
037: import org.eclipse.pde.core.plugin.PluginRegistry;
038: import org.eclipse.pde.internal.core.PDECore;
039:
040: public class CoreUtility {
041:
042: /**
043: * Read a file from an InputStream and write it to the file system.
044: *
045: * @param in InputStream from which to read.
046: * @param file output file to create.
047: * @exception IOException
048: */
049: public static void readFile(InputStream in, File file)
050: throws IOException {
051: FileOutputStream fos = null;
052: try {
053: fos = new FileOutputStream(file);
054:
055: byte buffer[] = new byte[1024];
056: int count;
057: while ((count = in.read(buffer, 0, buffer.length)) > 0) {
058: fos.write(buffer, 0, count);
059: }
060:
061: fos.close();
062: fos = null;
063:
064: in.close();
065: in = null;
066: } catch (IOException e) {
067: // close open streams
068: if (fos != null) {
069: try {
070: fos.close();
071: } catch (IOException ee) {
072: }
073: }
074:
075: if (in != null) {
076: try {
077: in.close();
078: } catch (IOException ee) {
079: }
080: }
081:
082: throw e;
083: }
084: }
085:
086: public static void addNatureToProject(IProject proj,
087: String natureId, IProgressMonitor monitor)
088: throws CoreException {
089: IProjectDescription description = proj.getDescription();
090: String[] prevNatures = description.getNatureIds();
091: String[] newNatures = new String[prevNatures.length + 1];
092: System.arraycopy(prevNatures, 0, newNatures, 0,
093: prevNatures.length);
094: newNatures[prevNatures.length] = natureId;
095: description.setNatureIds(newNatures);
096: proj.setDescription(description, monitor);
097: }
098:
099: public static void createFolder(IFolder folder)
100: throws CoreException {
101: if (!folder.exists()) {
102: IContainer parent = folder.getParent();
103: if (parent instanceof IFolder) {
104: createFolder((IFolder) parent);
105: }
106: folder.create(true, true, null);
107: }
108: }
109:
110: public static void createProject(IProject project, IPath location,
111: IProgressMonitor monitor) throws CoreException {
112: if (!Platform.getLocation().equals(location)) {
113: IProjectDescription desc = project.getWorkspace()
114: .newProjectDescription(project.getName());
115: desc.setLocation(location);
116: project.create(desc, monitor);
117: } else
118: project.create(monitor);
119: }
120:
121: public static String normalize(String text) {
122: if (text == null || text.trim().length() == 0)
123: return ""; //$NON-NLS-1$
124:
125: text = text.replaceAll("\\r|\\n|\\f|\\t", " "); //$NON-NLS-1$ //$NON-NLS-2$
126: return text.replaceAll("\\s+", " "); //$NON-NLS-1$ //$NON-NLS-2$
127: }
128:
129: public static void deleteContent(File curr) {
130: if (curr.exists()) {
131: if (curr.isDirectory()) {
132: File[] children = curr.listFiles();
133: if (children != null) {
134: for (int i = 0; i < children.length; i++) {
135: deleteContent(children[i]);
136: }
137: }
138: }
139: curr.delete();
140: }
141: }
142:
143: public static boolean guessUnpack(BundleDescription bundle) {
144: if (bundle == null)
145: return true;
146:
147: if (new File(bundle.getLocation()).isFile())
148: return false;
149:
150: IWorkspaceRoot root = PDECore.getWorkspace().getRoot();
151: IContainer container = root.getContainerForLocation(new Path(
152: bundle.getLocation()));
153: if (container == null)
154: return true;
155:
156: if (container instanceof IProject) {
157: try {
158: if (!((IProject) container)
159: .hasNature(JavaCore.NATURE_ID))
160: return true;
161: } catch (CoreException e) {
162: }
163: }
164:
165: IPluginModelBase model = PluginRegistry.findModel(bundle);
166: if (model == null)
167: return true;
168:
169: IPluginLibrary[] libraries = model.getPluginBase()
170: .getLibraries();
171: if (libraries.length == 0)
172: return false;
173:
174: for (int i = 0; i < libraries.length; i++) {
175: if (libraries[i].getName().equals(".")) //$NON-NLS-1$
176: return false;
177: }
178: return true;
179: }
180:
181: public static boolean jarContainsResource(File file,
182: String resource, boolean directory) {
183: ZipFile jarFile = null;
184: try {
185: jarFile = new ZipFile(file, ZipFile.OPEN_READ);
186: ZipEntry resourceEntry = jarFile.getEntry(resource);
187: if (resourceEntry != null)
188: return directory ? resourceEntry.isDirectory() : true;
189: } catch (IOException e) {
190: } catch (FactoryConfigurationError e) {
191: } finally {
192: try {
193: if (jarFile != null)
194: jarFile.close();
195: } catch (IOException e2) {
196: }
197: }
198: return false;
199: }
200:
201: public static void copyFile(IPath originPath, String name,
202: File target) {
203: File source = new File(originPath.toFile(), name);
204: if (source.exists() == false)
205: return;
206: FileInputStream is = null;
207: FileOutputStream os = null;
208: try {
209: is = new FileInputStream(source);
210: os = new FileOutputStream(target);
211: byte[] buf = new byte[1024];
212: long currentLen = 0;
213: int len = is.read(buf);
214: while (len != -1) {
215: currentLen += len;
216: os.write(buf, 0, len);
217: len = is.read(buf);
218: }
219: } catch (IOException e) {
220: } finally {
221: try {
222: if (is != null)
223: is.close();
224: if (os != null)
225: os.close();
226: } catch (IOException e) {
227: }
228: }
229: }
230:
231: public static org.eclipse.jface.text.Document getTextDocument(
232: File bundleLocation, String path) {
233: ZipFile jarFile = null;
234: InputStream stream = null;
235: try {
236: String extension = new Path(bundleLocation.getName())
237: .getFileExtension();
238: if ("jar".equals(extension) && bundleLocation.isFile()) { //$NON-NLS-1$
239: jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ);
240: ZipEntry manifestEntry = jarFile.getEntry(path);
241: if (manifestEntry != null) {
242: stream = jarFile.getInputStream(manifestEntry);
243: }
244: } else {
245: File file = new File(bundleLocation, path);
246: if (file.exists())
247: stream = new FileInputStream(file);
248: }
249: return getTextDocument(stream);
250: } catch (IOException e) {
251: } finally {
252: try {
253: if (jarFile != null)
254: jarFile.close();
255: } catch (IOException e) {
256: }
257: }
258: return null;
259: }
260:
261: public static org.eclipse.jface.text.Document getTextDocument(
262: InputStream in) {
263: ByteArrayOutputStream output = null;
264: String result = null;
265: try {
266: output = new ByteArrayOutputStream();
267:
268: byte buffer[] = new byte[1024];
269: int count;
270: while ((count = in.read(buffer, 0, buffer.length)) > 0) {
271: output.write(buffer, 0, count);
272: }
273:
274: result = output.toString("UTF-8"); //$NON-NLS-1$
275: output.close();
276: output = null;
277: in.close();
278: in = null;
279: } catch (IOException e) {
280: // close open streams
281: if (output != null) {
282: try {
283: output.close();
284: } catch (IOException ee) {
285: }
286: }
287:
288: if (in != null) {
289: try {
290: in.close();
291: } catch (IOException ee) {
292: }
293: }
294: }
295: return result == null ? null
296: : new org.eclipse.jface.text.Document(result);
297: }
298:
299: }
|