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