KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > builder > xml > DaoClasspathEntityResolver


1 /*
2  * Copyright 2004 Clinton Begin
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 com.ibatis.dao.engine.builder.xml;
17
18 import com.ibatis.common.resources.Resources;
19 import org.xml.sax.EntityResolver JavaDoc;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 public class DaoClasspathEntityResolver implements EntityResolver JavaDoc {
29
30   private static final String JavaDoc DTD_PATH_DAO = "com/ibatis/dao/engine/builder/xml/dao-2.dtd";
31
32   private static final Map JavaDoc doctypeMap = new HashMap JavaDoc();
33
34   static {
35     doctypeMap.put("http://www.ibatis.com/dtd/dao-2.dtd", DTD_PATH_DAO);
36     doctypeMap.put("http://ibatis.apache.org/dtd/dao-2.dtd", DTD_PATH_DAO);
37     doctypeMap.put("-//iBATIS.com//DTD DAO Configuration 2.0", DTD_PATH_DAO);
38     doctypeMap.put("-//iBATIS.com//DTD DAO Config 2.0", DTD_PATH_DAO);
39   }
40
41
42   /**
43    * Converts a public DTD into a local one
44    *
45    * @param publicId Unused but required by EntityResolver interface
46    * @param systemId The DTD that is being requested
47    * @return The InputSource for the DTD
48    * @throws org.xml.sax.SAXException If anything goes wrong
49    */

50   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
51       throws SAXException JavaDoc {
52     InputSource JavaDoc source = null;
53
54     try {
55       String JavaDoc path = (String JavaDoc) doctypeMap.get(publicId);
56       path = (String JavaDoc) doctypeMap.get(systemId);
57       if (path != null) {
58         InputStream JavaDoc in = null;
59         try {
60           in = Resources.getResourceAsStream(path);
61           source = new InputSource JavaDoc(in);
62         } catch (IOException JavaDoc e) {
63           // ignore, null is ok
64
}
65       }
66     } catch (Exception JavaDoc e) {
67       throw new SAXException JavaDoc(e.toString());
68     }
69
70     return source;
71   }
72
73 }
74
Popular Tags