Skip to main content
summaryrefslogtreecommitdiffstats
blob: caafaaf7e3a2fe2c874b030a038909d605bfefae (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*******************************************************************************
 * Copyright (c) 2001, 2008 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.wst.sse.core.internal.Logger;
import org.eclipse.wst.sse.core.internal.document.IDocumentLoader;
import org.eclipse.wst.sse.core.internal.encoding.EncodingMemento;
import org.eclipse.wst.sse.core.internal.encoding.EncodingRule;
import org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler;
import org.eclipse.wst.sse.core.internal.ltk.parser.BlockMarker;
import org.eclipse.wst.sse.core.internal.ltk.parser.BlockTagParser;
import org.eclipse.wst.sse.core.internal.ltk.parser.RegionParser;
import org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionHandler;
import org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionHandlerExtension;
import org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionParser;
import org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionParserExtension;
import org.eclipse.wst.sse.core.internal.provisional.IModelLoader;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredPartitioning;
import org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument;
import org.eclipse.wst.sse.core.internal.text.rules.StructuredTextPartitioner;
import org.eclipse.wst.sse.core.internal.util.Assert;


/**
 * This class reads a file and creates an Structured Model.
 */
public abstract class AbstractModelLoader implements IModelLoader {
	protected static final int encodingNameSearchLimit = 1000;

	private static long computeMem() {
		for (int i = 0; i < 5; i++) {
			System.gc();
			System.runFinalization();
		}
		return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
	}

	private boolean DEBUG = false;
	protected IDocumentLoader documentLoaderInstance;

	/**
	 * AbstractLoader constructor also initializes encoding converter/mapper
	 */
	public AbstractModelLoader() {
		super();
	}

	protected void addFactories(IStructuredModel model, List factoryList) {
		Assert.isNotNull(model);
		FactoryRegistry registry = model.getFactoryRegistry();
		Assert.isNotNull(registry, "IStructuredModel " + model.getId() + " has a null FactoryRegistry"); //$NON-NLS-1$ //$NON-NLS-2$
		if (factoryList != null) {
			Iterator iterator = factoryList.iterator();
			while (iterator.hasNext()) {
				INodeAdapterFactory factory = (INodeAdapterFactory) iterator.next();
				registry.addFactory(factory);
			}
		}
	}

	/**
	 * This method should perform all the model initialization required before
	 * it contains content, namely, it should call newModel, the
	 * createNewStructuredDocument(), then add those adapter factories which
	 * must be set before content is applied. This method should be called by
	 * "load" method. (this is tentative API)
	 */
	public IStructuredModel createModel() {
		documentLoaderInstance = null;
		IStructuredModel model = newModel();
		IEncodedDocument structuredDocument = getDocumentLoader().createNewStructuredDocument();
		if (structuredDocument instanceof IStructuredDocument) {
			model.setStructuredDocument((IStructuredDocument) structuredDocument);
			addFactories(model, getAdapterFactories());
			//
			initEmbeddedTypePre(model, (IStructuredDocument) structuredDocument);
			initEmbeddedTypePost(model);
			// For types with propagating adapters, its important
			// that the propagating adapter be in place before the contents
			// are set.
			preLoadAdapt(model);
		}
		return model;
	}

	public IStructuredModel createModel(IStructuredDocument structuredDocument, String baseLocation, IModelHandler handler) {
		documentLoaderInstance = null;
		IStructuredModel model = newModel();
		model.setBaseLocation(baseLocation);
		
		// handler must be set early, incase a re-init is 
		// required during creation.
		model.setModelHandler(handler);
		
		addFactories(model, getAdapterFactories());
		// For types with propagating adapters, it's important
		// that the propagating adapter be in place before the contents
		// are set.
		preLoadAdapt(model);
		initEmbeddedTypePre(model, structuredDocument);

		model.setStructuredDocument(structuredDocument);
		//
		initEmbeddedTypePost(model);

		return model;
	}

	/**
	 * This method is used for cloning models.
	 */
	public IStructuredModel createModel(IStructuredModel oldModel) {
		documentLoaderInstance = null;
		IStructuredModel newModel = newModel();
		IStructuredDocument oldStructuredDocument = oldModel.getStructuredDocument();
		IStructuredDocument newStructuredDocument = oldStructuredDocument.newInstance();
		newModel.setStructuredDocument(newStructuredDocument);
		// NOTE: we DO NOT simply add the standard ones to the new model
		// addFactories(newModel, getAdapterFactories());
		// Now, we take the opportunity to add Factories from the oldModel's
		// registry to the new model's registry .. if they do not already
		// exist there.
		duplicateFactoryRegistry(newModel, oldModel);
		if (newModel instanceof AbstractStructuredModel) {
			((AbstractStructuredModel) newModel).setContentTypeIdentifier(oldModel.getContentTypeIdentifier());
		}
		// addFactories(newModel, oldModel);
		initEmbeddedType(oldModel, newModel);
		// For types with propagating adapters, its important
		// that the propagating adapter be in place before the contents
		// are set.
		preLoadAdapt(newModel);
		return newModel;
	}

	private void duplicateFactoryRegistry(IStructuredModel newModel, IStructuredModel oldModel) {
		List oldAdapterFactories = oldModel.getFactoryRegistry().getFactories();
		List newAdapterFactories = new ArrayList();
		Iterator oldListIterator = oldAdapterFactories.iterator();
		while (oldListIterator.hasNext()) {
			INodeAdapterFactory oldAdapterFactory = (INodeAdapterFactory) oldListIterator.next();
			// now "clone" the adapterfactory
			newAdapterFactories.add(oldAdapterFactory.copy());
		}
		// now that we have the "cloned" list, add to new model
		addFactories(newModel, newAdapterFactories);
	}

	/**
	 * This method must return those factories which must be attached to the
	 * structuredModel before content is applied.
	 */
	public List getAdapterFactories() {
		// abstract method returns none
		return new ArrayList(0);
	}

	abstract public IDocumentLoader getDocumentLoader();

	/**
	 * Method initEmbeddedType, "pre"-stage. Nothing to do here in super class.
	 * 
	 * @param model
	 */
	protected void initEmbeddedTypePre(IStructuredModel model) {
	}

	/**
	 * Method initEmbeddedType, "pre"-stage. By default simply calls the
	 * version of this method that uses only the structured model.
	 * 
	 * @param model
	 *            the model for which to initialize
	 * @param structuredDocument
	 *            The structured document containing the text content for the
	 *            model, which may be a different instance than what is in the
	 *            model at this stage.
	 */
	protected void initEmbeddedTypePre(IStructuredModel model, IStructuredDocument structuredDocument) {
		initEmbeddedTypePre(model);
	}
		
	protected void initEmbeddedTypePost(IStructuredModel model) {
	}

	/**
	 * Method initEmbeddedType. Nothing to do here in super class.
	 * 
	 * @param oldModel
	 * @param newModel
	 */
	protected void initEmbeddedType(IStructuredModel oldModel, IStructuredModel newModel) {
	}

	public void load(IFile file, IStructuredModel model) throws IOException, CoreException {
		IEncodedDocument structuredDocument = model.getStructuredDocument();
		if (file == null)
			structuredDocument = getDocumentLoader().createNewStructuredDocument();
		else
			structuredDocument = getDocumentLoader().createNewStructuredDocument(file);

		// TODO: need to straighten out IEncodedDocument mess
		if (structuredDocument instanceof IStructuredDocument)
			transformInstance(model.getStructuredDocument(), (IStructuredDocument) structuredDocument);
		else
			model.getStructuredDocument().set(structuredDocument.get());

		// original hack
		// model.setStructuredDocument((IStructuredDocument)
		// structuredDocument);
		// ((IStructuredDocument) structuredDocument).fireNewDocument(this);
		documentLoaderInstance = null;
		// technicq of future
		// model.setStructuredDocument((IStructuredDocument)
		// structuredDocument);
		// documentLoaderInstance = null;
	}

	public void load(InputStream inputStream, IStructuredModel model, EncodingRule encodingRule) throws UnsupportedEncodingException, java.io.IOException {
		// note we don't open the stream, so we don't close it
		IEncodedDocument structuredDocument = model.getStructuredDocument();
		if (inputStream == null) {
			structuredDocument = getDocumentLoader().createNewStructuredDocument();
		}
		else {
			// assume's model has been initialized already with base location
			structuredDocument = getDocumentLoader().createNewStructuredDocument(model.getBaseLocation(), inputStream, encodingRule);
			// TODO: model's not designed for this!
			// we want to move to this "set" method, but the 'fire' was needed
			// as
			// a work around for strucutredModel not handling 'set' right, but
			// that 'fireNewDocument' method was causing unbalance
			// "aboutToChange" and "changed"
			// events.
			// model.setStructuredDocument((IStructuredDocument)
			// structuredDocument);
			// ((IStructuredDocument)
			// structuredDocument).fireNewDocument(this);
			model.getStructuredDocument().set(structuredDocument.get());

		}
		documentLoaderInstance = null;

	}

	/**
	 * deprecated -- use EncodingRule form
	 */
	synchronized public void load(InputStream inputStream, IStructuredModel model, String encodingName, String lineDelimiter) throws UnsupportedEncodingException, java.io.IOException {
		// note we don't open the stream, so we don't close it
		// TEMP work around to maintain previous function,
		// until everyone can change to EncodingRule.FORCE_DEFAULT
		if (encodingName != null && encodingName.trim().length() == 0) {
			// redirect to new method
			load(inputStream, model, EncodingRule.FORCE_DEFAULT);
		}
		else {
			load(inputStream, model, EncodingRule.CONTENT_BASED);
		}
	}

	public void load(String filename, InputStream inputStream, IStructuredModel model, String junk, String dummy) throws UnsupportedEncodingException, java.io.IOException {

		long memoryUsed = 0;
		if (DEBUG) {
			memoryUsed = computeMem();
			System.out.println("measuring heap memory for " + filename); //$NON-NLS-1$
			// System.out.println("heap memory used before load: " +
			// memoryUsed);
		}

		// during an initial load, we expect the olddocument to be empty
		// during re-load, however, it would be full.
		IEncodedDocument newstructuredDocument = null;
		IEncodedDocument oldStructuredDocument = model.getStructuredDocument();

		// get new document
		if (inputStream == null) {
			newstructuredDocument = getDocumentLoader().createNewStructuredDocument();
		}
		else {
			newstructuredDocument = getDocumentLoader().createNewStructuredDocument(filename, inputStream);
		}
		if (DEBUG) {
			long memoryAtEnd = computeMem();
			// System.out.println("heap memory used after loading new
			// document: " + memoryAtEnd);
			System.out.println("    heap memory implied used by document: " + (memoryAtEnd - memoryUsed)); //$NON-NLS-1$
		}


		// TODO: need to straighten out IEncodedDocument mess
		if (newstructuredDocument instanceof IStructuredDocument) {
			transformInstance((IStructuredDocument) oldStructuredDocument, (IStructuredDocument) newstructuredDocument);
		}
		else {
			// we don't really expect this case, just included for safety
			oldStructuredDocument.set(newstructuredDocument.get());
		}
		// original hack
		// model.setStructuredDocument((IStructuredDocument)
		// structuredDocument);
		// ((IStructuredDocument) structuredDocument).fireNewDocument(this);
		documentLoaderInstance = null;
		// technicq of future
		// model.setStructuredDocument((IStructuredDocument)
		// structuredDocument);
		// documentLoaderInstance = null;
		if (DEBUG) {
			long memoryAtEnd = computeMem();
			// System.out.println("heap memory used after setting to model: "
			// + memoryAtEnd);
			System.out.println("    heap memory implied used by document and model: " + (memoryAtEnd - memoryUsed)); //$NON-NLS-1$
		}

	}

	/**
	 * required by interface, being declared here abstractly just as another
	 * reminder.
	 */
	abstract public IStructuredModel newModel();

	/**
	 * There's nothing to do here in abstract class for initializing adapters.
	 * Subclasses can and should override this method and provide proper
	 * intialization (For example, to get DOM document and 'getAdapter' on it,
	 * so that the first node/notifier has the adapter on it.)
	 */
	protected void preLoadAdapt(IStructuredModel structuredModel) {


	}

	/**
	 * Normally, here in the abstact class, there's nothing to do, but we will
	 * reset text, since this MIGHT end up being called to recover from error
	 * conditions (e.g. IStructuredDocument exceptions) And, can be called by
	 * subclasses.
	 */
	public IStructuredModel reinitialize(IStructuredModel model) {
		// Note: the "minimumization" routines
		// of 'replaceText' allow many old nodes to pass through, when
		// really its assumed they are created anew.
		// so we need to use 'setText' (I think "setText' ends up
		// throwing a 'newModel' event though, that may have some
		// implications.
		model.getStructuredDocument().setText(this, model.getStructuredDocument().get());
		return model;
	}

	/**
	 * This method gets a fresh copy of the data, and repopulates the models
	 * ... by a call to setText on the structuredDocument. This method is
	 * needed in some cases where clients are sharing a model and then changes
	 * canceled. Say for example, one editor and several "displays" are
	 * sharing a model, if the editor is closed without saving changes, then
	 * the displays still need a model, but they should revert to the original
	 * unsaved version.
	 */
	synchronized public void reload(InputStream inputStream, IStructuredModel structuredModel) {
		documentLoaderInstance = null;
		try {
			// temp solution ... we should be able to do better (more
			// efficient) in future.
			// Adapters will (probably) need to be sensitive to the fact that
			// the document instance changed
			// (by being life cycle listeners)
			load(inputStream, structuredModel, EncodingRule.CONTENT_BASED);

			// // Note: we apparently read the data (and encoding) correctly
			// // before, we just need to make sure we followed the same rule
			// as
			// // before.
			// EncodingMemento previousMemento =
			// structuredModel.getStructuredDocument().getEncodingMemento();
			// EncodingRule previousRule = previousMemento.getEncodingRule();
			// //IFile file = ResourceUtil.getFileFor(structuredModel);
			// // Note: there's opportunity here for some odd behavior, if the
			// // settings have changed from the first load to the reload.
			// But,
			// // hopefully,
			// // will result in the intended behavior.
			// Reader allTextReader =
			// getDocumentLoader().readInputStream(inputStream, previousRule);
			//
			// // TODO: avoid use of String instance
			// getDocumentLoader().reload(structuredModel.getStructuredDocument(),
			// allTextReader);
			// // and now "reset" encoding memento to keep it current with the
			// // one
			// // that was just determined.
			// structuredModel.getStructuredDocument().setEncodingMemento(getDocumentLoader().getEncodingMemento());
			// structuredModel.setDirtyState(false);
			// StructuredTextUndoManager undoMgr =
			// structuredModel.getUndoManager();
			// if (undoMgr != null) {
			// undoMgr.reset();
			// }
		}
		catch (UnsupportedEncodingException e) {
			// couldn't happen. The program has apparently
			// read the model once, and there'd be no reason the encoding
			// could not be used again.
			Logger.logException("Warning:  XMLLoader::reload.  This exception really should not have happened!! But will attemp to continue after dumping stack trace", e); //$NON-NLS-1$
			throw new Error("Program Error", e); //$NON-NLS-1$
		}
		catch (IOException e) {
			// couldn't happen. The program has apparently
			// read the model once, and there'd be no (common) reason it
			// couldn't be loaded again.
			Logger.logException("Warning:  XMLLoader::reload.  This exception really should not have happened!! But will attemp to continue after dumping stack trace", e); //$NON-NLS-1$
			throw new Error("Program Error", e); //$NON-NLS-1$
		}
	}

	/**
	 * this work is done better elsewhere, but done here for this version to
	 * reduce changes. especially since the need for it should go away once we
	 * no longer need to re-use old document instance.
	 */
	private void transformInstance(IStructuredDocument oldInstance, IStructuredDocument newInstance) {
		/**
		 * https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=4920
		 * 
		 * JSP taglib support broken, correct by duplicating extended setup
		 * information (BlockTagParser extension,
		 * StructuredDocumentRegionParser extensions)
		 */
		RegionParser oldParser = oldInstance.getParser();
		RegionParser newParser = newInstance.getParser().newInstance();
		// Register all of the old StructuredDocumentRegionHandlers on the new
		// parser
		if (oldParser instanceof StructuredDocumentRegionParserExtension && newParser instanceof StructuredDocumentRegionParserExtension) {
			List oldHandlers = ((StructuredDocumentRegionParserExtension) oldParser).getStructuredDocumentRegionHandlers();
			for (int i = 0; i < oldHandlers.size(); i++) {
				StructuredDocumentRegionHandler handler = ((StructuredDocumentRegionHandler) oldHandlers.get(i));
				if (handler instanceof StructuredDocumentRegionHandlerExtension) {
					/**
					 * Skip the transferring here, the handler will do this
					 * after everything else but the source is transferred.
					 */
				}
				else {
					((StructuredDocumentRegionParser) oldParser).removeStructuredDocumentRegionHandler(handler);
					((StructuredDocumentRegionParser) newParser).addStructuredDocumentRegionHandler(handler);
					handler.resetNodes();
				}
			}
		}
		// Add any global BlockMarkers to the new parser
		if (oldParser instanceof BlockTagParser && newParser instanceof BlockTagParser) {
			List oldBlockMarkers = ((BlockTagParser) oldParser).getBlockMarkers();
			for (int i = 0; i < oldBlockMarkers.size(); i++) {
				BlockMarker blockMarker = ((BlockMarker) oldBlockMarkers.get(i));
				if (blockMarker.isGlobal()) {
					((BlockTagParser) newParser).addBlockMarker(blockMarker);
				}
			}
		}

		((BasicStructuredDocument) oldInstance).setParser(newParser);

		((BasicStructuredDocument) oldInstance).setReParser(newInstance.getReParser().newInstance());

		if (newInstance.getDocumentPartitioner() instanceof StructuredTextPartitioner) {
			StructuredTextPartitioner partitioner = null;
			if (oldInstance instanceof IDocumentExtension3 && newInstance instanceof IDocumentExtension3) {
				partitioner = ((StructuredTextPartitioner) ((IDocumentExtension3) newInstance).getDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING));
				if (partitioner != null) {
					partitioner = (StructuredTextPartitioner) partitioner.newInstance();
				}
				((IDocumentExtension3) oldInstance).setDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, partitioner);
			}
			if (partitioner == null) {
				partitioner = (StructuredTextPartitioner) ((StructuredTextPartitioner) newInstance.getDocumentPartitioner()).newInstance();
				oldInstance.setDocumentPartitioner(partitioner);
			}
			if (partitioner != null) {
				partitioner.connect(oldInstance);
			}
		}

		String existingLineDelimiter = null;
		try {
			existingLineDelimiter = newInstance.getLineDelimiter(0);
		}
		catch (BadLocationException e) {
			// if empty file, assume platform default
			// TODO: should be using user set preference, per content type?
			existingLineDelimiter = System.getProperty("line.separator"); //$NON-NLS-1$
		}

		oldInstance.setLineDelimiter(existingLineDelimiter); //$NON-NLS-1$);
		if (newInstance.getEncodingMemento() != null) {
			oldInstance.setEncodingMemento((EncodingMemento) newInstance.getEncodingMemento().clone());
		}

		/**
		 * https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=4920
		 * 
		 * JSP taglib support broken, correct by duplicating extended setup
		 * information (BlockTagParser extension,
		 * StructuredDocumentRegionParser extensions)
		 */
		if (oldParser instanceof StructuredDocumentRegionParserExtension && newParser instanceof StructuredDocumentRegionParserExtension) {
			List oldHandlers = ((StructuredDocumentRegionParserExtension) oldParser).getStructuredDocumentRegionHandlers();
			for (int i = 0; i < oldHandlers.size(); i++) {
				StructuredDocumentRegionHandler handler = ((StructuredDocumentRegionHandler) oldHandlers.get(i));
				if (handler instanceof StructuredDocumentRegionHandlerExtension) {
					StructuredDocumentRegionHandlerExtension handlerExtension = (StructuredDocumentRegionHandlerExtension) handler;
					handlerExtension.setStructuredDocument(oldInstance);
				}
			}
		}
		String holdString = newInstance.get();
		newInstance = null;
		oldInstance.set(holdString);
	}
}

Back to the top