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