1 23 24 package org.cofax.util; 25 26 import java.io.*; 27 import java.util.*; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 32 import org.cofax.*; 33 34 import org.w3c.dom.*; 35 import org.xml.sax.InputSource ; 36 37 48 49 public class XMLFeeder { 50 51 public static void main(String args[]) { 52 53 System.out.println("Cofax v1.0 - XML to SQL"); 55 56 String configLocation = args[0]; 58 XMLConfig configFile = new XMLConfig(); 59 configFile.setXMLFileName(configLocation); 60 if (!configFile.load()) { 61 System.err.println(configFile.getLastError()); 62 } 63 String dataStoreName = configFile.getString("dataStoreName"); 65 String dataStore = configFile.getString("dataStoreClass"); 66 String databaseIdentifier = configFile.getString("databaseIdentifier"); 67 String dbFailPrefix = configFile.getString("databaseFailureId"); 68 String xmlFailPrefix = configFile.getString("xmlFailureId"); 69 String xmlDelete = configFile.getString("xmlDelete"); 70 71 Properties dbProps = new Properties(); 72 73 dbProps.setProperty("drivers", configFile.getString("dataStoreDriver")); 74 dbProps.setProperty("logfile", configFile.getString("dataStoreLogFile")); 75 dbProps.setProperty(dataStoreName + ".url", configFile.getString("dataStoreUrl")); 76 dbProps.setProperty(dataStoreName + ".user", configFile.getString("dataStoreUser")); 77 dbProps.setProperty(dataStoreName + ".password", configFile.getString("dataStorePassword")); 78 dbProps.setProperty(dataStoreName + ".initconns", configFile.getString("dataStoreInitconns")); 79 dbProps.setProperty(dataStoreName + ".maxconns", configFile.getString("dataStoreMaxconns")); 80 dbProps.setProperty(dataStoreName + ".connusagelimit", configFile.getString("dataStoreConnusagelimit")); 81 dbProps.setProperty(dataStoreName + ".testquery", configFile.getString("dataStoreTestquery")); 82 dbProps.setProperty(dataStoreName + ".loglevel", configFile.getString("dataStoreLoglevel")); 83 84 String directoryPath = ""; 86 if (args.length == 2) { 87 directoryPath = args[1]; 88 } 89 90 if (directoryPath.equals("")) { 91 directoryPath = System.getProperty("user.dir"); 92 } 93 94 File Directory = new File(directoryPath); 95 96 try { 97 if (Directory.isDirectory()) { 98 System.out.println("Running on: " + directoryPath); 99 } else { 100 System.out.println("ERROR: Invalid directory: " + directoryPath); 101 System.exit(1); 102 } 103 } catch (SecurityException e) { 104 System.out.println("\nERROR: Security error accessing: " + directoryPath); 105 System.exit(1); 106 } 107 108 String files[] = Directory.list(); 110 111 DataStore db = null; 113 try { 114 db = (DataStore) (Class.forName(dataStore)).newInstance(); 115 db.init(dbProps); 116 db.setDataStoreName(dataStoreName); 117 } catch (ClassNotFoundException e) { 118 System.out.println("ERROR: Data store class not found.: " + e); 119 return; 120 } catch (InstantiationException e) { 121 System.out.println("ERROR loading data store class.: " + e); 122 return; 123 } catch (IllegalAccessException e) { 124 System.out.println("Error loading data store class.: " + e); 125 return; 126 } 127 128 try { 129 130 System.out.println("Total Number of files: " + files.length); 131 System.out.print("Connecting to SQL database..."); 132 133 HashMap xmlData = new HashMap(); 134 ArrayList mappings = new ArrayList(); 135 ArrayList relatedLinks = new ArrayList(); 136 int insertedCount = 0; 137 138 System.out.println("Connected."); 139 System.out.println(" Parsing XML files and inserting " + " into database ..."); 140 141 for (int x = 0; x < files.length; x++) { 143 144 if (files[x].endsWith(".xml")) { 145 146 xmlData.clear(); 148 mappings.clear(); 149 relatedLinks.clear(); 150 151 System.out.println("\nPARSING: " + files[x]); 152 153 String xmlFileType = loadFromXML(directoryPath + files[x], xmlData, mappings, relatedLinks); 154 155 File xmlFile = new File(directoryPath, files[x]); 156 157 if (!xmlFileType.equals("")) { 159 160 System.out.println("INSERTING: " + files[x]); 161 162 boolean success = false; 165 167 if (db.connect()) { 168 try { 169 success = db.insertArticle(xmlData, databaseIdentifier, xmlFileType, mappings, relatedLinks); 170 } catch (Exception e) { 171 } finally { 172 db.disConnect(); 173 } 174 } else { 175 176 System.out.println("\nERROR: Could not get a connection." + db.getLastError()); 177 continue; 178 179 } 180 181 System.out.println("Delete =" + xmlDelete); 182 if (success) { 184 try { 185 186 if (xmlDelete.equals("on")) { 187 xmlFile.delete(); 188 System.out.println("DELETED NUMBER: " + x + " FILE: " + files[x]); 189 } 190 191 } catch (SecurityException se) { 192 se.printStackTrace(); 193 } 194 195 insertedCount++; 196 System.out.println("SUCCEESS: Inserted Article: " + insertedCount); 197 198 } else { System.out.println("\nERROR: " + db.getLastError()); 200 try { 201 xmlFile.renameTo(new File(directoryPath, dbFailPrefix + files[x])); 202 } catch (SecurityException se) { 203 se.printStackTrace(); 204 } 205 } 206 } else { try { 208 xmlFile.renameTo(new File(directoryPath, xmlFailPrefix + files[x])); 209 } catch (SecurityException se) { 210 se.printStackTrace(); 211 } 212 } 214 } 216 } 218 System.out.print(" Done\n"); 219 System.out.print("Number of new articles inserted: " + insertedCount + "\n"); 220 System.out.print("Closing Database connection..."); 221 db.disConnect(); 222 db.destroy(); 223 System.out.print(" Done\n"); 224 225 } catch (Exception e) { 226 e.printStackTrace(); 227 } 228 229 } 231 static String loadFromXML(String xmlFileName, HashMap xmlData, ArrayList mappings, ArrayList relatedLinks) { 232 233 239 245 246 247 248 249 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 250 DocumentBuilder parser; 251 String fileToParse = "file:///" + xmlFileName; 252 try { 253 parser = factory.newDocumentBuilder(); } catch (Exception e) { 255 System.out.println("ERROR in XMLConfig: parse error on: " + fileToParse + "\n" + "EXCEPTION: " + e.toString()); 256 return null; 257 } 258 259 Document doc; 261 try { 262 doc = parser.parse(new InputSource (fileToParse)); 263 } catch (Exception e) { 264 System.out.println("ERROR in XMLConfig: parse error on: " + fileToParse + "\n" + "EXCEPTION: " + e.toString()); 265 return null; 266 } 267 268 270 String xmlFileType = ""; 273 Element root = doc.getDocumentElement(); 274 xmlFileType = root.getTagName(); 275 276 processDOMTree(doc, xmlData); 277 278 if (xmlFileType.equals("article")) { 279 getSubTagCodes(doc, mappings, "map"); 280 getSubTagCodes(doc, relatedLinks, "link"); 281 } 282 283 return xmlFileType; 284 285 } 286 287 static void processDOMTree(Node node, HashMap xmlData) { 288 289 int type = node.getNodeType(); 290 291 switch (type) { 292 293 case Node.DOCUMENT_NODE: { 294 processDOMTree(((Document) node).getDocumentElement(), xmlData); 295 break; 296 } 297 298 case Node.ELEMENT_NODE: { 299 300 String tagName = ""; 301 String tagValue = ""; 302 303 tagName = node.getParentNode().getNodeName() + ":" + node.getNodeName(); 304 305 NodeList children = node.getChildNodes(); 307 308 if (children != null && children.getLength() > 0) { 310 311 if (children.item(0).getNodeType() == Node.TEXT_NODE) { 315 316 tagValue = children.item(0).getNodeValue() + ""; 317 318 if (tagValue != null && !tagValue.equals("") && !tagName.equals("")) { 319 xmlData.put(tagName, tagValue); 320 } else { 321 xmlData.put(tagName, ""); 322 } 323 324 } 325 326 int len = children.getLength(); 328 for (int i = 0; i < len; i++) { 329 processDOMTree(children.item(i), xmlData); 330 ; 331 } 332 333 break; 334 335 } 336 337 } 338 339 case Node.TEXT_NODE: { 340 break; 341 } 342 343 } 344 345 } 346 347 static void getSubTagCodes(Document doc, ArrayList mappings, String subTagName) { 348 349 NodeList mapList = doc.getElementsByTagName(subTagName); 350 351 for (int forOne = 0; forOne < mapList.getLength(); forOne++) { 352 353 Node mapNode = mapList.item(forOne); 354 NodeList mapRow = mapNode.getChildNodes(); 355 HashMap map = new HashMap(); 356 map.clear(); 357 358 for (int forTwo = 0; forTwo < mapRow.getLength(); forTwo++) { 359 360 Node fieldNode = mapRow.item(forTwo); 361 int type = fieldNode.getNodeType(); 362 if (type == Node.ELEMENT_NODE) { 363 364 String tagName = ""; 365 String tagValue = ""; 366 367 tagName = fieldNode.getParentNode().getNodeName() + ":" + fieldNode.getNodeName(); 368 369 NodeList children = fieldNode.getChildNodes(); 371 372 if (children != null && children.getLength() > 0) { 374 375 if (children.item(0).getNodeType() == Node.TEXT_NODE) { 380 381 tagValue = children.item(0).getNodeValue() + ""; 382 383 if (tagValue != null && !tagValue.equals("") && !tagName.equals("")) { 384 map.put(tagName, tagValue); 385 } else { 386 map.put(tagName, ""); 387 } 388 389 } 392 } 394 } 396 } 398 if (!map.isEmpty()) { 399 mappings.add(map); 400 } 401 402 } 404 } 405 406 } 408 | Popular Tags |