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.

aboutsummaryrefslogtreecommitdiffstats
blob: ac88325fc0db75336bfc4f185025a7f46c0a1152 (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
/*******************************************************************************
 * Copyright (c) 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
 *******************************************************************************/
package org.eclipse.wst.html.core.internal.commentelement.handlers;



import org.eclipse.wst.xml.core.internal.commentelement.CommentElementHandler;
import org.eclipse.wst.xml.core.internal.commentelement.util.CommentElementFactory;
import org.eclipse.wst.xml.core.internal.commentelement.util.TagScanner;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
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.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class CommentElementHandlerForSSI implements CommentElementHandler {

	public Element createElement(Document document, String data, boolean isJSPTag) {
		ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
		if (modelQuery == null) {
			return null;
		}
		CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
		if (cm == null) {
			return null;
		}
		CMNamedNodeMap map = cm.getElements();
		if (map == null) {
			return null;
		}

		TagScanner scanner = new TagScanner(data, 1);
		String name = scanner.nextName();
		if (name == null) {
			return null;
		}
		StringBuffer buffer = new StringBuffer(name.length() + 4);
		buffer.append(SSI_PREFIX);
		buffer.append(':');
		buffer.append(name);
		String tagName = buffer.toString();
		// check if valid (defined) SSI tag or not
		if (map.getNamedItem(tagName) == null) {
			return null;
		}

		CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
		Element element = factory.create(tagName, CommentElementFactory.IS_START);

		// set attributes
		String attrName = scanner.nextName();
		while (attrName != null) {
			String attrValue = scanner.nextValue();
			Attr attr = document.createAttribute(attrName);
			if (attr != null) {
				if (attrValue != null)
					attr.setValue(attrValue);
				element.setAttributeNode(attr);
			}
			attrName = scanner.nextName();
		}
		return element;
	}

	public String generateStartTagContent(IDOMElement element) {
		ISourceGenerator generator = element.getModel().getGenerator();
		StringBuffer buffer = new StringBuffer();

		buffer.append('#');
		buffer.append(element.getLocalName());

		NamedNodeMap attributes = element.getAttributes();
		int length = attributes.getLength();
		for (int i = 0; i < length; i++) {
			Attr attr = (Attr) attributes.item(i);
			if (attr == null) {
				continue;
			}
			buffer.append(' ');
			String attrName = generator.generateAttrName(attr);
			if (attrName != null) {
				buffer.append(attrName);
			}
			String attrValue = generator.generateAttrValue(attr);
			if (attrValue != null) {
				// attr name only for HTML boolean and JSP
				buffer.append('=');
				buffer.append(attrValue);
			}
		}

		return buffer.toString();
	}

	public String generateEndTagContent(IDOMElement element) {
		return null; // always empty
	}

	public boolean isEmpty() {
		return true;
	}

	public boolean isCommentElement(IDOMElement element) {
		if (element == null) {
			return false;
		}
		Document document = element.getOwnerDocument();
		ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
		if (modelQuery == null) {
			return false;
		}
		CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
		if (cm == null) {
			return false;
		}
		CMNamedNodeMap map = cm.getElements();
		if (map == null) {
			return false;
		}
		String prefix = element.getPrefix();
		if (prefix == null || !prefix.equals(SSI_PREFIX)) {
			return false;
		}
		String tagName = element.getTagName();
		if (tagName.length() <= 4) {
			return false;
		}
		if (map.getNamedItem(tagName) == null) {
			return false;
		}
		else {
			return true;
		}
	}

	private static final String SSI_PREFIX = "ssi";//$NON-NLS-1$
}

Back to the top