Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b4ea3eb3fce8e71ec10f9102a8529a3fa44eaba2 (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
/*******************************************************************************
 * 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.dtd.core.internal.document;

import java.io.File;
import java.util.Iterator;
import java.util.Vector;

import org.eclipse.wst.dtd.core.document.DTDModel;
import org.eclipse.wst.dtd.core.internal.DTDFile;
import org.eclipse.wst.dtd.core.internal.DTDNode;
import org.eclipse.wst.dtd.core.internal.Entity;
import org.eclipse.wst.dtd.core.internal.NodeList;
import org.eclipse.wst.dtd.core.internal.event.IDTDFileListener;
import org.eclipse.wst.dtd.core.internal.event.NodesEvent;
import org.eclipse.wst.dtd.core.internal.util.DTDReferenceUpdater;
import org.eclipse.wst.dtd.core.internal.util.LabelValuePair;
import org.eclipse.wst.sse.core.IndexedRegion;
import org.eclipse.wst.sse.core.events.IStructuredDocumentListener;
import org.eclipse.wst.sse.core.events.NewDocumentEvent;
import org.eclipse.wst.sse.core.events.NoChangeEvent;
import org.eclipse.wst.sse.core.events.RegionChangedEvent;
import org.eclipse.wst.sse.core.events.RegionsReplacedEvent;
import org.eclipse.wst.sse.core.events.StructuredDocumentEvent;
import org.eclipse.wst.sse.core.events.StructuredDocumentRegionsReplacedEvent;
import org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel;
import org.eclipse.wst.sse.core.text.IStructuredDocument;
import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion;


public final class DTDModelImpl extends AbstractStructuredModel implements IStructuredDocumentListener, DTDModel {

	public static boolean deleteFile(String fileName) {
		boolean result = false;

		// create the temp File object
		File file = new File(fileName);
		if (file.exists())
			result = file.delete();
		return result;
	}

	private DTDFile document;

	// private List errorMessages = new ArrayList();

	// entity reference names found in the conditional IGNORE sections
	private Vector ignoredEntityRefs;

	private boolean refreshRequired = false;

	protected DTDReferenceUpdater refUpdater = new DTDReferenceUpdater();

	public DTDModelImpl() {
		super();
		document = new DTDFile(this);
		document.addDTDFileListener(new IDTDFileListener() {

			public void nodeChanged(DTDNode node) {
				if (node instanceof Entity) {
					Entity entity = (Entity) node;
					if (entity.isParameterEntity() && entity.isExternalEntity()) {
						// just say they have changed for now
						setReferencedModelsChanged();
					}
				}
			}

			public void nodesAdded(NodesEvent event) {
				checkIfExternalReferencesChanged(event);
			}

			public void nodesRemoved(NodesEvent event) {
				checkIfExternalReferencesChanged(event);
			}
		});
	}

	public void beginRecording(Object requester, String label) {
		super.beginRecording(requester, label);
		// clear reference updater cache
		getReferenceUpdater().clearCache();
	}

	private void checkIfExternalReferencesChanged(NodesEvent event) {
		Iterator iter = event.getNodes().iterator();
		while (iter.hasNext()) {
			DTDNode node = (DTDNode) iter.next();
			if (node instanceof Entity) {
				Entity entity = (Entity) node;
				if (entity.isParameterEntity() && entity.isExternalEntity()) {
					// just say they have changed for now
					setReferencedModelsChanged();
				}
			}
		}
	}

	//
	// The following function helps determine the list of things that
	// can be used in a parameter entity reference content
	// Optional parameter is to allow the currently used DTDEntity to
	// be included in the combobox.
	//
	public LabelValuePair[] createParmEntityContentItems(Entity entity) {
		NodeList entities = getDTDFile().getEntities();

		Vector items = new Vector();

		if (entity != null) {
			String name = "%" + entity.getName() + ";"; //$NON-NLS-1$ //$NON-NLS-2$
			items.addElement(new LabelValuePair(name, name));
		}

		for (Iterator i = entities.getNodes().iterator(); i.hasNext();) {
			Entity entityAt = (Entity) i.next();
			if (entityAt.isParameterEntity() && entityAt.isExternalEntity()) {
				String name = "%" + entityAt.getName() + ";"; //$NON-NLS-1$ //$NON-NLS-2$
				items.addElement(new LabelValuePair(name, name));
			}
		}
		LabelValuePair[] comboArray = new LabelValuePair[items.size()];
		items.copyInto(comboArray);
		return comboArray;
	}

	public void endRecording(Object requester) {
		super.endRecording(requester);
		// clear reference updater cache
		getReferenceUpdater().clearCache();
	}


	public DTDFile getDTDFile() {
		return document;
	}

	// Returns entity reference names that are in
	// the conditional IGNORE sections.
	public Vector getIgnoredEntityRefs() {
		if (ignoredEntityRefs == null)
			ignoredEntityRefs = new Vector();
		return ignoredEntityRefs;
	}

	public IndexedRegion getIndexedRegion(int offset) {
		if (this.document == null)
			return null;
		// System.out.println("getNode at " + offset + " returning = " +
		// this.document.getNodeAt(offset));

		return this.document.getNodeAt(offset);
	}

	public DTDReferenceUpdater getReferenceUpdater() {
		return refUpdater;
	}

	public boolean isReferencedModelsChanged() {
		return refreshRequired;
	}

	public boolean isRefreshRequired() {
		return refreshRequired;
	}

	public void newModel(NewDocumentEvent flatModelEvent) {
		document.newModel(flatModelEvent);
		// System.out.println("\nnewmodel");
		outputStructuredDocument(flatModelEvent);
	}

	public void noChange(NoChangeEvent flatModelEvent) {
		// System.out.println("\nnochange");
		outputStructuredDocument(flatModelEvent);

	}

	public void nodesReplaced(StructuredDocumentRegionsReplacedEvent flatModelEvent) {
		// System.out.println("\nnodesreplaced");
		document.nodesReplaced(flatModelEvent);
		outputStructuredDocument(flatModelEvent);

	}

	public void outputStructuredDocument(StructuredDocumentEvent flatModelEvent) {
		// System.out.println("structuredDocument source = '" +
		// flatModelEvent.getStructuredDocument().getText() + "'");
		// System.out.println("new String = '" +
		// flatModelEvent.getOriginalChanges() +"'");
		// System.out.println("deleted String = '" +
		// flatModelEvent.getDeletedText() +"'");
		// Enumeration e =
		// flatModelEvent.getStructuredDocument().getNodes().elements();
		// int i = 0;
		// for (; e.hasMoreElements(); i++)
		// {
		// BasicStructuredDocumentRegion node =
		// (BasicStructuredDocumentRegion) e.nextElement();
		// outputStructuredDocumentRegion(node);
		// System.out.println(" " + i +". " + node.hashCode() + " '"
		// +node.getText() + "'");
		// }
	}

	public void outputStructuredDocumentRegion(IStructuredDocumentRegion flatNode) {
		// int size = flatNode.getNumberOfRegions();
		// for (int i = 0; i < size; i++)
		// {
		// Region region = (Region) flatNode.getRegions().get(i);
		// System.out.println(i + ". " + region.getType());

		// } // end of for ()

	}

	public void regionChanged(RegionChangedEvent flatModelEvent) {
		// System.out.println("\nregion changed");
		document.regionChanged(flatModelEvent);
		// System.out.println("= " +
		// flatModelEvent.getStructuredDocumentRegion().getText());
		// System.out.println("region changed " +
		// flatModelEvent.getRegion().hashCode() + " = " +
		// flatModelEvent.getRegion());

		outputStructuredDocument(flatModelEvent);
	}

	public void regionsReplaced(RegionsReplacedEvent flatModelEvent) {
		// System.out.println("\nregion replaced");
		document.regionsReplaced(flatModelEvent);
		outputStructuredDocument(flatModelEvent);
	}

	public void setReferencedModelsChanged() {
		refreshRequired = true;
	}

	public void setRefreshRequired(boolean value) {
		refreshRequired = value;
	}


	/**
	 * @param newStructuredDocument
	 *            org.eclipse.wst.sse.core.text.IStructuredDocument
	 */
	public void setStructuredDocument(IStructuredDocument newStructuredDocument) {
		IStructuredDocument oldStructuredDocument = super.getStructuredDocument();
		if (newStructuredDocument == oldStructuredDocument)
			return; // noting to do

		if (oldStructuredDocument != null)
			oldStructuredDocument.removeDocumentChangingListener(this);
		super.setStructuredDocument(newStructuredDocument);
		if (newStructuredDocument.getLength() > 0) {
			newModel(new NewDocumentEvent(newStructuredDocument, this));
		}
		if (newStructuredDocument != null)
			newStructuredDocument.addDocumentChangingListener(this);
	}
}

Back to the top