1 11 package org.eclipse.pde.internal.core.target; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.pde.core.IModelChangedEvent; 25 import org.eclipse.pde.internal.core.IWorkspaceModel; 26 import org.eclipse.pde.internal.core.PDECore; 27 28 29 public class WorkspaceTargetModel extends TargetModel implements IWorkspaceModel { 30 31 private static final long serialVersionUID = 1L; 32 33 private IFile fFile; 34 35 private boolean fDirty; 36 37 private boolean fEditable; 38 39 public WorkspaceTargetModel(IFile file, boolean editable) { 40 fFile = file; 41 fEditable = editable; 42 } 43 44 47 public void load() throws CoreException { 48 if (fFile.exists()) { 49 InputStream stream = null; 50 try { 51 stream = new BufferedInputStream (fFile.getContents(true)); 52 load(stream, false); 53 } catch (CoreException e) { 54 } 55 } 56 } 57 58 61 public boolean isInSync() { 62 IPath path = fFile.getLocation(); 63 return path == null ? false : isInSync(path.toFile()); 64 } 65 66 69 public IResource getUnderlyingResource() { 70 return fFile; 71 } 72 73 76 public String getInstallLocation() { 77 return fFile.getLocation().toOSString(); 78 } 79 80 83 public void save() { 84 try { 85 String contents = getContents(); 86 ByteArrayInputStream stream = 87 new ByteArrayInputStream (contents.getBytes("UTF8")); if (fFile.exists()) { 89 fFile.setContents(stream, false, false, null); 90 } else { 91 fFile.create(stream, false, null); 92 } 93 stream.close(); 94 } catch (CoreException e) { 95 PDECore.logException(e); 96 } catch (IOException e) { 97 } 98 } 99 100 public String getContents() { 101 StringWriter swriter = new StringWriter (); 102 PrintWriter writer = new PrintWriter (swriter); 103 setLoaded(true); 104 save(writer); 105 writer.flush(); 106 try { 107 swriter.close(); 108 } catch (IOException e) { 109 } 110 return swriter.toString(); 111 } 112 113 114 117 public boolean isDirty() { 118 return fDirty; 119 } 120 121 124 public void save(PrintWriter writer) { 125 if (isLoaded()) { 126 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<?pde version=\"3.2\"?>"); writer.println(); 129 getTarget().write("", writer); } 131 setDirty(false); 132 } 133 134 137 public void setDirty(boolean dirty) { 138 fDirty = dirty; 139 } 140 141 144 public void fireModelChanged(IModelChangedEvent event) { 145 setDirty(true); 146 super.fireModelChanged(event); 147 } 148 149 152 public boolean isEditable() { 153 return fEditable; 154 } 155 156 159 public void reload() { 160 if (fFile.exists()) { 162 InputStream stream = null; 163 try { 164 stream = new BufferedInputStream (fFile.getContents(true)); 166 reload(stream, false); 168 setDirty(false); 170 } catch (CoreException e) { 171 } 173 } 174 } 175 } 176 | Popular Tags |