View Javadoc

1   /*
2    * Created on Feb 5, 2005
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package net.sf.gumshoe.indexer;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.lang.reflect.Constructor;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.logging.Logger;
17  
18  
19  import org.apache.lucene.document.Document;
20  
21  /***
22   * @author Gabor
23   * 
24   * TODO To change the template for this generated type comment go to Window -
25   * Preferences - Java - Code Style - Code Templates
26   */
27  public class ContentReaderFactory {
28  
29  	protected Logger logger=null;
30  	
31  	private static ContentReaderFactory instance = null;
32  	
33  	private Map contentReaders=new HashMap();
34  	
35  	private ContentReader defaultReader;
36  
37  	public static ContentReaderFactory getInstance() {
38  		if (instance == null) {
39  			synchronized (ContentReaderFactory.class) {
40  				if (instance == null) {
41  					try {
42  						instance = new ContentReaderFactory();
43  					} catch (Exception e) {
44  						// IGNORE
45  					}
46  				}
47  			}
48  		}
49  		return instance;
50  	}
51  
52  	/***
53  	 * @throws ClassNotFoundException
54  	 * @throws IOException
55  	 * 
56  	 */
57  	private ContentReaderFactory() throws Exception {
58  		super();
59  		logger=Logger.getLogger(getClass().getName());
60  		List subclasses = FindClasses.findClassesThatExtend(ContentReader.class);
61  		Iterator iter=subclasses.iterator();
62  		while (iter.hasNext()){
63  			String className=(String)iter.next();
64  			Class c=Class.forName(className);
65  			Constructor constructor=c.getConstructor(new Class[0]);
66  			ContentReader reader=(ContentReader)constructor.newInstance(new Object[0]);
67  			logger.fine("loading "+className);
68  			if (className.endsWith("DefaultContentReader")){
69  				defaultReader=reader;
70  			} else {
71  				List extensions=reader.getSupportedExtensions();
72  				Iterator iter1=extensions.iterator();
73  				while (iter1.hasNext()){
74  					String ext=(String)iter1.next();
75  					contentReaders.put(ext, reader);
76  				}
77  			}
78  		}
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * 
84  	 * @see net.sf.scavenger.ContentReader#getDocument()
85  	 */
86  	public Document getDocument(File f) throws Exception {
87  		return getReader(f).getDocument(f);
88  	}
89  
90  	/***
91  	 * @param f file to processed
92  	 * @return reader for this file/extension
93  	 */
94  	public ContentReader getReader(File f) {
95  		ContentReader cr=(ContentReader)contentReaders.get(getExtension(f));
96  		if (cr==null){
97  			cr=defaultReader;
98  		}
99  		return cr;
100 	}
101 	
102 	/*
103 	 * Return file extension
104 	 */
105 	public static String getExtension(File f){
106 		String extension="";
107         String baseName=getBaseName(f);
108         int index = baseName.lastIndexOf('.');
109         if ((index < 1) || (index == baseName.length() - 1)){
110             extension = "";
111         } else {
112             extension = baseName.substring(index + 1);
113         }
114         return extension;
115 	}
116 	
117     /***
118      * Returns the base name of the file.
119      */
120     public static String getBaseName(File f) {
121     	String baseName="";
122     	String path=f.getAbsolutePath();
123     	int index = path.lastIndexOf(File.separatorChar);
124         if (index == -1){
125                 baseName = path;
126         } else {
127                 baseName = path.substring(index + 1);
128         }
129         return baseName;
130     }
131 }