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: ec6b64d936d29ea67f87422beb0fa4ecb499b6ae (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
/*******************************************************************************
 * Copyright (c) 2001, 2005 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.jst.j2ee.internal.xml;



import java.io.FileNotFoundException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jst.j2ee.internal.J2EEConstants;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class J2EEXmlDtDEntityResolver implements org.xml.sax.EntityResolver {

	/** All the dtds that this resolver knows about; import strategies register these
	 * at startup */ 
	protected static Map supportedDtDs;
	static {
		registerDtD("http://www.w3.org/2001/xml.xsd", "xml.xsd");  //$NON-NLS-1$ //$NON-NLS-2$
		registerDtD("XMLSchema.dtd", "XMLSchema.dtd"); //$NON-NLS-1$ //$NON-NLS-2$
		registerDtD("datatypes.dtd", "datatypes.dtd"); //$NON-NLS-1$ //$NON-NLS-2$
	}
	public static J2EEXmlDtDEntityResolver INSTANCE = new J2EEXmlDtDEntityResolver();
/**
 * EjbXmlEntityResolver constructor comment.
 */
public J2EEXmlDtDEntityResolver() {
	super();
}
public static Map getSupportedDtDs() {
	if (supportedDtDs == null)
		supportedDtDs = new HashMap();
	return supportedDtDs;
}
/**
 * Maps the system id for the dtd to a local id to be retrieved loaded from the class path
 */
public static void registerDtD(String systemID, String localID) { 
    //Make sure local file exists on classpath first.
	
	ClassLoader loader = J2EEXmlDtDEntityResolver.class.getClassLoader();
	URL url = null;
	if (loader == null) {	    
		url = ClassLoader.getSystemResource(localID);
	} else {	    
		url = loader.getResource(localID);
	}
	if (url == null) {
		return;
	}
	getSupportedDtDs().put(systemID, localID);
	getSupportedDtDs().put(getShortName(systemID), localID);
}
/**
 * for a system id with a URL that begins with "http://java.sun.com/", check to see if that is a recognized dtd;
 * if so, load the dtd from the class path using the value of the registered dtd.
 *
 * @return an Input source on a locally resolved dtd, or null of the systemid does not start with "http://java.sun.com/"
 * 
 * @throws SAXException with a nested NotSupportedException if the dtd is not recognized
 * @throws FileNotFoundException if the resolved dtd cannot be loaded from the classpath
 */
public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) throws java.io.IOException, org.xml.sax.SAXException {
	String localResourceName = null;
	//boolean isJavaSytemId = false;
	if (shouldBeRegistered(systemId)) {
		localResourceName = (String)getSupportedDtDs().get(systemId);
		//isJavaSytemId = true;	
	} else {
		String shortName = getShortName(systemId);
		localResourceName = (String)getSupportedDtDs().get(shortName);
		if (localResourceName != null)
			systemId = localResourceName;
	}
		
	if (localResourceName == null) {
		return null;
	} 
	ClassLoader loader = getClass().getClassLoader();
	URL url = null;
	if (loader == null) {	    
		url = ClassLoader.getSystemResource(localResourceName);
	} else {	    
		url = loader.getResource(localResourceName);
	}
	
	
	if (url == null) {
		String message = J2EEXMLResourceHandler.getString(J2EEXMLResourceHandler.dtd_not_found_EXC_, (new Object[] {localResourceName}));// = "Could not parse xml because the resolved resource "{0}" could not be found in classpath"
		throw new java.io.FileNotFoundException(message);
	}
	
	InputSource result = new InputSource(url.toString());
	result.setPublicId(publicId);	
	// force the encoding to be UTF8
	result.setEncoding("UTF-8"); //$NON-NLS-1$
	
	return result;
}
protected boolean shouldBeRegistered(String systemId) {
	
	return systemId.startsWith(J2EEConstants.JAVA_SUN_COM_URL) 
		|| systemId.startsWith(J2EEConstants.WWW_W3_ORG_URL)
		|| systemId.startsWith(J2EEConstants.WWW_IBM_COM_URL);
}

/**
 * Returns the filename from the uri, or the segment after the last occurrence of a separator
 */
private static String getShortName(String uri) {
	String tempURI = uri.replace('\\', '/');
	while (tempURI.endsWith("/")) //$NON-NLS-1$
		tempURI = tempURI.substring(0, tempURI.length()-1);
	int lastIndex = tempURI.lastIndexOf('/');
	if (lastIndex == -1)
		return uri;
	return uri.substring(lastIndex+1, tempURI.length());
}
}



 

Back to the top