Mir goes GPL
[mir.git] / source / mircoders / input / XmlInputParser.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31 /**
32  * Title:        Indy
33  * Description:  Parses Xml-Files into the Database
34  * Copyright:    Copyright (c) 2001
35  * Company:      indymedia.de
36  * @author idfx
37  * @version 1.0
38  *
39  * formatted with JxBeauty (c) johann.langhofer@nextra.at
40  */
41
42
43
44 package mircoders.input;
45
46 import  java.io.*;
47 import  java.util.*;
48 import  java.lang.reflect.*;
49 import  mir.misc.*;
50 import  mir.storage.*;
51 import  mir.module.*;
52 import  mir.storage.*;
53 import  mir.module.*;
54 import  org.xml.sax.*;
55 import  org.xml.sax.helpers.*;
56 import  com.icl.saxon.aelfred.*;
57
58
59 /**
60  * put your documentation comment here
61  */
62 public class XmlInputParser {
63   static Logfile logger;
64
65   /**
66    * the main-method
67    * a DirectoryName should be given
68    */
69   public static void main (String[] args) {
70     //logging
71     File logDir = new File(args[0] + "LOG");
72     if (!logDir.exists()) {
73       logDir.mkdir();
74     }
75     logger = Logfile.getInstance(args[0] + "LOG/xml.log");
76     XmlInputParser xmlInputParser = new XmlInputParser();
77     //get the config-file
78     /* Commented out for now since it seems unused and need more
79      * info regarding it. Marc Heckmann <heckmann@hbe.ca>
80     MirConfig.initConfig("config"); */
81     //parse the xml-files in the given directory
82     xmlInputParser.parse(args[0]);
83     // stop freemarker templateCache (cracy)
84     HTMLTemplateProcessor.stopAutoUpdate();
85     //stop it
86     System.exit(0);
87   }
88
89   /**
90    * this method parses the xml-file an
91    * returns 0 if succesful
92    * returns -1 if failed
93    */
94   private boolean loadXml (String fileName) {
95     try {
96       XMLReader reader = new SAXDriver();
97       InputSource is = new InputSource(new FileInputStream(fileName));
98       reader.setContentHandler(new XmlHandler());
99       reader.parse(is);
100     } catch (IOException ex) {
101       logger.printError(ex.toString());
102       return  false;
103     } catch (SAXException ex) {
104       logger.printError(ex.toString());
105       return  false;
106     }
107     return  true;
108   }
109
110   /**
111    * Reads all XML-Files in the given Directory
112    * and returns a String[] with the filenames
113    * @param dir
114    * @return
115    */
116   public String[] readDir (String dir) {
117     File file = new File(dir);
118     String[] fileNames = file.list(new XmlFilenameFilter());
119     return  fileNames;
120   }
121
122   /**
123    * parses the XML-Files in the given Directory
124    * @param dir
125    */
126   public void parse (String dir) {
127     File goodDir = new File(dir + "/GOOD");
128     File badDir = new File(dir + "/BAD");
129     boolean result = false;
130     //read the directory
131     String[] fileNames = readDir(dir);
132     for (int i = 0; i < fileNames.length; i++) {
133       //parse every file
134       result = loadXml(dir + "/" + fileNames[i]);
135       if (result == true) {                     //if succesfully parsed
136         HashMap hash = XmlHandler.returnHash();
137         HashMap val = (HashMap)hash.get("values");
138         //set the default user
139         val.put("to_publisher", "5");
140         String table = (String)hash.get("table");
141         AbstractModule moduleInstance = null;
142         try {
143           Class databaseClass = Class.forName("mir.storage.Database" +
144               table);
145           Method m = databaseClass.getMethod("getInstance", null);
146           Database databaseInstance = (Database)m.invoke(null, null);
147           moduleInstance = (AbstractModule)Class.forName("mir.module.Module"
148               + table).newInstance();
149           //AbstractModule moduleInstance = new ModuleContent(databaseInstance);
150           moduleInstance.setStorage(databaseInstance);
151         } catch (Exception e) {
152           //logger.printError(e.toString());
153           result = false;
154         }
155         result = insert(val, moduleInstance);
156       }
157       if (result == false) {                    //if error
158         File file = new File(dir + "/" + fileNames[i]);
159         if (!badDir.exists()) {                 //exits Bad-Dir?
160           badDir.mkdir();
161         }
162         if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
163           logger.printError("Failed move to BAD: " + fileNames[i]);
164         }
165       }
166       else {                    //end if(result == false)
167         File file = new File(dir + "/" + fileNames[i]);
168         if (!goodDir.exists()) {                //exists Good-Dir?
169           goodDir.mkdir();
170         }
171         if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
172           logger.printError("Failed move to GOOD: " + fileNames[i]);
173         }
174         logger.printInfo("Successfully parsed: " + fileNames[i]);
175       }         //end else (result == true)
176     }           //end for
177   }             //end parse
178
179   /**
180    *   Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
181    *   im HttpRequest vorkommen und liefert sie als HashMap zurueck
182    *   @return HashMap
183    */
184   public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
185     ArrayList theFieldList;
186     try {
187       theFieldList = theStorage.getFields();
188     } catch (StorageObjectException e) {
189       logger.printError("Failed: " + e.toString());
190       return  null;
191     }
192     HashMap withValues = new HashMap();
193     String aField, aValue;
194     for (int i = 0; i < theFieldList.size(); i++) {
195       aField = (String)theFieldList.get(i);
196       aValue = (String)values.get(aField);
197       if (aValue != null)
198         withValues.put(aField, aValue);
199     }
200     return  withValues;
201   }
202
203   /**
204    * Inserts a hash with values in a table
205    * @param values
206    * @param module
207    * @return
208    */
209   public boolean insert (HashMap values, AbstractModule module) {
210     try {
211       HashMap withValues = getIntersectingValues(values, module.getStorageObject());
212       module.add(withValues);
213     } catch (Exception e) {
214       logger.printError("Failed to insert: " + e.toString());
215       return  false;
216     }
217     return  true;
218   }
219 }
220
221
222