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: 5dd9fe5c49a4f883a136d79bfee589f760706906 (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
/*******************************************************************************
 * 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;

import java.util.Hashtable;

import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.dtd.core.internal.parser.DTDRegionTypes;
import org.eclipse.wst.dtd.core.internal.text.RegionIterator;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;


// base class for an Element's contentmodel
public class Attribute extends DTDNode {

	public static final String CDATA = DTDCoreMessages._UI_CHARACTER_DATA_DESC; //$NON-NLS-1$
	public static final String ENTITIES = DTDCoreMessages._UI_ENTITY_NAMES_DESC; //$NON-NLS-1$
	public static final String ENTITY = DTDCoreMessages._UI_ENTITY_NAME_DESC; //$NON-NLS-1$
	public static final String ENUMERATED_NAME = DTDCoreMessages._UI_ENUM_NAME_TOKENS_DESC; //$NON-NLS-1$
	public static final String ENUMERATED_NOTATION = DTDCoreMessages._UI_ENUM_NOTATION_DESC; //$NON-NLS-1$
	public static final String FIXED = "#FIXED"; //$NON-NLS-1$
	public static final String ID = DTDCoreMessages._UI_IDENTIFIER_DESC; //$NON-NLS-1$
	public static final String IDREF = DTDCoreMessages._UI_ID_REFERENCE_DESC; //$NON-NLS-1$
	public static final String IDREFS = DTDCoreMessages._UI_ID_REFERENCES_DESC; //$NON-NLS-1$

	public static final String IMPLIED = "#IMPLIED"; //$NON-NLS-1$
	public static final String NMTOKEN = DTDCoreMessages._UI_NAME_TOKEN_DESC; //$NON-NLS-1$
	public static final String NMTOKENS = DTDCoreMessages._UI_NAME_TOKENS_DESC; //$NON-NLS-1$
	public static final String REQUIRED = "#REQUIRED"; //$NON-NLS-1$

	protected static Hashtable typeHash = new Hashtable();

	public static final String[] types = {CDATA, ID, IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN, NMTOKENS, ENUMERATED_NAME, ENUMERATED_NOTATION};

	{
		typeHash.put(DTDRegionTypes.CDATA_KEYWORD, CDATA);
		typeHash.put(DTDRegionTypes.ID_KEYWORD, ID);
		typeHash.put(DTDRegionTypes.IDREF_KEYWORD, IDREF);
		typeHash.put(DTDRegionTypes.IDREFS_KEYWORD, IDREFS);
		typeHash.put(DTDRegionTypes.ENTITY_KEYWORD, ENTITY);
		typeHash.put(DTDRegionTypes.ENTITIES_KEYWORD, ENTITIES);
		typeHash.put(DTDRegionTypes.NMTOKEN_KEYWORD, NMTOKEN);
		typeHash.put(DTDRegionTypes.NMTOKENS_KEYWORD, NMTOKENS);
		typeHash.put(DTDRegionTypes.NOTATION_KEYWORD, ENUMERATED_NOTATION);
		// this one's a special case since there is no keyword for
		// enumerated name tokens
		typeHash.put("()", ENUMERATED_NAME); //$NON-NLS-1$

		// now put the reverse in place. This gives us a 2 way lookup
		// for when we want to retrieve the value and when we want to set it
		typeHash.put(CDATA, "CDATA"); //$NON-NLS-1$
		typeHash.put(ID, "ID"); //$NON-NLS-1$
		typeHash.put(IDREF, "IDREF"); //$NON-NLS-1$
		typeHash.put(IDREFS, "IDREFS"); //$NON-NLS-1$
		typeHash.put(ENTITY, "ENTITY"); //$NON-NLS-1$
		typeHash.put(ENTITIES, "ENTITIES"); //$NON-NLS-1$
		typeHash.put(NMTOKEN, "NMTOKEN"); //$NON-NLS-1$
		typeHash.put(NMTOKENS, "NMTOKENS"); //$NON-NLS-1$
		typeHash.put(ENUMERATED_NAME, ""); //$NON-NLS-1$
		typeHash.put(ENUMERATED_NOTATION, "NOTATION"); //$NON-NLS-1$
	}

	private AttributeEnumList enumList = null;

	// public static final String IMPLIED = "IMPLIED";

	public Attribute(DTDFile file, IStructuredDocumentRegion flatNode) {
		super(file, flatNode);
	}

	public String getDefaultKind() {
		ITextRegion defaultKindRegion = getDefaultKindRegion();
		if (defaultKindRegion != null) {
			return getStructuredDTDDocumentRegion().getText(defaultKindRegion);
		}

		return ""; //$NON-NLS-1$
	}

	public ITextRegion getDefaultKindRegion() {
		RegionIterator iter = iterator();
		while (iter.hasNext()) {
			ITextRegion region = iter.next();
			if (region.getType() == DTDRegionTypes.IMPLIED_KEYWORD || region.getType() == DTDRegionTypes.REQUIRED_KEYWORD || region.getType() == DTDRegionTypes.FIXED_KEYWORD) {
				return region;
			}
		}
		return null;
	}

	public String getDefaultValue() {
		ITextRegion defaultValue = getNextQuotedLiteral(iterator());
		if (defaultValue != null) {
			return getValueFromQuotedRegion(defaultValue);
		}
		return ""; //$NON-NLS-1$
	}

	public AttributeEnumList getEnumList() {
		return enumList;
	}

	public Image getImage() {
		return DTDCorePlugin.getInstance().getImage(DTDResource.ATTRIBUTEICON);
	}

	public ITextRegion getNameRegion() {
		return getNextRegion(iterator(), DTDRegionTypes.ATTRIBUTE_NAME);
	}

	public ITextRegion getNextQuotedLiteral(RegionIterator iter) {
		while (iter.hasNext()) {
			ITextRegion region = iter.next();
			if (region.getType().equals(DTDRegionTypes.SINGLEQUOTED_LITERAL) || region.getType().equals(DTDRegionTypes.DOUBLEQUOTED_LITERAL)) {
				return region;
			}
		}
		return null;
	}

	protected int getOffsetAfterType() {
		ITextRegion typeRegion = getTypeRegion();

		String type = getType();
		boolean isEnumeration = type.equals(ENUMERATED_NAME) || type.equals(ENUMERATED_NOTATION);
		if (isEnumeration) {
			// now check if maybe this is an enumeration
			if (getEnumList() != null) {
				return getEnumList().getEndOffset();
			}
		}
		if (typeRegion != null) {
			return getStructuredDTDDocumentRegion().getEndOffset(typeRegion);
		}
		else {
			ITextRegion nameRegion = getNameRegion();
			return getStructuredDTDDocumentRegion().getEndOffset(nameRegion);
			// // create one
			// typeRegion =
			// findOrCreateTypeRegion((String)typeHash.get(CDATA));
		}
	}

	public String getType() {
		ITextRegion region = getTypeRegion();
		if (region != null) {
			String type = (String) typeHash.get(region.getType());
			if (type == null) {
				// just return the text of the type region since this may be
				// an entity representing the type;
				return getStructuredDTDDocumentRegion().getText(region);
			}
			return type;
		}
		else if (getEnumList() != null) {
			// enumerated name tokens don't have a type keyword. just
			// the existence of the left paren is enough
			return (String) typeHash.get("()"); //$NON-NLS-1$
		}

		return ""; //$NON-NLS-1$
	}

	public ITextRegion getTypeRegion() {
		RegionIterator iter = iterator();

		while (iter.hasNext()) {
			ITextRegion region = iter.next();
			if (region.getType() == DTDRegionTypes.CDATA_KEYWORD || region.getType() == DTDRegionTypes.ID_KEYWORD || region.getType() == DTDRegionTypes.IDREF_KEYWORD || region.getType() == DTDRegionTypes.IDREFS_KEYWORD || region.getType() == DTDRegionTypes.ENTITY_KEYWORD || region.getType() == DTDRegionTypes.ENTITIES_KEYWORD || region.getType() == DTDRegionTypes.NMTOKEN_KEYWORD || region.getType() == DTDRegionTypes.NMTOKENS_KEYWORD || region.getType() == DTDRegionTypes.NOTATION_KEYWORD || region.getType() == DTDRegionTypes.PARM_ENTITY_TYPE) {
				return region;
			}
		}
		return null;
	}

	public String getValueFromQuotedRegion(ITextRegion region) {
		String type = region.getType();
		if (type.equals(DTDRegionTypes.SINGLEQUOTED_LITERAL) || type.equals(DTDRegionTypes.DOUBLEQUOTED_LITERAL)) {
			String text = getStructuredDTDDocumentRegion().getText(region);
			return text.substring(1, text.length() - 1);
		}
		return ""; //$NON-NLS-1$
	}

	public void resolveRegions() {
		removeChildNodes();
		RegionIterator iter = iterator();

		while (iter.hasNext()) {
			ITextRegion currentRegion = iter.next();
			if (currentRegion.getType().equals(DTDRegionTypes.LEFT_PAREN)) {
				enumList = new AttributeEnumList(getDTDFile(), getStructuredDTDDocumentRegion());
			}
			if (enumList != null) {
				enumList.addRegion(currentRegion);
				if (currentRegion.getType() == DTDRegionTypes.RIGHT_PAREN) {
					return;
				}
			}
		}

	}

	public void setDefaultKind(Object requestor, String kind) {

		ITextRegion defaultKindRegion = getDefaultKindRegion();
		String oldDefaultKind = defaultKindRegion == null ? "" : getStructuredDTDDocumentRegion().getText(defaultKindRegion); //$NON-NLS-1$
		if (!kind.equals(oldDefaultKind)) {
			String newText = kind;
			int startOffset = 0;
			int length = 0;
			if (defaultKindRegion != null) {
				startOffset = getStructuredDTDDocumentRegion().getStartOffset(defaultKindRegion);
				length = getStructuredDTDDocumentRegion().getEndOffset(defaultKindRegion) - startOffset;
			}
			else {
				startOffset = getOffsetAfterType();
				newText = " " + newText; //$NON-NLS-1$
			}

			ITextRegion defaultValue = getNextQuotedLiteral(iterator());

			if (kind.equals(Attribute.FIXED) || kind.equals("")) { //$NON-NLS-1$
				if (defaultValue == null) {
					// we are changing to fixed and wehave no quoted region.
					// put in an empty value
					newText += " \"\""; //$NON-NLS-1$
				}
			}
			else {
				if (defaultValue != null) {
					length = getStructuredDTDDocumentRegion().getEndOffset(defaultValue) - startOffset;
				}
			}
			replaceText(requestor, startOffset, length, newText);
			// do something if there is no "kind" region
		}
	}

	public void setDefaultKind(String kind) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ATTR_DEFAULT_KIND); //$NON-NLS-1$
		setDefaultKind(this, kind);
		endRecording(this);
	}

	public void setDefaultValue(Object requestor, String value, boolean fixed) {
		ITextRegion defaultValue = getNextQuotedLiteral(iterator());
		String quoteChar = value.indexOf("\"") == -1 ? "\"" : "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		int startOffset = 0;
		int endOffset = 0;
		String newText = ""; //$NON-NLS-1$

		String oldValue = getDefaultValue();
		boolean oldKindIsFixed = getDefaultKind().equals(Attribute.FIXED);
		if (oldValue.equals(value) && fixed == oldKindIsFixed) {
			// nothing to do
			return;
		}

		if (defaultValue != null) {
			startOffset = getStructuredDTDDocumentRegion().getStartOffset(defaultValue);
			endOffset = getStructuredDTDDocumentRegion().getEndOffset(defaultValue);
		}

		ITextRegion defaultKindRegion = getDefaultKindRegion();
		if (defaultKindRegion != null) {
			startOffset = getStructuredDTDDocumentRegion().getStartOffset(defaultKindRegion);
			endOffset = endOffset == 0 ? getStructuredDTDDocumentRegion().getEndOffset(defaultKindRegion) : endOffset;
		}
		else {
			if (startOffset == 0) {
				endOffset = startOffset = getOffsetAfterType();
				newText += " "; //$NON-NLS-1$
			}
			ITextRegion typeRegion = getTypeRegion();
			if (typeRegion == null && getEnumList() == null) {
				// tack on a default type
				// newText += "CDATA ";
			}
		}
		if (fixed) {
			newText += "#FIXED "; //$NON-NLS-1$
		}
		else {
			if (getDefaultKind().equals("") && value.equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
				// if not fixed and value is "" then reset the default kind to
				// implied
				newText += "#IMPLIED"; //$NON-NLS-1$
			}
		}

		if (!getType().equals("") && !value.equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
			newText += quoteChar + value + quoteChar;
		}
		replaceText(requestor, startOffset, endOffset - startOffset, newText);
	}

	public void setDefaultValue(String value, boolean fixed) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ATTR_DEFAULT_VAL); //$NON-NLS-1$
		setDefaultValue(this, value, fixed);
		endRecording(this);
	}

	public void setType(Object requestor, String type) {
		String oldType = getType();
		if (!type.equals(oldType)) {
			boolean wasEnumeration = oldType.equals(ENUMERATED_NAME) || oldType.equals(ENUMERATED_NOTATION);
			boolean isEnumeration = type.equals(ENUMERATED_NAME) || type.equals(ENUMERATED_NOTATION);
			String newText = ""; //$NON-NLS-1$
			int startOffset = 0;
			int endOffset = 0;

			if (wasEnumeration && !isEnumeration) {
				// get rid of the old enumlist
				AttributeEnumList enumList = getEnumList();
				if (enumList != null) {
					startOffset = enumList.getStartOffset();
					endOffset = enumList.getEndOffset();
				}
			}

			ITextRegion region = getTypeRegion();
			if (region != null) {
				startOffset = getStructuredDTDDocumentRegion().getStartOffset(region);
				if (endOffset == 0) {
					endOffset = getStructuredDTDDocumentRegion().getEndOffset(region);
				}
			}
			else if (startOffset == 0) {
				ITextRegion nameRegion = getNameRegion();
				newText += " "; //$NON-NLS-1$
				endOffset = startOffset = getStructuredDTDDocumentRegion().getEndOffset(nameRegion);
			}

			String newTypeWord = (String) typeHash.get(type);
			if (newTypeWord == null) {
				// then this must be a parm entity being used in the type
				// use the type text directly
				newTypeWord = type;
			}

			newText += newTypeWord;

			if (isEnumeration && !wasEnumeration) {
				// put in a new numlist
				boolean isSpaceNeeded = !type.equals(ENUMERATED_NAME);
				newText += isSpaceNeeded ? " " : ""; //$NON-NLS-1$ //$NON-NLS-2$
				newText += "()"; //$NON-NLS-1$
			}
			replaceText(requestor, startOffset, endOffset - startOffset, newText);
			if (newTypeWord.equals("") && !type.equals(ENUMERATED_NAME)) { //$NON-NLS-1$
				// the set the defaultkind to ""
				// setDefaultKind(requestor, "");
				setDefaultValue(requestor, "", false); //$NON-NLS-1$
			}

		}
	}

	public void setType(String type) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ATTR_TYPE); //$NON-NLS-1$
		setType(this, type);
		endRecording(this);
	}
}

Back to the top