Skip to main content
summaryrefslogtreecommitdiffstats
blob: f0b1471a07b8919a5a9df5d9b392efd8945905b1 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 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.wst.html.core.internal.contentmodel;



import java.util.Iterator;

import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
import org.eclipse.wst.xml.core.internal.contentmodel.CMContent;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDataType;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;

/**
 * Base class for all Hed???? classes.
 */
abstract class HTMLElemDeclImpl extends CMContentImpl implements HTMLElementDeclaration, HTMLPropertyDeclaration {

	// DTD
	protected CMNamedNodeMapImpl attributes = null;
	protected String typeDefinitionName = ComplexTypeDefinitionFactory.CTYPE_EMPTY;
	/** Never access this field directly.  Instead, use getComplexTypeDefinition method. */
	private ComplexTypeDefinition typeDefinition = null;
	protected CMGroupImpl inclusion = null;
	protected CMGroupImpl exclusion = null;
	// advanced information
	protected CMNamedNodeMap prohibitedAncestors = null;
	protected int correctionType = CORRECT_NONE;
	protected int formatType = FORMAT_HTML;
	protected int layoutType = LAYOUT_NONE;
	protected int omitType = OMIT_NONE;
	protected boolean keepSpaces = false;
	protected boolean indentChild = false;
	protected ElementCollection elementCollection = null;
	protected AttributeCollection attributeCollection = null;
	protected final static CMNamedNodeMap EMPTY_MAP = new CMNamedNodeMap() {
		public int getLength() {
			return 0;
		}

		public CMNode getNamedItem(String name) {
			return null;
		}

		public CMNode item(int index) {
			return null;
		}

		public Iterator iterator() {
			return new Iterator() {
				public boolean hasNext() {
					return false;
				}

				public Object next() {
					return null;
				}

				public void remove() {
				}
			};
		}
	};

	/**
	 * HTMLElemDeclImpl constructor.
	 * In the HTML DTD, an element declaration has no specification
	 * for its occurrence.  Occurrence is specifed in content model, like
	 * <code>(LI)+</code>.  To avoid confusion (and complexity),
	 * occurrence of an element declaration is always 1 (it means, min = 1 and
	 * max = 1).  Instead, occurrence of CMGroup represents actual occurrence
	 * of the content.
	 * <br>
	 * @param name java.lang.String
	 */
	public HTMLElemDeclImpl(String elementName, ElementCollection collection) {
		super(elementName, 1, 1);
		elementCollection = collection;
		attributeCollection = collection.getAttributeCollection();
	}

	/**
	 */
	protected abstract void createAttributeDeclarations();

	private ComplexTypeDefinition createComplexTypeDefinition() {
		if (typeDefinitionName.equals(ComplexTypeDefinitionFactory.CTYPE_CDATA) || typeDefinitionName.equals(ComplexTypeDefinitionFactory.CTYPE_EMPTY) || typeDefinitionName.equals(ComplexTypeDefinitionFactory.CTYPE_PCDATA))
			return null;

		ComplexTypeDefinitionFactory factory = ComplexTypeDefinitionFactory.getInstance();
		if (factory == null)
			return null; // fatal error.

		ComplexTypeDefinition def = factory.createTypeDefinition(typeDefinitionName, elementCollection);
		return def;
	}

	/**
	 * Get an attribute declaration.
	 */
	public HTMLAttributeDeclaration getAttributeDeclaration(String attrName) {
		if (attributes == null) {
			createAttributeDeclarations();
			if (attributes == null)
				return null; // fail to create
		}

		CMNode cmnode = attributes.getNamedItem(attrName);
		if (cmnode == null) {
			return null;
		}
		else {
			return (HTMLAttributeDeclaration) cmnode; // already exists.
		}
	}

	/**
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public CMNamedNodeMap getAttributes() {
		if (attributes == null)
			createAttributeDeclarations(); // lazy eval.
		return attributes;
	}

	/**
	 * Get an instance of complex type definition.
	 */
	private ComplexTypeDefinition getComplexTypeDefinition() {
		if (typeDefinition == null)
			typeDefinition = createComplexTypeDefinition();
		return typeDefinition;
	}

	/**
	 * Content.<br>
	 * Element declarations which type is EMPTY or CDATA (maybe PCDATA)
	 * <strong>MUST</strong> override this method and always return null.
	 * This default implementation always tries to create a complex type definition
	 * instance and access to it.
	 * <br>
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public CMContent getContent() {
		ComplexTypeDefinition def = getComplexTypeDefinition(); // lazy eval.
		return (def != null) ? def.getContent() : null;
	}

	/**
	 * Content type.<br>
	 * Element declarations which type is EMPTY or CDATA (maybe PCDATA)
	 * <strong>MUST</strong> override this method and return an appropriate type.
	 * This default implementation always tries to create a complex type definition
	 * instance and access to it.
	 * <br>
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public int getContentType() {
		ComplexTypeDefinition def = getComplexTypeDefinition(); // lazy eval.
		return (def != null) ? def.getContentType() : CMElementDeclaration.CDATA;
	}

	/**
	 * @see HTMLElementDeclaration#getCorrectionType
	 */
	public int getCorrectionType() {
		return correctionType;
	}

