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


import java.util.Hashtable;

import org.eclipse.wst.common.contentmodel.CMDocument;
import org.eclipse.wst.common.contentmodel.modelquery.ModelQueryCMProvider;
import org.eclipse.wst.common.contentmodel.util.CMDocumentCache;
import org.eclipse.wst.html.core.contentmodel.HTMLCMDocumentFactory;
import org.eclipse.wst.html.core.document.HTMLDocumentTypeEntry;
import org.eclipse.wst.html.core.document.HTMLDocumentTypeRegistry;
import org.eclipse.wst.sse.core.contentmodel.CMDocType;
import org.eclipse.wst.xml.core.document.XMLDocument;
import org.eclipse.wst.xml.uriresolver.util.IdResolver;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;

/**
 * CMDocument provider for HTML and XHTML documents.
 */
public class HTMLModelQueryCMProvider implements ModelQueryCMProvider {


	private static CMDocument staticHTML = HTMLCMDocumentFactory.getCMDocument(CMDocType.HTML_DOC_TYPE);
	private static CMDocument staticCHTML = HTMLCMDocumentFactory.getCMDocument(CMDocType.CHTML_DOC_TYPE);
	private static HTMLDocumentTypeRegistry doctypeRegistry = HTMLDocumentTypeRegistry.getInstance();
	private static Hashtable buddyCache = new Hashtable();

	private XHTMLAssociationProvider xhtmlassoc = null;

	public HTMLModelQueryCMProvider(CMDocumentCache cache, IdResolver idResolver) {
		super();
		xhtmlassoc = new XHTMLAssociationProvider(cache, idResolver);
	}

	/**
	 * Returns the CMDocument that corresponds to the DOM Node.
	 * or null if no CMDocument is appropriate for the DOM Node.
	 */
	public CMDocument getCorrespondingCMDocument(Node node) {
		XMLDocument owner = getOwnerXMLDocument(node);
		if (owner == null)
			return null;

		String pid = getPublicId(owner);
		if (pid == null)
			return staticHTML;

		HTMLDocumentTypeEntry entry = doctypeRegistry.getEntry(pid);
		if (entry == null)
			return staticHTML;

		pid = entry.getPublicId();
		CMDocument dtdcm = xhtmlassoc.getXHTMLCMDocument(pid, entry.getSystemId());
		if (dtdcm == null) {
			if (pid != null && pid.equals(HTMLDocumentTypeRegistry.CHTML_PUBLIC_ID)) {
				return staticCHTML;
			}
			return staticHTML;
		}

		CMDocument buddycm = (CMDocument) buddyCache.get(pid);
		if (buddycm != null)
			return buddycm;

		buddycm = new CMDocumentForBuddySystem(dtdcm, entry.isXMLType());
		buddyCache.put(pid, buddycm);
		return buddycm;
	}

	// private methods
	private XMLDocument getOwnerXMLDocument(Node node) {
		if (node == null)
			return null;
		Document owner = (node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument();
		if (owner == null)
			return null;
		if (!(owner instanceof XMLDocument))
			return null;
		return (XMLDocument) owner;
	}

	private String getPublicId(XMLDocument doc) {
		if (doc == null)
			return null;
		DocumentType doctype = doc.getDoctype();
		return (doctype != null) ? doctype.getPublicId() : doc.getDocumentTypeId();
	}
}

Back to the top