View Javadoc

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