--- /dev/null
+/**
+ * Title: Indy
+ * Description: Parses Xml-Files into the Database
+ * Copyright: Copyright (c) 2001
+ * Company: indymedia.de
+ * @author idfx
+ * @version 1.0
+ *
+ * formatted with JxBeauty (c) johann.langhofer@nextra.at
+ */
+
+
+package mircoders.input;
+
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import webdb.misc.*;
+import webdb.storage.*;
+import webdb.module.*;
+import mir.storage.*;
+import mir.module.*;
+import org.xml.sax.*;
+import org.xml.sax.helpers.*;
+import com.icl.saxon.aelfred.*;
+
+
+/**
+ * put your documentation comment here
+ */
+public class XmlInputParser {
+ static Logfile logger;
+
+ /**
+ * the main-method
+ * a DirectoryName should be given
+ */
+ public static void main (String[] args) {
+ //logging
+ File logDir = new File(args[0] + "LOG");
+ if (!logDir.exists()) {
+ logDir.mkdir();
+ }
+ logger = Logfile.getInstance(args[0] + "LOG/xml.log");
+ XmlInputParser xmlInputParser = new XmlInputParser();
+ //get the config-file
+ Configuration.initConfig("config");
+ //parse the xml-files in the given directory
+ xmlInputParser.parse(args[0]);
+ // stop freemarker templateCache (cracy)
+ HTMLTemplateProcessor.stopAutoUpdate();
+ //stop it
+ System.exit(0);
+ }
+
+ /**
+ * this method parses the xml-file an
+ * returns 0 if succesful
+ * returns -1 if failed
+ */
+ private boolean loadXml (String fileName) {
+ try {
+ XMLReader reader = new SAXDriver();
+ InputSource is = new InputSource(new FileInputStream(fileName));
+ reader.setContentHandler(new XmlHandler());
+ reader.parse(is);
+ } catch (IOException ex) {
+ logger.printError(ex.toString());
+ return false;
+ } catch (SAXException ex) {
+ logger.printError(ex.toString());
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Reads all XML-Files in the given Directory
+ * and returns a String[] with the filenames
+ * @param dir
+ * @return
+ */
+ public String[] readDir (String dir) {
+ File file = new File(dir);
+ String[] fileNames = file.list(new XmlFilenameFilter());
+ return fileNames;
+ }
+
+ /**
+ * parses the XML-Files in the given Directory
+ * @param dir
+ */
+ public void parse (String dir) {
+ File goodDir = new File(dir + "/GOOD");
+ File badDir = new File(dir + "/BAD");
+ boolean result = false;
+ //read the directory
+ String[] fileNames = readDir(dir);
+ for (int i = 0; i < fileNames.length; i++) {
+ //parse every file
+ result = loadXml(dir + "/" + fileNames[i]);
+ if (result == true) { //if succesfully parsed
+ HashMap hash = XmlHandler.returnHash();
+ HashMap val = (HashMap)hash.get("values");
+ //set the default user
+ val.put("to_publisher", "5");
+ String table = (String)hash.get("table");
+ AbstractModule moduleInstance = null;
+ try {
+ Class databaseClass = Class.forName("mir.storage.Database" +
+ table);
+ Method m = databaseClass.getMethod("getInstance", null);
+ Database databaseInstance = (Database)m.invoke(null, null);
+ moduleInstance = (AbstractModule)Class.forName("mir.module.Module"
+ + table).newInstance();
+ //AbstractModule moduleInstance = new ModuleContent(databaseInstance);
+ moduleInstance.setStorage(databaseInstance);
+ } catch (Exception e) {
+ //logger.printError(e.toString());
+ result = false;
+ }
+ result = insert(val, moduleInstance);
+ }
+ if (result == false) { //if error
+ File file = new File(dir + "/" + fileNames[i]);
+ if (!badDir.exists()) { //exits Bad-Dir?
+ badDir.mkdir();
+ }
+ if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
+ logger.printError("Failed move to BAD: " + fileNames[i]);
+ }
+ }
+ else { //end if(result == false)
+ File file = new File(dir + "/" + fileNames[i]);
+ if (!goodDir.exists()) { //exists Good-Dir?
+ goodDir.mkdir();
+ }
+ if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
+ logger.printError("Failed move to GOOD: " + fileNames[i]);
+ }
+ logger.printInfo("Successfully parsed: " + fileNames[i]);
+ } //end else (result == true)
+ } //end for
+ } //end parse
+
+ /**
+ * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
+ * im HttpRequest vorkommen und liefert sie als HashMap zurueck
+ * @return HashMap
+ */
+ public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
+ ArrayList theFieldList;
+ try {
+ theFieldList = theStorage.getFields();
+ } catch (StorageObjectException e) {
+ logger.printError("Failed: " + e.toString());
+ return null;
+ }
+ HashMap withValues = new HashMap();
+ String aField, aValue;
+ for (int i = 0; i < theFieldList.size(); i++) {
+ aField = (String)theFieldList.get(i);
+ aValue = (String)values.get(aField);
+ if (aValue != null)
+ withValues.put(aField, aValue);
+ }
+ return withValues;
+ }
+
+ /**
+ * Inserts a hash with values in a table
+ * @param values
+ * @param module
+ * @return
+ */
+ public boolean insert (HashMap values, AbstractModule module) {
+ try {
+ HashMap withValues = getIntersectingValues(values, module.getStorageObject());
+ module.add(withValues);
+ } catch (Exception e) {
+ logger.printError("Failed to insert: " + e.toString());
+ return false;
+ }
+ return true;
+ }
+}
+
+
+