Skip to main content
summaryrefslogtreecommitdiffstats
blob: 48e22bb1cd0f939df226e457b35f7c141000ca49 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 *******************************************************************************/
package org.eclipse.jst.jsp.core.internal.contentmodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
import org.eclipse.core.filebuffers.IFileBuffer;
import org.eclipse.core.filebuffers.IFileBufferListener;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager;
import org.eclipse.jst.jsp.core.internal.parser.JSPSourceParser;
import org.eclipse.jst.jsp.core.taglib.ITaglibIndexListener;
import org.eclipse.jst.jsp.core.taglib.ITaglibRecordEvent;
import org.eclipse.jst.jsp.core.taglib.TaglibIndex;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument;
import org.eclipse.wst.sse.core.internal.util.Assert;

/**
 * Provides a direct mapping from IStructuredDocument to supporting
 * TLDCMDocumentManager.
 * 
 * Listens to the creation of JSP type TextFileBuffers and forces a text-less
 * reparse after connecting taglib-supporting listeners. Connecting the
 * listeners before the text is set would be ideal, but there is no way to
 * look up taglib references since the location is not yet knowable. Since
 * taglibs can affect the parsing of the document, a reparse is currently
 * required to react to custom tags with tagdependent content.
 * 
 * TODO: Remove the reparse penalty.
 */
public class TaglibController implements IDocumentSetupParticipant {

	class DocumentInfo implements ITaglibIndexListener {
		IStructuredDocument document;
		ITextFileBuffer textFileBuffer;
		TLDCMDocumentManager tldDocumentManager;

		public void indexChanged(ITaglibRecordEvent event) {
			int type = event.getType();
			if (type == ITaglibRecordEvent.CHANGED || type == ITaglibRecordEvent.REMOVED) {
				Object key = TLDCMDocumentManager.getUniqueIdentifier(event.getTaglibRecord());
				if (tldDocumentManager.getDocuments().containsKey(key)) {
					if (_debugCache) {
						System.out.println("TLDCMDocumentManager cleared its private CMDocument cache"); //$NON-NLS-1$
					}
					tldDocumentManager.getDocuments().remove(key);
					tldDocumentManager.getSourceParser().resetHandlers();

					if (document instanceof BasicStructuredDocument) {
						((BasicStructuredDocument) document).reparse(this);
					}
				}
			}
			tldDocumentManager.indexChanged(event);
		}
	}

	static final boolean _debugCache = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.jst.jsp.core/debug/tldcmdocument/cache")); //$NON-NLS-1$ //$NON-NLS-2$

