KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > io > IOFilterMan


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /**
32  * <p> Project: NightLabsBase </p>
33  * <p> Copyright: Copyright (c) 2004 </p>
34  * <p> Company: NightLabs GmbH (Germany) </p>
35  * <p> Creation Date: 30.05.2005 </p>
36  * <p> Author: Daniel Mazurek </p>
37 **/

38 package com.nightlabs.io;
39
40 import java.io.File JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * This is a Manager-Class for handling different types of IOFilters
50  */

51 public class IOFilterMan
52 {
53     /**
54      * contains all registered IOFilters
55      */

56   protected List JavaDoc ioFilters = new ArrayList JavaDoc();
57     
58   /**
59    * key: FileExtension (String)
60    * value: IOFilter Instance
61    */

62   protected Map JavaDoc fileExtension2IOFilter = new HashMap JavaDoc();
63   
64   public IOFilterMan() {
65     super();
66   }
67   
68     /**
69      * Adds / Registers an IOFilter
70      */

71   public void addIOFilter(IOFilter ioFilter)
72   {
73     if (ioFilter== null)
74       throw new NullPointerException JavaDoc("Param ioFilter must not be null!");
75
76     ioFilters.add(ioFilter);
77     fileExtension2IOFilter.put(ioFilter.getFileExtension(), ioFilter);
78   }
79     
80   /**
81    * Removes/unregisters an IOFilter
82    */

83   public void removeIOFilter(IOFilter ioFilter)
84   {
85     if (ioFilters.contains(ioFilter)) {
86       ioFilters.remove(ioFilter);
87       fileExtension2IOFilter.remove(ioFilter.getFileExtension());
88     }
89   }
90   
91   /**
92    * returns the IOFilter for the given file
93    *
94    * @param file The File
95    * @return the IOFilter for the given file
96    */

97   public IOFilter getIOFilter(File JavaDoc file)
98     {
99     if (file == null)
100       throw new IllegalArgumentException JavaDoc("Param file must not be null!");
101     
102     String JavaDoc fileExtension = getFileExtension(file);
103     if (fileExtension2IOFilter.containsKey(fileExtension))
104       return (IOFilter) fileExtension2IOFilter.get(fileExtension);
105     else
106       return null;
107   }
108   
109   /**
110    * returns the IOFilter for the given fileExtension
111    *
112    * @param fileExtension The FileExtension
113    * @return the IOFilter for the given FileExtension
114    */

115   public IOFilter getIOFilter(String JavaDoc fileExtension)
116     {
117     if (fileExtension == null)
118       throw new IllegalArgumentException JavaDoc("Param fileExtension must not be null!");
119     
120     if (fileExtension2IOFilter.containsKey(fileExtension))
121       return (IOFilter) fileExtension2IOFilter.get(fileExtension);
122     else
123       return null;
124   }
125   
126   /**
127    *
128    * @param file The File
129    * @return the FileExtention of the File
130    */

131   protected String JavaDoc getFileExtension(File JavaDoc file)
132   {
133     if (file == null)
134       throw new IllegalArgumentException JavaDoc("Param file must not be null!");
135     
136     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(file.getName());
137     int index = sb.lastIndexOf(".");
138     if (index == -1) {
139         return null;
140     }
141     return sb.substring(index+1);
142   }
143   
144   /**
145    * @return A Collection of Strings which contains all fileExtensions,
146    * for all registered IOFilters
147    */

148   public Collection JavaDoc getAvailableFileExtensions()
149   {
150     return fileExtension2IOFilter.keySet();
151   }
152   
153   /**
154    * @return String[] which contains all fileExtensions,
155    * for all registered IOFilters
156    */

157   public String JavaDoc[] getAvailableFileExtensionsAsStrings(boolean concatWildcard)
158   {
159     String JavaDoc[] fileExtensions = null;
160     int counter = 0;
161     for (Iterator JavaDoc it = fileExtension2IOFilter.keySet().iterator(); it.hasNext(); )
162     {
163         if (fileExtensions == null)
164             fileExtensions = new String JavaDoc[fileExtension2IOFilter.keySet().size()];
165         
166         String JavaDoc fileExtension = (String JavaDoc) it.next();
167         if (concatWildcard)
168             fileExtension = concatWildcard(fileExtension);
169         fileExtensions[counter] = fileExtension;
170         counter++;
171     }
172     return fileExtensions;
173   }
174   
175   public String JavaDoc[] getAvailableFileExtensionsAsStrings() {
176     return getAvailableFileExtensionsAsStrings(true);
177   }
178   
179     protected String JavaDoc concatWildcard(String JavaDoc s) {
180         return "*." + s;
181     }
182 }
183
Popular Tags