ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.12
Committed: Wed Aug 17 11:04:33 2005 UTC (19 years, 9 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_3-p4, V1_0_3-p3, V1_0_3-p2, before110xmlBRmerge, V1_0_4p1, V1_0_3-p1, V1_0_3, V1_0_2, V1_0_2_p1
Branch point for: v103_with_xml, v103_branch
Changes since 1.11: +3 -2 lines
Log Message:
Labelled possibly unused packages to check whether possible to remove them later.

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 Utilities::Verbose;
27     use ObjectUtilities::StorableObject;
28 williamc 1.1 require 5.004;
29 williamc 1.9 @ISA=qw(ObjectUtilities::StorableObject Utilities::Verbose);
30    
31    
32     sub new {
33     my $class=shift;
34     my $self={};
35     bless $self, $class;
36     $self->{dbstore}=shift;
37     $self->cache($self->{dbstore}->cache());
38     bless $self, $class;
39     return $self;
40     }
41    
42     sub cache {
43     my $self=shift;
44     if ( @_ ) {
45     $self->{cache}=shift;
46     $self->init();
47     }
48     return $self->{cache};
49     }
50 williamc 1.1
51     sub init {
52     my $self=shift;
53     $self->{lastsequence}=-1;
54 williamc 1.9 $self->{switch}=ActiveDoc::ActiveDoc->new($self->{dbstore});
55    
56     # -- 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.9 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.9 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.9 return $self->{url};
103 williamc 1.1 }
104    
105     sub ProcessedFile {
106     my $self=shift;
107 williamc 1.9 $self->{cache}->file("_preprocess_".$self->url());
108 williamc 1.1 }
109    
110     sub file {
111     my $self=shift;
112    
113 williamc 1.9 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 sashby 1.11
132 williamc 1.3 # -- check the input file snd output sequence numbers are in sync
133 williamc 1.9 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.9 $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.9 my $newfile=$self->{cache}->filename($outfile);
156 williamc 1.1
157 williamc 1.9 $self->process($self->{cache}->file($self->url()),$newfile);
158     $self->{cache}->store($outfile,$newfile);
159 williamc 1.3 $self->{lastsequence}=$basefilenumber;
160 williamc 1.9 # -- 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.9 # -- 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.9 # -- 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.9 # -- get all include objects to store themselves
199 williamc 1.1 print $fh $self->{lastsequence}."\n";
200 williamc 1.9 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.9 # -- recreate the cache
219     my $cacheinfo=<$fh>;
220     chomp $cacheinfo;
221     my ($type,$cachelocation)=split /:::/, $cacheinfo;
222     my $cache=$type->new($cachelocation);
223     $self->cache($cache);
224    
225     my $url=<$fh>;
226     chomp $url;
227     $self->url($url);
228 sashby 1.12
229     use ActiveDoc::IncFile;
230 williamc 1.1 while ( <$fh> ) {
231 williamc 1.3 if ( $_ eq ">\n") {
232 williamc 1.9 $inc=ActiveDoc::IncFile->new($self->{dbstore});
233 williamc 1.1 $inc->restore($fh);
234 williamc 1.3 push @{$self->{includesdesc}}, $inc;
235     # resurrect the appropriate object ID
236 williamc 1.9 push @{$self->{includes}}, $self->{switch}->getfile($inc->file());
237 williamc 1.1 }
238     }
239     $fh->close();
240     }
241    
242     sub _cleanup {
243     my $self=shift;
244 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
245 williamc 1.1 undef $inc;
246     }
247 williamc 1.3 undef @{$self->{includesdesc}};
248 williamc 1.1 undef @{$self->{includes}};
249     }
250    
251 williamc 1.3 sub _includefile {
252     my $self=shift;
253     my $fileobj=shift;
254     my $startline=shift;
255     my $endline=shift;
256     my $lines=shift;
257 sashby 1.12 use ActiveDoc::IncFile;
258 williamc 1.9 my $obj=ActiveDoc::IncFile->new();
259 williamc 1.3 my $url=$fileobj->url();
260 williamc 1.9 my $sn=$self->{dbstore}->sequence($url);
261 williamc 1.3 $obj->init($url,$startline,$endline,$lines, $sn);
262     push @{$self->{includes}}, $fileobj;
263     push @{$self->{includesdesc}}, $obj;
264     }
265    
266 williamc 1.1 # ------------------------ Tag Routines -------------------------------
267    
268     #
269     # Include tag
270    
271     sub Include_Start {
272     my $self=shift;
273     my $name=shift;
274     my $hashref=shift;
275    
276 williamc 1.9 $self->{switch}->checktag( $name,$hashref, "url");
277     $self->verbose("Including ".$$hashref{'url'});
278     my $fileObj=$self->{switch}->getfile($$hashref{'url'});
279 williamc 1.1 if ( defined $fileObj ) {
280     # dump out to our file in construction
281     my $fh=FileHandle->new();
282     my $outfilename=$fileObj->ProcessedFile();
283     $fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
284     print {$self->{fileout}} "\n";# always start an include on a new line
285 williamc 1.3 my $linecount=0;
286 williamc 1.1 while ( <$fh> ) {
287 sashby 1.10 # Enable comments in files. Skip line if it
288     # starts with a hash:
289     next if /^#/;
290     $linecount++;
291 williamc 1.1 print {$self->{fileout}} $_;
292     }
293     print {$self->{fileout}} "\n";# always end include with new line
294     undef $fh;
295 williamc 1.9 $self->_includefile($fileObj,
296     $self->{switch}->currentparser()->tagstartline(),
297     $self->{switch}->currentparser()->line(), $linecount);
298 williamc 1.1 }
299     }
300