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 |
|
|
# 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 |
|
|
# line(number) : Return the line and fileobj corresponding to number in
|
19 |
|
|
# processed file
|
20 |
|
|
# includefile(PreProcessedFileID, startline, endline, lines_in_file) :
|
21 |
|
|
# indicate a file object to be included in place of
|
22 |
|
|
# startline-endline in original file.
|
23 |
|
|
# update() : update the preprocessed file as required.
|
24 |
|
|
# store(filename) :
|
25 |
|
|
# restore(filename) :
|
26 |
|
|
|
27 |
|
|
package ActiveDoc::PreProcessedFile;
|
28 |
|
|
use ActiveDoc::ActiveDoc;
|
29 |
|
|
require 5.004;
|
30 |
|
|
@ISA=qw(ActiveDoc::ActiveDoc);
|
31 |
|
|
|
32 |
|
|
sub init {
|
33 |
|
|
my $self=shift;
|
34 |
|
|
$self->{lastsequence}=-1;
|
35 |
|
|
# Specific Tags
|
36 |
|
|
$self->newparse("include");
|
37 |
|
|
$self->addurltags("include");
|
38 |
|
|
$self->addtag("include","Include", \&Include_Start, $self,
|
39 |
|
|
"", $self, "", $self);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
sub line {
|
43 |
|
|
my $self=shift;
|
44 |
|
|
my $origline=shift;
|
45 |
|
|
|
46 |
|
|
my $fileob=$self;
|
47 |
|
|
my $line=$origline;
|
48 |
|
|
foreach $inc ( @{$self->{includes}} ) {
|
49 |
|
|
$startline=$inc->startline();
|
50 |
|
|
last if ( $line < $startline );
|
51 |
|
|
if ( $line > ($inc->lines()+$startline) ) {
|
52 |
|
|
$line=$line-$inc->lines()+1+($inc->endline()-$startline);
|
53 |
|
|
# n lines in original map to m lines in expanded
|
54 |
|
|
}
|
55 |
|
|
else { # must be in the include file
|
56 |
|
|
@fileobs=$self->config()->find($inc->file());
|
57 |
|
|
print $inc->file()." line = $line-$startline $fileob\n";
|
58 |
|
|
($line, $fileob)=$fileobs[0]->line($line-$startline+1);
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
print "line = $line $fileob\n";
|
63 |
|
|
return ($line, $fileob);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
sub url {
|
67 |
|
|
my $self=shift;
|
68 |
|
|
|
69 |
|
|
if ( @_ ) {
|
70 |
|
|
my $url=shift;
|
71 |
|
|
($self->{url}, $file)=$self->{urlhandler}->get($url);
|
72 |
|
|
}
|
73 |
|
|
else {
|
74 |
|
|
$self->{url};
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
sub ProcessedFile {
|
79 |
|
|
my $self=shift;
|
80 |
|
|
$self->config()->cache()->file("_preprocess_".$self->url());
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
sub file {
|
84 |
|
|
my $self=shift;
|
85 |
|
|
|
86 |
|
|
my ($url, $file)=$self->{urlhandler}->get($self->url());
|
87 |
|
|
print $file." = File\n";
|
88 |
|
|
return $file;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
sub includefile {
|
92 |
|
|
use ActiveDoc::IncFile;
|
93 |
|
|
my $self=shift;
|
94 |
|
|
my $fileobj=shift;
|
95 |
|
|
my $startline=shift;
|
96 |
|
|
my $endline=shift;
|
97 |
|
|
my $lines=shift;
|
98 |
|
|
|
99 |
|
|
my $obj=IncFile->new();
|
100 |
|
|
push @{$self->{includes}}, $obj;
|
101 |
|
|
$obj->init($fileobj,$startline,$endline,$lines);
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
sub update {
|
105 |
|
|
my $self=shift;
|
106 |
|
|
|
107 |
|
|
my $rv=0;
|
108 |
|
|
|
109 |
|
|
print "\nEntering Updating ".$self->url()."\n";
|
110 |
|
|
# check file update OK
|
111 |
|
|
if ( $self->{lastsequence} !=
|
112 |
|
|
$self->config()->cache()->updatenumber($self->url()) ){
|
113 |
|
|
$rv=1;
|
114 |
|
|
}
|
115 |
|
|
# check dependencies
|
116 |
|
|
foreach $inc ( @{$self->{includes}} ) {
|
117 |
|
|
@fileobs=$self->config()->find($inc->file());
|
118 |
|
|
$rv=$rv+$fileobjs[0]->update();
|
119 |
|
|
if ( $inc->lastsequence() ne
|
120 |
|
|
$self->ObjectSpace->sequence($inc->file()) ) {
|
121 |
|
|
$rv=0;
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
if ( $rv ) {
|
125 |
|
|
print " Need to Update\n";
|
126 |
|
|
# ---- sort out the preprocessed file in the cache
|
127 |
|
|
my $newfile=$self->config()->cache()->
|
128 |
|
|
filename("_preprocess_".$self->url());
|
129 |
|
|
$self->config()->cache()->store("_preprocess_".$self->url(),$newfile);
|
130 |
|
|
|
131 |
|
|
$self->process($self->config()->cache()->file($self->url()),$newfile);
|
132 |
|
|
$self->{lastsequence}++;
|
133 |
|
|
}
|
134 |
|
|
return $rv;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
sub process {
|
138 |
|
|
my $self=shift;
|
139 |
|
|
my $filein=shift;
|
140 |
|
|
my $fileout=shift;
|
141 |
|
|
|
142 |
|
|
#-- create a new file in the url cache
|
143 |
|
|
print "PreProcessing $file\n output= $fileout \n";
|
144 |
|
|
$self->_cleanup();
|
145 |
|
|
$self->{fileout}=FileHandle->new();
|
146 |
|
|
$self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
|
147 |
|
|
."$!\n";
|
148 |
|
|
|
149 |
|
|
# turn on the switch streamer
|
150 |
|
|
$self->parse("include", $self->{fileout}, "include_starttag");
|
151 |
|
|
|
152 |
|
|
$self->{fileout}->close();
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
sub store {
|
156 |
|
|
my $self=shift;
|
157 |
|
|
my $location=shift;
|
158 |
|
|
|
159 |
|
|
my $fh=$self->openfile(">".$location);
|
160 |
|
|
|
161 |
|
|
# get all include objects to store themselves
|
162 |
|
|
print $fh $self->{lastsequence}."\n";
|
163 |
|
|
print $fh ($self->url()?$self->url():"");
|
164 |
|
|
print $fh "\n";
|
165 |
|
|
foreach $inc ( @{$self->{includes}} ) {
|
166 |
|
|
print $fh ">\n";
|
167 |
|
|
$inc->store($fh);
|
168 |
|
|
}
|
169 |
|
|
close $fh;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
sub restore {
|
173 |
|
|
my $self=shift;
|
174 |
|
|
my $location=shift;
|
175 |
|
|
|
176 |
|
|
my $fh=$self->openfile($location);
|
177 |
|
|
|
178 |
|
|
$self->{lastsequence}=<$fh>;
|
179 |
|
|
chomp $self->{lastsequence};
|
180 |
|
|
$self->{url}=<$fh>;
|
181 |
|
|
chomp $self->{url};
|
182 |
|
|
while ( <$fh> ) {
|
183 |
|
|
if ( $_ eq ">") {
|
184 |
|
|
$inc=IncFile->new();
|
185 |
|
|
$inc->restore($fh);
|
186 |
|
|
push @{$self->{includes}}, $inc;
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
$fh->close();
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
sub _cleanup {
|
193 |
|
|
my $self=shift;
|
194 |
|
|
foreach $inc ( @{$self->{includes}} ) {
|
195 |
|
|
undef $inc;
|
196 |
|
|
}
|
197 |
|
|
undef @{$self->{includes}};
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
# ------------------------ Tag Routines -------------------------------
|
201 |
|
|
|
202 |
|
|
#
|
203 |
|
|
# Include tag
|
204 |
|
|
|
205 |
|
|
sub Include_Start {
|
206 |
|
|
my $self=shift;
|
207 |
|
|
my $name=shift;
|
208 |
|
|
my $hashref=shift;
|
209 |
|
|
|
210 |
|
|
$self->checktag( $name,$hashref, "url");
|
211 |
|
|
print "Including ".$$hashref{'url'}."\n";
|
212 |
|
|
my $fileObj=$self->getfile($$hashref{'url'});
|
213 |
|
|
if ( defined $fileObj ) {
|
214 |
|
|
push @{$self->{includes}}, $fileObj;
|
215 |
|
|
# dump out to our file in construction
|
216 |
|
|
my $fh=FileHandle->new();
|
217 |
|
|
my $outfilename=$fileObj->ProcessedFile();
|
218 |
|
|
print "Opening ".$fileObj->ProcessedFile()." for inclusion\n";
|
219 |
|
|
$fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
|
220 |
|
|
print {$self->{fileout}} "\n";# always start an include on a new line
|
221 |
|
|
while ( <$fh> ) {
|
222 |
|
|
print {$self->{fileout}} $_;
|
223 |
|
|
}
|
224 |
|
|
print {$self->{fileout}} "\n";# always end include with new line
|
225 |
|
|
undef $fh;
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
|