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


import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager;
import org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.XMLAssociationProvider;
import org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache;
import org.w3c.dom.Document;

/**
 * A Class to provide an association between XHTML documents and DTDs for
 * XHTML. This class is intended to be used only in HTMLModelQueryCMProvider.
 */
/*
 * This class closely resemble XMLModelQueryAssociationProvider.
 */
class XHTMLAssociationProvider extends XMLAssociationProvider {

	/**
	 * set USE_QUICK_CACHE to false to test effects of not caching at all.
	 */
	private static final boolean USE_QUICK_CACHE = true;
	protected URIResolver idResolver;
	private String fCachedGrammerURI;
	private String fCachedPublicID;
	private String fCachedSystemID;
	private boolean cached;

	public XHTMLAssociationProvider(CMDocumentCache cache, URIResolver idResolver) {
		super(cache);
		this.idResolver = idResolver;
		   
		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=136399. If the CM document URI
		// is resolved and cached at this level instruct the CM model manager to avoid 
		// re-resolving the URI.

		if (USE_QUICK_CACHE) {
		  documentManager.setPropertyEnabled(CMDocumentManager.PROPERTY_PERFORM_URI_RESOLUTION, false);
		}
	}

	/**
	 * 
	 * @param publicId
	 * @param systemId
	 * @return
	 */
	public CMDocument getXHTMLCMDocument(String publicId, String systemId) {
		if (idResolver == null)
			return null;
		String grammerURI = null;
		if (USE_QUICK_CACHE) {
			/*
			 * In parsing a document, we get many identical requests to this
			 * method, so instead of looking up (resolving) grammerURI each
			 * time, we'll just return previously cached one. Probably not
			 * worth have a more complex cache than that.
			 */
			if (cached && sameAs(fCachedPublicID, publicId) && sameAs(fCachedSystemID, systemId)) {
				grammerURI = fCachedGrammerURI;
			}
			else {
				grammerURI = idResolver.resolve(null, publicId, systemId);
				fCachedGrammerURI = grammerURI;
				fCachedPublicID = publicId;
				fCachedSystemID = systemId;
				cached = true;
			}
		}
		else {
			grammerURI = idResolver.resolve(null, publicId, systemId);
		}

		if (grammerURI == null)
			return null;

		/*
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=88896
		 * 
		 * We once called the deprecated 2 argument form of getCMDocument.
		 * 
		 * CMDocument cmDocument = documentManager.getCMDocument(publicId,
		 * grammerURI);
		 * 
		 * which eventually resulted in empty string for type, which I don't
		 * think the infrastructure handles any longer. So, I deleted
		 * deprecated methods, and switched to null for type argument.
		 * 
		 * 'null' means to "create based on uri".
		 * 
		 * FYI, 'dtd' would mean load only those registered as dtd's
		 * 
		 * CMDocument cmDocument = documentManager.getCMDocument(publicId,
		 * grammerURI); CMDocument cmDocument =
		 * documentManager.getCMDocument(publicId, grammerURI, "dtd");
		 */
		CMDocument cmDocument = documentManager.getCMDocument(publicId, grammerURI, null);
		return cmDocument;
	}

	/**
	 */
	protected String resolveGrammarURI(Document document, String publicId, String systemId) {
		return idResolver.resolve(null, publicId, systemId);
	}

	private boolean sameAs(String a, String b) {
		boolean result = false;
		if (a == null) {
			result = b == null;
		}
		else {
			result = a.equals(b);
		}
		return result;
	}

	/**
	 * This added and/or made public specifically for experimentation. It
	 * will change as this functionality becomes API. See
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=119084
	 */
	public String getCachedGrammerURI() {
		return fCachedGrammerURI;
	}
}

Back to the top