| 68 | 1 /* | 
|  | 2 Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com> | 
|  | 3 | 
|  | 4 Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 5 of this software and associated documentation files (the "Software"), to deal | 
|  | 6 in the Software without restriction, including without limitation the rights | 
|  | 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 8 copies of the Software, and to permit persons to whom the Software is | 
|  | 9 furnished to do so, subject to the following conditions: | 
|  | 10 | 
|  | 11 The above copyright notice and this permission notice shall be included in | 
|  | 12 all copies or substantial portions of the Software. | 
|  | 13 | 
|  | 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
|  | 20 THE SOFTWARE. | 
|  | 21 */ | 
|  | 22 | 
|  | 23 package fschmidt.tools; | 
|  | 24 | 
|  | 25 import java.io.File; | 
|  | 26 import java.util.Set; | 
|  | 27 import java.util.HashSet; | 
|  | 28 import java.util.Arrays; | 
|  | 29 import java.util.regex.Pattern; | 
|  | 30 import fschmidt.util.java.ObjectUtils; | 
|  | 31 | 
|  | 32 | 
|  | 33 /** | 
|  | 34  * Lists nextag packages.  Written for javadoc and used like: | 
|  | 35  * <pre> | 
|  | 36  *     javadoc `java fschmidt.tools.Packages` | 
|  | 37  * </pre> | 
|  | 38  * | 
|  | 39  * @author  frank | 
|  | 40  */ | 
|  | 41 public final class Packages { | 
|  | 42 | 
|  | 43 	public static void main (String args[]) { | 
|  | 44 		Set<String> list = new HashSet<String>(); | 
|  | 45         for( int i=0; i<args.length; i++ ) { | 
|  | 46             pkgs(list,args[i]); | 
|  | 47         } | 
|  | 48 		String[] a = (String[])list.toArray(new String[0]); | 
|  | 49 		for( int i=0; i<a.length; i++ ) { | 
|  | 50 			System.out.println(a[i]); | 
|  | 51 		} | 
|  | 52 	} | 
|  | 53 | 
|  | 54 	static void pkgs(Set<String> list,String pkg) { | 
|  | 55         String[] dirs = toDirs(pkg); | 
|  | 56         boolean hasFiles = false; | 
|  | 57         for( int j=0; j<dirs.length; j++ ) { | 
|  | 58             String dir = dirs[j]; | 
|  | 59     		String[] ls = new File(dir).list(); | 
|  | 60     		if( ls == null ) | 
|  | 61     			continue; | 
|  | 62     		String[] subs = new String[ls.length]; | 
|  | 63     		for( int i=0; i<ls.length; i++ ) { | 
|  | 64     			File f = new File(dir + File.separator + ls[i]); | 
|  | 65     			if( f.isDirectory() ) { | 
|  | 66                     pkgs( list, pkg + "." + ls[i] ); | 
|  | 67     			} | 
|  | 68                 if( f.getName().endsWith(".java") ) | 
|  | 69                     hasFiles = true; | 
|  | 70     		} | 
|  | 71         } | 
|  | 72         if( hasFiles ) | 
|  | 73             list.add(pkg); | 
|  | 74 	} | 
|  | 75 | 
|  | 76 	static String[] paths = System.getProperty("java.class.path").split(Pattern.quote(File.pathSeparator),-1); | 
|  | 77 | 
|  | 78     static String[] toDirs(String pkg) { | 
|  | 79         String relPath = ObjectUtils.join( | 
|  | 80             Arrays.asList(pkg.split("\\.",-1)), File.separator | 
|  | 81         ); | 
|  | 82         Set<String> list = new HashSet<String>(); | 
|  | 83 		for( int i=0; i<paths.length; i++ ) { | 
|  | 84 			String path = paths[i]; | 
|  | 85 			if( !path.endsWith(File.separator) ) | 
|  | 86 				path += File.separator; | 
|  | 87 			if( new File(path+relPath).exists() ) | 
|  | 88 				list.add(path+relPath); | 
|  | 89 		} | 
|  | 90 		return (String[])list.toArray(new String[0]); | 
|  | 91 	} | 
|  | 92 } |