first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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  mir.misc.*;
19 import  mir.storage.*;
20 import  mir.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     /* 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();
54     //stop it
55     System.exit(0);
56   }
57
58   /**
59    * this method parses the xml-file an
60    * returns 0 if succesful
61    * returns -1 if failed
62    */
63   private boolean loadXml (String fileName) {
64     try {
65       XMLReader reader = new SAXDriver();
66       InputSource is = new InputSource(new FileInputStream(fileName));
67       reader.setContentHandler(new XmlHandler());
68       reader.parse(is);
69     } catch (IOException ex) {
70       logger.printError(ex.toString());
71       return  false;
72     } catch (SAXException ex) {
73       logger.printError(ex.toString());
74       return  false;
75     }
76     return  true;
77   }
78
79   /**
80    * Reads all XML-Files in the given Directory
81    * and returns a String[] with the filenames
82    * @param dir
83    * @return
84    */
85   public String[] readDir (String dir) {
86     File file = new File(dir);
87     String[] fileNames = file.list(new XmlFilenameFilter());
88     return  fileNames;
89   }
90
91   /**
92    * parses the XML-Files in the given Directory
93    * @param dir
94    */
95   public void parse (String dir) {
96     File goodDir = new File(dir + "/GOOD");
97     File badDir = new File(dir + "/BAD");
98     boolean result = false;
99     //read the directory
100     String[] fileNames = readDir(dir);
101     for (int i = 0; i < fileNames.length; i++) {
102       //parse every file
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;
111         try {
112           Class databaseClass = Class.forName("mir.storage.Database" +
113               table);
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());
122           result = false;
123         }
124         result = insert(val, moduleInstance);
125       }
126       if (result == false) {                    //if error
127         File file = new File(dir + "/" + fileNames[i]);
128         if (!badDir.exists()) {                 //exits Bad-Dir?
129           badDir.mkdir();
130         }
131         if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
132           logger.printError("Failed move to BAD: " + fileNames[i]);
133         }
134       }
135       else {                    //end if(result == false)
136         File file = new File(dir + "/" + fileNames[i]);
137         if (!goodDir.exists()) {                //exists Good-Dir?
138           goodDir.mkdir();
139         }
140         if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
141           logger.printError("Failed move to GOOD: " + fileNames[i]);
142         }
143         logger.printInfo("Successfully parsed: " + fileNames[i]);
144       }         //end else (result == true)
145     }           //end for
146   }             //end parse
147
148   /**
149    *   Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
150    *   im HttpRequest vorkommen und liefert sie als HashMap zurueck
151    *   @return HashMap
152    */
153   public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
154     ArrayList theFieldList;
155     try {
156       theFieldList = theStorage.getFields();
157     } catch (StorageObjectException e) {
158       logger.printError("Failed: " + e.toString());
159       return  null;
160     }
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);
166       if (aValue != null)
167         withValues.put(aField, aValue);
168     }
169     return  withValues;
170   }
171
172   /**
173    * Inserts a hash with values in a table
174    * @param values
175    * @param module
176    * @return
177    */
178   public boolean insert (HashMap values, AbstractModule module) {
179     try {
180       HashMap withValues = getIntersectingValues(values, module.getStorageObject());
181       module.add(withValues);
182     } catch (Exception e) {
183       logger.printError("Failed to insert: " + e.toString());
184       return  false;
185     }
186     return  true;
187   }
188 }
189
190
191