001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.File;
018    
019    import java.util.Collections;
020    import java.util.HashSet;
021    import java.util.List;
022    import java.util.Set;
023    import java.util.Stack;
024    
025    /**
026     * @author Raymond Aug??
027     */
028    public class PackagingUtil {
029    
030            public static List<String> getPackagesFromPath(File file) {
031                    Set<String> packages = new HashSet<String>();
032                    Stack<String> packageStack = new Stack<String>();
033    
034                    subPackages(file, packages, packageStack);
035    
036                    List<String> list = ListUtil.fromCollection(packages);
037    
038                    Collections.sort(list);
039    
040                    return list;
041            }
042    
043            protected static void subPackages(
044                    File file, Set<String> packages, Stack<String> packageStack) {
045    
046                    for (File subFile : file.listFiles()) {
047                            if (subFile.isDirectory()) {
048                                    packageStack.push(subFile.getName());
049    
050                                    String packageName = StringUtil.merge(
051                                            packageStack, StringPool.PERIOD);
052    
053                                    if (packageName.contains(StringPool.PERIOD)) {
054                                            packages.add(packageName);
055                                    }
056    
057                                    subPackages(subFile, packages, packageStack);
058    
059                                    packageStack.pop();
060                            }
061                    }
062            }
063    
064    }