ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.10.2.1
Committed: Fri Feb 27 15:34:54 2004 UTC (21 years, 2 months ago) by sashby
Content type: text/plain
Branch: SCRAM_V1_BRANCH
CVS Tags: V1_pre0, SCRAM_V1, SCRAMV1_IMPORT
Branch point for: V1_pre1
Changes since 1.10: +1 -1 lines
Log Message:
First import of new SCRAM packages into CMS cvs repos.

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.9 # 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.9 # 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.9 use ActiveDoc::IncFile;
27     use Utilities::Verbose;
28     use ObjectUtilities::StorableObject;
29 williamc 1.1 require 5.004;
30 williamc 1.9 @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 williamc 1.1
52     sub init {
53     my $self=shift;
54     $self->{lastsequence}=-1;
55 williamc 1.9 $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 williamc 1.1 "", $self, "", $self);
62     }
63    
64 williamc 1.4 sub realline {
65 williamc 1.1 my $self=shift;
66     my $origline=shift;
67    
68     my $fileob=$self;
69     my $line=$origline;
70 williamc 1.3 for(my $i=0; $i<=$#{$self->{includesdesc}}; $i++ ) {
71     $inc=$self->{includesdesc}[$i];
72 williamc 1.1 $startline=$inc->startline();
73 williamc 1.5 last if ( $line <= $startline );
74 williamc 1.6 if ( $line >= ($inc->lines()+$startline+2) ) {
75     # take out the 2 carriage returns added
76 williamc 1.5 $line=$line-($inc->lines()+$startline-$inc->endline())-2;
77 williamc 1.1 # n lines in original map to m lines in expanded
78     }
79     else { # must be in the include file
80 williamc 1.7 ($line, $fileob)=$self->{includes}[$i]->realline($line-$startline);
81     last;
82 williamc 1.1 }
83     }
84    
85     return ($line, $fileob);
86 williamc 1.4 }
87    
88     sub line {
89     my $self=shift;
90 williamc 1.9 my $line=$self->{switch}->currentparser()->line();
91 williamc 1.4 return $line, $self;
92 williamc 1.1 }
93    
94     sub url {
95     my $self=shift;
96    
97     if ( @_ ) {
98 williamc 1.9 my $url=shift;
99     ($self->{url}, $file)=$self->{switch}->urlget($url);
100     $self->{switch}->filetoparse($file);
101     $self->{switch}->filenameref($self->{url}." ($file)");
102 williamc 1.6 }
103 williamc 1.9 return $self->{url};
104 williamc 1.1 }
105    
106     sub ProcessedFile {
107     my $self=shift;
108 williamc 1.9 $self->{cache}->file("_preprocess_".$self->url());
109 williamc 1.1 }
110    
111     sub file {
112     my $self=shift;
113    
114 williamc 1.9 my ($url, $file)=$self->{switch}->urlget($self->url());
115 williamc 1.8 $self->verbose("Getting file ".$self->url()." = $file");
116 williamc 1.1 return $file;
117     }
118    
119 williamc 1.6 sub ProcessFile {
120     my $self=shift;
121     return $self->file();
122     }
123 williamc 1.1
124     sub update {
125     my $self=shift;
126    
127     my $rv=0;
128 williamc 1.3 my $outfile="_preprocess_".$self->url();
129     my $fileobj;
130     my $sn;
131     @{$self->{updatedfiles}}=();
132 sashby 1.10.2.1
133 williamc 1.3 # -- check the input file snd output sequence numbers are in sync
134 williamc 1.9 my $basefilenumber=$self->{cache}->updatenumber($self->url());
135 williamc 1.3 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 williamc 1.9 $sn=$self->{dbstore}->sequence($fileobj->url());
146 williamc 1.3 if ( $self->{includesdesc}[$i]->lastsequence() != $sn ) {
147     $rv++;
148     push @{$self->{updatedfiles}},$fileobj->url(); # record for test
149     }
150     }
151 williamc 1.1 }
152 williamc 1.3
153     if ( $rv != 0 ) {
154 williamc 1.5 $self->verbose(" Need to Update ".$self->url());
155 williamc 1.1 # ---- sort out the preprocessed file in the cache
156 williamc 1.9 my $newfile=$self->{cache}->filename($outfile);
157 williamc 1.1
158 williamc 1.9 $self->process($self->{cache}->file($self->url()),$newfile);
159     $self->{cache}->store($outfile,$newfile);
160 williamc 1.3 $self->{lastsequence}=$basefilenumber;
161 williamc 1.9 # -- store self in the objectstore by url
162     $self->{dbstore}->store($self,$self->url());
163 williamc 1.3 }
164     else {
165 williamc 1.5 $self->verbose("No Need to Update ".$self->url());
166 williamc 1.1 }
167     return $rv;
168     }
169    
170 williamc 1.3 sub updatedfiles {
171     my $self=shift;
172     return @{$self->{updatedfiles}};
173     }
174    
175 williamc 1.1 sub process {
176     my $self=shift;
177     my $filein=shift;
178     my $fileout=shift;
179    
180 williamc 1.9 # -- create a new file in the url cache
181 williamc 1.1 $self->_cleanup();
182     $self->{fileout}=FileHandle->new();
183     $self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
184     ."$!\n";
185    
186 williamc 1.9 # -- turn on the switch streamer
187     $self->{switch}->parse("include", $self->{fileout}, "include_starttag");
188 williamc 1.1
189     $self->{fileout}->close();
190 williamc 1.5 $self->verbose("$fileout Created");
191 williamc 1.1 }
192    
193     sub store {
194     my $self=shift;
195     my $location=shift;
196    
197     my $fh=$self->openfile(">".$location);
198    
199 williamc 1.9 # -- get all include objects to store themselves
200 williamc 1.1 print $fh $self->{lastsequence}."\n";
201 williamc 1.9 print $fh ref($self->cache()).":::".$self->cache()->location()."\n";
202 williamc 1.1 print $fh ($self->url()?$self->url():"");
203     print $fh "\n";
204 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
205 williamc 1.1 print $fh ">\n";
206     $inc->store($fh);
207     }
208     close $fh;
209     }
210    
211     sub restore {
212     my $self=shift;
213     my $location=shift;
214    
215 williamc 1.3 my $fh=$self->openfile("<".$location);
216 williamc 1.1
217     $self->{lastsequence}=<$fh>;
218     chomp $self->{lastsequence};
219 williamc 1.9 # -- 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 williamc 1.1 while ( <$fh> ) {
230 williamc 1.3 if ( $_ eq ">\n") {
231 williamc 1.9 $inc=ActiveDoc::IncFile->new($self->{dbstore});
232 williamc 1.1 $inc->restore($fh);
233 williamc 1.3 push @{$self->{includesdesc}}, $inc;
234     # resurrect the appropriate object ID
235 williamc 1.9 push @{$self->{includes}}, $self->{switch}->getfile($inc->file());
236 williamc 1.1 }
237     }
238     $fh->close();
239     }
240    
241     sub _cleanup {
242     my $self=shift;
243 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
244 williamc 1.1 undef $inc;
245     }
246 williamc 1.3 undef @{$self->{includesdesc}};
247 williamc 1.1 undef @{$self->{includes}};
248     }
249    
250 williamc 1.3 sub _includefile {
251     my $self=shift;
252     my $fileobj=shift;
253     my $startline=shift;
254     my $endline=shift;
255     my $lines=shift;
256    
257 williamc 1.9 my $obj=ActiveDoc::IncFile->new();
258 williamc 1.3 my $url=$fileobj->url();
259 williamc 1.9 my $sn=$self->{dbstore}->sequence($url);
260 williamc 1.3 $obj->init($url,$startline,$endline,$lines, $sn);
261     push @{$self->{includes}}, $fileobj;
262     push @{$self->{includesdesc}}, $obj;
263     }
264    
265 williamc 1.1 # ------------------------ Tag Routines -------------------------------
266    
267     #
268     # Include tag
269    
270     sub Include_Start {
271     my $self=shift;
272     my $name=shift;
273     my $hashref=shift;
274    
275 williamc 1.9 $self->{switch}->checktag( $name,$hashref, "url");
276     $self->verbose("Including ".$$hashref{'url'});
277     my $fileObj=$self->{switch}->getfile($$hashref{'url'});
278 williamc 1.1 if ( defined $fileObj ) {
279     # dump out to our file in construction
280     my $fh=FileHandle->new();
281     my $outfilename=$fileObj->ProcessedFile();
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 williamc 1.3 my $linecount=0;
285 williamc 1.1 while ( <$fh> ) {
286 sashby 1.10 # Enable comments in files. Skip line if it
287     # starts with a hash:
288     next if /^#/;
289     $linecount++;
290 williamc 1.1 print {$self->{fileout}} $_;
291     }
292     print {$self->{fileout}} "\n";# always end include with new line
293     undef $fh;
294 williamc 1.9 $self->_includefile($fileObj,
295     $self->{switch}->currentparser()->tagstartline(),
296     $self->{switch}->currentparser()->line(), $linecount);
297 williamc 1.1 }
298     }
299