KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > util > ReadXML


1 /*
2  * ReadXML is part of the Cofax content management sytem library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other
19  * related informaion.
20  *
21  * $Header: /cvsroot/cofax/cofax/src/org/cofax/util/ReadXML.java,v 1.4.2.1 2006/12/11 16:30:02 fxrobin Exp $
22  */

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 JavaDoc;
32
33 import javax.xml.parsers.*;
34
35 /**
36  * An XML feed-import utility.
37  *
38  * @author Rajiv Pant
39  * @author Karl Martino
40  * @author Sam Cohen
41  * @author FX Robin
42  *
43  * @version 1.5.1
44  *
45  */

46
47 public class ReadXML {
48
49     public static void main(String JavaDoc args[]) {
50
51         // Print out a worthless version number
52
System.out.println("Cofax v1.0 - XML to SQL");
53
54         // Get the shared config file to run from
55
String JavaDoc configLocation = args[0];
56         XMLConfig configFile = new XMLConfig();
57         configFile.setXMLFileName(configLocation);
58         if (!configFile.load()) {
59             System.err.println(configFile.getLastError());
60         }
61         // Load variables from configFile
62
String JavaDoc dataStoreName = configFile.getString("dataStoreName");
63         String JavaDoc dataStore = configFile.getString("dataStoreClass");
64         String JavaDoc databaseIdentifier = configFile.getString("databaseIdentifier");
65         String JavaDoc dbFailPrefix = configFile.getString("databaseFailureId");
66         String JavaDoc xmlFailPrefix = configFile.getString("xmlFailureId");
67         String JavaDoc 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         // Get and check directory
83
String JavaDoc 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 JavaDoc e) {
102             System.out.println("\nERROR: Security error accessing: " + directoryPath);
103             System.exit(1);
104         }
105
106         // Get list of files in the directory to work on
107
String JavaDoc files[] = Directory.list();
108
109         // create and initialize cofax database class
110
DataStore db = null;
111         try {
112             db = (DataStore) (Class.forName(dataStore)).newInstance();
113             db.init(dbProps);
114             db.setDataStoreName(dataStoreName);
115         } catch (ClassNotFoundException JavaDoc e) {
116             System.out.println("ERROR: Data store class not found.: " + e);
117             return;
118         } catch (InstantiationException JavaDoc e) {
119             System.out.println("ERROR loading data store class.: " + e);
120             return;
121         } catch (IllegalAccessException JavaDoc 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             // Loop thru list of xml files and do the work!
140
for (int x = 0; x < files.length; x++) {
141
142                 if (files[x].endsWith(".xml")) {
143
144                     // Re-initialize data containers
145
xmlData.clear();
146                     mappings.clear();
147                     relatedLinks.clear();
148
149                     System.out.println("\nPARSING: " + files[x]);
150
151                     String JavaDoc xmlFileType = loadFromXML(directoryPath + files[x], xmlData, mappings, relatedLinks);
152
153                     File xmlFile = new File(directoryPath, files[x]);
154
155                     // If we have a proper xmlFile...
156
if (!xmlFileType.equals("")) {
157
158                         System.out.println("INSERTING: " + files[x]);
159
160                         // Based upon xmlType, execute
161
// different insert commands
162
boolean success = false;
163                         // Connect to database
164

165                         if (db.connect()) {
166                             try {
167                                 success = db.insertArticle(xmlData, databaseIdentifier, xmlFileType, mappings, relatedLinks);
168                             } catch (Exception JavaDoc 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 insert was a success then delete xmlfile
181
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 JavaDoc se) {
190                                 se.printStackTrace();
191                             }
192
193                             insertedCount++;
194                             System.out.println("SUCCEESS: Inserted Article: " + insertedCount);
195
196                         } else { // ERROR: database failure
197
System.out.println("\nERROR: " + db.getLastError());
198                             try {
199                                 xmlFile.renameTo(new File(directoryPath, dbFailPrefix + files[x]));
200                             } catch (SecurityException JavaDoc se) {
201                                 se.printStackTrace();
202                             }
203                         }
204                     } else { // ERROR BAD XML file
205
try {
206                             xmlFile.renameTo(new File(directoryPath, xmlFailPrefix + files[x]));
207                         } catch (SecurityException JavaDoc se) {
208                             se.printStackTrace();
209                         }
210                     } // if we have an xmlFileName....
211

212                 } // if the file is an xml file....
213

214             } // end for loop
215

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 JavaDoc e) {
224             e.printStackTrace();
225         }
226
227     } // public void main(String args[0])
228

