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