3 * Description: Parses Xml-Files into the Database
4 * Copyright: Copyright (c) 2001
5 * Company: indymedia.de
9 * formatted with JxBeauty (c) johann.langhofer@nextra.at
13 package mircoders.input;
17 import java.lang.reflect.*;
19 import webdb.storage.*;
20 import webdb.module.*;
24 import org.xml.sax.helpers.*;
25 import com.icl.saxon.aelfred.*;
29 * put your documentation comment here
31 public class XmlInputParser {
32 static Logfile logger;
36 * a DirectoryName should be given
38 public static void main (String[] args) {
40 File logDir = new File(args[0] + "LOG");
41 if (!logDir.exists()) {
44 logger = Logfile.getInstance(args[0] + "LOG/xml.log");
45 XmlInputParser xmlInputParser = new XmlInputParser();
47 Configuration.initConfig("config");
48 //parse the xml-files in the given directory
49 xmlInputParser.parse(args[0]);
50 // stop freemarker templateCache (cracy)
51 HTMLTemplateProcessor.stopAutoUpdate();
57 * this method parses the xml-file an
58 * returns 0 if succesful
59 * returns -1 if failed
61 private boolean loadXml (String fileName) {
63 XMLReader reader = new SAXDriver();
64 InputSource is = new InputSource(new FileInputStream(fileName));
65 reader.setContentHandler(new XmlHandler());
67 } catch (IOException ex) {
68 logger.printError(ex.toString());
70 } catch (SAXException ex) {
71 logger.printError(ex.toString());
78 * Reads all XML-Files in the given Directory
79 * and returns a String[] with the filenames
83 public String[] readDir (String dir) {
84 File file = new File(dir);
85 String[] fileNames = file.list(new XmlFilenameFilter());
90 * parses the XML-Files in the given Directory
93 public void parse (String dir) {
94 File goodDir = new File(dir + "/GOOD");
95 File badDir = new File(dir + "/BAD");
96 boolean result = false;
98 String[] fileNames = readDir(dir);
99 for (int i = 0; i < fileNames.length; i++) {
101 result = loadXml(dir + "/" + fileNames[i]);
102 if (result == true) { //if succesfully parsed
103 HashMap hash = XmlHandler.returnHash();
104 HashMap val = (HashMap)hash.get("values");
105 //set the default user
106 val.put("to_publisher", "5");
107 String table = (String)hash.get("table");
108 AbstractModule moduleInstance = null;
110 Class databaseClass = Class.forName("mir.storage.Database" +
112 Method m = databaseClass.getMethod("getInstance", null);
113 Database databaseInstance = (Database)m.invoke(null, null);
114 moduleInstance = (AbstractModule)Class.forName("mir.module.Module"
115 + table).newInstance();
116 //AbstractModule moduleInstance = new ModuleContent(databaseInstance);
117 moduleInstance.setStorage(databaseInstance);
118 } catch (Exception e) {
119 //logger.printError(e.toString());
122 result = insert(val, moduleInstance);
124 if (result == false) { //if error
125 File file = new File(dir + "/" + fileNames[i]);
126 if (!badDir.exists()) { //exits Bad-Dir?
129 if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
130 logger.printError("Failed move to BAD: " + fileNames[i]);
133 else { //end if(result == false)
134 File file = new File(dir + "/" + fileNames[i]);
135 if (!goodDir.exists()) { //exists Good-Dir?
138 if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
139 logger.printError("Failed move to GOOD: " + fileNames[i]);
141 logger.printInfo("Successfully parsed: " + fileNames[i]);
142 } //end else (result == true)
147 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
148 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
151 public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
152 ArrayList theFieldList;
154 theFieldList = theStorage.getFields();
155 } catch (StorageObjectException e) {
156 logger.printError("Failed: " + e.toString());
159 HashMap withValues = new HashMap();
160 String aField, aValue;
161 for (int i = 0; i < theFieldList.size(); i++) {
162 aField = (String)theFieldList.get(i);
163 aValue = (String)values.get(aField);
165 withValues.put(aField, aValue);
171 * Inserts a hash with values in a table
176 public boolean insert (HashMap values, AbstractModule module) {
178 HashMap withValues = getIntersectingValues(values, module.getStorageObject());
179 module.add(withValues);
180 } catch (Exception e) {
181 logger.printError("Failed to insert: " + e.toString());