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: 4e7ddcb52b54dc4c8d2c21deef9d82e234551669 (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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.document;



import org.eclipse.wst.html.core.internal.contentmodel.HTMLElementDeclaration;
import org.eclipse.wst.html.core.internal.contentmodel.HTMLPropertyDeclaration;
import org.eclipse.wst.html.core.internal.provisional.HTML40Namespace;
import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.xml.core.internal.contentmodel.CMContent;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMGroup;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList;
import org.eclipse.wst.xml.core.internal.document.CMNodeUtil;
import org.eclipse.wst.xml.core.internal.document.ModelParserAdapter;
import org.eclipse.wst.xml.core.internal.document.TagAdapter;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * HTMLDocumentImpl class
 */
public class HTMLModelParserAdapter implements ModelParserAdapter {
	/**
	 * note: I made public, temparily, so could be used by JSPLoader
	 */
	protected HTMLModelParserAdapter() {
		super();
	}

	private boolean shouldTerminateAt(CMElementDeclaration parent, CMElementDeclaration child) {
		if (!parent.supports(HTMLCMProperties.TERMINATORS))
			return false;
		java.util.Iterator i = (java.util.Iterator) parent.getProperty(HTMLCMProperties.TERMINATORS);
		if (i == null)
			return false;
		String nextName = child.getElementName();
		while (i.hasNext()) {
			// NOTE: CMElementDeclaration of child is not always HTMLCMElementDeclaration.
			// It might be one of based on DTD (for XHTML element).  So, comparison must
			// be performed ignoring case.
			// -- 3/20/2002
			String terminator = (String) i.next();
			if (terminator == null)
				continue;
			if (nextName.equalsIgnoreCase(terminator))
				return true;
		}
		return false;
	}

	public boolean isEndTagOmissible(Element element) {
		CMElementDeclaration dec = CMNodeUtil.getElementDeclaration(element);
		if (dec == null || !(dec instanceof HTMLPropertyDeclaration))
			return false;
		int type = ((HTMLPropertyDeclaration)dec ).getOmitType();
		return type == HTMLElementDeclaration.OMIT_BOTH || type == HTMLElementDeclaration.OMIT_END || type == HTMLElementDeclaration.OMIT_END_DEFAULT || type == HTMLElementDeclaration.OMIT_END_MUST;
	}

	/**
	 */
	public boolean canContain(Element element, Node child) {
		if (element == null || child == null)
			return false;
		IDOMElement impl = (IDOMElement) element;

		if (child.getNodeType() == Node.ELEMENT_NODE) {
			if (!impl.isGlobalTag())
				return true; // non HTML tag
			IDOMElement childElement = (IDOMElement) child;

			CMElementDeclaration myDec = CMNodeUtil.getElementDeclaration(element);
			if (myDec == null)
				return true;
			//if (!(myDec instanceof HTMLElementDeclaration)) return true;
			if (myDec.getContentType() == CMElementDeclaration.EMPTY)
				return false;

			if (!childElement.isGlobalTag())
				return true; // non HTML tag
			CMElementDeclaration childDec = CMNodeUtil.getElementDeclaration(childElement);
			if (childDec == null)
				return true;
			//if (!(childDec instanceof HTMLElementDeclaration)) return true;

			if (myDec instanceof HTMLElementDeclaration) {
				if (((Boolean) ((HTMLElementDeclaration) myDec).getProperty(HTMLCMProperties.IS_JSP)).booleanValue())
					return true;
			}
			if (shouldTerminateAt(myDec, childDec) && !isValidChild(myDec, childDec)) {
				return false;
			}

			String tagName = impl.getTagName();
			if (tagName == null)
				return true;
			String childName = childElement.getTagName();
			if (childName == null)
				return true;
			if (!impl.hasStartTag() && !impl.hasEndTag()) {
				// implicit element
				if (tagName.equalsIgnoreCase(childElement.getTagName()))
					return false;
				if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.HEAD)) {
					if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.META) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.TITLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.LINK) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.STYLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.BASE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.ISINDEX)) {
						return false;
					}
				}

				Node parent = element.getParentNode();
				if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
					IDOMElement parentElement = (IDOMElement) parent;
					if (!parentElement.hasStartTag() && !parentElement.hasEndTag()) {
						if (!canContain(parentElement, child))
							return false;
					}
				}
				return true;
			}

			// contexual termination for TABLE content tags
			boolean isTableContent = false;
			if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT)) {
				if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
					return true;
				isTableContent = true;
			}
			else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TR)) {
				if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
					return true;
				isTableContent = true;
			}
			else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TH)) {
				if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TR) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
					return true;
				isTableContent = true;
			}
			if (isTableContent) {
				// TABLE content tags should terminate non TABLE content tags,
				// if in TABLE
				for (Node parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) {
					if (parent.getNodeType() != Node.ELEMENT_NODE)
						break;
					IDOMElement parentElement = (IDOMElement) parent;
					String parentName = parentElement.getTagName();
					if (parentName == null)
						continue;
					if (parentName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
						return false;
				}
			}
			if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
				if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.NOEMBED))
					return false;
			}
		}
		else if (child.getNodeType() == Node.TEXT_NODE) {
			String tagName = impl.getTagName();
			if (tagName != null && tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
				IDOMText text = (IDOMText) child;
				if (!text.isElementContentWhitespace())
					return false;
			}
		}
		else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
			if (impl.isImplicitTag())
				return false;
		}

		return true;
	}

	/**
	 */
	public boolean canBeImplicitTag(Element element) {
		return false;
	}

	/**
	 */
	public boolean canBeImplicitTag(Element element, Node child) {
		return false;
	}

	/**
	 */
	public Element createCommentElement(Document document, String data, boolean isJSPTag) {
		if (document == null || data == null || data.length() == 0)
			return null;

		return createMetaElement(document, data, isJSPTag);
	}

	/**
	 * This routine create an implicit Element for given parent and child,
	 * such as HTML, BODY, HEAD, and TBODY for HTML document.
	 */
	public Element createImplicitElement(Document document, Node parent, Node child) {
		return null;
	}

	/**
	 */
	private Element createMetaElement(Document document, String data, boolean isJSPTag) {
		if (data == null || data.length() == 0)
			return null;

		TagScanner scanner = new TagScanner(data, 0, true); // one line
		String name = scanner.nextName();
		if (name == null || !name.equalsIgnoreCase(MetaData.METADATA))
			return null;

		String type = null;
		boolean isStartSpan = false;
		boolean isEndSpan = false;
		name = scanner.nextName();
		while (name != null) {
			String value = scanner.nextValue();
			if (name.equalsIgnoreCase(MetaData.TYPE)) {
				if (value == null)
					return null;
				if (value.equalsIgnoreCase(MetaData.DESIGNER_CONTROL)) {
					type = MetaData.DESIGNER_CONTROL;
				}
				else if (value.equalsIgnoreCase(MetaData.DYNAMIC_DATA)) {
					type = MetaData.DYNAMIC_DATA;
				}
				else if (value.equalsIgnoreCase(MetaData.AUTHOR_TIME_VISUAL)) {
					type = MetaData.AUTHOR_TIME_VISUAL;
				}
				else if (value.equalsIgnoreCase(MetaData.ANNOTATION)) {
					type = MetaData.ANNOTATION;
				}
				else {
					return null;
				}
			}
			else if (name.equalsIgnoreCase(MetaData.STARTSPAN)) {
				isStartSpan = true;
			}
			else if (name.equalsIgnoreCase(MetaData.ENDSPAN)) {
				if (!isStartSpan)
					isEndSpan = true;
			}
			name = scanner.nextName();
		}
		if (type == null)
			return null;
		if (!isStartSpan && !isEndSpan)
			return null;
		String metaData = null;
		int offset = scanner.getNextOffset(); // skip new line
		if (offset < data.length())
			metaData = data.substring(offset);
		if (metaData == null)
			metaData = new String();

		IDOMElement element = (IDOMElement) document.createElement(MetaData.PREFIX + type);

		MetaDataAdapter adapter = new MetaDataAdapter(type);
		if (isStartSpan) {
			if (metaData != null)
				adapter.setData(metaData);
		}
		else {
			if (metaData != null)
				adapter.setEndData(metaData);
		}
		element.addAdapter(adapter);
		adapter.setElement(element);
		element.setJSPTag(isJSPTag);

		return element;
	}

	/**
	 */
	public String getFindRootName(String tagName) {
		if (tagName == null)
			return null;
		// tag matching should not beyond TABLE tag except BODY tag
		if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.BODY))
			return null;
		return HTML40Namespace.ElementName.TABLE;
	}

	/**
	 */
	public boolean isAdapterForType(Object type) {
		return (type == ModelParserAdapter.class);
	}

	/**
	 */
	public boolean isEndTag(IDOMElement element) {
		TagAdapter adapter = (TagAdapter) element.getExistingAdapter(TagAdapter.class);
		if (adapter != null)
			return adapter.isEndTag();
		return element.isEndTag();
	}

	/**
	 */
	public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
		// do nothing on notifiy change
		// TODO: this means good candidate for regular platform adapter
	}

	private static boolean isValidChild(CMElementDeclaration parent, CMElementDeclaration child) {
		if (parent == null || child == null)
			return false;
		CMContent content = parent.getContent();
		if (content == null)
			return false;
		return isChild(content, child);
	}

	/**
	 */
	private static boolean isChild(CMContent content, CMElementDeclaration target) {
		switch (content.getNodeType()) {
			case CMNode.ELEMENT_DECLARATION :
				return isSameDeclaration((CMElementDeclaration) content, target);
			case CMNode.GROUP :
				CMNodeList children = ((CMGroup) content).getChildNodes();
				for (int i = 0; i < children.getLength(); i++) {
					CMNode child = children.item(i);
					switch (child.getNodeType()) {
						case CMNode.ELEMENT_DECLARATION :
							if (isSameDeclaration((CMElementDeclaration) child, target))
								return true;
							continue; // Go next child.
						case CMNode.GROUP :
							if (isChild((CMContent) child, target))
								return true;
							continue; // Go next child.
						default :
							continue; // Go next child.
					}
				}
		}
		return false;
	}

	/**
	 */
	private static boolean isSameDeclaration(CMElementDeclaration aDec, CMElementDeclaration otherDec) {
		return aDec.getElementName() == otherDec.getElementName();
	}

}

Back to the top