This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

Software developer at Scandiatransplant

Personal page there


Quick hack for sorted list of a Wiki repository. Add "?c=100" to get the 100 latest. Path to repository is hardcoded.

<%@page
   import="java.io.*, java.util.*"
   contentType="text/html"
%><%!

  String[] fileurlmappings = { 
      "/export/spare/home/ravn/wiki/tra", "/wiki/Wiki.jsp?page="
  };

  String dir = ".";

  %><%

  class Single_Entry {
    File f;
    String url;
    Single_Entry( File f, String url) {
      this.f = f;  
      this.url = url;
    }
  }

  int requested_number_of_items;

  Vector entries = new Vector();

  try {
    requested_number_of_items = Integer.parseInt(request.getParameter("c"));
  } catch (NumberFormatException e) {
    requested_number_of_items = 10;
 }

  // Gather file info

  for( int i = 0; i < fileurlmappings.length; i += 2) {

    File dir = new File( fileurlmappings[i]);
    if (dir.isDirectory()) {
      File[] files = dir.listFiles();
      for( int j = 0; j < files.length; j++) {
//        System.out.println( j + " " + files[j].getName());
        // TODO: URL is not protected
        if (! files[j].isDirectory()) {

          entries.add( new Single_Entry( files[j], fileurlmappings[ i+1] + files[j].getName()));
        }
      }
   } else {
    System.out.println( "error: " + dir.getName() + " is not a directory");
   }
  }

  // Sort.  Put newest first

  java.util.Collections.sort( entries, new java.util.Comparator() {
      public int compare( Object a, Object b) {
         if (((Single_Entry) a).f.lastModified() < ((Single_Entry) b).f.lastModified())
           return 1;
         if (((Single_Entry) a).f.lastModified() > ((Single_Entry) b).f.lastModified())
           return -1;
         return 0;
      }

      });
      
  int number_to_show = Math.min(entries.size(), requested_number_of_items);

%>
<dl>
<%
  for( int i = 0; i < number_to_show; i++) {
    Single_Entry se = (Single_Entry) entries.get(i);
    String entryName = se.f.getName();
    if (entryName.endsWith(".txt")) {
      entryName = entryName.substring( 0, entryName.length() - ".txt".length());
      se.url = se.url.substring( 0, se.url.length() - ".txt".length());
    }
    if (entryName.length() > 23) {
      entryName = entryName.substring( 0, 20) + "...";
    }



%>
   <dt>
     <a href="<%= se.url %>"><%= entryName %></a>
   </dt>
   <dt>
      <font size="-2" color="#999999"><%

// Voodoo to get short times for today and yesteday

 Calendar midnight_today = Calendar.getInstance();
 midnight_today.set( Calendar.HOUR_OF_DAY, 0);
 midnight_today.set( Calendar.MINUTE, 0);
 Calendar midnight_yesteday = Calendar.getInstance();
 midnight_yesteday.set( Calendar.HOUR_OF_DAY, 0);
 midnight_yesteday.set( Calendar.MINUTE, 0);
 midnight_yesteday.add( Calendar.DAY_OF_MONTH, -1);

 Calendar c = Calendar.getInstance();
 c.setTimeInMillis( se.f.lastModified());
 if ( c.before( midnight_yesteday)) {
%><%= new java.util.Date( se.f.lastModified()) %><% } else if (c.before(midnight_today)) {
%>Yesteday <%= c.get(Calendar.HOUR_OF_DAY) %>:<%= c.get(Calendar.MINUTE) %><% } else {
%><%= c.get(Calendar.HOUR_OF_DAY) %>:<%= c.get(Calendar.MINUTE) %><% } %>
</font>
   </dt>
<% } %>
</dl>


I am not claiming this is pretty or complete, but it is an alternative which might be interesting for some.


CategoryHomepage

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 28-Jan-2010 11:06 by 164.67.235.200.