229     static String JavaDoc loadFromXML(String JavaDoc xmlFileName, HashMap xmlData, ArrayList mappings, ArrayList relatedLinks) {
230
231         // Trys to parse the XML file. We use a validating XML parser.
232
// That means that XML files are validated against a DTD
233
// if mentioned and must be correct XMLwise.
234
// Any invalid character data, for example, will trigger
235
// an exception during parse.
236

237         /*
238          * FX ROBIN : old parsing, changing into JAXP DOMParser parser = new
239          * DOMParser() ; try { parser.parse("file:///" + xmlFileName) ; }
240          * catch(Exception e) { System.out.println("\n ERROR: parse error on: " +
241          * "file:///" + xmlFileName) ; System.out.println(e.toString()) ; return
242          * ""; }
243          */

244
245         /* New parsing */
246
247         /* FX ROBIN : modifying de DOM parsing with JAXP standard */
248         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
249         DocumentBuilder parser;
250         String JavaDoc fileToParse = "file:///" + xmlFileName;
251         try {
252             parser = factory.newDocumentBuilder(); // an XML parser object
253
} catch (Exception JavaDoc 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; // the document representation
260

261         try {
262             doc = parser.parse(new InputSource JavaDoc(fileToParse));
263         } catch (Exception JavaDoc e) {
264             System.out.println("ERROR in XMLConfig: parse error on: " + fileToParse + "\n" + "EXCEPTION: " + e.toString());
265             return null;
266         }
267
268         // parsed document is in doc variable
269

270         // Gets the root element name of the document,
271
// Used to determine which import process to use...
272
String JavaDoc 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 JavaDoc tagName = "";
301             String JavaDoc tagValue = "";
302
303             tagName = node.getParentNode().getNodeName() + ":" + node.getNodeName();
304
305             // Get the element node's children
306
NodeList children = node.getChildNodes();
307
308             // If there are children of the node, continue....
309
if (children != null && children.getLength() > 0) {
310
311                 // If the first child of this node is a TEXT_NODE,
312
// then get the value.
313
// Put the name and value in the xmlData HashMap
314
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                 // Process the other children of the node....
327
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 JavaDoc 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 JavaDoc tagName = "";
365                     String JavaDoc tagValue = "";
366
367                     tagName = fieldNode.getParentNode().getNodeName() + ":" + fieldNode.getNodeName();
368
369                     // Get the element node's children
370
NodeList children = fieldNode.getChildNodes();
371
372                     // If there are children of the node, continue....
373
if (children != null && children.getLength() > 0) {
374
375                         // If the first child is a text node, then get the
376
// value.
377
// Put the element name and value in the hashtable
378
// for processing...
379
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                         } // if (children.item(0).getNodeType() ==
390
// Node.TEXT_NODE)
391

392                     } // if (children != null && children.getLength() > 0)
393

394                 } // if (type == Node.ELEMENT_NODE)
395

396             } // for (int forTwo = 0; forTwo < mapRowNodeCount; forTwo++)
397

398             if (!map.isEmpty()) {
399                 mappings.add(map);
400             }
401
402         } // for (int forOne = 0; forOne < countOfMaps; forOne++)
403

404     }
405
406 } // end class ReadXML2
407

408
Popular Tags