1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package graphical;
25 
26 import java.io.File;
27 import java.io.FileFilter;
28 import java.util.ArrayList;
29 
30 /**
31  * Helper for directory access
32  *
33  * @author lla@openoffice.org
34  */
35 public class DirectoryHelper
36 {
37     ArrayList<String> m_aFileList = new ArrayList<String>();
38     boolean m_bRecursiveIsAllowed = true;
39 
setRecursiveIsAllowed(boolean _bValue)40     void setRecursiveIsAllowed(boolean _bValue)
41         {
42             m_bRecursiveIsAllowed = _bValue;
43         }
44 
45     /**
46      * Traverse over a given directory, and filter with a given FileFilter
47      * object and gives back the deep directory as a Object[] list, which
48      * contain a String object for every directory entry.
49      *
50      * <B>Example</B>
51      * List directory /bin, filter out all files which ends with '.prn'
52      *
53      *  FileFilter aFileFilter = new FileFilter()
54      *  {
55      *      public boolean accept( File pathname )
56      *          {
57      *              if (pathname.getName().endsWith(".prn"))
58      *              {
59      *                  return false;
60      *              }
61      *              return true;
62      *          }
63      *  };
64      *
65      * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
66      * for (int i=0;i<aList.length;i++)
67      * {
68      *     String aEntry = (String)aList[i];
69      *     System.out.println(aEntry);
70      * }
71      *
72      * @param _sDirectory
73      * @param _aFileFilter
74      * @param _bRecursiveIsAllowed
75      * @return list of directories
76      */
traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed )77     public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed )
78         {
79             DirectoryHelper a = new DirectoryHelper();
80             a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
81             a.traverse_impl(_sDirectory, _aFileFilter);
82             return a.m_aFileList.toArray();
83         }
84 
traverse( String _sDirectory, boolean _bRecursiveIsAllowed )85     public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed )
86         {
87             DirectoryHelper a = new DirectoryHelper();
88             a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
89             a.traverse_impl(_sDirectory, null);
90             return a.m_aFileList.toArray();
91         }
92 
traverse_impl( String afileDirectory, FileFilter _aFileFilter )93     void traverse_impl( String afileDirectory, FileFilter _aFileFilter )
94         {
95             File fileDirectory = new File(afileDirectory);
96             // Testing, if the file is a directory, and if so, it throws an exception
97             if ( !fileDirectory.isDirectory() )
98             {
99                 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
100             }
101 
102             // Getting all files and directories in the current directory
103             File[] aDirEntries;
104             if (_aFileFilter != null)
105             {
106                 aDirEntries = fileDirectory.listFiles(_aFileFilter);
107             }
108             else
109             {
110                 aDirEntries = fileDirectory.listFiles();
111             }
112 
113             // Iterating for each file and directory
114             for ( int i = 0; i < aDirEntries.length; ++i )
115             {
116                 if ( aDirEntries[ i ].isDirectory() )
117                 {
118                     if (m_bRecursiveIsAllowed == true)
119                     {
120                         // Recursive call for the new directory
121                         traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter );
122                     }
123                 }
124                 else
125                 {
126                     // adding file to List
127                     try
128                     {
129                         // Composing the URL by replacing all backslashs
130                         // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' );
131                         String aStr = aDirEntries[ i ].getAbsolutePath();
132                         m_aFileList.add(aStr);
133                     }
134                     catch( Exception exception )
135                     {
136                         exception.printStackTrace();
137                         break;
138                     }
139                 }
140             }
141         }
142 
143     // tests
144     // public static void main(String[] args)
145     //     {
146     //         String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo";
147     //         Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false );
148     //
149     //         for (int i=0;i<aDirectoryList.length;i++)
150     //         {
151     //             String sEntry = (String)aDirectoryList[i];
152     //             System.out.println(sEntry);
153     //         }
154     //     }
155 }
156 
157