KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > DozerResolver


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.util;
17
18 import java.io.InputStream JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.xml.sax.EntityResolver JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * EntityResolver implementation for the dozer mappings DTD, to load the DTD from the dozer classpath resp. JAR file.
28  *
29  * <p>
30  * Fetches "dozerbeanmapping.dtd" from the classpath resource "/dozerbeanmapping.dtd", no matter if specified as some
31  * local URL or as "http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd".
32  *
33  * @author garsombke.franz
34  */

35 public class DozerResolver implements EntityResolver JavaDoc {
36
37   private static final Log log = LogFactory.getLog(DozerResolver.class);
38
39   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
40     log.debug("Trying to resolve XML entity with public ID [" + publicId + "] and system ID [" + systemId + "]");
41     if (systemId != null && systemId.indexOf(MapperConstants.DTD_NAME) > systemId.lastIndexOf("/")) {
42       String JavaDoc dtdFile = systemId.substring(systemId.indexOf(MapperConstants.DTD_NAME));
43       log.debug("Trying to locate [" + dtdFile + "] in classpath");
44       try {
45         InputStream JavaDoc stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(dtdFile);
46         InputSource JavaDoc source = new InputSource JavaDoc(stream);
47         source.setPublicId(publicId);
48         source.setSystemId(systemId);
49         log.debug("Found dozerbeanmapping DTD [" + systemId + "] in classpath");
50         return source;
51       } catch (Exception JavaDoc ex) {
52         log.error("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
53       }
54     }
55     // use the default behaviour -> download from website or wherever
56
return null;
57   }
58 }
59
Popular Tags