01: package liquibase.parser;
02:
03: import org.xml.sax.EntityResolver;
04: import org.xml.sax.InputSource;
05:
06: import java.io.IOException;
07: import java.io.InputStream;
08:
09: /**
10: * Finds the LiquiBase schema from the classpath rather than fetching it over the Internet.
11: */
12: public class LiquibaseSchemaResolver implements EntityResolver {
13:
14: private static final String SEARCH_PACKAGE = "liquibase/";
15:
16: public InputSource resolveEntity(String publicId, String systemId)
17: throws IOException {
18: if (systemId != null) {
19: int iSlash = systemId.lastIndexOf('/');
20: if (iSlash >= 0) {
21: String xsdFile = systemId.substring(iSlash + 1);
22: try {
23: InputStream resourceAsStream = Thread
24: .currentThread().getContextClassLoader()
25: .getResourceAsStream(
26: SEARCH_PACKAGE + xsdFile);
27:
28: if (resourceAsStream == null) {
29: resourceAsStream = this .getClass()
30: .getClassLoader().getResourceAsStream(
31: SEARCH_PACKAGE + xsdFile);
32: }
33:
34: if (resourceAsStream == null) {
35: return null;
36: }
37: InputSource source = new InputSource(
38: resourceAsStream);
39: source.setPublicId(publicId);
40: source.setSystemId(systemId);
41: return source;
42: } catch (Exception ex) {
43: return null; // We don't have the schema, try the network
44: }
45: }
46: }
47: return null;
48: }
49:
50: }
|