	class FileBufferListener implements IFileBufferListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferContentAboutToBeReplaced(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void bufferContentAboutToBeReplaced(IFileBuffer buffer) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferContentReplaced(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void bufferContentReplaced(IFileBuffer buffer) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferCreated(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void bufferCreated(IFileBuffer buffer) {
			if (buffer instanceof ITextFileBuffer) {
				IDocument document = ((ITextFileBuffer) buffer).getDocument();
				// ignore non-JSP documents
				synchronized (_instance.fJSPdocuments) {
					if (!_instance.fJSPdocuments.contains(document))
						return;
				}
				Assert.isTrue(document instanceof IStructuredDocument, getClass().getName() + " SetupParticipant was called for non-IStructuredDocument"); //$NON-NLS-1$
				DocumentInfo info = new DocumentInfo();
				info.document = (IStructuredDocument) document;
				info.textFileBuffer = (ITextFileBuffer) buffer;
				info.tldDocumentManager = new TLDCMDocumentManager();
				info.tldDocumentManager.setSourceParser((JSPSourceParser) info.document.getParser());
				synchronized (_instance.fDocumentMap) {
					_instance.fDocumentMap.put(document, info);
				}
				TaglibIndex.addTaglibIndexListener(info);
				if (document instanceof BasicStructuredDocument) {
					((BasicStructuredDocument) document).reparse(this);
				}
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferDisposed(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void bufferDisposed(IFileBuffer buffer) {
			if (buffer instanceof ITextFileBuffer) {
				IDocument document = ((ITextFileBuffer) buffer).getDocument();
				synchronized (_instance.fJSPdocuments) {
					if (!_instance.fJSPdocuments.remove(document))
						return;
				}
			}
			DocumentInfo info = null;
			synchronized (fDocumentMap) {
				Object[] keys = fDocumentMap.keySet().toArray();
				for (int i = 0; i < keys.length; i++) {
					info = (DocumentInfo) fDocumentMap.get(keys[i]);
					if (info != null && info.textFileBuffer.equals(buffer)) {
						fDocumentMap.remove(keys[i]);
						break;
					}
				}
			}
			if (info != null) {
				info.tldDocumentManager.clearCache();
				TaglibIndex.removeTaglibIndexListener(info);
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#dirtyStateChanged(org.eclipse.core.filebuffers.IFileBuffer,
		 *      boolean)
		 */
		public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#stateChangeFailed(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void stateChangeFailed(IFileBuffer buffer) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#stateChanging(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void stateChanging(IFileBuffer buffer) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#stateValidationChanged(org.eclipse.core.filebuffers.IFileBuffer,
		 *      boolean)
		 */
		public void stateValidationChanged(IFileBuffer buffer, boolean isStateValidated) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#underlyingFileDeleted(org.eclipse.core.filebuffers.IFileBuffer)
		 */
		public void underlyingFileDeleted(IFileBuffer buffer) {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.core.filebuffers.IFileBufferListener#underlyingFileMoved(org.eclipse.core.filebuffers.IFileBuffer,
		 *      org.eclipse.core.runtime.IPath)
		 */
		public void underlyingFileMoved(IFileBuffer buffer, IPath path) {
		}


	}

	static TaglibController _instance = null;
	static private boolean fIsShutdown = false;

	public static ITextFileBuffer getFileBuffer(IDocument document) {
		synchronized (_instance.fDocumentMap) {
			DocumentInfo info = (DocumentInfo) _instance.fDocumentMap.get(document);
			if (info != null)
				return info.textFileBuffer;
			return null;
		}
	}

	/**
	 * @param manager
	 * @return
	 */
	public static ITextFileBuffer getFileBuffer(TLDCMDocumentManager manager) {
		// if _instance is null, we are already shutting donw
		if (_instance == null)
			return null;

		ITextFileBuffer buffer = null;
		synchronized (_instance.fDocumentMap) {
			Iterator docInfos = _instance.fDocumentMap.values().iterator();
			while (docInfos.hasNext() && buffer == null) {
				DocumentInfo info = (DocumentInfo) docInfos.next();
				if (info.tldDocumentManager.equals(manager))
					buffer = info.textFileBuffer;
			}
		}
		return buffer;
	}

	public static TLDCMDocumentManager getTLDCMDocumentManager(IDocument document) {
		// if _instance is null, we are already shutting donw
		if (_instance == null)
			return null;
		synchronized (_instance.fDocumentMap) {
			DocumentInfo info = (DocumentInfo) _instance.fDocumentMap.get(document);
			if (info != null)
				return info.tldDocumentManager;
			return null;

		}
	}

	private static synchronized boolean isShutdown() {
		return fIsShutdown;
	}

	private static synchronized void setShutdown(boolean isShutdown) {
		fIsShutdown = isShutdown;
	}

	public synchronized static void shutdown() {
		setShutdown(true);
		FileBuffers.getTextFileBufferManager().removeFileBufferListener(_instance.fBufferListener);
		_instance = null;
	}

	public synchronized static void startup() {
		if (_instance == null) {
			_instance = new TaglibController();
			FileBuffers.getTextFileBufferManager().addFileBufferListener(_instance.fBufferListener);
		}
		setShutdown(false);
	}

	IFileBufferListener fBufferListener;

	Map fDocumentMap;

	List fJSPdocuments;

	/*
	 * This constructor is only to be called as part of the FileBuffer
	 * framework
	 */
	public TaglibController() {
		super();
		fBufferListener = new FileBufferListener();
		fJSPdocuments = new ArrayList(1);
		fDocumentMap = new HashMap(1);
	}


	/*
	 * This method is only to be called as part of the FileBuffer framework
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument)
	 */
	public void setup(IDocument document) {
		// if we've already shutdown, just ignore
		if (isShutdown())
			return;
		// reference the shared instance's documents directly
		synchronized (_instance.fJSPdocuments) {
			_instance.fJSPdocuments.add(document);
		}
	}
}

Back to the top