KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > bundle > WorkspaceBundleModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.bundle;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.util.Map JavaDoc;
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 JavaDoc MANIFEST_VERSION = "Manifest-Version"; //$NON-NLS-1$
47

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 JavaDoc getContents() {
58         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
59         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
60         save(writer);
61         writer.flush();
62         try {
63             swriter.close();
64         } catch (IOException JavaDoc e) {
65         }
66         return swriter.toString();
67     }
68
69     public IResource getUnderlyingResource() {
70         return fUnderlyingResource;
71     }
72
73     public String JavaDoc getInstallLocation() {
74         // Ensure we have an underlying resource
75
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 JavaDoc stream = fUnderlyingResource.getContents(true);
98                 load(stream, false);
99                 stream.close();
100             } catch (Exception JavaDoc e) {
101                 PDECore.logException(e);
102             }
103         }
104     }
105
106     public boolean isInSync() {
107         // If we have no underlying resource, it probably got deleted from right
108
// underneath us; thus, the model is not in sync
109
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 we have no underlying resource, it probably got deleted from right
119
// underneath us; thus, there is nothing to update the time stamp for
120
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 JavaDoc contents = getContents();
133             ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(contents
134                     .getBytes("UTF-8")); //$NON-NLS-1$
135
if (fUnderlyingResource.exists()) {
136                 fUnderlyingResource.setContents(stream, false, false, null);
137             } else {
138                 // prevents Core Exception when META-INF folder does not exist
139
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 JavaDoc e) {
148         }
149     }
150
151     public void save(PrintWriter JavaDoc writer) {
152         IBundle bundle = getBundle();
153         Map JavaDoc headers = ((Bundle)bundle).getHeaders();
154         boolean addManifestVersion = headers.get(MANIFEST_VERSION) == null;
155         if (addManifestVersion)
156             headers.put(MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
157
try {
158             PluginConverter.getDefault().writeManifest(headers, writer);
159         } catch (IOException JavaDoc 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