ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/SimpleDoc.pm
Revision: 1.3
Committed: Mon Aug 28 07:43:21 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: V0_19_0, V0_18_5, V0_18_4, V_18_3_TEST, VO_18_3, V0_18_2, V0_18_1
Changes since 1.2: +54 -11 lines
Log Message:
merge from HPWbranch

File Contents

# User Rev Content
1 williamc 1.1 #
2     # SimpleDoc.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     # -----------
8 williamc 1.2 # Simple multi parsing functionality and group manipulation
9 williamc 1.1 #
10     # Interface
11     # ---------
12 williamc 1.3 # new([DocVersionTag]) : A new ActiveDoc object. You can also
13     # specify an alternative doc version tag
14 williamc 1.1 # filetoparse([filename]) : Set/Return the filename of document
15     # newparse(parselabel) : Create a new parse type
16     # parse(parselabel) : Parse the document file for the given parse level
17 williamc 1.3 # addtag(parselabel,tagname,start,obj,[text,obj,end,obj]) :
18 williamc 1.1 # Add tags to the parse given by label
19 williamc 1.2 # grouptag(tagname, parselabel) : Allow a tag to switch context
20     # - if not you can never turn a context off!
21 williamc 1.1 # checktag(tagname, hashref, param) : check for existence of param in
22     # hashref from a tag call
23     # includeparse(local_parsename, objparsename, activedoc) : copy the parse from
24     # one object to another
25     # currentparser() : return the current parser object
26     # currentparsename([name]) : get/set current parse name
27     #
28     # addignoretags(parsename) : add <ignore> </igonore> tags funtionality to the
29     # specified parse
30 williamc 1.2 # opengroup(name) : declare a group to be open
31     # closegroup(name) : declare a group to be closed
32     # allowgroup(name,parse) : allow a group so named
33     # disallowgroup(name,parse) : disallow the named group
34     # restoregroup(name,parse) : restore group access setting (that before last change)
35 williamc 1.3 # doctype() : return the (type,version) of the document
36     # as specified by the DocVersionTag
37     # filenameref(string) : A string to refer to the file in parse error messages
38     # etc. Default is filetoparse
39 williamc 1.1 # --------------- Error handling routines ---------------
40     # verbose(string) : Print string in verbosity mode
41     # verbosity(0|1) : verbosity off|on
42     # line() : return the current line number in the current parse
43     # tagstartline() : return the line number where the current tag was
44     # opened
45     # parseerror(string) : print error and associate with line number etc.
46     # error(string) : handle an error
47    
48     package ActiveDoc::SimpleDoc;
49     require 5.004;
50     use ActiveDoc::Parse;
51    
52     sub new {
53     my $class=shift;
54     $self={};
55     bless $self, $class;
56 williamc 1.3 $self->_initdoc("doc",@_);
57 williamc 1.1 return $self;
58     }
59    
60 williamc 1.3 sub doctype {
61     my $self=shift;
62     my $rv=1;
63    
64     undef $self->{docversion};
65     undef $self->{doctype};
66     $self->parse("doc");
67     return ($self->{doctype},$self->{docversion});
68     }
69    
70     sub filenameref {
71     my $self=shift;
72     if ( @_ ) {
73     $self->{filenameref}=shift;
74     }
75     return (defined $self->{filenameref})?$self->{filenameref}
76     :$self->filetoparse();
77     }
78    
79     sub _initdoc {
80     my $self=shift;
81     my $parsename=shift;
82    
83     $self->{doctag}="DOC";
84     if ( @_ ) {
85     $self->{doctag}=shift;
86     }
87     $self->newparse($parsename);
88     $self->addtag($parsename,$self->{doctag},\&Doc_Start, $self);
89 williamc 1.1 }
90    
91     sub verbosity {
92     my $self=shift;
93     $self->{verbose}=shift;
94     }
95    
96     sub verbose {
97     my $self=shift;
98     my $string=shift;
99    
100     if ( $self->{verbose} ) {
101     print ">".ref($self)."($self) : \n->".$string."\n";
102     }
103     }
104    
105     # ----- parse related routines --------------
106     sub parse {
107     my $self=shift;
108     $parselabel=shift;
109    
110     my $file=$self->filetoparse();
111 williamc 1.3 if ( -f $file ) {
112 williamc 1.1 if ( exists $self->{parsers}{$parselabel} ) {
113     $self->verbose("Parsing $parselabel in file $file");
114     $self->{currentparsename}=$parselabel;
115     $self->{currentparser}=$self->{parsers}{$parselabel};
116     $self->{parsers}{$parselabel}->parse($file,@_);
117     delete $self->{currentparser};
118     $self->{currentparsename}="";
119     $self->verbose("Parse $parselabel Complete");
120     }
121     }
122     else {
123 williamc 1.3 $self->error("Cannot parse \"$parselabel\" - file $file not known");
124 williamc 1.1 }
125     }
126    
127     sub currentparsename {
128     my $self=shift;
129     @_?$self->{currentparsename}=shift
130     :(defined $self->{currentparsename}?$self->{currentparsename}:"");
131     }
132    
133     sub currentparser {
134     my $self=shift;
135     return $self->{currentparser};
136     }
137    
138    
139     sub newparse {
140     my $self=shift;
141     my $parselabel=shift;
142    
143     $self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
144     # $self->{parsers}{$parselabel}->addgrouptags();
145     }
146    
147     sub addignoretags {
148     my $self=shift;
149     my $parselabel=shift;
150     $self->{parsers}{$parselabel}->addignoretags();
151     }
152    
153     sub cleartags {
154     my $self=shift;
155     my $parselabel=shift;
156    
157     $self->{parsers}{$parselabel}->cleartags();
158     }
159    
160    
161     sub includeparse {
162     my $self=shift;
163     my $parselabel=shift;
164     my $remoteparselabel=shift;
165     my $activedoc=shift;
166    
167     # Some error trapping
168     if ( ! exists $self->{parsers}{$parselabel} ) {
169     $self->error("Unknown local parse name specified");
170     }
171     if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) {
172     $self->error("Unknown parse name specified in remote obj $activedoc");
173     }
174    
175     #
176     my $rp=$activedoc->{parsers}{$remoteparselabel};
177     $self->{parsers}{$parselabel}->includeparse($rp);
178     }
179    
180     sub addtag {
181     my $self=shift;
182     my $parselabel=shift;
183 williamc 1.3 if ( ( $#_ != 6 ) && ( $#_ != 2) ) {
184 williamc 1.1 $self->error("Incorrect addtags specification\n".
185     "called with :\n@_ \n");
186     }
187     $self->{parsers}{$parselabel}->addtag(@_);
188     }
189    
190     sub filetoparse {
191     my $self=shift;
192    
193     if ( @_ ) {
194     $self->{filename}=shift;
195     }
196     return $self->{filename};
197 williamc 1.2 }
198     # --------- Group services
199     sub grouptag {
200     my $self=shift;
201     my $name=shift;
202     my $parselabel=shift;
203    
204     $self->{parsers}{$parselabel}->contexttag($name);
205     }
206    
207     sub opengroup {
208     my $self=shift;
209     my $name=shift;
210    
211     if ( defined $self->currentparser ) {
212     $self->currentparser()->opencontext($name);
213     }
214     else {
215     $self->error("Cannot Call opengroup outside of a parse (".
216     caller().")");
217     }
218     }
219    
220     sub closegroup {
221     my $self=shift;
222     my $name=shift;
223    
224     if ( defined $self->currentparser ) {
225     $self->currentparser()->closecontext($name);
226     }
227     else {
228     $self->error("Cannot Call closegroup outside of a parse (".
229     caller().")");
230     }
231     }
232    
233     sub allowgroup {
234     my $self=shift;
235     my $name=shift;
236     my $parselabel=shift;
237    
238     $self->{parsers}{$parselabel}->includecontext($name);
239     }
240    
241     sub disallowgroup {
242     my $self=shift;
243     my $name=shift;
244     my $parselabel=shift;
245    
246     $self->{parsers}{$parselabel}->excludecontext($name);
247 williamc 1.1 }
248    
249     # -------- Error Handling and Error services --------------
250    
251     sub error {
252     my $self=shift;
253     my $string=shift;
254    
255     die $string."\n";
256     }
257    
258     sub parseerror {
259     my $self=shift;
260     my $string=shift;
261    
262     if ( $self->currentparsename() eq "" ) {
263 williamc 1.3 $self->error("Error In file ".$self->filenameref."\n".$string);
264 williamc 1.1 }
265     else {
266     $line=$self->line();
267 williamc 1.3 print "Parse Error in ".$self->filenameref().", line ".
268 williamc 1.1 $line."\n";
269     print $string."\n";
270     exit;
271     }
272     }
273    
274     sub checktag {
275     my $self=shift;
276     my $tagname=shift;
277     my $hashref=shift;
278     my $param=shift;
279    
280     if ( ! exists $$hashref{$param} ) {
281     $self->parseerror("Incomplete Tag <$tagname> : $param required");
282     }
283     }
284    
285     sub line {
286     my $self=shift;
287     return $self->{currentparser}->line();
288     }
289    
290     sub tagstartline {
291     my $self=shift;
292     return $self->{currentparser}->tagstartline();
293     }
294    
295 williamc 1.3 # -- tag routines
296     sub Doc_Start {
297     my $self=shift;
298     my $name=shift;
299     my $hashref=shift;
300    
301     $self->checktag($name, $hashref, "type");
302     $self->checktag($name, $hashref, "version");
303    
304     $self->{doctype}=$$hashref{'type'};
305     $self->{docversion}=$$hashref{'version'};
306     }