1 | |
package liquibase.parser.core.xml; |
2 | |
|
3 | |
import org.xml.sax.InputSource; |
4 | |
|
5 | |
import java.io.IOException; |
6 | |
import java.io.InputStream; |
7 | |
import liquibase.logging.LogFactory; |
8 | |
import liquibase.logging.Logger; |
9 | |
import liquibase.resource.ResourceAccessor; |
10 | |
import liquibase.util.file.FilenameUtils; |
11 | |
import org.xml.sax.SAXException; |
12 | |
import org.xml.sax.ext.EntityResolver2; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public class LiquibaseEntityResolver implements EntityResolver2 { |
19 | |
|
20 | |
private static final String SEARCH_PACKAGE = "liquibase/parser/core/xml/"; |
21 | |
|
22 | |
private ResourceAccessor resourceAccessor; |
23 | |
private String basePath; |
24 | |
|
25 | 18 | private Logger log = LogFactory.getLogger(); |
26 | |
|
27 | 18 | public LiquibaseEntityResolver() { |
28 | |
|
29 | 18 | } |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public void useResoureAccessor(ResourceAccessor resourceAccessor, String basePath) { |
40 | 17 | this.resourceAccessor = resourceAccessor; |
41 | 17 | this.basePath = basePath; |
42 | 17 | } |
43 | |
|
44 | |
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) |
45 | |
throws SAXException, IOException { |
46 | 18 | InputSource resolved = null; |
47 | 18 | if (systemId != null && systemId.toLowerCase().endsWith(".xsd")) { |
48 | 18 | resolved = tryResolveLiquibaseSchema(systemId, publicId); |
49 | |
} |
50 | 18 | if (resolved == null && resourceAccessor != null && basePath != null && systemId != null) { |
51 | 0 | resolved = tryResolveFromResourceAccessor(systemId); |
52 | |
} |
53 | 18 | return resolved; |
54 | |
} |
55 | |
|
56 | |
private InputSource tryResolveLiquibaseSchema(String systemId, String publicId) { |
57 | 18 | if (systemId != null) { |
58 | 18 | int iSlash = systemId.lastIndexOf('/'); |
59 | 18 | if (iSlash >= 0) { |
60 | 18 | String xsdFile = systemId.substring(iSlash + 1); |
61 | |
try { |
62 | 18 | InputStream resourceAsStream = null; |
63 | 18 | if (Thread.currentThread().getContextClassLoader() != null) { |
64 | 18 | resourceAsStream = Thread.currentThread().getContextClassLoader() |
65 | |
.getResourceAsStream(SEARCH_PACKAGE + xsdFile); |
66 | |
} |
67 | 18 | if (resourceAsStream == null) { |
68 | 1 | resourceAsStream = this.getClass().getClassLoader() |
69 | |
.getResourceAsStream(SEARCH_PACKAGE + xsdFile); |
70 | |
} |
71 | 18 | if (resourceAsStream == null) { |
72 | 1 | return null; |
73 | |
} |
74 | 17 | InputSource source = new InputSource(resourceAsStream); |
75 | 17 | source.setPublicId(publicId); |
76 | 17 | source.setSystemId(systemId); |
77 | 17 | return source; |
78 | 0 | } catch (Exception ex) { |
79 | 0 | return null; |
80 | |
} |
81 | |
} |
82 | |
} |
83 | 0 | return null; |
84 | |
} |
85 | |
|
86 | |
private InputSource tryResolveFromResourceAccessor(String systemId) { |
87 | 0 | String path = FilenameUtils.concat(basePath, systemId); |
88 | |
try { |
89 | 0 | return new InputSource(resourceAccessor.getResourceAsStream(path)); |
90 | 0 | } catch (Exception ex) { |
91 | 0 | return null; |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
public InputSource getExternalSubset(String name, String baseURI) throws SAXException, IOException { |
96 | 0 | return null; |
97 | |
} |
98 | |
|
99 | |
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { |
100 | 0 | log.warning("Current XML parsers seems to not support EntityResolver2. External entities won't be correctly loaded"); |
101 | 0 | return tryResolveLiquibaseSchema(systemId, publicId); |
102 | |
} |
103 | |
|
104 | |
} |