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.*;
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("Inputlog");
45 XmlInputParser xmlInputParser = new XmlInputParser();
47 /* Commented out for now since it seems unused and need more
48 * info regarding it. Marc Heckmann <heckmann@hbe.ca>
49 MirConfig.initConfig("config"); */
50 //parse the xml-files in the given directory
51 xmlInputParser.parse(args[0]);
52 // stop freemarker templateCache (cracy)
53 HTMLTemplateProcessor.stopAutoUpdate();
59 * this method parses the xml-file an
60 * returns 0 if succesful
61 * returns -1 if failed
63 private boolean loadXml (String fileName) {
65 XMLReader reader = new SAXDriver();
66 InputSource is = new InputSource(new FileInputStream(fileName));
67 reader.setContentHandler(new XmlHandler());
69 } catch (IOException ex) {
70 logger.printError(ex.toString());
72 } catch (SAXException ex) {
73 logger.printError(ex.toString());
80 * Reads all XML-Files in the given Directory
81 * and returns a String[] with the filenames
85 public String[] readDir (String dir) {
86 File file = new File(dir);
87 String[] fileNames = file.list(new XmlFilenameFilter());
92 * parses the XML-Files in the given Directory
95 public void parse (String dir) {
96 File goodDir = new File(dir + "/GOOD");
97 File badDir = new File(dir + "/BAD");
98 boolean result = false;
100 String[] fileNames = readDir(dir);
101 for (int i = 0; i < fileNames.length; i++) {
103 result = loadXml(dir + "/" + fileNames[i]);
104 if (result == true) { //if succesfully parsed
105 HashMap hash = XmlHandler.returnHash();
106 HashMap val = (HashMap)hash.get("values");
107 //set the default user
108 val.put("to_publisher", "5");
109 String table = (String)hash.get("table");
110 AbstractModule moduleInstance = null;
112 Class databaseClass = Class.forName("mir.storage.Database" +
114 Method m = databaseClass.getMethod("getInstance", null);
115 Database databaseInstance = (Database)m.invoke(null, null);
116 moduleInstance = (AbstractModule)Class.forName("mir.module.Module"
117 + table).newInstance();
118 //AbstractModule moduleInstance = new ModuleContent(databaseInstance);
119 moduleInstance.setStorage(databaseInstance);
120 } catch (Exception e) {
121 //logger.printError(e.toString());
124 result = insert(val, moduleInstance);
126 if (result == false) { //if error
127 File file = new File(dir + "/" + fileNames[i]);
128 if (!badDir.exists()) { //exits Bad-Dir?
131 if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
132 logger.printError("Failed move to BAD: " + fileNames[i]);
135 else { //end if(result == false)
136 File file = new File(dir + "/" + fileNames[i]);
137 if (!goodDir.exists()) { //exists Good-Dir?
140 if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
141 logger.printError("Failed move to GOOD: " + fileNames[i]);
143 logger.printInfo("Successfully parsed: " + fileNames[i]);
144 } //end else (result == true)
149 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
150 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
153 public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
154 ArrayList theFieldList;
156 theFieldList = theStorage.getFields();
157 } catch (StorageObjectException e) {
158 logger.printError("Failed: " + e.toString());
161 HashMap withValues = new HashMap();
162 String aField, aValue;
163 for (int i = 0; i < theFieldList.size(); i++) {
164 aField = (String)theFieldList.get(i);
165 aValue = (String)values.get(aField);
167 withValues.put(aField, aValue);
173 * Inserts a hash with values in a table
178 public boolean insert (HashMap values, AbstractModule module) {
180 HashMap withValues = getIntersectingValues(values, module.getStorageObject());
181 module.add(withValues);
182 } catch (Exception e) {
183 logger.printError("Failed to insert: " + e.toString());