001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.bundle;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.PrintWriter;
016: import java.io.StringWriter;
017: import java.util.Map;
018:
019: import org.eclipse.core.resources.IContainer;
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IFolder;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IPath;
025: import org.eclipse.pde.core.IEditableModel;
026: import org.eclipse.pde.core.IModelChangedEvent;
027: import org.eclipse.pde.internal.core.PDECore;
028: import org.eclipse.pde.internal.core.converter.PluginConverter;
029: import org.eclipse.pde.internal.core.ibundle.IBundle;
030: import org.eclipse.pde.internal.core.ibundle.IBundleModelFactory;
031: import org.eclipse.pde.internal.core.text.bundle.BundleModelFactory;
032: import org.eclipse.pde.internal.core.util.CoreUtility;
033:
034: public class WorkspaceBundleModel extends BundleModel implements
035: IEditableModel {
036: private static final long serialVersionUID = 1L;
037:
038: private IFile fUnderlyingResource;
039:
040: private boolean fDirty;
041:
042: private boolean fEditable = true;
043:
044: private IBundleModelFactory fFactory;
045:
046: private static final String MANIFEST_VERSION = "Manifest-Version"; //$NON-NLS-1$
047:
048: public WorkspaceBundleModel(IFile file) {
049: fUnderlyingResource = file;
050: }
051:
052: public void fireModelChanged(IModelChangedEvent event) {
053: setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
054: super .fireModelChanged(event);
055: }
056:
057: public String getContents() {
058: StringWriter swriter = new StringWriter();
059: PrintWriter writer = new PrintWriter(swriter);
060: save(writer);
061: writer.flush();
062: try {
063: swriter.close();
064: } catch (IOException e) {
065: }
066: return swriter.toString();
067: }
068:
069: public IResource getUnderlyingResource() {
070: return fUnderlyingResource;
071: }
072:
073: public String getInstallLocation() {
074: // Ensure we have an underlying resource
075: if (fUnderlyingResource == null) {
076: return null;
077: }
078: IPath path = fUnderlyingResource.getLocation();
079: if (path == null)
080: return null;
081: return path.removeLastSegments(2).addTrailingSeparator()
082: .toOSString();
083: }
084:
085: public boolean isDirty() {
086: return fDirty;
087: }
088:
089: public boolean isEditable() {
090: return fEditable;
091: }
092:
093: public void load() {
094: if (fUnderlyingResource == null)
095: return;
096: if (fUnderlyingResource.exists()) {
097: try {
098: InputStream stream = fUnderlyingResource
099: .getContents(true);
100: load(stream, false);
101: stream.close();
102: } catch (Exception e) {
103: PDECore.logException(e);
104: }
105: }
106: }
107:
108: public boolean isInSync() {
109: // If we have no underlying resource, it probably got deleted from right
110: // underneath us; thus, the model is not in sync
111: if (fUnderlyingResource == null) {
112: return false;
113: } else if (fUnderlyingResource.getLocation() == null) {
114: return false;
115: }
116: return isInSync(fUnderlyingResource.getLocation().toFile());
117: }
118:
119: protected void updateTimeStamp() {
120: // If we have no underlying resource, it probably got deleted from right
121: // underneath us; thus, there is nothing to update the time stamp for
122: if (fUnderlyingResource == null) {
123: return;
124: } else if (fUnderlyingResource.getLocation() == null) {
125: return;
126: }
127: updateTimeStamp(fUnderlyingResource.getLocation().toFile());
128: }
129:
130: public void save() {
131: if (fUnderlyingResource == null)
132: return;
133: try {
134: String contents = getContents();
135: ByteArrayInputStream stream = new ByteArrayInputStream(
136: contents.getBytes("UTF-8")); //$NON-NLS-1$
137: if (fUnderlyingResource.exists()) {
138: fUnderlyingResource.setContents(stream, false, false,
139: null);
140: } else {
141: // prevents Core Exception when META-INF folder does not exist
142: IContainer parent = fUnderlyingResource.getParent();
143: if (!parent.exists() && parent instanceof IFolder)
144: CoreUtility.createFolder((IFolder) parent);
145: fUnderlyingResource.create(stream, false, null);
146: }
147: stream.close();
148: } catch (CoreException e) {
149: PDECore.logException(e);
150: } catch (IOException e) {
151: }
152: }
153:
154: public void save(PrintWriter writer) {
155: IBundle bundle = getBundle();
156: Map headers = ((Bundle) bundle).getHeaders();
157: boolean addManifestVersion = headers.get(MANIFEST_VERSION) == null;
158: if (addManifestVersion)
159: headers.put(MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
160: try {
161: PluginConverter.getDefault().writeManifest(headers, writer);
162: } catch (IOException e) {
163: } finally {
164: if (addManifestVersion)
165: headers.remove(MANIFEST_VERSION);
166: }
167: fDirty = false;
168: }
169:
170: public void setDirty(boolean dirty) {
171: fDirty = dirty;
172: }
173:
174: public void setEditable(boolean editable) {
175: fEditable = editable;
176: }
177:
178: public IBundleModelFactory getFactory() {
179: if (fFactory == null)
180: fFactory = new BundleModelFactory(this);
181: return fFactory;
182: }
183: }
|