1 11 package org.eclipse.pde.internal.core.bundle; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 import java.util.Map ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IFolder; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.pde.core.IEditableModel; 27 import org.eclipse.pde.core.IModelChangedEvent; 28 import org.eclipse.pde.internal.core.PDECore; 29 import org.eclipse.pde.internal.core.converter.PluginConverter; 30 import org.eclipse.pde.internal.core.ibundle.IBundle; 31 import org.eclipse.pde.internal.core.ibundle.IBundleModelFactory; 32 import org.eclipse.pde.internal.core.text.bundle.BundleModelFactory; 33 import org.eclipse.pde.internal.core.util.CoreUtility; 34 35 public class WorkspaceBundleModel extends BundleModel implements IEditableModel { 36 private static final long serialVersionUID = 1L; 37 38 private IFile fUnderlyingResource; 39 40 private boolean fDirty; 41 42 private boolean fEditable = true; 43 44 private IBundleModelFactory fFactory; 45 46 private static final String MANIFEST_VERSION = "Manifest-Version"; 48 public WorkspaceBundleModel(IFile file) { 49 fUnderlyingResource = file; 50 } 51 52 public void fireModelChanged(IModelChangedEvent event) { 53 setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED); 54 super.fireModelChanged(event); 55 } 56 57 public String getContents() { 58 StringWriter swriter = new StringWriter (); 59 PrintWriter writer = new PrintWriter (swriter); 60 save(writer); 61 writer.flush(); 62 try { 63 swriter.close(); 64 } catch (IOException e) { 65 } 66 return swriter.toString(); 67 } 68 69 public IResource getUnderlyingResource() { 70 return fUnderlyingResource; 71 } 72 73 public String getInstallLocation() { 74 if (fUnderlyingResource == null) { 76 return null; 77 } 78 IPath path = fUnderlyingResource.getLocation(); 79 if(path == null) 80 return null; 81 return path.removeLastSegments(2).addTrailingSeparator().toOSString(); 82 } 83 84 public boolean isDirty() { 85 return fDirty; 86 } 87 88 public boolean isEditable() { 89 return fEditable; 90 } 91 92 public void load() { 93 if (fUnderlyingResource == null) 94 return; 95 if (fUnderlyingResource.exists()) { 96 try { 97 InputStream stream = fUnderlyingResource.getContents(true); 98 load(stream, false); 99 stream.close(); 100 } catch (Exception e) { 101 PDECore.logException(e); 102 } 103 } 104 } 105 106 public boolean isInSync() { 107 if (fUnderlyingResource == null) { 110 return false; 111 } else if (fUnderlyingResource.getLocation() == null) { 112 return false; 113 } 114 return isInSync(fUnderlyingResource.getLocation().toFile()); 115 } 116 117 protected void updateTimeStamp() { 118 if (fUnderlyingResource == null) { 121 return; 122 } else if (fUnderlyingResource.getLocation() == null) { 123 return; 124 } 125 updateTimeStamp(fUnderlyingResource.getLocation().toFile()); 126 } 127 128 public void save() { 129 if (fUnderlyingResource == null) 130 return; 131 try { 132 String contents = getContents(); 133 ByteArrayInputStream stream = new ByteArrayInputStream (contents 134 .getBytes("UTF-8")); if (fUnderlyingResource.exists()) { 136 fUnderlyingResource.setContents(stream, false, false, null); 137 } else { 138 IContainer parent = fUnderlyingResource.getParent(); 140 if (!parent.exists() && parent instanceof IFolder) 141 CoreUtility.createFolder((IFolder)parent); 142 fUnderlyingResource.create(stream, false, null); 143 } 144 stream.close(); 145 } catch (CoreException e) { 146 PDECore.logException(e); 147 } catch (IOException e) { 148 } 149 } 150 151 public void save(PrintWriter writer) { 152 IBundle bundle = getBundle(); 153 Map headers = ((Bundle)bundle).getHeaders(); 154 boolean addManifestVersion = headers.get(MANIFEST_VERSION) == null; 155 if (addManifestVersion) 156 headers.put(MANIFEST_VERSION, "1.0"); try { 158 PluginConverter.getDefault().writeManifest(headers, writer); 159 } catch (IOException e) { 160 } finally { 161 if (addManifestVersion) 162 headers.remove(MANIFEST_VERSION); 163 } 164 fDirty = false; 165 } 166 167 public void setDirty(boolean dirty) { 168 fDirty = dirty; 169 } 170 171 public void setEditable(boolean editable) { 172 fEditable = editable; 173 } 174 175 public IBundleModelFactory getFactory() { 176 if (fFactory == null) 177 fFactory = new BundleModelFactory(this); 178 return fFactory; 179 } 180 } 181 | Popular Tags |