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 |
# 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 |
# 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(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 |
$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 realline {
|
65 |
my $self=shift;
|
66 |
my $origline=shift;
|
67 |
|
68 |
my $fileob=$self;
|
69 |
my $line=$origline;
|
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+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 |
($line, $fileob)=$self->{includes}[$i]->realline($line-$startline);
|
81 |
last;
|
82 |
}
|
83 |
}
|
84 |
|
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->{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->{cache}->file("_preprocess_".$self->url());
|
109 |
}
|
110 |
|
111 |
sub file {
|
112 |
my $self=shift;
|
113 |
|
114 |
my ($url, $file)=$self->{switch}->urlget($self->url());
|
115 |
$self->verbose("Getting file ".$self->url()." = $file");
|
116 |
return $file;
|
117 |
}
|
118 |
|
119 |
sub ProcessFile {
|
120 |
my $self=shift;
|
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 |
# -- 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 |
|
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->{cache}->filename($outfile);
|
157 |
|
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
|
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->{switch}->parse("include", $self->{fileout}, "include_starttag");
|
188 |
|
189 |
$self->{fileout}->close();
|
190 |
$self->verbose("$fileout Created");
|
191 |
}
|
192 |
|
193 |
sub store {
|
194 |
my $self=shift;
|
195 |
my $location=shift;
|
196 |
|
197 |
my $fh=$self->openfile(">".$location);
|
198 |
|
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->{includesdesc}} ) {
|
205 |
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 |
my $fh=$self->openfile("<".$location);
|
216 |
|
217 |
$self->{lastsequence}=<$fh>;
|
218 |
chomp $self->{lastsequence};
|
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 ">\n") {
|
231 |
$inc=ActiveDoc::IncFile->new($self->{dbstore});
|
232 |
$inc->restore($fh);
|
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();
|
239 |
}
|
240 |
|
241 |
sub _cleanup {
|
242 |
my $self=shift;
|
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 |
#
|
268 |
# Include tag
|
269 |
|
270 |
sub Include_Start {
|
271 |
my $self=shift;
|
272 |
my $name=shift;
|
273 |
my $hashref=shift;
|
274 |
|
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 ) {
|
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 |
my $linecount=0;
|
285 |
while ( <$fh> ) {
|
286 |
$linecount++;
|
287 |
print {$self->{fileout}} $_;
|
288 |
}
|
289 |
print {$self->{fileout}} "\n";# always end include with new line
|
290 |
undef $fh;
|
291 |
$self->_includefile($fileObj,
|
292 |
$self->{switch}->currentparser()->tagstartline(),
|
293 |
$self->{switch}->currentparser()->line(), $linecount);
|
294 |
}
|
295 |
}
|
296 |
|