Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71b2db9a6013c4bd421f11583726f80ac773fe12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved.   This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.core.document;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.MalformedInputException;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.jface.text.IDocument;
import org.eclipse.wst.encoding.CodedIO;
import org.eclipse.wst.encoding.EncodingMemento;
import org.eclipse.wst.encoding.IContentDescriptionExtended;
import org.eclipse.wst.encoding.exceptions.MalformedInputExceptionWithDetail;

public class StructuredDocumentLoader {

	private ITextFileBufferManager fBufferManager;

	public IEncodedDocument createNewStructuredDocument(IFile iFile, IProgressMonitor monitor) throws IOException, CoreException {
		IDocument document = null;
		IPath locationPath = iFile.getFullPath();
	
		if (iFile.exists()) {
	
			getBufferManager().connect(locationPath, monitor);
			ITextFileBuffer buffer = getBufferManager().getTextFileBuffer(locationPath);
			document = buffer.getDocument();
		}
		else {
			document = getBufferManager().createEmptyDocument(locationPath);
		}
	
		return (IEncodedDocument) document;
	}

	/**
	 * @return
	 */
	protected ITextFileBufferManager getBufferManager() {
		if (fBufferManager == null) {
			fBufferManager = FileBuffers.getTextFileBufferManager();
		}
		return fBufferManager;
	}

	/**
	 * @see org.eclipse.wst.sse.core.document.IDocumentLoader#createNewStructuredDocument(org.eclipse.core.resources.IFile)
	 * @deprecated - use form with progress monitor
	 */
	public IEncodedDocument createNewStructuredDocument(IFile iFile) throws IOException, CoreException {
		return createNewStructuredDocument(iFile, null);
	}

	public IEncodedDocument createNewStructuredDocument(IPath locationPath, IProgressMonitor monitor) throws IOException, CoreException {
		IDocument document = null;
		File file = null;
		file = FileBuffers.getSystemFileAtLocation(locationPath);
		if (file.exists()) {
			getBufferManager().connect(locationPath, monitor);
			ITextFileBuffer buffer = getBufferManager().getTextFileBuffer(locationPath);
			document = buffer.getDocument();
		}
		else {
			document = getBufferManager().createEmptyDocument(locationPath);
		}
		return (IEncodedDocument) document;
	}

	public void release(IFile iFile, IProgressMonitor monitor) throws CoreException {
		IPath locationPath = iFile.getFullPath();
		getBufferManager().disconnect(locationPath, monitor);
	
	}

	/**
	 * @param reader
	 * @return @throws
	 *         IOException
	 */
	private EncodingMemento getEncodingMemento(Reader reader) throws IOException {
		IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, null, new QualifiedName[]{IContentDescriptionExtended.ENCODING_MEMENTO});
		EncodingMemento encodingMemento = null;
		if (description != null) {
			encodingMemento = (EncodingMemento) description.getProperty(IContentDescriptionExtended.ENCODING_MEMENTO);
		}
		return encodingMemento;
	}

	/**
	 * Very mechanical method, just to read the characters, once the reader is
	 * correctly created. Can throw MalFormedInputException.
	 */
	protected StringBuffer readInputStream(Reader reader) throws IOException {
	
		int fBlocksRead = 0;
		StringBuffer buffer = new StringBuffer();
		int numRead = 0;
		try {
			char tBuff[] = new char[CodedIO.MAX_BUF_SIZE];
			while ((numRead = reader.read(tBuff, 0, tBuff.length)) != -1) {
				buffer.append(tBuff, 0, numRead);
				fBlocksRead++;
			}
		}
		catch (MalformedInputException e) {
			EncodingMemento encodingMemento = getEncodingMemento(reader);
			throw new MalformedInputExceptionWithDetail(encodingMemento.getJavaCharsetName(), fBlocksRead * CodedIO.MAX_BUF_SIZE + e.getInputLength());
		}
		return buffer;
	}

	protected void setDocumentContentsFromReader(IEncodedDocument structuredDocument, Reader reader) throws IOException {
	
		StringBuffer allText = readInputStream(reader);
		structuredDocument.set(allText.toString());
	}

	public void reload(IEncodedDocument document, Reader reader) throws IOException {
		setDocumentContentsFromReader(document, reader);
	
	}

}

Back to the top