ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.6
Committed: Mon Feb 21 14:30:08 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.5: +12 -7 lines
Log Message:
ProcessFile method - override

File Contents

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