KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > DTDEntityResolver


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19
20 import org.directwebremoting.extend.DwrConstants;
21 import org.directwebremoting.util.Logger;
22 import org.directwebremoting.util.Messages;
23 import org.xml.sax.EntityResolver JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
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 JavaDoc
34 {
35     /* (non-Javadoc)
36      * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
37      */

38     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc
39     {
40         for (int i = 0; i < MAPPINGS.length; i++)
41         {
42             if (publicId.equals(MAPPINGS[i][0]))
43             {
44                 if (i != MAPPINGS.length - 1)
45                 {
46                     String JavaDoc doctype = "<!DOCTYPE dwr PUBLIC \"" +
47                         MAPPINGS[MAPPINGS.length - 1][0] + "\" \"http://getahead.org/dwr/" +
48                         MAPPINGS[MAPPINGS.length - 1][1] + "\">";
49
50                     log.warn("Deprecated public id in dwr.xml. Use: " + doctype);
51                 }
52
53                 String JavaDoc dtdname = DwrConstants.PACKAGE + MAPPINGS[i][1];
54                 InputStream JavaDoc raw = getClass().getResourceAsStream(dtdname);
55                 return new InputSource JavaDoc(raw);
56             }
57         }
58
59         throw new SAXException JavaDoc(Messages.getString("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 JavaDoc[][] MAPPINGS =
68     {
69         { "-//GetAhead Limited//DTD Direct Web Remoting 0.4//EN", "/dwr10.dtd"},
70         { "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN", "/dwr10.dtd"},
71         { "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN", "/dwr20.dtd"},
72     };
73
74     /**
75      * The log stream
76      */

77     private static final Logger log = Logger.getLogger(DTDEntityResolver.class);
78 }
79
Popular Tags