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: 963391616c8cc93d69cf691524ced475fa65ad4a (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
/*******************************************************************************
 * 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.validate;


import java.util.Iterator;

import org.eclipse.wst.html.core.internal.modelquery.HMQUtil;
import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
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.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.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

final class CMUtil {

	/**
	 * Never instantiate!
	 */
	private CMUtil() {
		super();
	}

	/**
	 * You cannot always retrieve HTMLElementDeclaration via an Element instance.
	 * Because, it occasionally a JSP custom tag. -- 9/7/2001
	 */
	public static CMElementDeclaration getDeclaration(Element target) {
		Document doc = target.getOwnerDocument();
		ModelQuery query = ModelQueryUtil.getModelQuery(doc);
		return query.getCMElementDeclaration(target);
	}

	/**
	 */
	public static boolean isCaseSensitive(CMElementDeclaration decl) {
		if (decl == null || (!decl.supports(HTMLCMProperties.SHOULD_IGNORE_CASE)))
			return false;
		return !((Boolean) decl.getProperty(HTMLCMProperties.SHOULD_IGNORE_CASE)).booleanValue();
	}

	/**
	 */
	private static boolean isChild(CMContent content, CMElementDeclaration target) {
		switch (content.getNodeType()) {
			case CMNode.ELEMENT_DECLARATION :
				if (isWholeTagOmissible((CMElementDeclaration) content))
					if (isChild(((CMElementDeclaration) content).getContent(), target))
						return true;
				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 (isWholeTagOmissible((CMElementDeclaration) child))
								if (isChild(((CMElementDeclaration) child).getContent(), target))
									return true;
							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;
	}

	/**
	 */
	public static boolean isEndTagOmissible(CMElementDeclaration decl) {
		if (!(decl.supports(HTMLCMProperties.OMIT_TYPE)))
			return false;
		String omitType = (String) decl.getProperty(HTMLCMProperties.OMIT_TYPE);
		return !omitType.equals(HTMLCMProperties.Values.OMIT_NONE);
	}

	/**
	 */
	public static boolean isWholeTagOmissible(CMElementDeclaration decl) {
		if (!(decl.supports(HTMLCMProperties.OMIT_TYPE)))
			return false;
		String omitType = (String) decl.getProperty(HTMLCMProperties.OMIT_TYPE);
		return omitType.equals(HTMLCMProperties.Values.OMIT_BOTH);
	}
	
	/**
	 */
	public static boolean isJSP(CMElementDeclaration decl) {
		if (!decl.supports(HTMLCMProperties.IS_JSP))
			return false;
		return ((Boolean) decl.getProperty(HTMLCMProperties.IS_JSP)).booleanValue();
	}

	/**
	 */
	public static boolean isXHTML(CMElementDeclaration decl) {
		if (!decl.supports(HTMLCMProperties.IS_XHTML))
			return false;
		return ((Boolean) decl.getProperty(HTMLCMProperties.IS_XHTML)).booleanValue();
	}

	/**
	 * The method to distinguish HTML and XHTML from other mark up.
	 * This method returns true if the target is,
	 * (1) not JSP,
	 * (2) not SSI.
	 */
	public static boolean isHTML(CMElementDeclaration decl) {
		return (!isJSP(decl)) && (!isSSI(decl));
	}

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

	/**
	 */
	public static boolean isSSI(CMElementDeclaration edec) {
		if (edec == null)
			return false;
		if (!edec.supports(HTMLCMProperties.IS_SSI))
			return false;
		return ((Boolean) edec.getProperty(HTMLCMProperties.IS_SSI)).booleanValue();
	}

	/**
	 * Call this method only when the parent content type is one of
	 * the following: ANY, ELEMENT, or MIXED.
	 */
	public static boolean isValidChild(CMElementDeclaration parent, CMElementDeclaration child) {
		if (parent == null || child == null)
			return false;
		if (isHTML(parent) && (!isHTML(child)))
			return true;
		CMContent content = parent.getContent();
		if (content == null)
			return false;
		return isChild(content, child);
	}

	public static boolean isForeign(Element target) {
		if (!(target instanceof IDOMElement))
			return true;
		IDOMElement element = (IDOMElement) target;
		return !element.isGlobalTag();
	}

	/**
	 * This method returns true if all of the following conditions are met:
	 * (1) value type is ENUM,
	 * (2) only one value is defined in the enumeration,
	 * (3) the value has same name to the attribute name.
	 */
	public static boolean isBooleanAttr(CMAttributeDeclaration adec) {
		CMDataType attrtype = adec.getAttrType();
		if (attrtype == null)
			return false;
		if (attrtype.getDataTypeName() != CMDataType.ENUM)
			return false;
		String[] values = attrtype.getEnumeratedValues();
		if (values.length != 1)
			return false;
		return values[0].equals(adec.getAttrName());
	}

	public static boolean isValidInclusion(CMElementDeclaration decl, Element parent) {
		Iterator iter = HMQUtil.getInclusions(parent).iterator();
		while (iter.hasNext()) {
			CMElementDeclaration inclusion = (CMElementDeclaration) iter.next();
			if (isSameDeclaration(decl, inclusion))
				return true;
		}
		return false;
	}
	
	/**
	 * The method to distinguish HTML and XHTML from other mark up.
	 * This method returns true if the target is,
	 * (1) not JSP,
	 * (2) not SSI.
	 */
	public static boolean isObsolete(CMNode decl) {
		return decl.supports(HTMLCMProperties.IS_OBSOLETE) && ((Boolean)(decl.getProperty(HTMLCMProperties.IS_OBSOLETE))).booleanValue();
	}
	
	
}

Back to the top