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.text;
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.io.UnsupportedEncodingException;
019:
020: import javax.xml.parsers.FactoryConfigurationError;
021: import javax.xml.parsers.ParserConfigurationException;
022:
023: import org.eclipse.core.resources.IFile;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.jface.text.IDocument;
026: import org.eclipse.pde.core.IModel;
027: import org.eclipse.pde.core.IWritable;
028: import org.eclipse.pde.internal.core.PDECore;
029: import org.eclipse.pde.internal.core.util.SAXParserWrapper;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.helpers.DefaultHandler;
032:
033: public abstract class XMLEditingModel extends AbstractEditingModel {
034:
035: public XMLEditingModel(IDocument document, boolean isReconciling) {
036: super (document, isReconciling);
037: }
038:
039: /* (non-Javadoc)
040: * @see org.eclipse.pde.core.IModel#load(java.io.InputStream, boolean)
041: */
042: public void load(InputStream source, boolean outOfSync) {
043: try {
044: fLoaded = true;
045: SAXParserWrapper parser = new SAXParserWrapper();
046: parser.parse(source, createDocumentHandler(this , true));
047: } catch (SAXException e) {
048: fLoaded = false;
049: } catch (IOException e) {
050: } catch (ParserConfigurationException e) {
051: } catch (FactoryConfigurationError e) {
052: }
053: }
054:
055: protected abstract DefaultHandler createDocumentHandler(
056: IModel model, boolean reconciling);
057:
058: public void adjustOffsets(IDocument document) {
059: try {
060: SAXParserWrapper parser = new SAXParserWrapper();
061: parser.parse(getInputStream(document),
062: createDocumentHandler(this , false));
063: } catch (SAXException e) {
064: } catch (IOException e) {
065: } catch (ParserConfigurationException e) {
066: } catch (FactoryConfigurationError e) {
067: }
068: }
069:
070: /**
071: * @return
072: */
073: private boolean isResourceFile() {
074: if (getUnderlyingResource() == null) {
075: return false;
076: } else if ((getUnderlyingResource() instanceof IFile) == false) {
077: return false;
078: }
079: return true;
080: }
081:
082: public void save() {
083: if (isResourceFile() == false) {
084: return;
085: }
086: try {
087: IFile file = (IFile) getUnderlyingResource();
088: String contents = getContents();
089: ByteArrayInputStream stream = new ByteArrayInputStream(
090: contents.getBytes("UTF8")); //$NON-NLS-1$
091: if (file.exists()) {
092: file.setContents(stream, false, false, null);
093: } else {
094: file.create(stream, false, null);
095: }
096: stream.close();
097: } catch (CoreException e) {
098: PDECore.logException(e);
099: } catch (IOException e) {
100: }
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
105: */
106: public void reload() {
107: if (isResourceFile() == false) {
108: return;
109: }
110: IFile file = (IFile) getUnderlyingResource();
111: // Underlying file has to exist in order to reload the model
112: if (file.exists()) {
113: InputStream stream = null;
114: try {
115: // Get the file contents
116: stream = new BufferedInputStream(file.getContents(true));
117: // Load the model using the last saved file contents
118: reload(stream, false);
119: // Remove the dirty (*) indicator from the editor window
120: setDirty(false);
121: } catch (CoreException e) {
122: // Ignore
123: }
124: }
125: }
126:
127: public void reload(IDocument document) {
128: // Get the document's text
129: String text = document.get();
130: InputStream stream = null;
131:
132: try {
133: // Turn the document's text into a stream
134: stream = new ByteArrayInputStream(text.getBytes("UTF8")); //$NON-NLS-1$
135: // Reload the model using the stream
136: reload(stream, false);
137: // Remove the dirty (*) indicator from the editor window
138: setDirty(false);
139: } catch (UnsupportedEncodingException e) {
140: PDECore.logException(e);
141: } catch (CoreException e) {
142: // Ignore
143: }
144: }
145:
146: public String getContents() {
147: StringWriter swriter = new StringWriter();
148: PrintWriter writer = new PrintWriter(swriter);
149: setLoaded(true);
150: save(writer);
151: writer.flush();
152: try {
153: swriter.close();
154: } catch (IOException e) {
155: }
156: return swriter.toString();
157: }
158:
159: /* (non-Javadoc)
160: * @see org.eclipse.pde.internal.core.text.AbstractEditingModel#save(java.io.PrintWriter)
161: */
162: public void save(PrintWriter writer) {
163: if (isLoaded()) {
164: getRoot().write("", writer); //$NON-NLS-1$
165: }
166: setDirty(false);
167: }
168:
169: protected abstract IWritable getRoot();
170: }
|