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.common;
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: * Helper class for listing files / folders.
042: *
043: * @author Sun Microsystems, Inc.
044: */
045: public final class FileListing {
046: /**
047: * Flag to indicate recursive listing.
048: */
049: private static boolean sRecurse = false;
050:
051: /**
052: * Filter class to be used.
053: */
054: private static FilenameFilter sFilter = null;
055:
056: /**
057: * Logger object.
058: */
059: private static Logger sLog = Logger
060: .getLogger("com.sun.jbi.binding.file.util");
061:
062: /**
063: * Gets the list of files in a folder.
064: *
065: * @param aDir folder name.
066: * @param bRecurse will recurse if true.
067: * @param aFilter filname filter.
068: *
069: * @return List of file names.
070: */
071: public static synchronized List getFileListing(File aDir,
072: boolean bRecurse, FilenameFilter aFilter) {
073: sRecurse = bRecurse;
074: sFilter = aFilter;
075:
076: if ((aDir == null) || (aDir.getName().trim().equals(""))) {
077: sLog.severe("Directory is null");
078:
079: return null;
080: }
081:
082: return getListing(aDir);
083: }
084:
085: public static String getXMLFile(String path) {
086: InputFileFilter myFilter = new InputFileFilter();
087: myFilter.setFilterexpression(".*\\.xml");
088: sFilter = myFilter;
089: String xmlfile = null;
090: try {
091: xmlfile = ((File) getListing(new File(path)).get(0))
092: .getAbsolutePath();
093: } catch (Exception e) {
094: ;
095: }
096: sFilter = null;
097: return xmlfile;
098:
099: }
100:
101: public static String getWSDLFile(String path) {
102: InputFileFilter myFilter = new InputFileFilter();
103: myFilter.setFilterexpression(".*\\.wsdl");
104: sFilter = myFilter;
105: String xmlfile = null;
106: try {
107: xmlfile = ((File) getListing(new File(path)).get(0))
108: .getAbsolutePath();
109: } catch (Exception e) {
110: ;
111: }
112:
113: sFilter = null;
114:
115: return xmlfile;
116:
117: }
118:
119: /**
120: * Recursively walk a directory tree and return a List of all Files found;
121: * the List is sorted using File.compareTo.
122: *
123: * @param aDir is a valid directory, which can be read.
124: *
125: * @return array of folder names
126: */
127: public static synchronized String[] getFolderListing(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.getAbsolutePath();
150: counter++;
151: }
152: }
153:
154: return result;
155: }
156:
157: /**
158: * Util method to get the list of folders.
159: *
160: * @param aDir folder name for which contents have to be listed.
161: *
162: * @return array of folder names.
163: */
164: public static synchronized String[] getFolderNameListing(File aDir) {
165: File[] dirs = aDir.listFiles();
166:
167: if (dirs == null) {
168: return null;
169: }
170:
171: List filesDirs = Arrays.asList(dirs);
172:
173: if (filesDirs == null) {
174: return null;
175: }
176:
177: Iterator filesIter = filesDirs.iterator();
178: String[] result = new String[filesDirs.size()];
179: File file = null;
180: int counter = 0;
181:
182: while (filesIter.hasNext()) {
183: file = (File) filesIter.next();
184:
185: if (!file.isFile()) {
186: result[counter] = file.getName();
187: counter++;
188: }
189: }
190:
191: return result;
192: }
193:
194: /**
195: * Recursively walk a directory tree and return a List of all Files found;
196: * the List is sorted using File.compareTo.
197: *
198: * @param aDir is a valid directory, which can be read.
199: *
200: * @return list of files.
201: */
202: private static List getListing(File aDir) {
203: List result = new ArrayList();
204: File[] filesAndDirs = aDir.listFiles(sFilter);
205:
206: if (filesAndDirs == null) {
207: return null;
208: }
209:
210: List filesDirs = Arrays.asList(filesAndDirs);
211:
212: if (filesDirs == null) {
213: return null;
214: }
215:
216: Iterator filesIter = filesDirs.iterator();
217: File file = null;
218:
219: while (filesIter.hasNext()) {
220: file = (File) filesIter.next();
221:
222: if (!file.isFile()) {
223: //must be a directory
224: //recursive call!
225: if (sRecurse) {
226: List deeperList = getListing(file);
227: result.addAll(deeperList);
228: }
229: } else {
230: result.add(file);
231: }
232: }
233:
234: return result;
235: }
236:
237: }
|