2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
33 * Description: Parses Xml-Files into the Database
34 * Copyright: Copyright (c) 2001
35 * Company: indymedia.de
39 * formatted with JxBeauty (c) johann.langhofer@nextra.at
44 package mircoders.input;
48 import java.lang.reflect.*;
55 import org.xml.sax.helpers.*;
56 import com.icl.saxon.aelfred.*;
60 * put your documentation comment here
62 public class XmlInputParser {
63 static Logfile logger;
67 * a DirectoryName should be given
69 public static void main (String[] args) {
71 File logDir = new File(args[0] + "LOG");
72 if (!logDir.exists()) {
75 logger = Logfile.getInstance(args[0] + "LOG/xml.log");
76 XmlInputParser xmlInputParser = new XmlInputParser();
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();
90 * this method parses the xml-file an
91 * returns 0 if succesful
92 * returns -1 if failed
94 private boolean loadXml (String fileName) {
96 XMLReader reader = new SAXDriver();
97 InputSource is = new InputSource(new FileInputStream(fileName));
98 reader.setContentHandler(new XmlHandler());
100 } catch (IOException ex) {
101 logger.printError(ex.toString());
103 } catch (SAXException ex) {
104 logger.printError(ex.toString());
111 * Reads all XML-Files in the given Directory
112 * and returns a String[] with the filenames
116 public String[] readDir (String dir) {
117 File file = new File(dir);
118 String[] fileNames = file.list(new XmlFilenameFilter());
123 * parses the XML-Files in the given Directory
126 public void parse (String dir) {
127 File goodDir = new File(dir + "/GOOD");
128 File badDir = new File(dir + "/BAD");
129 boolean result = false;
131 String[] fileNames = readDir(dir);
132 for (int i = 0; i < fileNames.length; i++) {
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;
143 Class databaseClass = Class.forName("mir.storage.Database" +
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());
155 result = insert(val, moduleInstance);
157 if (result == false) { //if error
158 File file = new File(dir + "/" + fileNames[i]);
159 if (!badDir.exists()) { //exits Bad-Dir?
162 if (!file.renameTo(new File(dir + "/BAD/" + fileNames[i]))) {
163 logger.printError("Failed move to BAD: " + fileNames[i]);
166 else { //end if(result == false)
167 File file = new File(dir + "/" + fileNames[i]);
168 if (!goodDir.exists()) { //exists Good-Dir?
171 if (!file.renameTo(new File(dir + "/GOOD/" + fileNames[i]))) {
172 logger.printError("Failed move to GOOD: " + fileNames[i]);
174 logger.printInfo("Successfully parsed: " + fileNames[i]);
175 } //end else (result == true)
180 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
181 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
184 public HashMap getIntersectingValues (HashMap values, StorageObject theStorage) {
185 ArrayList theFieldList;
187 theFieldList = theStorage.getFields();
188 } catch (StorageObjectException e) {
189 logger.printError("Failed: " + e.toString());
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);
198 withValues.put(aField, aValue);
204 * Inserts a hash with values in a table
209 public boolean insert (HashMap values, AbstractModule module) {
211 HashMap withValues = getIntersectingValues(values, module.getStorageObject());
212 module.add(withValues);
213 } catch (Exception e) {
214 logger.printError("Failed to insert: " + e.toString());