1 | |
package org.apache.torque.task; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileWriter; |
5 | |
import java.io.IOException; |
6 | |
import java.io.StringWriter; |
7 | |
|
8 | |
import javax.xml.parsers.DocumentBuilder; |
9 | |
import javax.xml.parsers.DocumentBuilderFactory; |
10 | |
import javax.xml.parsers.ParserConfigurationException; |
11 | |
import javax.xml.transform.OutputKeys; |
12 | |
import javax.xml.transform.Result; |
13 | |
import javax.xml.transform.Transformer; |
14 | |
import javax.xml.transform.TransformerFactory; |
15 | |
import javax.xml.transform.dom.DOMSource; |
16 | |
import javax.xml.transform.stream.StreamResult; |
17 | |
import javax.xml.xpath.XPath; |
18 | |
import javax.xml.xpath.XPathConstants; |
19 | |
import javax.xml.xpath.XPathFactory; |
20 | |
|
21 | |
import org.apache.tools.ant.BuildException; |
22 | |
import org.apache.tools.ant.Task; |
23 | |
import org.w3c.dom.Document; |
24 | |
import org.w3c.dom.Element; |
25 | |
import org.w3c.dom.NodeList; |
26 | |
import org.xml.sax.SAXException; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class TorqueMergeXMLDoc extends Task { |
35 | |
|
36 | |
private static final String DESCRIPTION_ATTR = "description"; |
37 | |
private static final String JAVA_NAME_ATTR = "javaName"; |
38 | |
private static final String NAME_ATTR = "name"; |
39 | |
private static final String COLUMN_ELEMENT = "column"; |
40 | |
|
41 | |
private File schemaWithDesc; |
42 | |
private File dbSchema; |
43 | |
private String schemaWithDescString; |
44 | |
private String dbSchemaString; |
45 | |
private Document schemaWithDescDoc; |
46 | |
private Document dbSchemaDoc; |
47 | |
|
48 | |
public void setDbSchemaString(String dbSchemaString) { |
49 | 0 | this.dbSchemaString = dbSchemaString; |
50 | 0 | dbSchema = new File(dbSchemaString); |
51 | |
|
52 | 0 | } |
53 | |
|
54 | |
public void setSchemaWithDescString(String schemaWithDescString) { |
55 | 0 | this.schemaWithDescString = schemaWithDescString; |
56 | 0 | schemaWithDesc = new File(schemaWithDescString); |
57 | |
|
58 | 0 | } |
59 | |
|
60 | |
public File getDbSchema() { |
61 | 0 | return dbSchema; |
62 | |
} |
63 | |
|
64 | |
public void setSchemaWithDesc(String schemaWithDescString) { |
65 | 0 | this.schemaWithDesc = new File(schemaWithDescString); |
66 | |
|
67 | 0 | } |
68 | |
|
69 | |
public void setDbSchema(String dbSchemaString) { |
70 | 0 | this.dbSchema = new File(dbSchemaString); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static Document setDocument(File file) throws ParserConfigurationException, SAXException, IOException { |
83 | 0 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
84 | |
DocumentBuilder documentBuilder; |
85 | 0 | documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
86 | 0 | Document xmlDocument = documentBuilder.parse(file); |
87 | 0 | xmlDocument.getDocumentElement().normalize(); |
88 | 0 | return xmlDocument; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void mergeSchemas(File schemaWithDesc, File dbSchema) throws Exception { |
99 | |
|
100 | 0 | schemaWithDescDoc = setDocument(schemaWithDesc); |
101 | 0 | dbSchemaDoc = setDocument(dbSchema); |
102 | 0 | dbSchemaDoc = createNewXML(dbSchemaDoc, schemaWithDescDoc); |
103 | |
|
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public Document createNewXML(Document dbSchemaDoc, Document schemaWithDescDoc) throws Exception { |
111 | |
|
112 | 0 | XPath xpath = XPathFactory.newInstance().newXPath(); |
113 | |
|
114 | 0 | NodeList listOfTablesInSchema = (NodeList) xpath.evaluate("/database/table", dbSchemaDoc, |
115 | |
XPathConstants.NODESET); |
116 | |
|
117 | 0 | for (int tableIndex = 0; tableIndex < listOfTablesInSchema.getLength(); tableIndex++) { |
118 | 0 | Element tableElementInSchema = (Element) listOfTablesInSchema.item(tableIndex); |
119 | 0 | String tableName = tableElementInSchema.getAttribute(NAME_ATTR); |
120 | |
|
121 | 0 | Element tableDescElem = (Element) xpath.evaluate("/database/table[@" + NAME_ATTR + "='" + tableName + "']", |
122 | |
schemaWithDescDoc, XPathConstants.NODE); |
123 | 0 | if (tableDescElem != null) { |
124 | 0 | String tableDescription = tableDescElem.getAttribute(DESCRIPTION_ATTR); |
125 | 0 | String tableJavaName = tableDescElem.getAttribute(JAVA_NAME_ATTR); |
126 | 0 | tableElementInSchema.setAttribute(DESCRIPTION_ATTR, tableDescription); |
127 | 0 | tableElementInSchema.setAttribute(JAVA_NAME_ATTR, tableJavaName); |
128 | 0 | NodeList columnElements = tableElementInSchema.getElementsByTagName(COLUMN_ELEMENT); |
129 | 0 | for (int columnIndex = 0; columnIndex < columnElements.getLength(); columnIndex++) { |
130 | |
|
131 | 0 | Element columnElementInSchema = (Element) columnElements.item(columnIndex); |
132 | 0 | String columnName = columnElementInSchema.getAttribute(NAME_ATTR); |
133 | 0 | Element columnDescElement = null; |
134 | 0 | NodeList columnDescElements = tableDescElem.getElementsByTagName(COLUMN_ELEMENT); |
135 | 0 | for (int columnDescIndex = 0; columnDescIndex < columnDescElements.getLength(); columnDescIndex++) { |
136 | 0 | Element element = (Element) columnDescElements.item(columnDescIndex); |
137 | 0 | if (columnName.equals(element.getAttribute(NAME_ATTR))) { |
138 | 0 | columnDescElement = element; |
139 | 0 | break; |
140 | |
} |
141 | |
} |
142 | 0 | if (columnDescElement != null) { |
143 | 0 | String columnDescription = columnDescElement.getAttribute(DESCRIPTION_ATTR); |
144 | 0 | String columnJavaName = columnDescElement.getAttribute(JAVA_NAME_ATTR); |
145 | 0 | columnElementInSchema.setAttribute(DESCRIPTION_ATTR, columnDescription); |
146 | 0 | columnElementInSchema.setAttribute(JAVA_NAME_ATTR, columnJavaName); |
147 | |
} |
148 | |
} |
149 | |
} |
150 | |
} |
151 | 0 | return dbSchemaDoc; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void writeXMLToFile(Document newXMLDocument) throws Exception { |
161 | 0 | File dbSchema = this.getDbSchema(); |
162 | 0 | TransformerFactory tFactory = TransformerFactory.newInstance(); |
163 | |
|
164 | 0 | Transformer transformer = tFactory.newTransformer(); |
165 | 0 | transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "database.dtd"); |
166 | 0 | DOMSource domSource = new DOMSource(newXMLDocument); |
167 | 0 | StringWriter writer = new StringWriter(); |
168 | 0 | Result result = new StreamResult(writer); |
169 | 0 | transformer.transform(domSource, result); |
170 | 0 | FileWriter fileWriter = new FileWriter(dbSchema); |
171 | |
|
172 | 0 | if (dbSchema.exists()) { |
173 | 0 | StringBuffer bufferedWriter = new StringBuffer(writer.toString()); |
174 | 0 | fileWriter.write(bufferedWriter.toString()); |
175 | 0 | fileWriter.close(); |
176 | 0 | System.out.println("The data has been written"); |
177 | 0 | } else { |
178 | 0 | System.out.println("This file is not exist"); |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
public void execute() throws BuildException { |
184 | 0 | setDbSchema(dbSchemaString); |
185 | 0 | setSchemaWithDesc(schemaWithDescString); |
186 | 0 | if (!schemaWithDesc.exists()) { |
187 | 0 | System.out.println("no schema file with description can be located"); |
188 | 0 | return; |
189 | |
} |
190 | |
try { |
191 | 0 | mergeSchemas(schemaWithDesc, dbSchema); |
192 | 0 | writeXMLToFile(dbSchemaDoc); |
193 | 0 | } catch (Exception e) { |
194 | 0 | throw new BuildException(e); |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | |
} |