ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.2
Committed: Fri Nov 19 17:15:28 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.1: +0 -5 lines
Log Message:
database lookup OK - still unnessasarily updating

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 # 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 ($line, $fileob)=$fileobs[0]->line($line-$startline+1);
58 }
59 }
60
61 return ($line, $fileob);
62 }
63
64 sub url {
65 my $self=shift;
66
67 if ( @_ ) {
68 my $url=shift;
69 ($self->{url}, $file)=$self->{urlhandler}->get($url);
70 }
71 else {
72 $self->{url};
73 }
74 }
75
76 sub ProcessedFile {
77 my $self=shift;
78 $self->config()->cache()->file("_preprocess_".$self->url());
79 }
80
81 sub file {
82 my $self=shift;
83
84 my ($url, $file)=$self->{urlhandler}->get($self->url());
85 return $file;
86 }
87
88 sub includefile {
89 use ActiveDoc::IncFile;
90 my $self=shift;
91 my $fileobj=shift;
92 my $startline=shift;
93 my $endline=shift;
94 my $lines=shift;
95
96 my $obj=IncFile->new();
97 push @{$self->{includes}}, $obj;
98 $obj->init($fileobj,$startline,$endline,$lines);
99 }
100
101 sub update {
102 my $self=shift;
103
104 my $rv=0;
105
106 # check file update OK
107 if ( $self->{lastsequence} !=
108 $self->config()->cache()->updatenumber($self->url()) ){
109 $rv=1;
110 }
111 # check dependencies
112 foreach $inc ( @{$self->{includes}} ) {
113 @fileobs=$self->config()->find($inc->file());
114 $rv=$rv+$fileobjs[0]->update();
115 if ( $inc->lastsequence() ne
116 $self->ObjectSpace->sequence($inc->file()) ) {
117 $rv=0;
118 }
119 }
120 if ( $rv ) {
121 print " Need to Update\n";
122 # ---- sort out the preprocessed file in the cache
123 my $newfile=$self->config()->cache()->
124 filename("_preprocess_".$self->url());
125 $self->config()->cache()->store("_preprocess_".$self->url(),$newfile);
126
127 $self->process($self->config()->cache()->file($self->url()),$newfile);
128 $self->{lastsequence}++;
129 }
130 return $rv;
131 }
132
133 sub process {
134 my $self=shift;
135 my $filein=shift;
136 my $fileout=shift;
137
138 #-- create a new file in the url cache
139 print "PreProcessing $file\n output= $fileout \n";
140 $self->_cleanup();
141 $self->{fileout}=FileHandle->new();
142 $self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
143 ."$!\n";
144
145 # turn on the switch streamer
146 $self->parse("include", $self->{fileout}, "include_starttag");
147
148 $self->{fileout}->close();
149 }
150
151 sub store {
152 my $self=shift;
153 my $location=shift;
154
155 my $fh=$self->openfile(">".$location);
156
157 # get all include objects to store themselves
158 print $fh $self->{lastsequence}."\n";
159 print $fh ($self->url()?$self->url():"");
160 print $fh "\n";
161 foreach $inc ( @{$self->{includes}} ) {
162 print $fh ">\n";
163 $inc->store($fh);
164 }
165 close $fh;
166 }
167
168 sub restore {
169 my $self=shift;
170 my $location=shift;
171
172 my $fh=$self->openfile($location);
173
174 $self->{lastsequence}=<$fh>;
175 chomp $self->{lastsequence};
176 $self->{url}=<$fh>;
177 chomp $self->{url};
178 while ( <$fh> ) {
179 if ( $_ eq ">") {
180 $inc=IncFile->new();
181 $inc->restore($fh);
182 push @{$self->{includes}}, $inc;
183 }
184 }
185 $fh->close();
186 }
187
188 sub _cleanup {
189 my $self=shift;
190 foreach $inc ( @{$self->{includes}} ) {
191 undef $inc;
192 }
193 undef @{$self->{includes}};
194 }
195
196 # ------------------------ Tag Routines -------------------------------
197
198 #
199 # Include tag
200
201 sub Include_Start {
202 my $self=shift;
203 my $name=shift;
204 my $hashref=shift;
205
206 $self->checktag( $name,$hashref, "url");
207 print "Including ".$$hashref{'url'}."\n";
208 my $fileObj=$self->getfile($$hashref{'url'});
209 if ( defined $fileObj ) {
210 push @{$self->{includes}}, $fileObj;
211 # dump out to our file in construction
212 my $fh=FileHandle->new();
213 my $outfilename=$fileObj->ProcessedFile();
214 $fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
215 print {$self->{fileout}} "\n";# always start an include on a new line
216 while ( <$fh> ) {
217 print {$self->{fileout}} $_;
218 }
219 print {$self->{fileout}} "\n";# always end include with new line
220 undef $fh;
221 }
222 }
223