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