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: 1f806e5c089c91226afa8a7db4fff73ee39b7692 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 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.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.html.core.internal.contentproperties.HTMLContentProperties;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter;
import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapterImpl;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocumentType;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;

/**
 */
public class HTMLDocumentTypeAdapter extends DocumentTypeAdapterImpl implements HTMLDocumentTypeConstants {

	private HTMLDocumentTypeAdapterFactory fFactory = null;
	private HTMLDocumentTypeEntry entry = null;
	private boolean isXMLType = false;
	private final static String XML = "xml"; //$NON-NLS-1$
	private final static String XHTML = "xhtml"; //$NON-NLS-1$
	private final static String WML = "wml"; //$NON-NLS-1$


	/**
	 */
	protected HTMLDocumentTypeAdapter() {
		super();
	}

	/**
	 */
	protected HTMLDocumentTypeAdapter(IDOMDocument document, HTMLDocumentTypeAdapterFactory factory) {
		super(document);

		this.fFactory = factory;

		// initialize
		documentTypeChanged();
	}

	/**
	 */
	private void documentTypeChanged() {
		IDOMDocument document = getDocument();
		if (document == null)
			return; // error
		IDOMModel model = document.getModel();
		if (model == null)
			return; // error

		IFile file = getFile(model);

		// find DOCTYPE delcaration and Public ID
		String publicId = null;
		DocumentType newDocumentType = findDocumentType(document);
		if (newDocumentType != null) {
			publicId = newDocumentType.getPublicId();
		}
		else {
			// lookup default set by contentsettings
			publicId = HTMLContentProperties.getProperty(HTMLContentProperties.DOCUMENT_TYPE, file, true);
		}

		// lookup DOCTYPE registry
		HTMLDocumentTypeEntry newEntry = null;
		if (publicId != null) {
			newEntry = HTMLDocumentTypeRegistry.getInstance().getEntry(publicId);
		}

		boolean newXMLType = (newEntry != null ? newEntry.isXMLType() : false);
		boolean newWMLType = (newEntry != null ? newEntry.isWMLType() : false);

		if (!newXMLType) {
			// find XML declaration
			if (findXMLNode(document) != null) {
				newXMLType = true;
			}

			// check file extension
			if (file != null) {
				String ext = file.getFileExtension();
				if (ext != null && ext.equalsIgnoreCase(XHTML)) {
					newXMLType = true;
				}

				if (ext != null && ext.equalsIgnoreCase(WML)) {
					newXMLType = true;
					newWMLType = true;
				}
			}

		}

		if (newEntry == null) {
			// lookup system default
			if (newXMLType && newDocumentType == null) {
				// use default XHTML, if it's XML and no document type
				// declared
				if (newWMLType)
					newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_WML);
				else
					newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_XHTML);

			}
			else {
				newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_HTML);
			}
			if (newEntry == null)
				return; // error
		}

		if (newDocumentType == null) {
			DocumentType oldDocumentType = getDocumentType();
			if (oldDocumentType == null || oldDocumentType.getName() != newEntry.getName()) {
				// create implicit DocumentType
				DOMImplementation impl = document.getImplementation();
				if (impl != null) {
					String name = newEntry.getName();
					publicId = newEntry.getPublicId();
					String systemId = newEntry.getSystemId();
					newDocumentType = impl.createDocumentType(name, publicId, systemId);
				}
			}
		}

		boolean notify = false;
		if (this.entry != null) { // do not notify on initialization
			notify = (newEntry != this.entry || newXMLType != this.isXMLType);
		}

		if (newDocumentType != null)
			setDocumentType(newDocumentType);
		this.entry = newEntry;
		this.isXMLType = newXMLType;

		if (notify)
			notifyDocumentTypeChanged();
	}

	/**
	 */
	private IDOMDocumentType findDocumentType(IDOMDocument document) {
		IDOMDocumentType documentType = (IDOMDocumentType) document.getDoctype();
		if (documentType != null && documentType.getExistingAdapter(DocumentTypeAdapter.class) == null) {
			// watch future changes
			documentType.addAdapter(this);
		}
		return documentType;
	}

	/**
	 */
	private Node findXMLNode(Document document) {
		for (Node child = document.getFirstChild(); child != null; child = child.getNextSibling()) {
			if (child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE)
				continue;
			String target = child.getNodeName();
			if (target != null && target.equals(XML)) {
				return child;
			}
		}
		return null;
	}

	/**
	 */
	public int getAttrNameCase() {
		if (isXMLType())
			return LOWER_CASE; // XHTML
		return this.fFactory.getAttrNameCase();
	}

	private IFile getFile(IStructuredModel model) {
		IFile result = null;
		String location = model.getBaseLocation();
		if (location != null) {
			IPath path = new Path(location);
			if (path.segmentCount() > 1) {
				result = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
			}
		}
		return result;
	}

	/**
	 */
	public int getTagNameCase() {
		if (isXMLType())
			return LOWER_CASE; // XHTML
		return this.fFactory.getTagNameCase();
	}

	/**
	 */
	public boolean hasFeature(String feature) {
		if (feature == null)
			return false;
		if (feature.equals(HTML))
			return true;
		if (feature.equals(SSI))
			return true;
		if (feature.equals(FRAMESET)) {
			if (this.entry == null)
				return false;
			return this.entry.hasFrameset();
		}
		return false;
	}

	/**
	 */
	public boolean isXMLType() {
		return this.isXMLType;
	}

	/**
	 */
	public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
		if (notifier == null)
			return;
		if (notifier instanceof IDOMDocument) {
			if (eventType != INodeNotifier.STRUCTURE_CHANGED)
				return;
		}
		else {
			if (eventType != INodeNotifier.CHANGE)
				return;
		}
		documentTypeChanged();
	}

	/**
	 */
	public void release() {
		super.release();
	}
}

Back to the top