1 16 17 package org.webdocwf.util.loader.generator; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.io.File ; 26 import java.io.*; 27 import java.util.*; 28 import org.apache.xml.serialize.OutputFormat; 29 import org.apache.xml.serialize.XMLSerializer; 30 import org.apache.xml.serialize.Method; 31 32 import org.w3c.dom.Document ; 33 34 import org.w3c.dom.Element ; 35 36 import org.webdocwf.util.loader.LoaderException; 37 import org.webdocwf.util.loader.logging.Logger; 38 import org.webdocwf.util.loader.logging.StandardLogger; 39 40 46 public class LoadJobWriter { 47 48 private Document document; 49 private Logger logger; 50 51 56 57 public LoadJobWriter(InputParameters generatorParameters) throws LoaderException { 58 59 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 60 try { 61 setLogger(); 62 this.logger.write("normal", "Generating of LoaderJob.olj file is started."); 63 loadJobWriter(generatorParameters, factory); 64 this.logger.write("normal", "Generating of LoaderJob.olj file is finished."); 65 } catch (ParserConfigurationException pce) { 66 String msg = "Parser with specified options can't be built! Exception has occurred when parser is" + "try to parse the LoaderJob.olj file!"; 68 LoaderException le = new LoaderException(msg + "\n" + pce.getMessage(), (Throwable ) pce); 69 throw le; 70 } 71 } 72 79 private void loadJobWriter(InputParameters generatorParameters, DocumentBuilderFactory factory) throws ParserConfigurationException , LoaderException { 80 81 this.logger.write("normal", "\tloadJobWriter method is started."); 82 DocumentBuilder builder = factory.newDocumentBuilder(); 83 document = builder.newDocument(); 85 Element root = (Element ) document.createElement("loaderJob"); 87 document.appendChild(root); 88 89 LoaderJobAttributes loaderJob = new LoaderJobAttributes(); 90 91 root.setAttribute("logMode", loaderJob.getLogMode()); 92 root.setAttribute("objectIDIncrement", loaderJob.getObjectIDIncrement()); 93 root.setAttribute("objectIDTableName", loaderJob.getObjectIDTableName()); 94 root.setAttribute("objectIDColumnName", loaderJob.getObjectIDColumnName()); 95 96 Element childRoot = (Element ) document.createElement("jdbcDefaultParameters"); 98 root.appendChild(childRoot); 99 100 Element childRoot2 = (Element ) document.createElement("jdbcSourceParameters"); 102 childRoot.appendChild(childRoot2); 103 104 JdbcParameters sourceJdbc = new JdbcParameters("source", generatorParameters); 105 106 childRoot2.setAttribute("dbVendor", sourceJdbc.getDbVendor()); 107 childRoot2.setAttribute("driverName", sourceJdbc.getDriverName()); 108 109 Hashtable sourceTable = sourceJdbc.getAllParameters(); 111 Enumeration enumSource = sourceTable.keys(); 112 while (enumSource.hasMoreElements()) { 113 Element childRoot3 = (Element ) document.createElement("jdbcSourceParameter"); 114 childRoot2.appendChild(childRoot3); 115 String key = (String ) enumSource.nextElement(); 116 childRoot3.setAttribute("name", key); 117 childRoot3.setAttribute("value", (String ) sourceTable.get(key)); 118 } 119 120 Element childRoot5 = (Element ) document.createElement("jdbcTargetParameters"); 122 childRoot.appendChild(childRoot5); 123 124 JdbcParameters targetJdbc = new JdbcParameters("target", generatorParameters); 125 126 129 childRoot5.setAttribute("dbVendor", targetJdbc.getDbVendor()); 130 childRoot5.setAttribute("driverName", targetJdbc.getDriverName()); 131 132 Hashtable targetTable = targetJdbc.getAllParameters(); 134 Enumeration enumTarget = targetTable.keys(); 135 while (enumTarget.hasMoreElements()) { 136 Element childRoot6 = (Element ) document.createElement("jdbcTargetParameter"); 137 childRoot5.appendChild(childRoot6); 138 String key = (String ) enumTarget.nextElement(); 139 childRoot6.setAttribute("name", key); 140 childRoot6.setAttribute("value", (String ) targetTable.get(key)); 141 } 142 143 Vector sqlStatement = generatorParameters.getSqlToGenerate(); 144 int sqlFileType = 0; 145 while (sqlFileType < 6) { 146 if (sqlFileType == 3) { 148 Element childRoot20 = (Element ) document.createElement("definitionInclude"); 150 root.appendChild(childRoot20); 151 Element childRoot21 = (Element ) document.createElement("include"); 153 childRoot20.appendChild(childRoot21); 154 155 IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes(); 156 157 String pathXml = includeTagAttributes.getHref(); 159 pathXml.trim(); 160 childRoot21.setAttribute("href", pathXml); 161 childRoot21.setAttribute("parse", includeTagAttributes.getParse()); 162 } 163 if (sqlFileType == 4 && generatorParameters.getAlterTablePrimaryKey().equalsIgnoreCase("false")) { 164 } else { 166 if (generatorParameters.getRestoreMode().equalsIgnoreCase("false")) { 167 if (sqlStatement.get(sqlFileType) != null) { 168 Element childRoot10 = (Element ) document.createElement("sql"); 169 root.appendChild(childRoot10); 170 SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType); 171 childRoot10.setAttribute("name", sqlTagAttributes.getName()); 172 childRoot10.setAttribute("commit", sqlTagAttributes.getCommit()); 174 childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue()); 176 177 Element childRoot11 = (Element ) document.createElement("sqlStmt"); 179 childRoot10.appendChild(childRoot11); 180 181 Element childRoot12 = (Element ) document.createElement("include"); 183 childRoot11.appendChild(childRoot12); 184 185 String pathSql = sqlTagAttributes.getHref(); 187 pathSql.trim(); 188 childRoot12.setAttribute("href", pathSql); 189 childRoot12.setAttribute("parse", sqlTagAttributes.getParse()); 190 } 192 } else if (generatorParameters.getRestoreMode().equalsIgnoreCase("true")) { 193 Element childRoot10 = (Element ) document.createElement("sql"); 195 root.appendChild(childRoot10); 196 SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType); 197 childRoot10.setAttribute("name", sqlTagAttributes.getName()); 198 childRoot10.setAttribute("commit", sqlTagAttributes.getCommit()); 200 childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue()); 202 Element childRoot11 = (Element ) document.createElement("sqlStmt"); 204 childRoot10.appendChild(childRoot11); 205 206 Element childRoot12 = (Element ) document.createElement("include"); 208 childRoot11.appendChild(childRoot12); 209 String pathSql = sqlTagAttributes.getHref(); 211 pathSql.trim(); 212 childRoot12.setAttribute("href", pathSql); 213 childRoot12.setAttribute("parse", sqlTagAttributes.getParse()); 214 } 215 } 216 sqlFileType++; 217 } 218 if (generatorParameters.getGenerateXml().equalsIgnoreCase("true")) { 219 try { 220 File file = null; 221 String slash = null; 222 if (generatorParameters.getGeneratorOutput().equalsIgnoreCase("")) { 223 file = new File (""); 224 slash = ""; 225 } else { 226 file = new File (generatorParameters.getGeneratorOutput()); 227 slash = "/"; 228 } 229 if (!file.exists()) 230 file.mkdirs(); 231 232 FileOutputStream os = new FileOutputStream(file + slash + "LoaderJob.olj"); 233 OutputFormat of = new OutputFormat(); of.setIndenting(true); 235 of.setIndent(4); 236 of.setMethod(Method.XML); 237 of.setPreserveSpace(false); 238 of.setLineWidth(0); 239 XMLSerializer out = new XMLSerializer(os, of); 240 241 out.serialize(document); 243 } catch (Exception e) { 244 String msg = "Exception has occurred when we try to set output format for LoaderJob.olj file."; 245 LoaderException le = new LoaderException(msg + "\n" + e.getMessage(), (Throwable ) e); 247 this.logger.write("full", "Exception has occurred when we try to set output format for LoaderJob.olj file." + "\n" + le.getStackTraceAsString()); 248 throw le; 250 } 251 } 252 this.logger.write("normal", "\tloadJobWriter method is finished."); 253 } 254 255 259 private void setLogger() { 260 this.logger = StandardLogger.getCentralLogger(); 261 } 262 263 } | Popular Tags |