| 1 | # | ||
| 2 | # Copyright (C) 1998, 1999 Ken MacLeod | ||
| 3 | # XML::Grove::Builder is free software; you can redistribute it and/or | ||
| 4 | # modify it under the same terms as Perl itself. | ||
| 5 | # | ||
| 6 | # | ||
| 7 | # $Id: Builder.pm,v 1.5 1999/08/17 15:01:28 kmacleod Exp $ | ||
| 8 | # | ||
| 9 | |||
| 10 | # | ||
| 11 | # implements these handlers: | ||
| 12 | # | ||
| 13 | # PerlSAX defined handlers | ||
| 14 | # | ||
| 15 | # start_document -- beginning of a document | ||
| 16 | # end_document -- end of a document | ||
| 17 | # start_element -- beginning of an element | ||
| 18 | # end_element -- end of an element | ||
| 19 | # characters -- character data | ||
| 20 | # ignorable_whitespace -- ignorable whitespace in element content | ||
| 21 | # processing_instruction -- processing instruction (PI) | ||
| 22 | # | ||
| 23 | # Additional handlers | ||
| 24 | # | ||
| 25 | # record_end -- record_end | ||
| 26 | # ext_entity -- external entity definition | ||
| 27 | # subdoc_entity -- subdoc entity definition | ||
| 28 | # ext_sgml_entity -- external SGML text entity definition | ||
| 29 | # int_entity -- internal entity definition | ||
| 30 | # ext_entity_ref -- external entityt reference | ||
| 31 | # int_entity_ref -- internal entity reference | ||
| 32 | # notation -- notation definition | ||
| 33 | # comment -- comment | ||
| 34 | # start_subdoc -- start of subdoc entity | ||
| 35 | # end_subdoc -- end of subdoc entity | ||
| 36 | # appinfo -- defined value of APPINFO | ||
| 37 | # conforming -- document is conforming | ||
| 38 | # error -- error | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | |||
| 42 | package XML::Grove::Builder; | ||
| 43 | |||
| 44 | use XML::Grove; | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $type = shift; | ||
| 48 | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||
| 49 | |||
| 50 | return bless $self, $type; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub start_document { | ||
| 54 | my $self = shift; | ||
| 55 | my $document = shift; | ||
| 56 | |||
| 57 | $self->{lists} = []; | ||
| 58 | $self->{cur_list} = []; | ||
| 59 | $self->{Grove} = new XML::Grove::Document (%$document, | ||
| 60 | Contents => $self->{cur_list}); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub end_document { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | my $grove = $self->{Grove}; | ||
| 67 | |||
| 68 | delete $self->{cur_list}; | ||
| 69 | delete $self->{lists}; | ||
| 70 | delete $self->{Grove}; | ||
| 71 | |||
| 72 | return $grove; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub start_element { | ||
| 76 | my $self = shift; | ||
| 77 | my $properties = shift; | ||
| 78 | |||
| 79 | my $contents = []; | ||
| 80 | $properties->{Contents} = $contents; | ||
| 81 | |||
| 82 | my $element = new XML::Grove::Element ($properties); | ||
| 83 | push @{ $self->{lists} }, $self->{cur_list}; | ||
| 84 | push @{ $self->{cur_list} }, $element; | ||
| 85 | $self->{cur_list} = $contents; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub end_element { | ||
| 89 | my $self = shift; | ||
| 90 | $self->{cur_list} = pop @{ $self->{lists} }; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub characters { | ||
| 94 | my $self = shift; | ||
| 95 | push @{ $self->{cur_list} }, new XML::Grove::Characters (@_); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub ignorable_whitespace { | ||
| 99 | my $self = shift; | ||
| 100 | push @{ $self->{cur_list} }, new XML::Grove::Characters (@_); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub processing_instruction { | ||
| 104 | my $self = shift; | ||
| 105 | push @{ $self->{cur_list} }, new XML::Grove::PI (@_); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub record_end { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | push @{ $self->{cur_list} }, new XML::Grove::Characters (Data => "\n"); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub external_entity_decl { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | my $ext_entity = new XML::Grove::Entity::External (@_); | ||
| 118 | my $notation = $ext_entity->{Notation}; | ||
| 119 | if (defined($notation) && !ref($notation)) { | ||
| 120 | $ext_entity->{Notation} = $self->{Grove}{Notations}{$notation}; | ||
| 121 | } | ||
| 122 | $self->{Grove}{Entities}{$ext_entity->{Name}} = $ext_entity; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub subdoc_entity_decl { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | my $ext_entity = new XML::Grove::Entity::SubDoc (@_); | ||
| 129 | $self->{Grove}{Entities}{$ext_entity->{Name}} = $ext_entity; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub external_sgml_entity_decl { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $ext_entity = new XML::Grove::Entity::SGML (@_); | ||
| 136 | $self->{Grove}{Entities}{$ext_entity->{Name}} = $ext_entity; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub internal_entity_decl { | ||
| 140 | my $self = shift; | ||
| 141 | |||
| 142 | my $int_entity = new XML::Grove::Entity (@_); | ||
| 143 | $self->{Grove}{Entities}{$int_entity->{Name}} = $int_entity; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub external_entity_ref { | ||
| 147 | my $self = shift; | ||
| 148 | my $properties = shift; | ||
| 149 | |||
| 150 | my $ext_entity = $self->{Grove}{Entities}{$properties->{Name}}; | ||
| 151 | if (defined $ext_entity) { | ||
| 152 | push @{ $self->{cur_list} }, $ext_entity; | ||
| 153 | } elsif (!defined $self->{'warn_undefined_entity'}{$properties->{Name}}) { | ||
| 154 | $self->{'warn_undefined_entity'}{$properties->{Name}} = 1; | ||
| 155 | $self->error ({ Message => "XML::Grove::Builder: external entity \`$properties->{Name}' not defined" }); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub internal_entity_ref { | ||
| 160 | my $self = shift; | ||
| 161 | my $properties = shift; | ||
| 162 | |||
| 163 | my $int_entity = $self->{Grove}{Entities}{$properties->{Name}}; | ||
| 164 | if (defined $int_entity) { | ||
| 165 | push @{ $self->{cur_list} }, $int_entity; | ||
| 166 | } elsif (!defined $self->{'warn_undefined_entity'}{$properties->{Name}}) { | ||
| 167 | $self->{'warn_undefined_entity'}{$properties->{Name}} = 1; | ||
| 168 | $self->error ({ Message => "XML::Grove::Builder: internal entity \`$properties->{Name}' not defined" }); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | sub notation_decl { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | my $notation = new XML::Grove::Notation (@_); | ||
| 176 | $self->{Grove}{Notations}{$notation->{Name}} = $notation; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub comment { | ||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | my $comment = new XML::Grove::Comment (@_); | ||
| 183 | push @{ $self->{cur_list} }, $comment; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub subdoc_start { | ||
| 187 | my $self = shift; | ||
| 188 | my $properties = shift; | ||
| 189 | |||
| 190 | my $contents = []; | ||
| 191 | $properties->{Contents} = $contents; | ||
| 192 | |||
| 193 | my $subdoc = new XML::Grove::SubDoc ($properties); | ||
| 194 | push @{ $self->{lists} }, $self->{cur_list}; | ||
| 195 | push @{ $self->{cur_list} }, $subdoc; | ||
| 196 | $self->{cur_list} = $contents; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub subdoc_end { | ||
| 200 | my $self = shift; | ||
| 201 | $self->{cur_list} = pop @{ $self->{lists} }; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub appinfo { | ||
| 205 | my $self = shift; | ||
| 206 | my $appinfo = shift; | ||
| 207 | |||
| 208 | $self->{Grove}{AppInfo} = $appinfo->{AppInfo}; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub conforming { | ||
| 212 | my $self = shift; | ||
| 213 | |||
| 214 | $self->{Grove}{Conforming} = 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub warning { | ||
| 218 | my $self = shift; | ||
| 219 | my $error = shift; | ||
| 220 | |||
| 221 | push (@{ $self->{Grove}{Errors} }, $error); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub error { | ||
| 225 | my $self = shift; | ||
| 226 | my $error = shift; | ||
| 227 | |||
| 228 | push (@{ $self->{Grove}{Errors} }, $error); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub fatal_error { | ||
| 232 | my $self = shift; | ||
| 233 | my $error = shift; | ||
| 234 | |||
| 235 | push (@{ $self->{Grove}{Errors} }, $error); | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; | ||
| 239 | |||
| 240 | __END__ |