View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db.darrt.ant; 22 23 24 // Java imports 25 import java.io.File; 26 import java.io.FileWriter; 27 import java.util.Iterator; 28 import java.util.Map; 29 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 33 import org.apache.axis.utils.DOM2Writer; 34 import org.apache.log4j.Logger; 35 import org.apache.tools.ant.BuildException; 36 import org.enableit.db.darrt.DataHandler; 37 import org.w3c.dom.Document; 38 import org.w3c.dom.Element; 39 import org.w3c.dom.Node; 40 41 42 /*** 43 * @author Tim Stephenson 44 */ 45 public class DataHandlerTask extends AbstractSchemaTask { 46 /*** 47 * Root element for the data document. 48 */ 49 private static final String EL_DATA = "data"; 50 51 /*** 52 * The Log4J <code>Logger</code> doing the logging. 53 */ 54 private static Logger logger = Logger.getLogger(DataHandlerTask.class); 55 56 /*** 57 * CVS info ABOUT this class and its current version 58 */ 59 public static final String ABOUT = "$Id: DataHandlerTask.java,v 1.8 2004/03/20 05:22:18 tim Exp $"; 60 private String outputFile; 61 62 /* 63 * Properties 64 */ 65 private String tableNamePattern; 66 private String outputDir; 67 68 // Constructors --------------------------------------------------------------- 69 70 /*** 71 * Default Constructor 72 */ 73 public DataHandlerTask() { 74 } 75 76 /* 77 * Methods 78 */ 79 80 /*** 81 * Sets the target (output) directory. 82 * @deprecated Replaced with seperate attributes for dir and file. 83 */ 84 public void setOutput(String output) { 85 setOutputDir(output); 86 } 87 88 /*** 89 * Sets the target (output) directory. 90 */ 91 public void setOutputDir(String output) { 92 this.outputDir = output; 93 } 94 95 /*** 96 * Sets the target (output) file. 97 */ 98 public void setOutputFile(String output) { 99 this.outputFile = output; 100 } 101 102 /*** 103 * Set the table name pattern. 104 */ 105 public void setTableNamePattern(String tableNamePattern) { 106 this.tableNamePattern = tableNamePattern; 107 } 108 109 /*** 110 * Run the schema export. 111 */ 112 public void execute() { 113 logger.info("METHOD_ENTRY: execute"); 114 115 try { 116 log("Exporting data..."); 117 log("\t... from schema " + schemaName); 118 119 DataHandler dh = new DataHandler(); 120 121 dh.setOnlineRefSchema(getReferenceProvider()); 122 dh.setRefSchemaName(schemaName); 123 dh.setTablePattern(tableNamePattern); 124 125 Map rowDocs = dh.export(); 126 127 // Construct single DOM from all document data 128 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 129 DocumentBuilder db = dbf.newDocumentBuilder(); 130 Document doc = db.newDocument(); 131 Element root = doc.createElement(EL_DATA); 132 133 // write the per table files 134 if ((outputDir != null) && (outputDir.trim().length() > 0)) { 135 logger.warn("Table data dir: " + outputDir); 136 137 for (Iterator i = rowDocs.keySet().iterator(); i.hasNext();) { 138 String key = (String) i.next(); 139 140 File outputDirFile = new File(outputDir); 141 142 if (!outputDirFile.exists()) { 143 outputDirFile.mkdirs(); 144 } 145 146 FileWriter out = new FileWriter(new File(outputDir, 147 key + ".xml")); 148 149 // false = do not omit xml decl, true = use pretty print 150 DOM2Writer.serializeAsXML((Document) rowDocs.get(key), out, 151 false, true); 152 out.close(); 153 154 Node nodeToImport = ((Document) rowDocs.get(key)) 155 .getDocumentElement(); 156 Node importedNode = doc.importNode(nodeToImport, true); 157 158 root.appendChild(importedNode); 159 } 160 } 161 162 doc.appendChild(root); 163 164 // Write single data file 165 if ((outputFile != null) && (outputFile.trim().length() > 0)) { 166 FileWriter out2 = new FileWriter(new File(outputFile)); 167 168 // false = do not omit xml decl, true = use pretty print 169 DOM2Writer.serializeAsXML(doc, out2, false, true); 170 out2.close(); 171 } 172 } catch (Exception e) { 173 throw new BuildException(e.getMessage(), e); 174 } 175 176 logger.info("METHOD_EXIT: execute"); 177 } 178 }

This page was automatically generated by Maven