ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.8
Committed: Wed Mar 1 11:47:38 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Branch point for: HPWbranch, V0_9branch
Changes since 1.7: +2 -2 lines
Log Message:
Add new verbose comments

File Contents

# User Rev Content
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 williamc 1.4 # realline(number): Return the line and fileobj corresponding to number in
19 williamc 1.1 # 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 williamc 1.4 sub realline {
40 williamc 1.1 my $self=shift;
41     my $origline=shift;
42    
43     my $fileob=$self;
44     my $line=$origline;
45 williamc 1.3 for(my $i=0; $i<=$#{$self->{includesdesc}}; $i++ ) {
46     $inc=$self->{includesdesc}[$i];
47 williamc 1.1 $startline=$inc->startline();
48 williamc 1.5 last if ( $line <= $startline );
49 williamc 1.6 if ( $line >= ($inc->lines()+$startline+2) ) {
50     # take out the 2 carriage returns added
51 williamc 1.5 $line=$line-($inc->lines()+$startline-$inc->endline())-2;
52 williamc 1.1 # n lines in original map to m lines in expanded
53     }
54     else { # must be in the include file
55 williamc 1.7 ($line, $fileob)=$self->{includes}[$i]->realline($line-$startline);
56     last;
57 williamc 1.1 }
58     }
59    
60     return ($line, $fileob);
61 williamc 1.4 }
62    
63     sub line {
64     my $self=shift;
65     my $line=$self->{currentparser}->line();
66     return $line, $self;
67 williamc 1.1 }
68    
69     sub url {
70     my $self=shift;
71    
72     if ( @_ ) {
73 williamc 1.6 my $url=shift;
74     ($self->{url}, $file)=$self->{urlhandler}->get($url);
75     }
76     else {
77     $self->{url};
78     }
79 williamc 1.1 }
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 williamc 1.8 $self->verbose("Getting file ".$self->url()." = $file");
91 williamc 1.1 return $file;
92     }
93    
94 williamc 1.6 sub ProcessFile {
95     my $self=shift;
96     return $self->file();
97     }
98 williamc 1.1
99     sub update {
100     my $self=shift;
101    
102     my $rv=0;
103 williamc 1.3 my $outfile="_preprocess_".$self->url();
104     my $fileobj;
105     my $sn;
106     @{$self->{updatedfiles}}=();
107 williamc 1.1
108 williamc 1.3 # -- check the input file snd output sequence numbers are in sync
109     my $basefilenumber=$self->config()->cache()->updatenumber($self->url());
110     if ( $basefilenumber != $self->{lastsequence} ) {
111     $rv=1;
112     }
113     else {
114     # -- update dependencies
115     for (my $i=0; $i<=$#{$self->{includes}}; $i++ ) {
116     $fileobj=$self->{includes}[$i];
117     $rv=$rv+$fileobj->update(); # make sure it up to date
118    
119     # -- has it changed since last time we built this object
120     $sn=$self->config()->sequence($fileobj->url());
121     if ( $self->{includesdesc}[$i]->lastsequence() != $sn ) {
122     $rv++;
123     push @{$self->{updatedfiles}},$fileobj->url(); # record for test
124     }
125     }
126 williamc 1.1 }
127 williamc 1.3
128     if ( $rv != 0 ) {
129 williamc 1.5 $self->verbose(" Need to Update ".$self->url());
130 williamc 1.1 # ---- sort out the preprocessed file in the cache
131 williamc 1.8 my $newfile=$self->config()->cache()->filename($outfile);
132 williamc 1.3 $self->config()->cache()->store($outfile,$newfile);
133 williamc 1.1
134     $self->process($self->config()->cache()->file($self->url()),$newfile);
135 williamc 1.3 $self->{lastsequence}=$basefilenumber;
136     # store self in the objectstore by url
137     $self->config->store($self,$self->url());
138     }
139     else {
140 williamc 1.5 $self->verbose("No Need to Update ".$self->url());
141 williamc 1.1 }
142     return $rv;
143     }
144    
145 williamc 1.3 sub updatedfiles {
146     my $self=shift;
147     return @{$self->{updatedfiles}};
148     }
149    
150 williamc 1.1 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 williamc 1.5 $self->verbose("$fileout Created");
166 williamc 1.1 }
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 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
179 williamc 1.1 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 williamc 1.3 my $fh=$self->openfile("<".$location);
190 williamc 1.1
191     $self->{lastsequence}=<$fh>;
192     chomp $self->{lastsequence};
193     $self->{url}=<$fh>;
194     chomp $self->{url};
195     while ( <$fh> ) {
196 williamc 1.3 if ( $_ eq ">\n") {
197 williamc 1.1 $inc=IncFile->new();
198     $inc->restore($fh);
199 williamc 1.3 push @{$self->{includesdesc}}, $inc;
200     # resurrect the appropriate object ID
201     push @{$self->{includes}}, $self->getfile($inc->file());
202 williamc 1.1 }
203     }
204     $fh->close();
205     }
206    
207     sub _cleanup {
208     my $self=shift;
209 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
210 williamc 1.1 undef $inc;
211     }
212 williamc 1.3 undef @{$self->{includesdesc}};
213 williamc 1.1 undef @{$self->{includes}};
214     }
215    
216 williamc 1.3 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 williamc 1.1 # ------------------------ 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 williamc 1.3 my $linecount=0;
252 williamc 1.1 while ( <$fh> ) {
253 williamc 1.3 $linecount++;
254 williamc 1.1 print {$self->{fileout}} $_;
255     }
256     print {$self->{fileout}} "\n";# always end include with new line
257     undef $fh;
258 williamc 1.3 $self->_includefile($fileObj, $self->{currentparser}->tagstartline(),
259     $self->{currentparser}->line(), $linecount);
260 williamc 1.1 }
261     }
262