	/**
	 * HTML element doesn't have any data type.  So, this method always
	 * returns <code>null</code>.<br>
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public CMDataType getDataType() {
		return null;
	}

	/**
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public String getElementName() {
		return getNodeName();
	}

	/**
	 * Exclusion.
	 * Almost elements don't have a exclusion.
	 * Only classes those have exclusion should override this method.
	 */
	public CMContent getExclusion() {
		return null;
	}

	/**
	 * Default format type is <code>FORMAT_HTML</code>.<br>
	 */
	public int getFormatType() {
		return formatType;
	}

	/**
	 * Inclusion.
	 * Almost elements don't have a inclusion.
	 * Only classes those have inclusion should override this method.
	 */
	public CMContent getInclusion() {
		return null;
	}

	/**
	 */
	public int getLayoutType() {
		return layoutType;
	}

	/**
	 * Line break hint is strongly related to layout type.
	 * Indeed, in the C++DOM, it is determined from layout type only.
	 * So, this implementation, as the default implementation for all declarations,
	 * also determines from layout type only.<br>
	 * @return int
	 */
	public int getLineBreakHint() {
		switch (getLayoutType()) {
			case HTMLElementDeclaration.LAYOUT_BLOCK :
				return HTMLElementDeclaration.BREAK_BEFORE_START_AND_AFTER_END;
			case HTMLElementDeclaration.LAYOUT_BREAK :
				return HTMLElementDeclaration.BREAK_AFTER_START;
			case HTMLElementDeclaration.LAYOUT_HIDDEN :
				return HTMLElementDeclaration.BREAK_BEFORE_START_AND_AFTER_END;
			default :
				return HTMLElementDeclaration.BREAK_NONE;
		}
	}

	/**
	 * No HTML element has local elements.  So, this method always
	 * returns an empty map.
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
	 */
	public CMNamedNodeMap getLocalElements() {
		return EMPTY_MAP;
	}

	/**
	 * @see org.eclipse.wst.xml.core.internal.contentmodel.CMNode
	 */
	public int getNodeType() {
		return CMNode.ELEMENT_DECLARATION;
	}

	/**
	 */
	public int getOmitType() {
		return omitType;
	}

	/**
	 */
	public CMNamedNodeMap getProhibitedAncestors() {
		return EMPTY_MAP;
	}

	/**
	 */
	public boolean supports(String propertyName) {
		if (propertyName.equals(HTMLCMProperties.SHOULD_IGNORE_CASE)) {
			return true;
		}
		else if (propertyName.equals(HTMLCMProperties.CONTENT_HINT)) {
			ComplexTypeDefinition def = getComplexTypeDefinition();
			return (def != null);
		}
		else {
			PropertyProvider pp = PropertyProviderFactory.getProvider(propertyName);
			if (pp == null)
				return false;
			return pp.supports(this);
		}

	}

	/**
	 */
	public Object getProperty(String propertyName) {
		if (propertyName.equals(HTMLCMProperties.SHOULD_IGNORE_CASE)) {
			return new Boolean(true);
		}
		else if (propertyName.equals(HTMLCMProperties.CONTENT_HINT)) {
			ComplexTypeDefinition def = getComplexTypeDefinition();
			return (def != null) ? def.getPrimaryCandidate() : null;
		}
		else {
			PropertyProvider pp = PropertyProviderFactory.getProvider(propertyName);
			if (pp == null)
				return null;
			return pp.get(this);
		}
	}

	/**
	 * Return element names which terminates this element.<br>
	 * @return java.util.Iterator
	 */
	protected Iterator getTerminators() {
		return null;
	}

	/**
	 * return true when the element is a JSP element.
	 */
	public boolean isJSP() {
		return false;
	}

	/**
	 * In some elements, such as APPLET, a source generator should indent child
	 * elements that their parents.  That is, a source generator should generate
	 * source  of APPLET and PARAMS like this:
	 * <PRE>
	 *   &lt;APPLET ...&gt;
	 *     &lt;PARAM ... &gt;
	 *     &lt;PARAM ... &gt;
	 *   &lt;/APPLET&gt;
	 * <PRE>
	 * @return boolean
	 */
	public boolean shouldIndentChildSource() {
		return indentChild;
	}

	/**
	 * Most of elements can compact spaces in their child text nodes.
	 * Some special elements should keep them in their source.
	 * @return boolean
	 */
	public boolean shouldKeepSpaces() {
		return keepSpaces;
	}

	/**
	 * @return boolean
	 */
	public boolean shouldTerminateAt(HTMLElementDeclaration nextElement) {
		Iterator i = getTerminators();
		if (i == null)
			return false;
		String nextName = nextElement.getElementName();
		while (i.hasNext()) {
			if (nextName.equals(i.next()))
				return true;
		}
		return false;
	}
}

Back to the top