View Javadoc

    1   /*
    2    * Copyright (c) 2001-2003 The XDoclet team
    3    * All rights reserved.
    4    */
    5   package xjavadoc;
    6
    7   import java.io.File;
    8   import java.io.FileFilter;
    9   import java.util.LinkedList;
    10
    11  /***
    12   * Various static utility methods
    13   *
    14   * @author         Aslak Hellesøy
    15   * @created        20. januar 2002
    16   */
    17  public class Util
    18  {
    19
    20  	private final static FileFilter _javaFilter =
    21  		new FileFilter()
    22  		{
    23  			public boolean accept( File f )
    24  			{
    25  				return f.getName().endsWith( ".java" );
    26  			}
    27  		};
    28
    29  	private final static FileFilter _dirFilter =
    30  		new FileFilter()
    31  		{
    32  			public boolean accept( File f )
    33  			{
    34  				return f.isDirectory();
    35  			}
    36  		};
    37
    38  	/***
    39  	 * Returns an array of String containing relative names of all java files under
    40  	 * root.
    41  	 *
    42  	 * @param root  the root directory
    43  	 * @return      java file names
    44  	 */
    45  	public static String[] getJavaFiles( File root )
    46  	{
    47  		LinkedList javaFileNames = new LinkedList();
    48
    49  		descend( root, "", javaFileNames );
    50  		return ( String[] ) javaFileNames.toArray( new String[javaFileNames.size()] );
    51  	}
    52
    53  	public static String getPackageNameFor( String qualifiedName )
    54  	{
    55  		String packageName = null;
    56  		int lastDotIndex = qualifiedName.lastIndexOf( '.' );
    57
    58  		if( lastDotIndex == -1 )
    59  		{
    60  			// default package
    61  			packageName = "";
    62  		}
    63  		else
    64  		{
    65  			packageName = qualifiedName.substring( 0, lastDotIndex );
    66  		}
    67  		returng> packageName;
    68  	}
    69
    70  	public> static String getQualifiedNameFor( String packageName, String unqualifiedName )
    71  	{
    72  		if>( packageName.equals( "" ) )
    73  		{
    74  			return unqualifiedName;
    75  		}
    76  		else
    77  		{
    78  			returnng> packageName + "." + unqualifiedName;
    79  		}
    80  	}
    81
    82  	public final static StringBuffer appendDimensionAsString( final int n, final StringBuffer sb )
    83  	{
    84  		for( int i = 0; i < n; i++ )
    85  		{
    86  			sb.append( "[]" );
    87  		}
    88  		return sb;
    89  	}
    90
    91  	public final static String toString( Object[] array, String delimiter )
    92  	{
    93  		StringBuffer sb = new StringBuffer();
    94
    95  		for( int i = 0; i < array.length; i++ )
    96  		{
    97  			sb.append( array[i].toString() );
    98  			if( i < array.length - 1 )
    99  			{
    100 				sb.append( delimiter );
    101 			}
    102 		}
    103 		return sb.toString();
    104 	}
    105 	/***
    106 	 * Return only class name of a full qualified (package+classname) string.
    107 	 *
    108 	 * @param qualifiedName
    109 	 * @return
    110 	 */
    111 	public static String classNameFromQualifiedClassName( String qualifiedName )
    112 	{
    113 		if( qualifiedName == null )
    114 		{
    115 			throw new IllegalArgumentException( "qualifiedName can't be null!" );
    116 		}
    117
    118 		int dot_index = qualifiedName.lastIndexOf( '.' );
    119
    120 		if( dot_index != -1 )
    121 			return qualifiedName.substring( dot_index + 1 );
    122 		else
    123 			return qualifiedName;
    124 	}
    125
    126 	/***
    127 	 * Recursively descends a directory and build a list of relative file names for
    128 	 * java files.
    129 	 *
    130 	 * @param root           the root directory
    131 	 * @param dirName        current directory relative filename
    132 	 * @param javaFileNames  the list where java file names will be added
    133 	 */
    134 	private static void descend( File root, String dirName, LinkedList javaFileNames )
    135 	{
    136 		File dir = new File( root, dirName );
    137
    138 		File[] javaFiles = dir.listFiles( _javaFilter );
    139
    140 		for( int i = 0; i < javaFiles.length; i++ )
    141 		{
    142 			StringBuffer javaFileName = new StringBuffer();
    143
    144 			if( dirName.length() != 0 )
    145 			{
    146 				javaFileName.append( dirName ).append( File.separator );
    147 			}
    148 			javaFileName.append( javaFiles[i].getName() );
    149 			javaFileNames.add( javaFileName.toString() );
    150 		}
    151
    152 		File[] subDirs = dir.listFiles( _dirFilter );
    153
    154 		for( int i = 0; i < subDirs.length; i++ )
    155 		{
    156 			StringBuffer subDirName = new StringBuffer( dirName );
    157
    158 			if( dirName.length() != 0 )
    159 			{
    160 				subDirName.append( File.separator );
    161 			}
    162 			subDirName.append( subDirs[i].getName() );
    163 			descend( root, subDirName.toString(), javaFileNames );
    164 		}
    165 	}
    166 }