1 11 package org.eclipse.pde.internal.core.util; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.zip.ZipEntry ; 20 import java.util.zip.ZipFile ; 21 22 import javax.xml.parsers.FactoryConfigurationError ; 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 51 public static void readFile(InputStream in, File file) throws IOException { 52 FileOutputStream fos = null; 53 try { 54 fos = new FileOutputStream (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 e) { 68 if (fos != null) { 70 try { 71 fos.close(); 72 } catch (IOException ee) { 73 } 74 } 75 76 if (in != null) { 77 try { 78 in.close(); 79 } catch (IOException ee) { 80 } 81 } 82 83 throw e; 84 } 85 } 86 87 public static void addNatureToProject(IProject proj, String natureId, 88 IProgressMonitor monitor) throws CoreException { 89 IProjectDescription description = proj.getDescription(); 90 String [] prevNatures = description.getNatureIds(); 91 String [] newNatures = new String [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 normalize(String text) { 120 if (text == null || text.trim().length() == 0) 121 return ""; 123 text = text.replaceAll("\\r|\\n|\\f|\\t", " "); return text.replaceAll("\\s+", " "); } 126 127 public static void deleteContent(File curr) { 128 if (curr.exists()) { 129 if (curr.isDirectory()) { 130 File [] 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 (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(".")) return false; 172 } 173 return true; 174 } 175 176 public static boolean jarContainsResource(File file, String resource, boolean directory) { 177 ZipFile jarFile = null; 178 try { 179 jarFile = new ZipFile (file, ZipFile.OPEN_READ); 180 ZipEntry resourceEntry = jarFile.getEntry(resource); 181 if (resourceEntry != null) 182 return directory ? resourceEntry.isDirectory() : true; 183 } catch (IOException e) { 184 } catch (FactoryConfigurationError e) { 185 } finally { 186 try { 187 if (jarFile != null) 188 jarFile.close(); 189 } catch (IOException e2) { 190 } 191 } 192 return false; 193 } 194 195 public static void copyFile(IPath originPath, String name, File target) { 196 File source = new File (originPath.toFile(), name); 197 if (source.exists() == false) 198 return; 199 FileInputStream is = null; 200 FileOutputStream os = null; 201 try { 202 is = new FileInputStream (source); 203 os = new FileOutputStream (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 e) { 213 } finally { 214 try { 215 if (is != null) 216 is.close(); 217 if (os != null) 218 os.close(); 219 } catch (IOException e) { 220 } 221 } 222 } 223 224 public static org.eclipse.jface.text.Document getTextDocument(File bundleLocation, String path) { 225 ZipFile jarFile = null; 226 InputStream stream = null; 227 try { 228 String extension = new Path(bundleLocation.getName()).getFileExtension(); 229 if ("jar".equals(extension) && bundleLocation.isFile()) { jarFile = new ZipFile (bundleLocation, ZipFile.OPEN_READ); 231 ZipEntry manifestEntry = jarFile.getEntry(path); 232 if (manifestEntry != null) { 233 stream = jarFile.getInputStream(manifestEntry); 234 } 235 } else { 236 File file = new File (bundleLocation, path); 237 if (file.exists()) 238 stream = new FileInputStream (file); 239 } 240 return getTextDocument(stream); 241 } catch (IOException e) { 242 } finally { 243 try { 244 if (jarFile != null) 245 jarFile.close(); 246 } catch (IOException e) { 247 } 248 } 249 return null; 250 } 251 252 public static org.eclipse.jface.text.Document getTextDocument(InputStream in) { 253 ByteArrayOutputStream output = null; 254 String result = null; 255 try { 256 output = new ByteArrayOutputStream (); 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"); output.close(); 266 output = null; 267 in.close(); 268 in = null; 269 } catch (IOException e) { 270 if (output != null) { 272 try { 273 output.close(); 274 } catch (IOException ee) { 275 } 276 } 277 278 if (in != null) { 279 try { 280 in.close(); 281 } catch (IOException ee) { 282 } 283 } 284 } 285 return result == null ? null : new org.eclipse.jface.text.Document(result); 286 } 287 288 } 289 | Popular Tags |