| 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.TransformerConfigurationException; |
| 15 | |
import javax.xml.transform.TransformerException; |
| 16 | |
import javax.xml.transform.TransformerFactory; |
| 17 | |
import javax.xml.transform.dom.DOMSource; |
| 18 | |
import javax.xml.transform.stream.StreamResult; |
| 19 | |
|
| 20 | |
import org.apache.tools.ant.BuildException; |
| 21 | |
import org.apache.tools.ant.Task; |
| 22 | |
import org.w3c.dom.DOMImplementation; |
| 23 | |
import org.w3c.dom.Document; |
| 24 | |
import org.w3c.dom.Element; |
| 25 | |
import org.w3c.dom.Node; |
| 26 | |
import org.w3c.dom.NodeList; |
| 27 | |
import org.xml.sax.SAXException; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class TorqueXMLWithDesc extends Task { |
| 35 | |
File outputFile; |
| 36 | |
File inputFile; |
| 37 | |
String inputFileString; |
| 38 | |
String outputFileString; |
| 39 | |
|
| 40 | |
public void setOutputFileString(String outputFileString) { |
| 41 | 0 | this.outputFileString = outputFileString; |
| 42 | 0 | outputFile = new File(outputFileString); |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
public void setInputFileString(String inputFileString) { |
| 46 | 0 | this.inputFileString = inputFileString; |
| 47 | 0 | inputFile = new File(inputFileString); |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public Document createXMLWithDescription() throws ParserConfigurationException, SAXException, IOException { |
| 59 | 0 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 60 | 0 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 61 | 0 | Document inputDocument = documentBuilder.parse(inputFile); |
| 62 | 0 | DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); |
| 63 | 0 | inputDocument.getDocumentElement().normalize(); |
| 64 | 0 | Document outputDocument = domImplementation.createDocument(null, null, null); |
| 65 | 0 | Element outputRootElement = outputDocument.createElement("database"); |
| 66 | 0 | outputDocument.appendChild(outputRootElement); |
| 67 | 0 | NodeList listOfTableElementsInInput = inputDocument.getElementsByTagName("table"); |
| 68 | |
|
| 69 | 0 | for (int i = 0; i < listOfTableElementsInInput.getLength(); i++) { |
| 70 | 0 | Node currentTableNode = listOfTableElementsInInput.item(i); |
| 71 | 0 | if ((currentTableNode.getNodeType() == Node.ELEMENT_NODE)) { |
| 72 | 0 | Element currentInputTableElement = (Element) currentTableNode; |
| 73 | 0 | Element currentOutputTableElement = outputDocument.createElement("table"); |
| 74 | 0 | System.out.println(currentTableNode.getAttributes().toString()); |
| 75 | 0 | currentOutputTableElement.setAttribute("name", currentInputTableElement.getAttribute("name")); |
| 76 | 0 | currentOutputTableElement.setAttribute("description", ""); |
| 77 | 0 | currentOutputTableElement.setAttribute("javaName", ""); |
| 78 | 0 | NodeList listOfChildNodesInCurrentTableNode = currentTableNode.getChildNodes(); |
| 79 | 0 | for (int j = 0; j < listOfChildNodesInCurrentTableNode.getLength(); j++) { |
| 80 | 0 | Node currentChildNode = listOfChildNodesInCurrentTableNode.item(j); |
| 81 | 0 | if (currentChildNode.getNodeName().equals("column")) { |
| 82 | 0 | if (currentChildNode.getNodeType() == Node.ELEMENT_NODE) { |
| 83 | 0 | Element currentInputColumnElement = (Element) currentChildNode; |
| 84 | 0 | Element currentOutputColumnElement = outputDocument.createElement("column"); |
| 85 | 0 | currentOutputColumnElement.setAttribute("name", |
| 86 | |
currentInputColumnElement.getAttribute("name")); |
| 87 | 0 | currentOutputColumnElement.setAttribute("description", ""); |
| 88 | 0 | currentOutputColumnElement.setAttribute("javaName", ""); |
| 89 | 0 | currentOutputTableElement.appendChild(currentOutputColumnElement); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
} |
| 93 | 0 | outputRootElement.appendChild(currentOutputTableElement); |
| 94 | |
} |
| 95 | |
} |
| 96 | 0 | return outputDocument; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void writeXMLToFile(Document newXMLDocument) { |
| 105 | |
|
| 106 | 0 | TransformerFactory tFactory = TransformerFactory.newInstance(); |
| 107 | |
try { |
| 108 | 0 | Transformer transformer = tFactory.newTransformer(); |
| 109 | 0 | transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "database.dtd"); |
| 110 | 0 | DOMSource domSource = new DOMSource(newXMLDocument); |
| 111 | 0 | StringWriter writer = new StringWriter(); |
| 112 | 0 | Result result = new StreamResult(writer); |
| 113 | 0 | transformer.transform(domSource, result); |
| 114 | 0 | FileWriter fileWriter = new FileWriter(outputFile); |
| 115 | |
|
| 116 | 0 | if (outputFile.exists()) { |
| 117 | 0 | StringBuffer bufferedWriter = new StringBuffer(writer.toString()); |
| 118 | 0 | fileWriter.write(bufferedWriter.toString()); |
| 119 | 0 | fileWriter.close(); |
| 120 | 0 | System.out.println("The data has been written"); |
| 121 | 0 | } else { |
| 122 | 0 | System.out.println("This file is not exist"); |
| 123 | |
} |
| 124 | |
|
| 125 | 0 | } catch (TransformerConfigurationException e) { |
| 126 | |
|
| 127 | 0 | e.printStackTrace(); |
| 128 | 0 | } catch (TransformerException e) { |
| 129 | |
|
| 130 | 0 | e.printStackTrace(); |
| 131 | 0 | } catch (IOException e) { |
| 132 | |
|
| 133 | 0 | e.printStackTrace(); |
| 134 | 0 | } |
| 135 | |
|
| 136 | 0 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
@Override |
| 142 | |
public void execute() throws BuildException { |
| 143 | |
try { |
| 144 | 0 | writeXMLToFile(createXMLWithDescription()); |
| 145 | 0 | } catch (ParserConfigurationException e) { |
| 146 | |
|
| 147 | 0 | e.printStackTrace(); |
| 148 | 0 | } catch (SAXException e) { |
| 149 | |
|
| 150 | 0 | e.printStackTrace(); |
| 151 | 0 | } catch (IOException e) { |
| 152 | |
|
| 153 | 0 | e.printStackTrace(); |
| 154 | 0 | } |
| 155 | 0 | } |
| 156 | |
} |