ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
(Generate patch)

Comparing COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm (file contents):
Revision 1.1 by williamc, Fri Nov 19 15:26:59 1999 UTC vs.
Revision 1.10 by sashby, Fri Oct 11 14:23:24 2002 UTC

# Line 9 | Line 9
9   #
10   # Interface
11   # ---------
12 < # new()         : A new PreProcessedFile object
12 > # new(cache,dbstore)  : A new PreProcessedFile object
13   # url([url])    : return the full url. With an argument will take the given
14   #                 url and expand it in the context of the current url base.
15   # file()        : return the filename corresponding to url of the document
16   # ProcessedFile() : return the filename corresponding to processed url
17   #                       of the document
18 < # line(number)  : Return the line and fileobj corresponding to number in
19 < #                 processed file
20 < # includefile(PreProcessedFileID, startline, endline, lines_in_file) :
21 < #                  indicate a file object to be included in place of
22 < #                  startline-endline in original file.
23 < # update()          : update the preprocessed file as required.
24 < # store(filename)   :
18 > # realline(number): Return the line and fileobj corresponding to number in
19 > #                   processed file
20 > # update()        : update the preprocessed file as required.
21 > # store(filename) :
22   # restore(filename) :
23  
24   package ActiveDoc::PreProcessedFile;
25   use ActiveDoc::ActiveDoc;
26 + use ActiveDoc::IncFile;
27 + use Utilities::Verbose;
28 + use ObjectUtilities::StorableObject;
29   require 5.004;
30 < @ISA=qw(ActiveDoc::ActiveDoc);
30 > @ISA=qw(ObjectUtilities::StorableObject Utilities::Verbose);
31 >
32 >
33 > sub new {
34 >        my $class=shift;
35 >        my $self={};
36 >        bless $self, $class;
37 >        $self->{dbstore}=shift;
38 >        $self->cache($self->{dbstore}->cache());
39 >        bless $self, $class;
40 >        return $self;
41 > }
42 >
43 > sub cache {
44 >        my $self=shift;
45 >        if ( @_ ) {
46 >           $self->{cache}=shift;
47 >           $self->init();
48 >        }
49 >        return $self->{cache};
50 > }
51  
52   sub init {
53          my $self=shift;
54          $self->{lastsequence}=-1;
55 <        # Specific Tags
56 <        $self->newparse("include");
57 <        $self->addurltags("include");
58 <        $self->addtag("include","Include", \&Include_Start, $self,      
55 >        $self->{switch}=ActiveDoc::ActiveDoc->new($self->{dbstore});
56 >
57 >        # -- Specific Tags
58 >        $self->{switch}->newparse("include");
59 >        $self->{switch}->addbasetags("include");
60 >        $self->{switch}->addtag("include","include", \&Include_Start, $self,    
61                                            "", $self, "", $self);
62   }
63  
64 < sub line {
64 > sub realline {
65          my $self=shift;
66          my $origline=shift;
67  
68          my $fileob=$self;
69          my $line=$origline;
70 <        foreach $inc ( @{$self->{includes}} ) {
70 >        for(my $i=0; $i<=$#{$self->{includesdesc}}; $i++ ) {
71 >          $inc=$self->{includesdesc}[$i];
72            $startline=$inc->startline();
73 <          last if ( $line < $startline );
74 <          if ( $line > ($inc->lines()+$startline) ) {
75 <            $line=$line-$inc->lines()+1+($inc->endline()-$startline);
73 >          last if ( $line <= $startline );
74 >          if ( $line >= ($inc->lines()+$startline+2) ) {
75 >                                        # take out the 2 carriage returns added
76 >            $line=$line-($inc->lines()+$startline-$inc->endline())-2;
77                           # n lines in original map to m lines in expanded
78            }
79            else { # must be in the include file
80 <            @fileobs=$self->config()->find($inc->file());
81 <        print $inc->file()." line = $line-$startline $fileob\n";
58 <            ($line, $fileob)=$fileobs[0]->line($line-$startline+1);
80 >            ($line, $fileob)=$self->{includes}[$i]->realline($line-$startline);
81 >            last;
82            }
83          }
84          
62        print "line = $line $fileob\n";
85          return ($line, $fileob);
86   }
87  
88 + sub line {
89 +        my $self=shift;
90 +        my $line=$self->{switch}->currentparser()->line();
91 +        return $line, $self;
92 + }
93 +
94   sub url {
95          my $self=shift;
96  
97          if ( @_ ) {
98 <          my $url=shift;
99 <          ($self->{url}, $file)=$self->{urlhandler}->get($url);
100 <        }
101 <        else {
102 <          $self->{url};
103 <        }
98 >           my $url=shift;
99 >          ($self->{url}, $file)=$self->{switch}->urlget($url);
100 >          $self->{switch}->filetoparse($file);
101 >          $self->{switch}->filenameref($self->{url}." ($file)");
102 >        }
103 >        return $self->{url};
104   }
105  
106   sub ProcessedFile {
107          my $self=shift;
108 <        $self->config()->cache()->file("_preprocess_".$self->url());
108 >        $self->{cache}->file("_preprocess_".$self->url());
109   }
110  
111   sub file {
112          my $self=shift;
113          
114 <        my ($url, $file)=$self->{urlhandler}->get($self->url());
115 <        print $file." = File\n";
114 >        my ($url, $file)=$self->{switch}->urlget($self->url());
115 >        $self->verbose("Getting file ".$self->url()." = $file");
116          return $file;
117   }
118  
119 < sub includefile {
92 <        use ActiveDoc::IncFile;
119 > sub ProcessFile {
120          my $self=shift;
121 <        my $fileobj=shift;
95 <        my $startline=shift;
96 <        my $endline=shift;
97 <        my $lines=shift;
98 <        
99 <        my $obj=IncFile->new();
100 <        push @{$self->{includes}}, $obj;
101 <        $obj->init($fileobj,$startline,$endline,$lines);
121 >        return $self->file();
122   }
123  
124   sub update {
125          my $self=shift;
126  
127          my $rv=0;
128 +        my $outfile="_preprocess_".$self->url();
129 +        my $fileobj;
130 +        my $sn;
131 +        @{$self->{updatedfiles}}=();
132          
133 <        print "\nEntering Updating ".$self->url()."\n";
134 <        # check file update OK
135 <        if ( $self->{lastsequence} !=
136 <                $self->config()->cache()->updatenumber($self->url()) ){
137 <          $rv=1;
138 <        }
139 <        # check dependencies
140 <        foreach $inc ( @{$self->{includes}} ) {
141 <           @fileobs=$self->config()->find($inc->file());
142 <           $rv=$rv+$fileobjs[0]->update();
143 <           if ( $inc->lastsequence() ne
144 <                        $self->ObjectSpace->sequence($inc->file()) ) {
145 <                $rv=0;  
146 <           }
133 >        # -- check the input file snd output sequence numbers are in sync
134 >        my $basefilenumber=$self->{cache}->updatenumber($self->url());
135 >        if ( $basefilenumber != $self->{lastsequence} ) {
136 >            $rv=1;
137 >        }
138 >        else {
139 >          # -- update dependencies
140 >          for (my $i=0; $i<=$#{$self->{includes}}; $i++ ) {
141 >             $fileobj=$self->{includes}[$i];
142 >             $rv=$rv+$fileobj->update(); # make sure it up to date
143 >
144 >             # -- has it changed since last time we built this object
145 >             $sn=$self->{dbstore}->sequence($fileobj->url());
146 >             if ( $self->{includesdesc}[$i]->lastsequence() != $sn ) {
147 >               $rv++;
148 >               push @{$self->{updatedfiles}},$fileobj->url(); # record for test
149 >             }
150 >          }
151          }
152 <        if ( $rv ) {
153 <         print " Need to Update\n";
152 >
153 >        if ( $rv != 0 ) {
154 >         $self->verbose(" Need to Update ".$self->url());
155            # ---- sort out the preprocessed file in the cache
156 <          my $newfile=$self->config()->cache()->
128 <                                filename("_preprocess_".$self->url());
129 <          $self->config()->cache()->store("_preprocess_".$self->url(),$newfile);
156 >          my $newfile=$self->{cache}->filename($outfile);
157            
158 <          $self->process($self->config()->cache()->file($self->url()),$newfile);
159 <          $self->{lastsequence}++;
158 >          $self->process($self->{cache}->file($self->url()),$newfile);
159 >          $self->{cache}->store($outfile,$newfile);
160 >          $self->{lastsequence}=$basefilenumber;
161 >          # -- store self in the objectstore by url
162 >          $self->{dbstore}->store($self,$self->url());
163 >        }
164 >        else {
165 >            $self->verbose("No Need to Update ".$self->url());
166          }
167          return $rv;
168   }
169  
170 + sub updatedfiles {
171 +        my $self=shift;
172 +        return @{$self->{updatedfiles}};
173 + }
174 +
175   sub process {
176          my $self=shift;
177          my $filein=shift;
178          my $fileout=shift;
179  
180 <        #-- create a new file in the url cache
143 <        print "PreProcessing $file\n output= $fileout \n";
180 >        # -- create a new file in the url cache
181          $self->_cleanup();
182          $self->{fileout}=FileHandle->new();
183          $self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
184                                  ."$!\n";
185  
186 <        # turn on the switch streamer
187 <        $self->parse("include", $self->{fileout}, "include_starttag");
186 >        # -- turn on the switch streamer
187 >        $self->{switch}->parse("include", $self->{fileout}, "include_starttag");
188  
189          $self->{fileout}->close();
190 +        $self->verbose("$fileout Created");
191   }
192  
193   sub store {
# Line 158 | Line 196 | sub store {
196  
197          my $fh=$self->openfile(">".$location);
198  
199 <        # get all include objects to store themselves
199 >        # -- get all include objects to store themselves
200          print $fh $self->{lastsequence}."\n";
201 +        print $fh ref($self->cache()).":::".$self->cache()->location()."\n";
202          print $fh ($self->url()?$self->url():"");
203          print $fh "\n";
204 <        foreach $inc ( @{$self->{includes}} ) {
204 >        foreach $inc ( @{$self->{includesdesc}} ) {
205            print $fh ">\n";
206            $inc->store($fh);
207          }
# Line 173 | Line 212 | sub restore {
212          my $self=shift;
213          my $location=shift;
214  
215 <        my $fh=$self->openfile($location);
215 >        my $fh=$self->openfile("<".$location);
216  
217          $self->{lastsequence}=<$fh>;
218          chomp $self->{lastsequence};
219 <        $self->{url}=<$fh>;
220 <        chomp $self->{url};
219 >        # -- recreate the cache
220 >        my $cacheinfo=<$fh>;
221 >        chomp $cacheinfo;
222 >        my ($type,$cachelocation)=split /:::/, $cacheinfo;
223 >        my $cache=$type->new($cachelocation);
224 >        $self->cache($cache);
225 >
226 >        my $url=<$fh>;
227 >        chomp $url;
228 >        $self->url($url);
229          while ( <$fh> ) {
230 <           if ( $_ eq ">") {
231 <                $inc=IncFile->new();
230 >           if ( $_ eq ">\n") {
231 >                $inc=ActiveDoc::IncFile->new($self->{dbstore});
232                  $inc->restore($fh);
233 <                push @{$self->{includes}}, $inc;
233 >                push @{$self->{includesdesc}}, $inc;
234 >                # resurrect the appropriate object ID
235 >                push @{$self->{includes}}, $self->{switch}->getfile($inc->file());
236             }
237          }
238          $fh->close();
# Line 191 | Line 240 | sub restore {
240  
241   sub _cleanup {
242          my $self=shift;
243 <        foreach $inc ( @{$self->{includes}} ) {
243 >        foreach $inc ( @{$self->{includesdesc}} ) {
244            undef $inc;
245          }
246 +        undef @{$self->{includesdesc}};
247          undef @{$self->{includes}};
248   }
249  
250 + sub _includefile {
251 +        my $self=shift;
252 +        my $fileobj=shift;
253 +        my $startline=shift;
254 +        my $endline=shift;
255 +        my $lines=shift;
256 +        
257 +        my $obj=ActiveDoc::IncFile->new();
258 +        my $url=$fileobj->url();
259 +        my $sn=$self->{dbstore}->sequence($url);
260 +        $obj->init($url,$startline,$endline,$lines, $sn);
261 +        push @{$self->{includes}}, $fileobj;
262 +        push @{$self->{includesdesc}}, $obj;
263 + }
264 +
265   # ------------------------ Tag Routines -------------------------------
266  
267   #
# Line 207 | Line 272 | sub Include_Start {
272          my $name=shift;
273          my $hashref=shift;
274  
275 <        $self->checktag( $name,$hashref, "url");
276 <        print "Including ".$$hashref{'url'}."\n";
277 <        my $fileObj=$self->getfile($$hashref{'url'});
275 >        $self->{switch}->checktag( $name,$hashref, "url");
276 >        $self->verbose("Including ".$$hashref{'url'});
277 >        my $fileObj=$self->{switch}->getfile($$hashref{'url'});
278          if ( defined $fileObj ) {
214        push @{$self->{includes}}, $fileObj;
279            # dump out to our file in construction
280            my $fh=FileHandle->new();
281            my $outfilename=$fileObj->ProcessedFile();
218        print "Opening ".$fileObj->ProcessedFile()." for inclusion\n";
282            $fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
283            print {$self->{fileout}} "\n";# always start an include on a new line
284 +          my $linecount=0;
285            while ( <$fh> ) {
286 +          # Enable comments in files. Skip line if it
287 +          # starts with a hash:
288 +          next if /^#/;
289 +          $linecount++;
290              print {$self->{fileout}} $_;
291            }
292            print {$self->{fileout}} "\n";# always end include with new line
293            undef $fh;
294 +          $self->_includefile($fileObj,
295 +                $self->{switch}->currentparser()->tagstartline(),
296 +                $self->{switch}->currentparser()->line(), $linecount);
297          }
298   }
299  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines