KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > builder > xml > SqlMapClasspathEntityResolver


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.sqlmap.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 import java.util.Collections JavaDoc;
28
29 /**
30  * Offline entity resolver for the iBATIS DTDs
31  */

32 public class SqlMapClasspathEntityResolver implements EntityResolver JavaDoc {
33
34   private static final String JavaDoc SQL_MAP_CONFIG_DTD = "com/ibatis/sqlmap/engine/builder/xml/sql-map-config-2.dtd";
35   private static final String JavaDoc SQL_MAP_DTD = "com/ibatis/sqlmap/engine/builder/xml/sql-map-2.dtd";
36
37   private static final Map JavaDoc doctypeMap = new HashMap JavaDoc();
38
39   static {
40     doctypeMap.put("http://www.ibatis.com/dtd/sql-map-config-2.dtd", SQL_MAP_CONFIG_DTD);
41     doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-config-2.dtd", SQL_MAP_CONFIG_DTD);
42     doctypeMap.put("-//iBATIS.com//DTD SQL Map Config 2.0//EN", SQL_MAP_CONFIG_DTD);
43
44     doctypeMap.put("http://www.ibatis.com/dtd/sql-map-2.dtd", SQL_MAP_DTD);
45     doctypeMap.put("http://ibatis.apache.org/dtd/sql-map-2.dtd", SQL_MAP_DTD);
46     doctypeMap.put("-//iBATIS.com//DTD SQL Map 2.0//EN", SQL_MAP_DTD);
47   }
48
49
50   /**
51    * Converts a public DTD into a local one
52    *
53    * @param publicId Unused but required by EntityResolver interface
54    * @param systemId The DTD that is being requested
55    * @return The InputSource for the DTD
56    * @throws SAXException If anything goes wrong
57    */

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