/************************************************************************** /* This class implements a manager for DocumentFactories. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* This library is free software; you can redistribute it and/or modify /* it under the terms of the GNU Lesser General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This library is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Lesser General Public License for more details. /* /* You should have received a copy of the GNU Lesser General Public License /* along with this library; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.lib; import java.io.*; import java.util.*; import de.bablokb.luala.lib.*; /** This class implements a manager for DocumentFactories. @version $Revision: 1.4 $ @author $Author: bablokb $ */ public class DocumentFactoryManager { //////////////////////////////////////////////////////////////////////////// /** The hashtable of available factories. */ private Hashtable iFactoryTable; //////////////////////////////////////////////////////////////////////////// /** Default-Constructor. */ public DocumentFactoryManager() { iFactoryTable = new Hashtable(); initFactoryTable(); } //////////////////////////////////////////////////////////////////////////// /** Initialize the factory hashtable.

TODO: read info from a properties-file. */ private void initFactoryTable() { iFactoryTable. put("_unknown", new Entry("_unknown","de.bablokb.luala.lib.StandardDocumentFactory")); iFactoryTable. put("txt",new Entry("txt","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("java",new Entry("java","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("c",new Entry("c","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("html",new Entry("html","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("htm",new Entry("htm","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("xml",new Entry("xml","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("jsp",new Entry("jsp","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("ps",new Entry("ps","de.bablokb.luala.lib.TextDocumentFactory")); iFactoryTable. put("pdf",new Entry("pdf","de.bablokb.luala.lib.PDFDocumentFactory")); } //////////////////////////////////////////////////////////////////////////// /** Return the factory for a given file. @param name The name of the document @throws FactoryException if the factory-class is not found or can't be accessed */ public DocumentFactory getFactory(String name) throws FactoryException { int lastDot = name.lastIndexOf('.'); String extension = null; Entry entry = null; if (lastDot != -1 && lastDot < name.length()) { extension = name.substring(lastDot+1).toLowerCase(); entry = (Entry) iFactoryTable.get(extension); } if (entry == null) entry = guessFactory(name); if (entry != null) return entry.getFactory(); else return null; } //////////////////////////////////////////////////////////////////////////// /** Guess the correct factory. Under unix, try to use the external file-command. @param name Name of the file. */ private Entry guessFactory(String name) { int lastDot = name.lastIndexOf('.'); if (lastDot == -1 || lastDot == name.length()) return (Entry) iFactoryTable.get("txt"); String lowerName = name.toLowerCase(); if (lowerName.startsWith("makefile") || lowerName.startsWith("copying")) return (Entry) iFactoryTable.get("txt"); return (Entry) iFactoryTable.get("_unknown"); } //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /** An entry in the factory-hashtable. */ class Entry { String iExtension, iClassName; DocumentFactory iFactory; Entry(String extension, String className) { iExtension = extension; iClassName = className; iFactory = null; } DocumentFactory getFactory() throws FactoryException { try { if (iFactory == null) iFactory = (DocumentFactory) Class.forName(iClassName).newInstance(); return iFactory; } catch (Exception e) { throw new FactoryException(e); } } } }