1 11 package org.eclipse.pde.internal.core.feature; 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 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 22 import org.eclipse.core.resources.IFile; 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.NLResourceHelper; 29 import org.eclipse.pde.internal.core.PDECore; 30 31 public class WorkspaceFeatureModel extends AbstractFeatureModel 32 implements 33 IEditableModel { 34 private static final long serialVersionUID = 1L; 35 private boolean dirty; 36 private IFile file; 37 private boolean editable = true; 38 39 public WorkspaceFeatureModel() { 40 super(); 41 } 42 public WorkspaceFeatureModel(IFile file) { 43 setFile(file); 44 } 45 public void fireModelChanged(IModelChangedEvent event) { 46 setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED); 47 super.fireModelChanged(event); 48 } 49 50 protected NLResourceHelper createNLResourceHelper() { 51 try { 52 if (file == null || file.getLocation() == null) 54 return null; 55 IPath path = file.getLocation().removeLastSegments(1); 56 String installLocation = path.toOSString(); 57 if (installLocation.startsWith("file:") == false) installLocation = "file:" + installLocation; URL url = new URL (installLocation + "/"); String name = "feature"; NLResourceHelper helper = new NLResourceHelper(name, new URL []{url}); 62 return helper; 64 } catch (MalformedURLException e) { 65 return null; 66 } 67 } 68 69 public String getContents() { 70 StringWriter swriter = new StringWriter (); 71 PrintWriter writer = new PrintWriter (swriter); 72 setLoaded(true); 73 save(writer); 74 writer.flush(); 75 try { 76 swriter.close(); 77 } catch (IOException e) { 78 } 79 return swriter.toString(); 80 } 81 public IFile getFile() { 82 return file; 83 } 84 public String getInstallLocation() { 85 IPath path = file.getParent().getLocation(); 86 return path == null ? null : path.toOSString(); 87 } 88 public IResource getUnderlyingResource() { 89 return file; 90 } 91 public boolean isDirty() { 92 return dirty; 93 } 94 public boolean isEditable() { 95 return editable; 96 } 97 98 public boolean isInSync() { 99 return isInSync(file.getLocation().toFile()); 100 } 101 102 protected void updateTimeStamp() { 103 updateTimeStamp(file.getLocation().toFile()); 104 } 105 public void load() { 106 if (file == null) 107 return; 108 if (file.exists()) { 109 try { 110 InputStream stream = new BufferedInputStream (file.getContents(true)); 111 load(stream, false); 112 stream.close(); 113 } catch (CoreException e) { 114 } catch (IOException e) { 115 PDECore.logException(e); 116 } 117 } else { 118 this.feature = new Feature(); 119 feature.model = this; 120 setLoaded(true); 121 } 122 } 123 public void save() { 124 if (file == null) 125 return; 126 try { 127 String contents = getContents(); 128 ByteArrayInputStream stream = new ByteArrayInputStream (contents 129 .getBytes("UTF8")); if (file.exists()) { 131 file.setContents(stream, false, false, null); 132 } else { 133 file.create(stream, false, null); 134 } 135 stream.close(); 136 } catch (CoreException e) { 137 PDECore.logException(e); 138 } catch (IOException e) { 139 } 140 } 141 public void save(PrintWriter writer) { 142 if (isLoaded()) { 143 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); feature.write("", writer); } 147 setDirty(false); 148 } 149 public void setDirty(boolean dirty) { 150 this.dirty = dirty; 151 } 152 public void setEditable(boolean newEditable) { 153 editable = newEditable; 154 } 155 public void setFile(IFile newFile) { 156 file = newFile; 157 } 159 } 160 | Popular Tags |