001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)FileListing.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.sequencing.util;
030:
031: import java.io.File;
032: import java.io.FilenameFilter;
033:
034: import java.util.ArrayList;
035: import java.util.Arrays;
036: import java.util.Iterator;
037: import java.util.List;
038: import java.util.logging.Logger;
039:
040: /**
041: * FileListing.
042: *
043: * @author Sun Microsystems, Inc.
044: */
045: public final class FileListing {
046: /**
047: *
048: */
049:
050: /**
051: *
052: */
053: private static boolean sRecurse = false;
054:
055: /**
056: *
057: */
058:
059: /**
060: *
061: */
062: private static FilenameFilter sFilter = null;
063:
064: /**
065: *
066: */
067:
068: /**
069: *
070: */
071: private static Logger sLog = Logger
072: .getLogger("com.sun.jbi.egine.sequencing.util");
073:
074: /**
075: * Gets the file listing.
076: *
077: * @param aDir directory name
078: * @param bRecurse true if recurse
079: * @param aFilter filter
080: *
081: * @return list of files.
082: */
083: public static List getFileListing(File aDir, boolean bRecurse,
084: FilenameFilter aFilter) {
085: sRecurse = bRecurse;
086: sFilter = aFilter;
087:
088: if ((aDir == null) || (aDir.getName().trim().equals(""))) {
089: sLog.severe("Directory is null");
090:
091: return null;
092: }
093:
094: return getListing(aDir);
095: }
096:
097: /**
098: * Recursively walk a directory tree and return a List of all Files found;
099: * the List is sorted using File.compareTo.
100: *
101: * @param aDir is a valid directory, which can be read.
102: *
103: * @return string array of folders
104: */
105: public static String[] getFolderListing(File aDir) {
106: File[] dirs = aDir.listFiles();
107:
108: if (dirs == null) {
109: sLog.severe("No folders match the Filter criteria ");
110:
111: return null;
112: }
113:
114: List filesDirs = Arrays.asList(dirs);
115:
116: if (filesDirs == null) {
117: sLog.severe("Could not convert Folder Array to List");
118:
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: * returns a listing of folders under a folder.
141: *
142: * @param aDir folder name
143: *
144: * @return listing
145: */
146: public static 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: * XML file.
178: *
179: * @param path path to be searched.
180: *
181: * @return xml file name.
182: */
183: public static String getXMLFile(String path) {
184: InputFileFilter myFilter = new InputFileFilter();
185: myFilter.setFilterexpression(".*\\.xml");
186: sFilter = myFilter;
187:
188: String xmlfile = null;
189:
190: try {
191: xmlfile = ((File) getListing(new File(path)).get(0))
192: .getAbsolutePath();
193: } catch (Exception e) {
194: ;
195: }
196:
197: sFilter = null;
198:
199: return xmlfile;
200: }
201:
202: /**
203: * Recursively walk a directory tree and return a List of all Files found;
204: * the List is sorted using File.compareTo.
205: *
206: * @param aDir is a valid directory, which can be read.
207: *
208: * @return list of files.
209: */
210: private static List getListing(File aDir) {
211: List result = new ArrayList();
212: File[] filesAndDirs = aDir.listFiles(sFilter);
213:
214: if (filesAndDirs == null) {
215: sLog.info("No Files match the Filter criteria ");
216:
217: return null;
218: }
219:
220: List filesDirs = Arrays.asList(filesAndDirs);
221:
222: if (filesDirs == null) {
223: sLog.severe("Could not convert File Array to List");
224:
225: return null;
226: }
227:
228: Iterator filesIter = filesDirs.iterator();
229: File file = null;
230:
231: while (filesIter.hasNext()) {
232: file = (File) filesIter.next();
233:
234: if (!file.isFile()) {
235: //must be a directory
236: //recursive call!
237: if (sRecurse) {
238: sLog.info("RECURSING");
239:
240: List deeperList = getListing(file);
241: result.addAll(deeperList);
242: }
243: } else {
244: result.add(file);
245: }
246: }
247:
248: return result;
249: }
250: }
|