ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.8.4.1
Committed: Thu Aug 17 15:59:22 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.8: +57 -39 lines
Log Message:
Integrated to simpledoc from dev

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