releasing/cbrtools/perl/TableFormatter.pm
author jjkang
Fri, 25 Jun 2010 18:37:20 +0800
changeset 602 3145852acc89
permissions -rw-r--r--
add releasing to new structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
package TableFormatter;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
use constant TABLE_MARGIN => 3;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
sub New {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
  my $class = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
  my $inidata = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
  my $args = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
  # SUBCLASSES: don't store $iniData anywhere, because then you'll
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
  # get a reference loop and a memory leak.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
  my $self = bless {}, (ref $class || $class);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
  $self->{args} = $args;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
  return $self;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
### Static methods
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
sub CreateFormatter {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
  my $type = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
  my $inidata = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
  my $args = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
  die "Error: couldn't create a formatter without a type" unless $type;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
  $type = ucfirst($type);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
  my $class = "TableFormatter::$type";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
  eval "require $class";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
  if ($@) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
    die "Could not load the table formatter \"$class\" for format \"$type\" because $@";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
  return $class->New($inidata, $args) or die "Could not create the table formatter \"$class\" for format \"$type\"";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
### Private
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
sub FindColWidths {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
  my $data = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
  my @widths;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
  return [] unless defined ($data->[0]);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
  my $numCols = scalar(@{$data->[0]});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
  for (my $col = 0; $col < $numCols; ++$col) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
    my $width = $self->FindWidestColElement($data, $col);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
    $width += TABLE_MARGIN unless ($col == $numCols-1);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
    # Don't pad the last column in case it gives us unnecessary line wrapping
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
    push @widths, $width;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
  return \@widths;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
sub FindWidestColElement {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
  my $data = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
  my $col = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
  my $widest = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
  my $numRows = scalar(@{$data});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
  for (my $row = 0; $row < $numRows; ++$row) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
    my $this = $data->[$row][$col];
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
    my $lengthThis = length($this);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
    if ($lengthThis > $widest) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
      $widest = $lengthThis;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
  return $widest;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
TableFormatter.pm - An abstract superclass for table formatting classes.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
=head1 INTERFACE FOR SUBCLASSES
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
=head2 New
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
Creates a formatter.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
=head2 PrintTable
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
Prints a table. Two arguments: firstly, a 2D array of the data. Secondly, a Boolean specifying whether the first row is a header row.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
None.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
 Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
=cut