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.site;
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 WorkspaceSiteModel extends AbstractSiteModel implements
031: IEditableModel {
032: private static final long serialVersionUID = 1L;
033: private boolean fDirty;
034: private IFile fFile;
035: private boolean fEditable = true;
036:
037: public WorkspaceSiteModel(IFile file) {
038: fFile = file;
039: }
040:
041: public void fireModelChanged(IModelChangedEvent event) {
042: setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
043: super .fireModelChanged(event);
044: }
045:
046: protected NLResourceHelper createNLResourceHelper() {
047: try {
048: IPath path = fFile.getLocation().removeLastSegments(1);
049: String installLocation = path.toOSString();
050: if (installLocation.startsWith("file:") == false) //$NON-NLS-1$
051: installLocation = "file:" + installLocation; //$NON-NLS-1$
052: URL url = new URL(installLocation + "/"); //$NON-NLS-1$
053: String name = "site"; //$NON-NLS-1$
054: NLResourceHelper helper = new NLResourceHelper(name,
055: new URL[] { url });
056: return helper;
057: } catch (MalformedURLException e) {
058: return null;
059: }
060: }
061:
062: public String getContents() {
063: StringWriter swriter = new StringWriter();
064: PrintWriter writer = new PrintWriter(swriter);
065: setLoaded(true);
066: save(writer);
067: writer.flush();
068: try {
069: swriter.close();
070: } catch (IOException e) {
071: }
072: return swriter.toString();
073: }
074:
075: public IFile getFile() {
076: return fFile;
077: }
078:
079: public String getInstallLocation() {
080: return fFile.getParent().getLocation().toOSString();
081: }
082:
083: public IResource getUnderlyingResource() {
084: return fFile;
085: }
086:
087: public boolean isDirty() {
088: return fDirty;
089: }
090:
091: public boolean isEditable() {
092: return fEditable;
093: }
094:
095: public boolean isInSync() {
096: return isInSync(fFile.getLocation().toFile());
097: }
098:
099: protected void updateTimeStamp() {
100: updateTimeStamp(fFile.getLocation().toFile());
101: }
102:
103: public void load() {
104: if (fFile.exists()) {
105: InputStream stream = null;
106: try {
107: stream = new BufferedInputStream(fFile
108: .getContents(true));
109: load(stream, false);
110: } catch (CoreException e) {
111: } finally {
112: try {
113: if (stream != null)
114: stream.close();
115: } catch (IOException e) {
116: }
117: }
118: } else {
119: this .site = new Site();
120: site.model = this ;
121: setLoaded(true);
122: }
123: }
124:
125: public void save() {
126: try {
127: String contents = getContents();
128: ByteArrayInputStream stream = new ByteArrayInputStream(
129: contents.getBytes("UTF8")); //$NON-NLS-1$
130: if (fFile.exists()) {
131: fFile.setContents(stream, false, false, null);
132: } else {
133: fFile.create(stream, false, null);
134: }
135: stream.close();
136: } catch (CoreException e) {
137: PDECore.logException(e);
138: } catch (IOException e) {
139: }
140: }
141:
142: public void save(PrintWriter writer) {
143: if (isLoaded()) {
144: writer
145: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
146: site.write("", writer); //$NON-NLS-1$
147: }
148: setDirty(false);
149: }
150:
151: public void setDirty(boolean dirty) {
152: fDirty = dirty;
153: }
154:
155: public void setEditable(boolean newEditable) {
156: fEditable = newEditable;
157: }
158: }
|