PageRenderTime 68ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/commands/dir.java

#
Java | 117 lines | 93 code | 18 blank | 6 comment | 13 complexity | bcdd9e428a91c1fc749e6e0a1a72240a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package bsh.commands;
  2. import java.io.*;
  3. import bsh.*;
  4. import java.util.Date;
  5. import java.util.Vector;
  6. import java.util.GregorianCalendar;
  7. import java.util.Calendar;
  8. /**
  9. This is an example of a bsh command written in Java for speed.
  10. */
  11. public class dir
  12. {
  13. static final String [] months = { "Jan", "Feb", "Mar", "Apr",
  14. "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  15. public static String usage() {
  16. return "usage: dir( String dir )\n dir()";
  17. }
  18. public static void invoke( Interpreter env, NameSpace namespace ) {
  19. //String dir = getCWD( namespace );
  20. String dir = ".";
  21. invoke( env, namespace, dir );
  22. }
  23. public static void invoke(
  24. Interpreter env, NameSpace namespace, String dir )
  25. {
  26. File file;
  27. try {
  28. file = env.pathToFile( dir );
  29. } catch (IOException e ) {
  30. env.println("error reading path: "+e);
  31. return;
  32. }
  33. if ( !file.exists() || !file.canRead() ) {
  34. env.println( "Can't read " + file );
  35. return;
  36. }
  37. if ( !file.isDirectory() ) {
  38. env.println("'"+dir+"' is not a directory");
  39. }
  40. String [] files = file.list();
  41. files = bubbleSort(files);
  42. for( int i=0; i< files.length; i++ ) {
  43. File f = new File( dir + File.separator + files[i] );
  44. StringBuffer sb = new StringBuffer();
  45. sb.append( f.canRead() ? "r": "-" );
  46. sb.append( f.canWrite() ? "w": "-" );
  47. sb.append( "_" );
  48. sb.append( " ");
  49. Date d = new Date(f.lastModified());
  50. GregorianCalendar c = new GregorianCalendar();
  51. c.setTime(d);
  52. int day = c.get(Calendar.DAY_OF_MONTH);
  53. sb.append( months[ c.get(Calendar.MONTH) ] + " " + day );
  54. if ( day < 10 )
  55. sb.append(" ");
  56. sb.append(" ");
  57. // hack to get fixed length 'length' field
  58. int fieldlen = 8;
  59. StringBuffer len = new StringBuffer();
  60. for(int j=0; j<fieldlen; j++)
  61. len.append(" ");
  62. len.insert(0, f.length());
  63. len.setLength(fieldlen);
  64. // hack to move the spaces to the front
  65. int si = len.toString().indexOf(" ");
  66. if ( si != -1 ) {
  67. String pad = len.toString().substring(si);
  68. len.setLength(si);
  69. len.insert(0, pad);
  70. }
  71. sb.append( len );
  72. sb.append( " " + f.getName() );
  73. if ( f.isDirectory() )
  74. sb.append("/");
  75. env.println( sb.toString() );
  76. }
  77. }
  78. public static String [] bubbleSort( String [] in ) {
  79. Vector v = new Vector();
  80. for(int i=0; i<in.length; i++)
  81. v.addElement(in[i]);
  82. int n = v.size();
  83. boolean swap = true;
  84. while ( swap ) {
  85. swap = false;
  86. for(int i=0; i<(n-1); i++)
  87. if ( ((String)v.elementAt(i)).compareTo(
  88. ((String)v.elementAt(i+1)) ) > 0 ) {
  89. String tmp = (String)v.elementAt(i+1);
  90. v.removeElementAt( i+1 );
  91. v.insertElementAt( tmp, i );
  92. swap = true;
  93. }
  94. }
  95. String [] out = new String [ n ];
  96. v.copyInto(out);
  97. return out;
  98. }
  99. }