ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.8.4.2
Committed: Mon Aug 21 18:40:19 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.8.4.1: +19 -4 lines
Log Message:
Mods for PreProcessedFile int change

File Contents

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