1 | |
package liquibase.parser.core.xml; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.io.InputStream; |
5 | |
|
6 | |
import javax.xml.parsers.SAXParser; |
7 | |
import javax.xml.parsers.SAXParserFactory; |
8 | |
|
9 | |
import liquibase.changelog.ChangeLogParameters; |
10 | |
import liquibase.changelog.DatabaseChangeLog; |
11 | |
import liquibase.exception.ChangeLogParseException; |
12 | |
import liquibase.logging.LogFactory; |
13 | |
import liquibase.parser.ChangeLogParser; |
14 | |
import liquibase.resource.ResourceAccessor; |
15 | |
import liquibase.util.file.FilenameUtils; |
16 | |
|
17 | |
import org.xml.sax.ErrorHandler; |
18 | |
import org.xml.sax.InputSource; |
19 | |
import org.xml.sax.SAXException; |
20 | |
import org.xml.sax.SAXNotRecognizedException; |
21 | |
import org.xml.sax.SAXNotSupportedException; |
22 | |
import org.xml.sax.SAXParseException; |
23 | |
import org.xml.sax.XMLReader; |
24 | |
|
25 | |
public class XMLChangeLogSAXParser implements ChangeLogParser { |
26 | |
|
27 | |
private SAXParserFactory saxParserFactory; |
28 | |
|
29 | 35 | public XMLChangeLogSAXParser() { |
30 | 35 | saxParserFactory = SAXParserFactory.newInstance(); |
31 | |
|
32 | 35 | if (System.getProperty("java.vm.version").startsWith("1.4")) { |
33 | 0 | saxParserFactory.setValidating(false); |
34 | 0 | saxParserFactory.setNamespaceAware(false); |
35 | |
} else { |
36 | 35 | saxParserFactory.setValidating(true); |
37 | 35 | saxParserFactory.setNamespaceAware(true); |
38 | |
} |
39 | 35 | } |
40 | |
|
41 | |
public int getPriority() { |
42 | 63 | return PRIORITY_DEFAULT; |
43 | |
} |
44 | |
|
45 | |
public static String getSchemaVersion() { |
46 | 32 | return "2.0"; |
47 | |
} |
48 | |
|
49 | |
public static String getDatabaseChangeLogNameSpace() { |
50 | 56 | return "http://www.liquibase.org/xml/ns/dbchangelog"; |
51 | |
} |
52 | |
|
53 | |
public boolean supports(String changeLogFile, ResourceAccessor resourceAccessor) { |
54 | 13 | return changeLogFile.endsWith("xml"); |
55 | |
} |
56 | |
|
57 | |
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, |
58 | |
ResourceAccessor resourceAccessor) throws ChangeLogParseException { |
59 | |
|
60 | 17 | InputStream inputStream = null; |
61 | |
try { |
62 | 17 | SAXParser parser = saxParserFactory.newSAXParser(); |
63 | |
try { |
64 | 17 | parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", |
65 | |
"http://www.w3.org/2001/XMLSchema"); |
66 | 0 | } catch (SAXNotRecognizedException e) { |
67 | |
|
68 | 0 | } catch (SAXNotSupportedException e) { |
69 | |
|
70 | 17 | } |
71 | |
|
72 | 17 | XMLReader xmlReader = parser.getXMLReader(); |
73 | 17 | LiquibaseEntityResolver resolver = new LiquibaseEntityResolver(); |
74 | 17 | resolver.useResoureAccessor(resourceAccessor, FilenameUtils.getFullPath(physicalChangeLogLocation)); |
75 | 17 | xmlReader.setEntityResolver(resolver); |
76 | 17 | xmlReader.setErrorHandler(new ErrorHandler() { |
77 | |
public void warning(SAXParseException exception) throws SAXException { |
78 | 0 | LogFactory.getLogger().warning(exception.getMessage()); |
79 | 0 | throw exception; |
80 | |
} |
81 | |
|
82 | |
public void error(SAXParseException exception) throws SAXException { |
83 | 1 | LogFactory.getLogger().severe(exception.getMessage()); |
84 | 1 | throw exception; |
85 | |
} |
86 | |
|
87 | |
public void fatalError(SAXParseException exception) throws SAXException { |
88 | 0 | LogFactory.getLogger().severe(exception.getMessage()); |
89 | 0 | throw exception; |
90 | |
} |
91 | |
}); |
92 | |
|
93 | 17 | inputStream = resourceAccessor.getResourceAsStream(physicalChangeLogLocation); |
94 | 17 | if (inputStream == null) { |
95 | 1 | throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist"); |
96 | |
} |
97 | |
|
98 | 16 | XMLChangeLogSAXHandler contentHandler = new XMLChangeLogSAXHandler(physicalChangeLogLocation, |
99 | |
resourceAccessor, changeLogParameters); |
100 | 16 | xmlReader.setContentHandler(contentHandler); |
101 | 16 | xmlReader.parse(new InputSource(inputStream)); |
102 | |
|
103 | 15 | return contentHandler.getDatabaseChangeLog(); |
104 | 1 | } catch (ChangeLogParseException e) { |
105 | 1 | throw e; |
106 | 0 | } catch (IOException e) { |
107 | 0 | throw new ChangeLogParseException("Error Reading Migration File: " + e.getMessage(), e); |
108 | 1 | } catch (SAXParseException e) { |
109 | 1 | throw new ChangeLogParseException("Error parsing line " + e.getLineNumber() + " column " |
110 | |
+ e.getColumnNumber() + " of " + physicalChangeLogLocation + ": " + e.getMessage(), e); |
111 | 0 | } catch (SAXException e) { |
112 | 0 | Throwable parentCause = e.getException(); |
113 | 0 | while (parentCause != null) { |
114 | 0 | if (parentCause instanceof ChangeLogParseException) { |
115 | 0 | throw ((ChangeLogParseException) parentCause); |
116 | |
} |
117 | 0 | parentCause = parentCause.getCause(); |
118 | |
} |
119 | 0 | String reason = e.getMessage(); |
120 | 0 | String causeReason = null; |
121 | 0 | if (e.getCause() != null) { |
122 | 0 | causeReason = e.getCause().getMessage(); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | 0 | if (reason == null) { |
129 | 0 | if (causeReason != null) { |
130 | 0 | reason = causeReason; |
131 | |
} else { |
132 | 0 | reason = "Unknown Reason"; |
133 | |
} |
134 | |
} |
135 | |
|
136 | 0 | throw new ChangeLogParseException("Invalid Migration File: " + reason, e); |
137 | 0 | } catch (Exception e) { |
138 | 0 | throw new ChangeLogParseException(e); |
139 | |
} finally { |
140 | 17 | if (inputStream != null) { |
141 | |
try { |
142 | 16 | inputStream.close(); |
143 | 0 | } catch (IOException e) { |
144 | |
|
145 | 33 | } |
146 | |
} |
147 | |
} |
148 | |
} |
149 | |
} |