1 23 24 package org.cofax.util; 25 26 import java.io.*; 27 import java.util.*; 28 import org.cofax.*; 29 30 import org.w3c.dom.*; 31 import org.xml.sax.InputSource ; 32 33 import javax.xml.parsers.*; 34 35 46 47 public class ReadXML { 48 49 public static void main(String args[]) { 50 51 System.out.println("Cofax v1.0 - XML to SQL"); 53 54 String configLocation = args[0]; 56 XMLConfig configFile = new XMLConfig(); 57 configFile.setXMLFileName(configLocation); 58 if (!configFile.load()) { 59 System.err.println(configFile.getLastError()); 60 } 61 String dataStoreName = configFile.getString("dataStoreName"); 63 String dataStore = configFile.getString("dataStoreClass"); 64 String databaseIdentifier = configFile.getString("databaseIdentifier"); 65 String dbFailPrefix = configFile.getString("databaseFailureId"); 66 String xmlFailPrefix = configFile.getString("xmlFailureId"); 67 String xmlDelete = configFile.getString("xmlDelete"); 68 69 Properties dbProps = new Properties(); 70 71 dbProps.setProperty("drivers", configFile.getString("dataStoreDriver")); 72 dbProps.setProperty("logfile", configFile.getString("dataStoreLogFile")); 73 dbProps.setProperty(dataStoreName + ".url", configFile.getString("dataStoreUrl")); 74 dbProps.setProperty(dataStoreName + ".user", configFile.getString("dataStoreUser")); 75 dbProps.setProperty(dataStoreName + ".password", configFile.getString("dataStorePassword")); 76 dbProps.setProperty(dataStoreName + ".initconns", configFile.getString("dataStoreInitconns")); 77 dbProps.setProperty(dataStoreName + ".maxconns", configFile.getString("dataStoreMaxconns")); 78 dbProps.setProperty(dataStoreName + ".connusagelimit", configFile.getString("dataStoreConnusagelimit")); 79 dbProps.setProperty(dataStoreName + ".testquery", configFile.getString("dataStoreTestquery")); 80 dbProps.setProperty(dataStoreName + ".loglevel", configFile.getString("dataStoreLoglevel")); 81 82 String directoryPath = ""; 84 if (args.length == 2) { 85 directoryPath = args[1]; 86 } 87 88 if (directoryPath.equals("")) { 89 directoryPath = System.getProperty("user.dir"); 90 } 91 92 File Directory = new File(directoryPath); 93 94 try { 95 if (Directory.isDirectory()) { 96 System.out.println("Running on: " + directoryPath); 97 } else { 98 System.out.println("ERROR: Invalid directory: " + directoryPath); 99 System.exit(1); 100 } 101 } catch (SecurityException e) { 102 System.out.println("\nERROR: Security error accessing: " + directoryPath); 103 System.exit(1); 104 } 105 106 String files[] = Directory.list(); 108 109 DataStore db = null; 111 try { 112 db = (DataStore) (Class.forName(dataStore)).newInstance(); 113 db.init(dbProps); 114 db.setDataStoreName(dataStoreName); 115 } catch (ClassNotFoundException e) { 116 System.out.println("ERROR: Data store class not found.: " + e); 117 return; 118 } catch (InstantiationException e) { 119 System.out.println("ERROR loading data store class.: " + e); 120 return; 121 } catch (IllegalAccessException e) { 122 System.out.println("Error loading data store class.: " + e); 123 return; 124 } 125 126 try { 127 128 System.out.println("Total Number of files: " + files.length); 129 System.out.print("Connecting to SQL database..."); 130 131 HashMap xmlData = new HashMap(); 132 ArrayList mappings = new ArrayList(); 133 ArrayList relatedLinks = new ArrayList(); 134 int insertedCount = 0; 135 136 System.out.println("Connected."); 137 System.out.println(" Parsing XML files and inserting " + " into database ..."); 138 139 for (int x = 0; x < files.length; x++) { 141 142 if (files[x].endsWith(".xml")) { 143 144 xmlData.clear(); 146 mappings.clear(); 147 relatedLinks.clear(); 148 149 System.out.println("\nPARSING: " + files[x]); 150 151 String xmlFileType = loadFromXML(directoryPath + files[x], xmlData, mappings, relatedLinks); 152 153 File xmlFile = new File(directoryPath, files[x]); 154 155 if (!xmlFileType.equals("")) { 157 158 System.out.println("INSERTING: " + files[x]); 159 160 boolean success = false; 163 165 if (db.connect()) { 166 try { 167 success = db.insertArticle(xmlData, databaseIdentifier, xmlFileType, mappings, relatedLinks); 168 } catch (Exception e) { 169 } finally { 170 db.disConnect(); 171 } 172 } else { 173 174 System.out.println("\nERROR: Could not get a connection." + db.getLastError()); 175 continue; 176 177 } 178 179 System.out.println("Delete =" + xmlDelete); 180 if (success) { 182 try { 183 184 if (xmlDelete.equals("on")) { 185 xmlFile.delete(); 186 System.out.println("DELETED NUMBER: " + x + " FILE: " + files[x]); 187 } 188 189 } catch (SecurityException se) { 190 se.printStackTrace(); 191 } 192 193 insertedCount++; 194 System.out.println("SUCCEESS: Inserted Article: " + insertedCount); 195 196 } else { System.out.println("\nERROR: " + db.getLastError()); 198 try { 199 xmlFile.renameTo(new File(directoryPath, dbFailPrefix + files[x])); 200 } catch (SecurityException se) { 201 se.printStackTrace(); 202 } 203 } 204 } else { try { 206 xmlFile.renameTo(new File(directoryPath, xmlFailPrefix + files[x])); 207 } catch (SecurityException se) { 208 se.printStackTrace(); 209 } 210 } 212 } 214 } 216 System.out.print(" Done\n"); 217 System.out.print("Number of new articles inserted: " + insertedCount + "\n"); 218 System.out.print("Closing Database connection..."); 219 db.disConnect(); 220 db.destroy(); 221 System.out.print(" Done\n"); 222 223 } catch (Exception e) { 224 e.printStackTrace(); 225 } 226 227 } 229 static String loadFromXML(String xmlFileName, HashMap xmlData, ArrayList mappings, ArrayList relatedLinks) { 230 231 237 244 245 246 247 248 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 249 DocumentBuilder parser; 250 String fileToParse = "file:///" + xmlFileName; 251 try { 252 parser = factory.newDocumentBuilder(); } catch (Exception e) { 254 System.out.println("ERROR in XMLConfig: parse error on: " + fileToParse + "\n" + "EXCEPTION: " + e.toString()); 255 return null; 256 } 257 258 int i; 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 |