001 /* PAVLOV -- Multiple Choice Study System 002 * Copyright (C) 2000 - 2004 T.J. Willis 003 * 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License 006 * as published by the Free Software Foundation; either version 2 007 * of the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. 017 * 018 * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/library/LibraryLoader.java,v 1.7 2004/06/24 11:41:56 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.library; 021 import org.xml.sax.*; 022 import org.xml.sax.helpers.*; 023 import java.io.*; 024 import net.sourceforge.pavlov.zipUtilities.XMLFileFilter; 025 import java.util.*; 026 import org.apache.log4j.*; 027 028 029 /** 030 * Utility class to load all the books in a given directory to a Library. 031 * @author <a href="mailto:tj_willis@users.sourceforge.net"></a> 032 * @version 1.0 033 * @see Library 034 * @see LibraryXMLHandler 035 * @has 1 Has 1 net.sourceforge.pavlov.library.BookXMLHandler 036 */ 037 public class LibraryLoader 038 { 039 040 /** 041 * Loads all the book files in the directory specified by the String 042 * fname into a library and returns the resulting library. 043 * 044 * @param fname a <code>String</code> value 045 * @return a <code>Library</code> value 046 */ 047 public static Library loadLibrary(String fname) 048 { 049 File dir = new File(fname); 050 if(dir==null) return null; 051 if(!dir.exists()) return null; 052 if(!dir.isDirectory()) return null; 053 if(!dir.canRead()) return null; 054 055 //System.out.println("Got the directory"); 056 /* 057 XMLReader xr = null; 058 try 059 { 060 xr = XMLReaderFactory.createXMLReader(); 061 } catch (Exception e) { 062 return null; 063 } 064 */ 065 XMLFileFilter filt = new XMLFileFilter(); 066 File filearr[] = dir.listFiles(filt); 067 List v = Arrays.asList(filearr); 068 Iterator it = v.iterator(); 069 070 Library lib = new Library(); 071 lib.setDirectory(dir); 072 // Create SAX 2 parser... 073 074 //BookXMLHandler br = new BookXMLHandler(); 075 076 // Set the ContentHandler... 077 //xr.setContentHandler(br); 078 079 while (it.hasNext()) { 080 File cand = (File) it.next(); 081 Book b = null; 082 083 try { 084 /* 085 FileReader fr = new FileReader(cand); 086 //FileInputStream frs = new FileInputStream(cand); 087 InputSource is = new InputSource(fr); 088 is.setEncoding("UTF8"); 089 xr.parse(is); 090 b = br.getBook(); 091 */ 092 b = Book.loadFrom(cand); 093 lib.addBook(b); 094 //System.out.println("Added Book : " + b.getName()); 095 } catch (Exception e) { 096 e.printStackTrace(); 097 } 098 } 099 return lib; 100 } 101 }