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