001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.feature;
011:
012: import java.io.BufferedInputStream;
013: import java.io.ByteArrayInputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.PrintWriter;
017: import java.io.StringWriter;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020:
021: import org.eclipse.core.resources.IFile;
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.NLResourceHelper;
028: import org.eclipse.pde.internal.core.PDECore;
029:
030: public class WorkspaceFeatureModel extends AbstractFeatureModel
031: implements IEditableModel {
032: private static final long serialVersionUID = 1L;
033: private boolean dirty;
034: private IFile file;
035: private boolean editable = true;
036:
037: public WorkspaceFeatureModel() {
038: super ();
039: }
040:
041: public WorkspaceFeatureModel(IFile file) {
042: setFile(file);
043: }
044:
045: public void fireModelChanged(IModelChangedEvent event) {
046: setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
047: super .fireModelChanged(event);
048: }
049:
050: protected NLResourceHelper createNLResourceHelper() {
051: try {
052: // TODO revisit this method
053: if (file == null || file.getLocation() == null)
054: return null;
055: IPath path = file.getLocation().removeLastSegments(1);
056: String installLocation = path.toOSString();
057: if (installLocation.startsWith("file:") == false) //$NON-NLS-1$
058: installLocation = "file:" + installLocation; //$NON-NLS-1$
059: URL url = new URL(installLocation + "/"); //$NON-NLS-1$
060: String name = "feature"; //$NON-NLS-1$
061: NLResourceHelper helper = new NLResourceHelper(name,
062: new URL[] { url });
063: //helper.setFile(file);
064: return helper;
065: } catch (MalformedURLException e) {
066: return null;
067: }
068: }
069:
070: public String getContents() {
071: StringWriter swriter = new StringWriter();
072: PrintWriter writer = new PrintWriter(swriter);
073: setLoaded(true);
074: save(writer);
075: writer.flush();
076: try {
077: swriter.close();
078: } catch (IOException e) {
079: }
080: return swriter.toString();
081: }
082:
083: public IFile getFile() {
084: return file;
085: }
086:
087: public String getInstallLocation() {
088: IPath path = file.getParent().getLocation();
089: return path == null ? null : path.toOSString();
090: }
091:
092: public IResource getUnderlyingResource() {
093: return file;
094: }
095:
096: public boolean isDirty() {
097: return dirty;
098: }
099:
100: public boolean isEditable() {
101: return editable;
102: }
103:
104: public boolean isInSync() {
105: return isInSync(file.getLocation().toFile());
106: }
107:
108: protected void updateTimeStamp() {
109: updateTimeStamp(file.getLocation().toFile());
110: }
111:
112: public void load() {
113: if (file == null)
114: return;
115: if (file.exists()) {
116: try {
117: InputStream stream = new BufferedInputStream(file
118: .getContents(true));
119: load(stream, false);
120: stream.close();
121: } catch (CoreException e) {
122: } catch (IOException e) {
123: PDECore.logException(e);
124: }
125: } else {
126: this .feature = new Feature();
127: feature.model = this ;
128: setLoaded(true);
129: }
130: }
131:
132: public void save() {
133: if (file == null)
134: return;
135: try {
136: String contents = getContents();
137: ByteArrayInputStream stream = new ByteArrayInputStream(
138: contents.getBytes("UTF8")); //$NON-NLS-1$
139: if (file.exists()) {
140: file.setContents(stream, false, false, null);
141: } else {
142: file.create(stream, false, null);
143: }
144: stream.close();
145: } catch (CoreException e) {
146: PDECore.logException(e);
147: } catch (IOException e) {
148: }
149: }
150:
151: public void save(PrintWriter writer) {
152: if (isLoaded()) {
153: writer
154: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
155: //writer.println("<!DOCTYPE feature SYSTEM \"dtd/feature.dtd\">");
156: feature.write("", writer); //$NON-NLS-1$
157: }
158: setDirty(false);
159: }
160:
161: public void setDirty(boolean dirty) {
162: this .dirty = dirty;
163: }
164:
165: public void setEditable(boolean newEditable) {
166: editable = newEditable;
167: }
168:
169: public void setFile(IFile newFile) {
170: file = newFile;
171: //setEditable(newFile.isReadOnly()==false);
172: }
173: }
|