1 /*
2  * @(#)HSFilter.java	1.6 06/10/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistribution of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc. or the names of
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * This software is provided "AS IS," without a warranty of any
23  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
24  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
26  * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
27  * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
28  * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
29  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
30  * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
31  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
32  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
33  * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or
37  * intended for use in the design, construction, operation or
38  * maintenance of any nuclear facility.
39  */
40 
41 
42 package sunw.demo.newmerge;
43 
44 import java.io.*;
45 import javax.swing.*;
46 import javax.swing.filechooser.*;
47 import java.util.jar.JarFile;
48 import java.util.zip.ZipEntry;
49 import java.util.Enumeration;
50 
51 /**
52  * @author  Richard Gregor
53  * @version	1.6	10/30/06
54  */
55 public class HSFilter extends javax.swing.filechooser.FileFilter {
56 
accept(File f)57     public boolean accept(File f) {
58         if (f.isDirectory()) {
59             return true;
60         }
61 
62         String extension = getExtension(f);
63         if (extension != null) {
64             if (extension.equals("hs"))
65                 return true;
66             else if (extension.equals("jar"))
67                 return hasHs(f);
68         } else {
69             return false;
70         }
71 
72         return false;
73     }
74 
75     /*
76      * Get the extension of a file.
77      */
getExtension(File f)78     public String getExtension(File f) {
79         String ext = null;
80         if(f != null){
81             String s = f.getName();
82             int i = s.lastIndexOf('.');
83             if (i > 0 &&  i < s.length() - 1) {
84                 ext = s.substring(i+1).toLowerCase();
85             }
86         }
87         return ext;
88     }
89     /**
90      * Returns true if *.jar file contains *hs file
91      */
hasHs(File jarfile)92     public boolean hasHs(File jarfile){
93         try{
94             JarFile jar = new JarFile(jarfile);
95             Enumeration entries = jar.entries();
96             while(entries.hasMoreElements()){
97                 ZipEntry entry = (ZipEntry)entries.nextElement();
98                 String entryName = entry.getName();
99                 if(entryName.endsWith(".hs"))
100                     return true;
101             }
102         }catch(IOException e){
103             System.err.println(e);
104         }
105         return false;
106     }
107 
108 
109     // The description of this filter
getDescription()110     public String getDescription() {
111         return "*.hs, *.jar";
112     }
113 }
114