rapture.folder.util.NameHeirarchyFromURL Maven / Gradle / Ivy
/**
* The MIT License (MIT)
*
* Copyright (c) 2011-2016 Incapture Technologies LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package rapture.folder.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import rapture.common.RaptureFolderInfo;
/**
* Get RaptureFolderInfo structures via various means
*
* @author amkimian
*
*/
public class NameHeirarchyFromURL {
private static Logger log = Logger.getLogger(NameHeirarchyFromURL.class);
private NameHeirarchyFromURL() {
}
public static Map> getFolderStructure(String rootFolderUrl) {
return getFoldersFromFileSystemUrl(rootFolderUrl);
}
private static Map> getFoldersFromFileSystemUrl(String rootFolderUrl) {
Map> ret = new HashMap>();
File[] files = new File(rootFolderUrl).listFiles();
log.info("Files are " + files);
workWith(ret, rootFolderUrl, "", files);
return ret;
}
private static void workWith(Map> ret, String rootFolderUrl, String prefix, File[] files) {
for(File f : files) {
if (f.isFile()) {
log.info("Found file " + f.getName());
RaptureFolderInfo info = new RaptureFolderInfo();
info.setFolder(false);
info.setName(f.getName());
addEntry(ret, prefix, info);
} else {
log.info("Found folder " + f.getName());
String newRootFolder = rootFolderUrl + "/" + f.getName();
String newPrefix = prefix + (prefix.isEmpty() ? "" : "/") + f.getName();
log.info("Lower name is " + newPrefix);
RaptureFolderInfo info = new RaptureFolderInfo();
info.setFolder(true);
info.setName(f.getName());
addEntry(ret, prefix, info);
File[] innerFiles = f.listFiles();
log.info("Inner files are " + innerFiles);
workWith(ret, newRootFolder, newPrefix, innerFiles);
}
}
}
private static void addEntry(Map> ret, String rootFolderUrl, RaptureFolderInfo info) {
if (!ret.containsKey(rootFolderUrl)) {
ret.put(rootFolderUrl, new ArrayList());
}
ret.get(rootFolderUrl).add(info);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy