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: 66dd565bf16a970410b8257ad5076622157ce00a (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
/*******************************************************************************
 * Copyright (c) 2001, 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.core.internal.document;

import org.eclipse.wst.xml.core.internal.XMLCoreMessages;
import org.eclipse.wst.xml.core.internal.provisional.IXMLCharEntity;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;

/**
 */
public class SourceValidator {

	private NodeImpl node = null;

	/**
	 */
	public SourceValidator(Node node) {
		super();

		if (node != null) {
			this.node = (NodeImpl) node;
		}
	}

	/**
	 */
	public String convertSource(String source) {
		if (source == null)
			return null;
		if (this.node == null)
			return null; // error

		// setup conversion conditions
		boolean acceptTag = false;
		boolean acceptClose = false;
		boolean acceptQuote = false;
		boolean acceptAmpersand = false;
		boolean acceptEntityRef = true;
		boolean acceptJSPEnd = true;
		String endTagName = null;
		if (this.node.getNodeType() == Node.ATTRIBUTE_NODE) {
			IDOMDocument document = (IDOMDocument) this.node.getOwnerDocument();
			if (document != null && document.isJSPType())
				acceptTag = true;
			if (acceptTag) {
				Attr attr = (Attr) this.node;
				ElementImpl element = (ElementImpl) attr.getOwnerElement();
				if (element != null && element.isJSPTag())
					acceptTag = false;
			}
			// if the source does not include single quote,
			// double quote is valid
			acceptQuote = (source.indexOf('\'') < 0);
		} else if (this.node.getNodeType() == Node.TEXT_NODE) {
			TextImpl text = (TextImpl) this.node;
			if (text.isJSPContent()) {
				int index = source.indexOf(JSPTag.TAG_CLOSE);
				if (index < 0)
					return source;
				acceptTag = true;
				acceptClose = true;
				acceptQuote = true;
				acceptAmpersand = true;
				acceptJSPEnd = false;
			} else if (text.isCDATAContent()) {
				endTagName = text.getParentNode().getNodeName();
				if (endTagName == null)
					return null; // error
				acceptTag = true;
				acceptClose = true;
				acceptQuote = true;
				acceptAmpersand = true;
			}
		} else {
			IDOMDocument document = null;
			if (this.node.getNodeType() == Node.DOCUMENT_NODE) {
				document = (IDOMDocument) this.node;
			} else {
				document = (IDOMDocument) this.node.getOwnerDocument();
			}
			if (document != null && document.isJSPType())
				acceptTag = true;
		}

		StringBuffer buffer = null;
		int copiedLength = 0;
		int length = source.length();
		for (int i = 0; i < length; i++) {
			String ref = null;
			char c = source.charAt(i);
			switch (c) {
				case '<' :
					if (acceptTag) {
						if (endTagName != null) {
							if (!matchEndTag(source, i + 1, endTagName))
								continue;
						} else {
							int skip = skipTag(source, i + 1);
							if (skip >= 0) {
								i += skip;
								continue;
							}
						}
						// invalid JSP tag
					}
					ref = IXMLCharEntity.LT_REF;
					break;
				case '>' :
					if (acceptClose)
						continue;
					ref = IXMLCharEntity.GT_REF;
					break;
				case '&' :
					if (acceptAmpersand)
						continue;
					if (acceptEntityRef) {
						int skip = skipEntityRef(source, i + 1);
						if (skip >= 0) {
							i += skip;
							continue;
						}
					}
					ref = IXMLCharEntity.AMP_REF;
					break;
				case '"' :
					if (acceptQuote)
						continue;
					ref = IXMLCharEntity.QUOT_REF;
					break;
				case '%' :
					if (acceptJSPEnd)
						continue;
					if (source.charAt(i + 1) != '>')
						continue;
					i++;
					ref = IXMLCharEntity.GT_REF;
					break;
				default :
					continue;
			}

			if (ref != null) {
				if (buffer == null) {
					buffer = new StringBuffer(length + 8);
				}
				if (i > copiedLength) {
					buffer.append(source.substring(copiedLength, i));
				}
				buffer.append(ref);
				copiedLength = i + 1; // skip this character
			}
		}

		if (buffer != null) {
			if (copiedLength < length) {
				buffer.append(source.substring(copiedLength, length));
			}
			return buffer.toString();
		}
		return source;
	}

	/**
	 */
	private final boolean matchEndTag(String source, int offset, String endTagName) {
		if (source == null || endTagName == null)
			return false;
		int length = source.length();
		if (offset < 0 || offset >= length)
			return false;
		if (source.charAt(offset) != '/')
			return false;
		offset++;
		int end = offset + endTagName.length();
		if (end > length)
			return false;
		return endTagName.equalsIgnoreCase(source.substring(offset, end));
	}

	/**
	 */
	private final int skipEntityRef(String source, int offset) {
		if (source == null)
			return -1;
		if (offset < 0 || offset >= source.length())
			return -1;
		DocumentImpl document = (DocumentImpl) this.node.getOwnerDocument();
		if (document == null)
			return -1; // error

		int end = source.indexOf(';', offset);
		if (end < 0 || end == offset)
			return -1;
		String name = source.substring(offset, end);
		if (name == null || document.getCharValue(name) == null)
			return -1;
		return (end + 1 - offset);
	}

	/**
	 */
	private final int skipTag(String source, int offset) {
		if (source == null)
			return -1;
		if (offset < 0 || offset >= source.length())
			return -1;

		int end = offset;
		if (source.charAt(offset) == '%') {
			// JSP tag
			int found = source.indexOf(JSPTag.TAG_CLOSE, offset + 1);
			if (found < 0)
				return -1; // invalid JSP tag
			end = found + 2;
		} else {
			// normal tag
			int found = source.indexOf('>', offset);
			if (found < 0)
				return -1; // invalid tag
			end = found + 1;
		}
		return (end - offset);
	}

	/**
	 */
	public boolean validateSource(String source) throws InvalidCharacterException {
		if (source == null)
			return true;
		if (this.node == null)
			return false; // error
		String message = null;

		// setup validation conditions
		boolean acceptTag = false;
		boolean acceptClose = false;
		boolean acceptQuote = true;
		boolean acceptEntityRef = true;
		String endTagName = null;
		if (this.node.getNodeType() == Node.ATTRIBUTE_NODE) {
			IDOMDocument document = (IDOMDocument) this.node.getOwnerDocument();
			if (document != null && document.isJSPType())
				acceptTag = true;
			if (acceptTag) {
				Attr attr = (Attr) this.node;
				ElementImpl element = (ElementImpl) attr.getOwnerElement();
				if (element != null && element.isJSPTag())
					acceptTag = false;
			}
			// if the source does not include single quote,
			// double quote is valid
			acceptQuote = (source.indexOf('\'') < 0);
		} else if (this.node.getNodeType() == Node.TEXT_NODE) {
			TextImpl text = (TextImpl) this.node;
			if (text.isJSPContent()) {
				int index = source.indexOf(JSPTag.TAG_CLOSE);
				if (index < 0)
					return true;
				message = XMLCoreMessages.Invalid_character_gt_fo_ERROR_;
				throw new InvalidCharacterException(message, '>', index + 1);
			} else if (text.isCDATAContent()) {
				endTagName = text.getParentNode().getNodeName();
				if (endTagName == null)
					return false; // error
				acceptTag = true;
				acceptClose = true;
			}
		} else {
			IDOMDocument document = null;
			if (this.node.getNodeType() == Node.DOCUMENT_NODE) {
				document = (IDOMDocument) this.node;
			} else {
				document = (IDOMDocument) this.node.getOwnerDocument();
			}
			if (document != null && document.isJSPType())
				acceptTag = true;
		}

		char c = 0;
		int length = source.length();
		for (int i = 0; i < length; i++) {
			c = source.charAt(i);
			switch (c) {
				case '<' :
					if (acceptTag) {
						if (endTagName != null) {
							if (!matchEndTag(source, i + 1, endTagName))
								continue;
						} else {
							int skip = skipTag(source, i + 1);
							if (skip >= 0) {
								i += skip;
								continue;
							}
						}
						// invalid tag
					}
					message = XMLCoreMessages.Invalid_character_lt_fo_ERROR_;
					break;
				case '>' :
					if (acceptClose)
						continue;
					message = XMLCoreMessages.Invalid_character_gt_fo_ERROR_;
					break;
				case '&' :
					if (acceptEntityRef) {
						if (endTagName != null)
							continue;
						int skip = skipEntityRef(source, i + 1);
						if (skip >= 0) {
							i += skip;
							continue;
						}
						// invalid entity reference
					}
					message = XMLCoreMessages.Invalid_character_amp_fo_ERROR_;
					break;
				case '"' :
					if (acceptQuote)
						continue;
					message = XMLCoreMessages.Invalid_character__f_EXC_;
					break;
				default :
					continue;
			}

			if (message != null) {
				throw new InvalidCharacterException(message, c, i);
			}
		}

		return true;
	}
}

Back to the top