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