1 |
#
|
2 |
# Parse.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
# -----------
|
8 |
# maintain parse configurations
|
9 |
#
|
10 |
# Interface
|
11 |
# ---------
|
12 |
# new() : A new Parse object
|
13 |
# addtag(name,start,text,end,$object) : Add a new tag
|
14 |
# addgrouptags() : add <Group> tag functionality
|
15 |
# addignoretags() : add <ignore> tag functionality
|
16 |
# parse(filename,[streamhandle], [streamexcludetag]) :
|
17 |
# parse the given file - turn on the stream
|
18 |
# function of the switcher if a filehandle
|
19 |
# supplied as a second argument
|
20 |
# line() : return the current linenumber in the file
|
21 |
# tagstartline() : return the linenumber of the last tag opening
|
22 |
# includeparse(Parse) : include the settings from another parse object
|
23 |
# tags() : return list of defined tags
|
24 |
# cleartags() : clear of all tags
|
25 |
# opencontext(name) : open a parse context
|
26 |
# closecontext(name) : close a parse context
|
27 |
# includecontext(name) : Process when in a given context
|
28 |
# excludecontext(name) : No Processing when given context
|
29 |
# contexttag(tagname) : Register the tagname as one able to change context
|
30 |
# if not registerd - the close tag will be ignored
|
31 |
# too if outside of the specified context!
|
32 |
|
33 |
package ActiveDoc::Parse;
|
34 |
require 5.004;
|
35 |
use XML::Parser;
|
36 |
|
37 |
sub new()
|
38 |
{
|
39 |
my $class=shift;
|
40 |
$self={};
|
41 |
bless $self, $class;
|
42 |
my ($dataclass, $parse_style)=@_;
|
43 |
|
44 |
$self->{xmlparser} = new XML::Parser (
|
45 |
Style => $parse_style,
|
46 |
ParseParamEnt => 1,
|
47 |
ErrorContext => 3,
|
48 |
Pkg => $dataclass);
|
49 |
return $self;
|
50 |
}
|
51 |
|
52 |
sub parsefilelist()
|
53 |
{
|
54 |
my $self=shift;
|
55 |
my ($files)=@_;
|
56 |
print __PACKAGE__."::parsefilelist(): Not used?\n";
|
57 |
}
|
58 |
|
59 |
sub filehead ()
|
60 |
{
|
61 |
my $self=shift;
|
62 |
my $data=@_;
|
63 |
if (@_)
|
64 |
{
|
65 |
$self->{filehead}=shift;
|
66 |
return;
|
67 |
}
|
68 |
return $self->{filehead} || "";
|
69 |
}
|
70 |
|
71 |
sub filetail ()
|
72 |
{
|
73 |
my $self=shift;
|
74 |
if (@_)
|
75 |
{
|
76 |
$self->{filetail}=shift;
|
77 |
return;
|
78 |
}
|
79 |
return $self->{filetail} || "";
|
80 |
}
|
81 |
|
82 |
sub parse()
|
83 |
{
|
84 |
my $self=shift;
|
85 |
my ($file)=@_;
|
86 |
eval
|
87 |
{
|
88 |
$self->{data} = $self->{xmlparser}->parse($self->getfilestring_($file));
|
89 |
};
|
90 |
if ($@)
|
91 |
{
|
92 |
print STDERR "**** ERROR: Failed parsing file: $file\n$@\n";
|
93 |
}
|
94 |
return $self;
|
95 |
}
|
96 |
|
97 |
sub getfilestring_()
|
98 |
{
|
99 |
my $self=shift;
|
100 |
my ($file)=@_;
|
101 |
my $filestring="";
|
102 |
my $read=0;
|
103 |
if (($file!~/\.xml$/) && ($file!~/\/\.SCRAM\/InstalledTools\/[^\/]+$/))
|
104 |
{
|
105 |
eval("use SCRAM::Doc2XML");
|
106 |
if (!$@)
|
107 |
{
|
108 |
my $xmlconvertor = SCRAM::Doc2XML->new();
|
109 |
my $xml=$xmlconvertor->convert($file);
|
110 |
$filestring = join("",@$xml);
|
111 |
$xmlconvertor->clean();
|
112 |
$read=1;
|
113 |
}
|
114 |
}
|
115 |
if (!$read)
|
116 |
{
|
117 |
open (IN, "< $file") or die __PACKAGE__.": Cannot read file $file: $!\n";
|
118 |
$filestring = join("", <IN>);
|
119 |
close (IN) or die __PACKAGE__.": Cannot read file $file: $!\n";
|
120 |
}
|
121 |
$filestring = $self->filehead().$filestring.$self->filetail();
|
122 |
# Strip spaces at the beginning and end of the line:
|
123 |
$filestring =~ s/^\s+//g;
|
124 |
$filestring =~ s/\s+$//g;
|
125 |
# Finally strip the newlines:
|
126 |
#$filestring =~ s/\n//g;
|
127 |
# Strip out spaces in between tags:
|
128 |
#$filestring =~ s/>\s+</></g;
|
129 |
$self->{filestring}=$filestring;
|
130 |
return $filestring;
|
131 |
}
|
132 |
|
133 |
sub data()
|
134 |
{
|
135 |
my $self=shift;
|
136 |
return $self->{data}->[0];
|
137 |
}
|
138 |
|
139 |
sub includeparse
|
140 |
{
|
141 |
my $self=shift;
|
142 |
my $obj=shift;
|
143 |
my $tag;
|
144 |
|
145 |
# copy the tags from the remote parse object
|
146 |
foreach $tag ( $obj->tags() )
|
147 |
{
|
148 |
$self->addtag($tag,$obj->{tags}->tagsettings($tag));
|
149 |
}
|
150 |
}
|
151 |
|
152 |
sub addtag
|
153 |
{
|
154 |
my $self=shift;
|
155 |
$self->{tags}->addtag(@_);
|
156 |
}
|
157 |
|
158 |
1;
|