Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b112c286ed24a381148259aa2400caa925cd1ee (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
/*******************************************************************************
 * 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.document;



import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.sse.core.INodeNotifier;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.ui.contentproperties.ContentSettings;
import org.eclipse.wst.sse.ui.contentproperties.ContentSettingsChangeSubject;
import org.eclipse.wst.sse.ui.contentproperties.IContentSettings;
import org.eclipse.wst.sse.ui.contentproperties.IContentSettingsListener;
import org.eclipse.wst.xml.core.document.DocumentTypeAdapter;
import org.eclipse.wst.xml.core.document.XMLDocument;
import org.eclipse.wst.xml.core.document.XMLDocumentType;
import org.eclipse.wst.xml.core.document.XMLModel;
import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapterImpl;
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 IContentSettingsListener, HTMLDocumentTypeConstants {

	private HTMLDocumentTypeAdapterFactory factory = 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(XMLDocument document, HTMLDocumentTypeAdapterFactory factory) {
		super(document);

		this.factory = factory;

		// initialize
		documentTypeChanged();

		ContentSettingsChangeSubject.getSubject().addListener(this);
	}

	/**
	 */
	public void contentSettingsChanged(IResource resource) {
		if (resource == null)
			return;
		XMLDocument document = getDocument();
		if (document == null)
			return;
		XMLModel model = document.getModel();
		if (model == null)
			return;
		IFile file = getFile(model);
		if (file == null)
			return;
		IProject project = file.getProject();
		if (project == null)
			return;
		if (!project.equals(resource.getProject()))
			return;
		documentTypeChanged();
	}

	/**
	 */
	private void documentTypeChanged() {
		XMLDocument document = getDocument();
		if (document == null)
			return; // error
		XMLModel 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 = getDefaultPublicId(file);
		}

		// 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 XMLDocumentType findDocumentType(XMLDocument document) {
		XMLDocumentType documentType = (XMLDocumentType) 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.factory.getAttrNameCase();
	}

	/**
	 */
	private String getDefaultPublicId(IFile file) {
		if (file == null)
			return null;
		IProject project = file.getProject();
		if (project == null)
			return null;
		IContentSettings settings = ContentSettings.getInstance();
		if (settings == null)
			return null;
		String publicId = settings.getProperty(file, IContentSettings.HTML_DOCUMENT_TYPE);
		if (publicId == null || publicId.length() == 0) {
			// look up project default
			publicId = settings.getProperty(project, IContentSettings.HTML_DOCUMENT_TYPE);
		}
		return publicId;
	}

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

	/**
	 */
	public int getTagNameCase() {
		if (isXMLType())
			return LOWER_CASE; // XHTML
		return this.factory.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 XMLDocument) {
			if (eventType != INodeNotifier.STRUCTURE_CHANGED)
				return;
		} else {
			if (eventType != INodeNotifier.CHANGE)
				return;
		}
		documentTypeChanged();
	}

	/**
	 */
	public void release() {
		ContentSettingsChangeSubject.getSubject().removeListener(this);
		super.release();
	}
}

Back to the top