ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.8.4.1
Committed: Thu Aug 17 15:59:22 2000 UTC (24 years, 9 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.8: +57 -39 lines
Log Message:
Integrated to simpledoc from dev

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