001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * FileListing.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license term
009: */
010: package com.sun.jbi.engine.sequencing.util;
011:
012: import java.io.File;
013: import java.io.FilenameFilter;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.logging.Logger;
020:
021: /**
022: * FileListing.
023: *
024: * @author Sun Microsystems, Inc.
025: */
026: public final class FileListing {
027: /**
028: *
029: */
030:
031: /**
032: *
033: */
034: private static boolean sRecurse = false;
035:
036: /**
037: *
038: */
039:
040: /**
041: *
042: */
043: private static FilenameFilter sFilter = null;
044:
045: /**
046: *
047: */
048:
049: /**
050: *
051: */
052: private static Logger sLog = Logger
053: .getLogger("com.sun.jbi.egine.sequencing.util");
054:
055: /**
056: * Gets the file listing.
057: *
058: * @param aDir directory name
059: * @param bRecurse true if recurse
060: * @param aFilter filter
061: *
062: * @return list of files.
063: */
064: public static List getFileListing(File aDir, boolean bRecurse,
065: FilenameFilter aFilter) {
066: sRecurse = bRecurse;
067: sFilter = aFilter;
068:
069: if ((aDir == null) || (aDir.getName().trim().equals(""))) {
070: sLog.severe("Directory is null");
071:
072: return null;
073: }
074:
075: return getListing(aDir);
076: }
077:
078: /**
079: * Recursively walk a directory tree and return a List of all Files found;
080: * the List is sorted using File.compareTo.
081: *
082: * @param aDir is a valid directory, which can be read.
083: *
084: * @return string array of folders
085: */
086: public static String[] getFolderListing(File aDir) {
087: File[] dirs = aDir.listFiles();
088:
089: if (dirs == null) {
090: sLog.severe("No folders match the Filter criteria ");
091:
092: return null;
093: }
094:
095: List filesDirs = Arrays.asList(dirs);
096:
097: if (filesDirs == null) {
098: sLog.severe("Could not convert Folder Array to List");
099:
100: return null;
101: }
102:
103: Iterator filesIter = filesDirs.iterator();
104: String[] result = new String[filesDirs.size()];
105: File file = null;
106: int counter = 0;
107:
108: while (filesIter.hasNext()) {
109: file = (File) filesIter.next();
110:
111: if (!file.isFile()) {
112: result[counter] = file.getAbsolutePath();
113: counter++;
114: }
115: }
116:
117: return result;
118: }
119:
120: /**
121: * returns a listing of folders under a folder.
122: *
123: * @param aDir folder name
124: *
125: * @return listing
126: */
127: public static String[] getFolderNameListing(File aDir) {
128: File[] dirs = aDir.listFiles();
129:
130: if (dirs == null) {
131: return null;
132: }
133:
134: List filesDirs = Arrays.asList(dirs);
135:
136: if (filesDirs == null) {
137: return null;
138: }
139:
140: Iterator filesIter = filesDirs.iterator();
141: String[] result = new String[filesDirs.size()];
142: File file = null;
143: int counter = 0;
144:
145: while (filesIter.hasNext()) {
146: file = (File) filesIter.next();
147:
148: if (!file.isFile()) {
149: result[counter] = file.getName();
150: counter++;
151: }
152: }
153:
154: return result;
155: }
156:
157: /**
158: * XML file.
159: *
160: * @param path path to be searched.
161: *
162: * @return xml file name.
163: */
164: public static String getXMLFile(String path) {
165: InputFileFilter myFilter = new InputFileFilter();
166: myFilter.setFilterexpression(".*\\.xml");
167: sFilter = myFilter;
168:
169: String xmlfile = null;
170:
171: try {
172: xmlfile = ((File) getListing(new File(path)).get(0))
173: .getAbsolutePath();
174: } catch (Exception e) {
175: ;
176: }
177:
178: sFilter = null;
179:
180: return xmlfile;
181: }
182:
183: /**
184: * Recursively walk a directory tree and return a List of all Files found;
185: * the List is sorted using File.compareTo.
186: *
187: * @param aDir is a valid directory, which can be read.
188: *
189: * @return list of files.
190: */
191: private static List getListing(File aDir) {
192: List result = new ArrayList();
193: File[] filesAndDirs = aDir.listFiles(sFilter);
194:
195: if (filesAndDirs == null) {
196: sLog.info("No Files match the Filter criteria ");
197:
198: return null;
199: }
200:
201: List filesDirs = Arrays.asList(filesAndDirs);
202:
203: if (filesDirs == null) {
204: sLog.severe("Could not convert File Array to List");
205:
206: return null;
207: }
208:
209: Iterator filesIter = filesDirs.iterator();
210: File file = null;
211:
212: while (filesIter.hasNext()) {
213: file = (File) filesIter.next();
214:
215: if (!file.isFile()) {
216: //must be a directory
217: //recursive call!
218: if (sRecurse) {
219: sLog.info("RECURSING");
220:
221: List deeperList = getListing(file);
222: result.addAll(deeperList);
223: }
224: } else {
225: result.add(file);
226: }
227: }
228:
229: return result;
230: }
231: }
|