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