f27902d3e48bda5558bcda90962b36547839eec0
[mir.git] / source / mircoders / input / XmlInputParser.java
1 /**
2  * Title:        Indy
3  * Description:  Parses Xml-Files into the Database
4  * Copyright:    Copyright (c) 2001
5  * Company:      indymedia.de
6  * @author idfx
7  * @version 1.0
8  *
9  * formatted with JxBeauty (c) johann.langhofer@nextra.at
10  */
11
12
13 package mircoders.input;
14
15 import  java.io.*;
16 import  java.util.*;
17 import  java.lang.reflect.*;
18 import  webdb.misc.*;
19 import  webdb.storage.*;
20 import  webdb.module.*;
21 import  mir.storage.*;
22 import  mir.module.*;
23 import  org.xml.sax.*;
24 import  org.xml.sax.helpers.*;
25 import  com.icl.saxon.aelfred.*;
26
27
28 /**
29  * put your documentation comment here
30  */
31 public class XmlInputParser {
32   static Logfile logger;
33
34   /**
35    * the main-method
36    * a DirectoryName should be given
37    */
38   public static void main (String[] args) {
39     //logging
40     File logDir = new File(args[0] + "LOG");
41     if (!logDir.exists()) {
42       logDir.mkdir();
43     }
44     logger = Logfile.getInstance(args[0] + "LOG/xml.log");
45     XmlInputParser xmlInputParser = new XmlInputParser();
46     //get the config-file
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();
52     //stop it
53     System.exit(0);
54   }
55
56   /**
57    * this method parses the xml-file an
58    * returns 0 if succesful
59    * returns -1 if failed
60    */
61   private boolean loadXml (String fileName) {
62     try {
63       XMLReader reader = new SAXDriver();
64       InputSource is = new InputSource(new FileInputStream(fileName));
65       reader.setContentHandler(new XmlHandler());
66       reader.parse(is);
67     } catch (IOException ex) {
68       logger.printError(ex.toString());
69       return  false;
70     } catch (SAXException ex) {
71       logger.printError(ex.toString());
72       return  false;
73     }
74     return  true;
75   }
76
77   /**
78    * Reads all XML-Files in the given Directory
79    * and returns a String[] with the filenames
80    * @param dir
81    * @return
82    */
83   public String[] readDir (String dir) {
84     File file = new File(dir);
85     String[] fileNames = file.list(new XmlFilenameFilter());
86     return  fileNames;
87   }
88
89   /**
90    * parses the XML-Files in the given Directory
91    * @param dir
92    */
93   public void parse (String dir) {
94     File goodDir = new File(dir + "/GOOD");
95     File badDir = new File(dir + "/BAD");
96     boolean result = false;
97     //read the directory
98     String[] fileNames = readDir(dir);
99     for (int i = 0; i < fileNames.length; i++) {
100       //parse every file
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;
109         try {
110           Class databaseClass = Class.forName("mir.storage.Database" +
111               table);
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());
120           result = false;
121         }
122         result = insert(val, moduleInstance);
123       }
124       if (result == false) {                    //if error
125         File file = new File(dir + "/" + fileNames[i]);
126         if (!badDir.exists()) {                 //exits Bad-Dir?
127           badDir.mkdir();
128         }
129         if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
130           logger.printError("Failed move to BAD: " + fileNames[i]);
131         }
132       }
133       else {                    //end if(result == false)
134         File file = new File(dir + "/" + fileNames[i]);
135         if (!goodDir.exists()) {                //exists Good-Dir?
136           goodDir.mkdir();
137         }
138         if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
139           logger.printError("Failed move to GOOD: " + fileNames[i]);
140         }
141         logger.printInfo("Successfully parsed: " + fileNames[i]);
142       }         //end else (result == true)
143     }           //end for
144   }             //end parse
145
146   /**
147    *   Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
148    *   im HttpRequest vorkommen und liefert sie als HashMap zurueck
149    *   @return HashMap
150    */
151   public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
152     ArrayList theFieldList;
153     try {
154       theFieldList = theStorage.getFields();
155     } catch (StorageObjectException e) {
156       logger.printError("Failed: " + e.toString());
157       return  null;
158     }
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);
164       if (aValue != null)
165         withValues.put(aField, aValue);
166     }
167     return  withValues;
168   }
169
170   /**
171    * Inserts a hash with values in a table
172    * @param values
173    * @param module
174    * @return
175    */
176   public boolean insert (HashMap values, AbstractModule module) {
177     try {
178       HashMap withValues = getIntersectingValues(values, module.getStorageObject());
179       module.add(withValues);
180     } catch (Exception e) {
181       logger.printError("Failed to insert: " + e.toString());
182       return  false;
183     }
184     return  true;
185   }
186 }
187
188
189