01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.impl;
17:
18: import java.io.InputStream;
19:
20: import org.apache.commons.logging.LogFactory;
21: import org.apache.commons.logging.Log;
22: import org.directwebremoting.extend.DwrConstants;
23: import org.directwebremoting.util.Messages;
24: import org.xml.sax.EntityResolver;
25: import org.xml.sax.InputSource;
26: import org.xml.sax.SAXException;
27:
28: /**
29: * We want to read the config files in validating mode, and keep the DTD within
30: * the dwr.jar file so we need to be able to help the parser find the DTD.
31: * @author Joe Walker [joe at getahead dot ltd dot uk]
32: */
33: public final class DTDEntityResolver implements EntityResolver {
34: /* (non-Javadoc)
35: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
36: */
37: public InputSource resolveEntity(String publicId, String systemId)
38: throws SAXException {
39: for (int i = 0; i < MAPPINGS.length; i++) {
40: if (publicId.equals(MAPPINGS[i][0])) {
41: if (i != MAPPINGS.length - 1) {
42: String doctype = "<!DOCTYPE dwr PUBLIC \""
43: + MAPPINGS[MAPPINGS.length - 1][0]
44: + "\" \"http://getahead.org/dwr/"
45: + MAPPINGS[MAPPINGS.length - 1][1] + "\">";
46:
47: log.warn("Deprecated public id in dwr.xml. Use: "
48: + doctype);
49: }
50:
51: String dtdname = DwrConstants.PACKAGE + MAPPINGS[i][1];
52: InputStream raw = getClass().getResourceAsStream(
53: dtdname);
54: return new InputSource(raw);
55: }
56: }
57:
58: throw new SAXException(Messages.getString(
59: "DTDEntityResolver.ResolveFailed", publicId, systemId));
60: }
61:
62: /**
63: * An array of mappings from public ids to files to read from.
64: * The last one of these is the "only true DTD" the others are deprecated
65: * so be careful if you re-order them.
66: */
67: private static final String[][] MAPPINGS = {
68: { "-//GetAhead Limited//DTD Direct Web Remoting 0.4//EN",
69: "/dwr10.dtd" },
70: { "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN",
71: "/dwr10.dtd" },
72: { "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN",
73: "/dwr20.dtd" },
74: { "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN",
75: "/dwr30.dtd" }, };
76:
77: /**
78: * The log stream
79: */
80: private static final Log log = LogFactory
81: .getLog(DTDEntityResolver.class);
82: }
|