1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package convwatch;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.File;
31*cdf0e10cSrcweir import java.io.FileFilter;
32*cdf0e10cSrcweir import java.util.ArrayList;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /**
35*cdf0e10cSrcweir  * Helper for directory access
36*cdf0e10cSrcweir  *
37*cdf0e10cSrcweir  * @author Lars.Langhans@sun.com
38*cdf0e10cSrcweir  */
39*cdf0e10cSrcweir public class DirectoryHelper
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir     ArrayList m_aFileList = new ArrayList();
42*cdf0e10cSrcweir     boolean m_bRecursiveIsAllowed = true;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     void setRecursiveIsAllowed(boolean _bValue)
45*cdf0e10cSrcweir         {
46*cdf0e10cSrcweir             m_bRecursiveIsAllowed = _bValue;
47*cdf0e10cSrcweir         }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     /**
50*cdf0e10cSrcweir      * Traverse over a given directory, and filter with a given FileFilter
51*cdf0e10cSrcweir      * object and gives back the deep directory as a Object[] list, which
52*cdf0e10cSrcweir      * contain a String object for every directory entry.
53*cdf0e10cSrcweir      *
54*cdf0e10cSrcweir      * <B>Example</B>
55*cdf0e10cSrcweir      * List directory /bin, filter out all files which ends with '.prn'
56*cdf0e10cSrcweir      *
57*cdf0e10cSrcweir      *  FileFilter aFileFilter = new FileFilter()
58*cdf0e10cSrcweir      *  {
59*cdf0e10cSrcweir      *      public boolean accept( File pathname )
60*cdf0e10cSrcweir      *          {
61*cdf0e10cSrcweir      *              if (pathname.getName().endsWith(".prn"))
62*cdf0e10cSrcweir      *              {
63*cdf0e10cSrcweir      *                  return false;
64*cdf0e10cSrcweir      *              }
65*cdf0e10cSrcweir      *              return true;
66*cdf0e10cSrcweir      *          }
67*cdf0e10cSrcweir      *  };
68*cdf0e10cSrcweir      *
69*cdf0e10cSrcweir      * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
70*cdf0e10cSrcweir      * for (int i=0;i<aList.length;i++)
71*cdf0e10cSrcweir      * {
72*cdf0e10cSrcweir      *     String aEntry = (String)aList[i];
73*cdf0e10cSrcweir      *     System.out.println(aEntry);
74*cdf0e10cSrcweir      * }
75*cdf0e10cSrcweir      *
76*cdf0e10cSrcweir      */
77*cdf0e10cSrcweir     public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed )
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             DirectoryHelper a = new DirectoryHelper();
80*cdf0e10cSrcweir             a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
81*cdf0e10cSrcweir             a.traverse_impl(_sDirectory, _aFileFilter);
82*cdf0e10cSrcweir             return a.m_aFileList.toArray();
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed )
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir             DirectoryHelper a = new DirectoryHelper();
88*cdf0e10cSrcweir             a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
89*cdf0e10cSrcweir             a.traverse_impl(_sDirectory, null);
90*cdf0e10cSrcweir             return a.m_aFileList.toArray();
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     void traverse_impl( String afileDirectory, FileFilter _aFileFilter )
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir             File fileDirectory = new File(afileDirectory);
96*cdf0e10cSrcweir             // Testing, if the file is a directory, and if so, it throws an exception
97*cdf0e10cSrcweir             if ( !fileDirectory.isDirectory() )
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
100*cdf0e10cSrcweir             }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             // Getting all files and directories in the current directory
103*cdf0e10cSrcweir             File[] aDirEntries;
104*cdf0e10cSrcweir             if (_aFileFilter != null)
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 aDirEntries = fileDirectory.listFiles(_aFileFilter);
107*cdf0e10cSrcweir             }
108*cdf0e10cSrcweir             else
109*cdf0e10cSrcweir             {
110*cdf0e10cSrcweir                 aDirEntries = fileDirectory.listFiles();
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             // Iterating for each file and directory
114*cdf0e10cSrcweir             for ( int i = 0; i < aDirEntries.length; ++i )
115*cdf0e10cSrcweir             {
116*cdf0e10cSrcweir                 if ( aDirEntries[ i ].isDirectory() )
117*cdf0e10cSrcweir                 {
118*cdf0e10cSrcweir                     if (m_bRecursiveIsAllowed == true)
119*cdf0e10cSrcweir                     {
120*cdf0e10cSrcweir                         // Recursive call for the new directory
121*cdf0e10cSrcweir                         traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter );
122*cdf0e10cSrcweir                     }
123*cdf0e10cSrcweir                 }
124*cdf0e10cSrcweir                 else
125*cdf0e10cSrcweir                 {
126*cdf0e10cSrcweir                     // adding file to List
127*cdf0e10cSrcweir                     try
128*cdf0e10cSrcweir                     {
129*cdf0e10cSrcweir                         // Composing the URL by replacing all backslashs
130*cdf0e10cSrcweir                         // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' );
131*cdf0e10cSrcweir                         String aStr = aDirEntries[ i ].getAbsolutePath();
132*cdf0e10cSrcweir                         m_aFileList.add(aStr);
133*cdf0e10cSrcweir                     }
134*cdf0e10cSrcweir                     catch( Exception exception )
135*cdf0e10cSrcweir                     {
136*cdf0e10cSrcweir                         exception.printStackTrace();
137*cdf0e10cSrcweir                         break;
138*cdf0e10cSrcweir                     }
139*cdf0e10cSrcweir                 }
140*cdf0e10cSrcweir             }
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     // tests
144*cdf0e10cSrcweir     // public static void main(String[] args)
145*cdf0e10cSrcweir     //     {
146*cdf0e10cSrcweir     //         String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo";
147*cdf0e10cSrcweir     //         Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false );
148*cdf0e10cSrcweir     //
149*cdf0e10cSrcweir     //         for (int i=0;i<aDirectoryList.length;i++)
150*cdf0e10cSrcweir     //         {
151*cdf0e10cSrcweir     //             String sEntry = (String)aDirectoryList[i];
152*cdf0e10cSrcweir     //             System.out.println(sEntry);
153*cdf0e10cSrcweir     //         }
154*cdf0e10cSrcweir     //     }
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157