KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * XMLFeeder 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/XMLFeeder.java,v 1.2.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
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31
32 import org.cofax.*;
33
34 import org.w3c.dom.*;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * An XML feed-import utility.
39  *
40  * @author Rajiv Pant
41  * @author Karl Martino
42  * @author Sam Cohen
43  * @author FX Robin
44  *
45  * @version 1.5.1
46  *
47  */

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

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

214                 } // if the file is an xml file....
215

216             } // end for loop
217

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

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

239         /*
240          * Old parsing -- DOMParser parser = new DOMParser() ; try {
241          * parser.parse("file:///" + xmlFileName) ; } catch(Exception e) {
242          * System.out.println("\n ERROR: parse error on: " + "file:///" +
243          * xmlFileName) ; System.out.println(e.toString()) ; return ""; }
244          */

245
246         /* New parsing */
247
248         /* FX ROBIN : modifying de DOM parsing with JAXP standard */
249         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
250         DocumentBuilder JavaDoc parser;
251         String JavaDoc fileToParse = "file:///" + xmlFileName;
252         try {
253             parser = factory.newDocumentBuilder(); // an XML parser object
254
} catch (Exception JavaDoc e) {
255             System.out.println("ERROR in XMLConfig: parse error on: " + fileToParse + "\n" + "EXCEPTION: " + e.toString());
256             return null;
257         }
258
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