001    package org.apache.torque.task;
002    
003    import java.io.File;
004    import java.io.FileWriter;
005    import java.io.IOException;
006    import java.io.StringWriter;
007    
008    import javax.xml.parsers.DocumentBuilder;
009    import javax.xml.parsers.DocumentBuilderFactory;
010    import javax.xml.parsers.ParserConfigurationException;
011    import javax.xml.transform.OutputKeys;
012    import javax.xml.transform.Result;
013    import javax.xml.transform.Transformer;
014    import javax.xml.transform.TransformerConfigurationException;
015    import javax.xml.transform.TransformerException;
016    import javax.xml.transform.TransformerFactory;
017    import javax.xml.transform.dom.DOMSource;
018    import javax.xml.transform.stream.StreamResult;
019    
020    import org.apache.tools.ant.BuildException;
021    import org.apache.tools.ant.Task;
022    import org.w3c.dom.DOMImplementation;
023    import org.w3c.dom.Document;
024    import org.w3c.dom.Element;
025    import org.w3c.dom.Node;
026    import org.w3c.dom.NodeList;
027    import org.xml.sax.SAXException;
028    
029    /**
030     * This class is task used to generate xml file with database information 
031     * and its description.
032     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
033     */
034    public class TorqueXMLWithDesc extends Task {
035            File outputFile;
036            File inputFile;
037            String inputFileString;
038            String outputFileString;
039            public void setOutputFileString(String outputFileString) {
040                    this.outputFileString = outputFileString;
041                    outputFile = new File(outputFileString);
042            }
043            public void setInputFileString(String inputFileString){
044                    this.inputFileString = inputFileString;
045                    inputFile = new File(inputFileString);
046            }
047            
048            /**
049             * Returns a document object with table and 
050             * column name from the input file and a blank description      
051             * @return Document document object with table/column names and blank description added
052             * @throws ParserConfigurationException
053             * @throws SAXException
054             * @throws IOException
055             */
056            public Document createXMLWithDescription()
057                            throws ParserConfigurationException, SAXException, IOException {
058                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
059                                    .newInstance();
060                    DocumentBuilder documentBuilder = documentBuilderFactory
061                                    .newDocumentBuilder();
062                    Document inputDocument = documentBuilder.parse(inputFile);
063                    DOMImplementation domImplementation = documentBuilder
064                                    .getDOMImplementation();
065                    inputDocument.getDocumentElement().normalize();
066                    Document outputDocument = domImplementation.createDocument(null, null,
067                                    null);
068                    Element outputRootElement = outputDocument.createElement("database");
069                    outputDocument.appendChild(outputRootElement);
070                    NodeList listOfTableElementsInInput = inputDocument
071                                    .getElementsByTagName("table");
072    
073                    for (int i = 0; i < listOfTableElementsInInput.getLength(); i++) {
074                            Node currentTableNode = listOfTableElementsInInput.item(i);
075                            if ((currentTableNode.getNodeType() == Node.ELEMENT_NODE)) {
076                                    Element currentInputTableElement = (Element) currentTableNode;
077                                    Element currentOutputTableElement = outputDocument
078                                                    .createElement("table");
079                                    System.out.println(currentTableNode.getAttributes().toString());
080                                    currentOutputTableElement.setAttribute("name",
081                                                    currentInputTableElement.getAttribute("name"));
082                                    currentOutputTableElement.setAttribute("description", "");
083                                    currentOutputTableElement.setAttribute("javaName", "");
084                                    NodeList listOfChildNodesInCurrentTableNode = currentTableNode
085                                                    .getChildNodes();
086                                    for (int j = 0; j < listOfChildNodesInCurrentTableNode
087                                                    .getLength(); j++) {
088                                            Node currentChildNode = listOfChildNodesInCurrentTableNode
089                                                            .item(j);
090                                            if (currentChildNode.getNodeName().equals("column")) {
091                                                    if (currentChildNode.getNodeType() == Node.ELEMENT_NODE) {
092                                                            Element currentInputColumnElement = (Element) currentChildNode;
093                                                            Element currentOutputColumnElement = outputDocument
094                                                                            .createElement("column");
095                                                            currentOutputColumnElement.setAttribute("name",
096                                                                            currentInputColumnElement
097                                                                                            .getAttribute("name"));
098                                                            currentOutputColumnElement.setAttribute(
099                                                                            "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    }