bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / convwatch / DirectoryHelper.java
blob60896af030b8fbd488b719113b7c5cf9951ef868
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package convwatch;
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.util.ArrayList;
25 /**
26 * Helper for directory access
28 public class DirectoryHelper
30 ArrayList<String> m_aFileList = new ArrayList<String>();
31 boolean m_bRecursiveIsAllowed = true;
33 void setRecursiveIsAllowed(boolean _bValue)
35 m_bRecursiveIsAllowed = _bValue;
38 /**
39 * Traverse over a given directory, and filter with a given FileFilter
40 * object and gives back the deep directory as a Object[] list, which
41 * contain a String object for every directory entry.
43 * <B>Example</B>
44 * List directory /bin, filter out all files which ends with '.prn'
46 * FileFilter aFileFilter = new FileFilter()
47 * {
48 * public boolean accept( File pathname )
49 * {
50 * if (pathname.getName().endsWith(".prn"))
51 * {
52 * return false;
53 * }
54 * return true;
55 * }
56 * };
58 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
59 * for (int i=0;i<aList.length;i++)
60 * {
61 * String aEntry = (String)aList[i];
62 * System.out.println(aEntry);
63 * }
66 public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed )
68 DirectoryHelper a = new DirectoryHelper();
69 a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
70 a.traverse_impl(_sDirectory, _aFileFilter);
71 return a.m_aFileList.toArray();
74 public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed )
76 DirectoryHelper a = new DirectoryHelper();
77 a.setRecursiveIsAllowed(_bRecursiveIsAllowed);
78 a.traverse_impl(_sDirectory, null);
79 return a.m_aFileList.toArray();
82 void traverse_impl( String afileDirectory, FileFilter _aFileFilter )
84 File fileDirectory = new File(afileDirectory);
85 // Testing, if the file is a directory, and if so, it throws an exception
86 if ( !fileDirectory.isDirectory() )
88 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
91 // Getting all files and directories in the current directory
92 File[] aDirEntries;
93 if (_aFileFilter != null)
95 aDirEntries = fileDirectory.listFiles(_aFileFilter);
97 else
99 aDirEntries = fileDirectory.listFiles();
102 // Iterating for each file and directory
103 for ( int i = 0; i < aDirEntries.length; ++i )
105 if ( aDirEntries[ i ].isDirectory() )
107 if (m_bRecursiveIsAllowed == true)
109 // Recursive call for the new directory
110 traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter );
113 else
115 // adding file to List
118 // Composing the URL by replacing all backslashs
119 String aStr = aDirEntries[ i ].getAbsolutePath();
120 m_aFileList.add(aStr);
122 catch( Exception exception )
124 exception.printStackTrace();
125 break;