| 1 | ## @file | ||
| 2 | # (Enter your file info here) | ||
| 3 | # | ||
| 4 | # @copy 2007 MailerMailer LLC | ||
| 5 | # $Id: SCF.pm 498 2008-08-22 15:35:28Z kamelkev $ | ||
| 6 | |||
| 7 | ## @class RWDE::Doxy::SCF | ||
| 8 | # S(ource)C(ode)F(file) is going to be implementing DoxGenerator interface it's going to be doxygenable | ||
| 9 | package RWDE::Doxy::SCF; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Error qw(:try); | ||
| 15 | use RWDE::Exceptions; | ||
| 16 | |||
| 17 | use RWDE::Doxy::ClassData; | ||
| 18 | use RWDE::Doxy::MethodData; | ||
| 19 | |||
| 20 | use base qw(RWDE::RObject RWDE::Doxy::DocGenerator); | ||
| 21 | |||
| 22 | our (%fields, %static_fields, %modifiable_fields, @fieldnames, @static_fieldnames, @modifiable_fieldnames); | ||
| 23 | |||
| 24 | use vars qw($VERSION); | ||
| 25 | $VERSION = sprintf "%d", q$Revision: 518 $ =~ /(\d+)/; | ||
| 26 | |||
| 27 | BEGIN { | ||
| 28 | |||
| 29 | #all of the static fields present in the list table | ||
| 30 | %static_fields = ( | ||
| 31 | |||
| 32 | # Field => [Type, Descr] | ||
| 33 | ); | ||
| 34 | |||
| 35 | #all of the fields allowed to be modified in the list table | ||
| 36 | %modifiable_fields = ( | ||
| 37 | |||
| 38 | # Field => [Type, Descr] | ||
| 39 | |||
| 40 | file_location => [ 'char', 'Location of the file' ], | ||
| 41 | file_content_in => [ 'array', 'Content of the file_name. Each element is one line of the file' ], | ||
| 42 | file_content_out => [ 'array', 'Conetnt for the output file' ], | ||
| 43 | file_index => [ 'int', 'An index of a line of file_content' ], | ||
| 44 | file_current_class => [ 'char', 'Cursor for class name that is being processed.' ], | ||
| 45 | file_classes => [ 'hash', 'A hash of (class name, class_data) pairs. All classes in this file' ], | ||
| 46 | |||
| 47 | #Array is used because we need to preserver order. | ||
| 48 | file_info => [ 'array', 'list of file-related doxygen info lines that existed in the source file_content' ] | ||
| 49 | ); | ||
| 50 | |||
| 51 | %fields = (%static_fields, %modifiable_fields); | ||
| 52 | |||
| 53 | @static_fieldnames = sort keys %static_fields; | ||
| 54 | @modifiable_fieldnames = sort keys %modifiable_fields; | ||
| 55 | @fieldnames = sort keys %fields; | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | ## @method void initialize() | ||
| 60 | # (Enter initialize info here) | ||
| 61 | sub initialize() { | ||
| 62 | my ($self, $params) = @_; | ||
| 63 | |||
| 64 | throw RWDE::DataMissingException({ info => 'No file_location specified' }) | ||
| 65 | unless defined($$params{file_location}); | ||
| 66 | |||
| 67 | $self->file_location($$params{file_location}); | ||
| 68 | $self->file_content_in($self->_read_file()); | ||
| 69 | $self->file_content_out([]); | ||
| 70 | $self->file_index(0); | ||
| 71 | $self->file_classes({}); | ||
| 72 | $self->file_current_class(undef); | ||
| 73 | $self->file_info([]); | ||
| 74 | |||
| 75 | return (); | ||
| 76 | } | ||
| 77 | |||
| 78 | ## @method object current_line() | ||
| 79 | # Return file_content[file_index], the data residing in the file_index of file_content | ||
| 80 | # @return The current line | ||
| 81 | sub current_line() { | ||
| 82 | my ($self, $params) = @_; | ||
| 83 | |||
| 84 | return undef | ||
| 85 | unless ($self->file_index < scalar @{ $self->file_content_in }); | ||
| 86 | |||
| 87 | #return file_content[file_index] if we are within the bounds of the array, undef oterwise | ||
| 88 | return $self->file_content_in->[ $self->file_index ]; | ||
| 89 | } | ||
| 90 | |||
| 91 | ## @method void next_line() | ||
| 92 | # Increment the file_index + 1 | ||
| 93 | sub next_line() { | ||
| 94 | my ($self, $params) = @_; | ||
| 95 | |||
| 96 | $self->file_index($self->file_index + 1); | ||
| 97 | return (); | ||
| 98 | } | ||
| 99 | |||
| 100 | #Reset the file_index | ||
| 101 | # @return (Enter explanation for return value here) | ||
| 102 | ## @method void reset_file_index() | ||
| 103 | # (Enter reset_file_index info here) | ||
| 104 | sub reset_file_index() { | ||
| 105 | my ($self, $params) = @_; | ||
| 106 | |||
| 107 | $self->file_index(0); | ||
| 108 | return (); | ||
| 109 | } | ||
| 110 | |||
| 111 | #Reset the file_index | ||
| 112 | ## @method object reset_file_current_class() | ||
| 113 | # (Enter reset_file_current_class info here) | ||
| 114 | # @return (Enter explanation for return value here) | ||
| 115 | sub reset_file_current_class() { | ||
| 116 | my ($self, $params) = @_; | ||
| 117 | |||
| 118 | return $self->file_current_class(undef); | ||
| 119 | } | ||
| 120 | |||
| 121 | #Reset the file_content_out to an empty array | ||
| 122 | ## @method void reset_file_content_out() | ||
| 123 | # (Enter reset_file_content_out info here) | ||
| 124 | sub reset_file_content_out() { | ||
| 125 | my ($self, $params) = @_; | ||
| 126 | |||
| 127 | $self->file_content_out([]); | ||
| 128 | |||
| 129 | return (); | ||
| 130 | } | ||
| 131 | |||
| 132 | ## @method object get_file_name() | ||
| 133 | # Convert absolute file name to relative file name | ||
| 134 | # '/web/imageserver/testing/AddKbAndTipIdTester.pl' => 'AddKbAndTipIdTester.pl' | ||
| 135 | # @return filename | ||
| 136 | sub get_file_name() { | ||
| 137 | my ($self, $params) = @_; | ||
| 138 | |||
| 139 | my @split = split('/', $self->file_location); | ||
| 140 | return pop @split; | ||
| 141 | } | ||
| 142 | |||
| 143 | ## @method object get_file_directory() | ||
| 144 | # Get the file directory for the file_location | ||
| 145 | # @return file directory | ||
| 146 | sub get_file_directory() { | ||
| 147 | my ($self, $params) = @_; | ||
| 148 | |||
| 149 | #Return the path excluding the relative name | ||
| 150 | #Ex: '/web/imageserver/testing/ManyAssessors.pm' => '/web/imageserver/testing/' | ||
| 151 | #Return empty string if relative path = absolute path | ||
| 152 | #Ex: 'ManyAssessors.pm' => '' | ||
| 153 | return ($self->file_location =~ /(.*)(\/)(.*)/) ? ($1 . $2) : ''; | ||
| 154 | } | ||
| 155 | |||
| 156 | ## @method protected object _read_file() | ||
| 157 | # Get the file contents from the file_location | ||
| 158 | # @return Contents of the file as array | ||
| 159 | sub _read_file() { | ||
| 160 | my ($self, $params) = @_; | ||
| 161 | |||
| 162 | open(HANDLE, $self->file_location()) | ||
| 163 | or throw RWDE::DevelException({ info => 'Could not open file' }); | ||
| 164 | |||
| 165 | my @file_array = <HANDLE>; | ||
| 166 | |||
| 167 | close(HANDLE) | ||
| 168 | or throw RWDE::DevelException({ info => 'Could not close file' }); | ||
| 169 | |||
| 170 | return \@file_array; | ||
| 171 | } | ||
| 172 | |||
| 173 | ## @method void save($save_file_name) | ||
| 174 | # Write the documented file to disk. If param save_file_name exists, then move current file to file_location + save_file_name. | ||
| 175 | # Place new file at old location. | ||
| 176 | # If params save_file_name dne, then simply save it to file_location . ".doxy" | ||
| 177 | # @param save_file_name The name of the destination file to save to | ||
| 178 | sub save() { | ||
| 179 | my ($self, $params) = @_; | ||
| 180 | |||
| 181 | my $new_file_path; | ||
| 182 | |||
| 183 | #If a relative file name is passed in: move the existing file at file_location over to file_directory . save_file_name | ||
| 184 | #Save new file into former location. | ||
| 185 | if (defined($$params{save_file_name})) { | ||
| 186 | rename($self->file_location, $self->get_file_directory() . $$params{save_file_name}); | ||
| 187 | $new_file_path = $self->file_location(); | ||
| 188 | } | ||
| 189 | |||
| 190 | #Otherwise simply append .doxy to the end of the current location and leave alone the original file. | ||
| 191 | else { | ||
| 192 | $new_file_path = $self->file_location . '.doxy'; | ||
| 193 | } | ||
| 194 | |||
| 195 | open(WRITE_HANDLE, '+>' . $new_file_path) | ||
| 196 | or throw RWDE::DevelException({ info => 'Could not open file' }); | ||
| 197 | my $out = join('', @{ $self->file_content_out }); | ||
| 198 | print WRITE_HANDLE $out; | ||
| 199 | close(WRITE_HANDLE) | ||
| 200 | or throw RWDE::DevelException({ info => 'Could not close file' }); | ||
| 201 | |||
| 202 | return (); | ||
| 203 | } | ||
| 204 | |||
| 205 | ## @method void add_to_file_out($content) | ||
| 206 | # Add new line to update_file_data | ||
| 207 | # @param content (Enter explanation for param here) | ||
| 208 | sub add_to_file_out() { | ||
| 209 | my ($self, $params) = @_; | ||
| 210 | |||
| 211 | my @lines; | ||
| 212 | if (ref $$params{content} eq 'ARRAY') { | ||
| 213 | @lines = @{ $$params{content} }; | ||
| 214 | } | ||
| 215 | elsif (defined $$params{content}) { | ||
| 216 | push @lines, $$params{content}; | ||
| 217 | } | ||
| 218 | |||
| 219 | foreach my $line (@lines) { | ||
| 220 | push(@{ $self->file_content_out }, $line); | ||
| 221 | } | ||
| 222 | |||
| 223 | return (); | ||
| 224 | } | ||
| 225 | |||
| 226 | ## @method void add_range_to_file_out($end_index, $start_index) | ||
| 227 | # Copy lines over from file_content to file_content_out | ||
| 228 | # @param end_index Last line number to copy | ||
| 229 | # @param start_index First line number to copy | ||
| 230 | sub add_range_to_file_out() { | ||
| 231 | my ($self, $params) = @_; | ||
| 232 | |||
| 233 | my @required = qw( start_index end_index ); | ||
| 234 | RWDE::RObject->check_params({ required => \@required, supplied => $params }); | ||
| 235 | |||
| 236 | for (my $file_index = $$params{start_index} ; $file_index <= $$params{end_index} ; $file_index++) { | ||
| 237 | $self->add_to_file_out({ content => $self->file_content_in->[$file_index] }); | ||
| 238 | } | ||
| 239 | |||
| 240 | return (); | ||
| 241 | } | ||
| 242 | |||
| 243 | ## @method object add_new_class() | ||
| 244 | # Create a new class data object and add it to file_classes hash | ||
| 245 | # @return (Enter explanation for return value here) | ||
| 246 | sub add_new_class() { | ||
| 247 | my ($self, $params) = @_; | ||
| 248 | |||
| 249 | my $class_data = new RWDE::Doxy::ClassData($params); | ||
| 250 | |||
| 251 | $self->file_classes->{ $class_data->class_name } = $class_data; | ||
| 252 | |||
| 253 | return $class_data; | ||
| 254 | } | ||
| 255 | |||
| 256 | ## @method object get_class($class_name) | ||
| 257 | # Returns the class_method object for a class name or creates and returns a new one if one does not exists. | ||
| 258 | # @param class_name (Enter explanation for param here) | ||
| 259 | # @return (Enter explanation for return value here) | ||
| 260 | sub get_class() { | ||
| 261 | my ($self, $params) = @_; | ||
| 262 | |||
| 263 | my @required = qw( class_name ); | ||
| 264 | RWDE::RObject->check_params({ required => \@required, supplied => $params }); | ||
| 265 | |||
| 266 | my $class_data; | ||
| 267 | |||
| 268 | if (not defined($self->file_classes->{ $$params{class_name} })) { | ||
| 269 | $class_data = $self->add_new_class({ class_name => $$params{class_name} }); | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | $class_data = $self->file_classes->{ $$params{class_name} }; | ||
| 273 | } | ||
| 274 | |||
| 275 | return $class_data; | ||
| 276 | } | ||
| 277 | |||
| 278 | ## @method object get_current_class() | ||
| 279 | # Returns the current class object in class_data form | ||
| 280 | # @return (Enter explanation for return value here) | ||
| 281 | sub get_current_class() { | ||
| 282 | my ($self, $params) = @_; | ||
| 283 | |||
| 284 | return $self->get_class({ class_name => $self->file_current_class }); | ||
| 285 | } | ||
| 286 | |||
| 287 | ## @method void add_info($info_line) | ||
| 288 | # Add a param to the parameter list | ||
| 289 | # @param info_line (Enter explanation for param here) | ||
| 290 | sub add_info() { | ||
| 291 | my ($self, $params) = @_; | ||
| 292 | |||
| 293 | throw RWDE::DataMissingException({ info => 'No info line to add to info_list' }) | ||
| 294 | unless defined($$params{info_line}); | ||
| 295 | |||
| 296 | my $file_info = $self->file_info; | ||
| 297 | |||
| 298 | push(@{$file_info}, $$params{info_line}); | ||
| 299 | |||
| 300 | return (); | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | 1; |