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