| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XML::XMLWriter::Element - XML::XMLWriter Element Class | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | ###################################################################### | ||
| 8 | |||
| 9 | package XML::XMLWriter::Element; | ||
| 10 | require 5.8.0; | ||
| 11 | |||
| 12 | ###################################################################### | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use Carp; | ||
| 16 | use XML::XMLWriter::PCData; | ||
| 17 | |||
| 18 | ###################################################################### | ||
| 19 | |||
| 20 | =head2 In General | ||
| 21 | |||
| 22 | Down there are some methods which to you might seem quite useless | ||
| 23 | since they simply call some other expression which you could also call | ||
| 24 | directly. I recommend to use them though since in feature releases | ||
| 25 | they might do more and then you surley don't want to modify your | ||
| 26 | code. I just document what they call to make clear what they do so far. | ||
| 27 | |||
| 28 | =head2 Methods | ||
| 29 | |||
| 30 | =head3 new ($elementname, $ParseDTD_object, \%arguments, $pcdata, | ||
| 31 | $Element_parent_object) | ||
| 32 | |||
| 33 | Constructor. Complains by outputting a warning if the attribute list | ||
| 34 | or some attribute value is not allowed by the DTD. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | ###################################################################### | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my($class,$elem,$dtd,$args,$text,$parent) = @_; | ||
| 42 | my $self = bless({}, ref($class) || $class); | ||
| 43 | $dtd->attr_list_value_allowed($elem,$args) or carp $dtd->errstr if($dtd and (ref($args) eq 'HASH')); | ||
| 44 | $self->{elem} = $elem; | ||
| 45 | $self->{args} = $args; | ||
| 46 | $self->{dtd} = $dtd; | ||
| 47 | $self->{child} = []; | ||
| 48 | $self->{parent} = $parent; | ||
| 49 | $self->_text($text) if($text); | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | ###################################################################### | ||
| 54 | |||
| 55 | =head3 _text (@pcdata) | ||
| 56 | |||
| 57 | Same as C<_pcdata>. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | ###################################################################### | ||
| 62 | |||
| 63 | sub _text { | ||
| 64 | my $self = shift; | ||
| 65 | return $self->_pcdata(@_); | ||
| 66 | } | ||
| 67 | |||
| 68 | ###################################################################### | ||
| 69 | |||
| 70 | =head3 _pcdata (@pcdata) | ||
| 71 | |||
| 72 | Adds a XML::XMLWriter::PCData object to the child list. But returns | ||
| 73 | the elements object (C<$self>). See the POD of PCData.pm for more | ||
| 74 | information on what is done with @pcdata. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | ###################################################################### | ||
| 79 | |||
| 80 | sub _pcdata { | ||
| 81 | my ($self,@text) = @_; | ||
| 82 | push @{$self->{child}}, XML::XMLWriter::PCData->new(@text); | ||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | ###################################################################### | ||
| 87 | |||
| 88 | =head3 _entity ($ref) | ||
| 89 | |||
| 90 | Simply calls and returns C<_cdata('&$ref;');>. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | ###################################################################### | ||
| 95 | |||
| 96 | sub _entity { | ||
| 97 | my($self,$ref) = @_; | ||
| 98 | return $self->_cdata("&$ref;"); | ||
| 99 | } | ||
| 100 | |||
| 101 | ###################################################################### | ||
| 102 | |||
| 103 | =head3 _comment ($comment) | ||
| 104 | |||
| 105 | Simply calls and returns C<_cdata('E<lt>!-- $comment --E<gt>');>. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | ###################################################################### | ||
| 110 | |||
| 111 | sub _comment { | ||
| 112 | my($self,$comment) = @_; | ||
| 113 | return $self->_cdata("<!-- $comment -->"); | ||
| 114 | } | ||
| 115 | |||
| 116 | ###################################################################### | ||
| 117 | |||
| 118 | =head3 _pi ($target, $data) | ||
| 119 | |||
| 120 | Adds an processing instruction to the child list by calling and | ||
| 121 | returning C<_cdata('E<lt>?$target $data?E<gt>');>. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | ###################################################################### | ||
| 126 | |||
| 127 | sub _pi { | ||
| 128 | my($self,$target,$data) = @_; | ||
| 129 | return $self->_cdata("<?$target $data?>"); | ||
| 130 | } | ||
| 131 | |||
| 132 | ###################################################################### | ||
| 133 | |||
| 134 | =head3 _cdata (@cdata) | ||
| 135 | |||
| 136 | Simply adds every element that @cdata contains to the child list. | ||
| 137 | You can also call directly C<_push_child(@cdata)>. | ||
| 138 | |||
| 139 | Returns the object it was called on. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | ###################################################################### | ||
| 144 | |||
| 145 | sub _cdata { | ||
| 146 | my($self,@cdata) = @_; | ||
| 147 | #push @{$self->{child}}, @cdata; | ||
| 148 | $self->_push_child(@cdata); | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | ###################################################################### | ||
| 153 | |||
| 154 | =head3 _add ($element, \%arguments, $pcdata) | ||
| 155 | |||
| 156 | Adds element/tag $element to the child list. It'll produce a warning | ||
| 157 | if the DTD doesn't allow that operation. Depending on whether the DTD | ||
| 158 | says that the newly created element is a empty element or not, the | ||
| 159 | object C<_add> was called on or the newly created object is returned. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | ###################################################################### | ||
| 164 | |||
| 165 | sub _add { | ||
| 166 | my($self,$elem,$args,$text) = @_; | ||
| 167 | croak "2. argument must be a hash reference" if(defined($args) and ref($args) ne 'HASH'); | ||
| 168 | $self->{dtd}->child_allowed($self->{elem}, $elem) or carp $self->{dtd}->errstr if($self->{dtd}); | ||
| 169 | local $_ = XML::XMLWriter::Element->new($elem,$self->{dtd},$args,$text,$self); | ||
| 170 | push @{$self->{child}}, $_; | ||
| 171 | return ($self->{dtd} and $self->{dtd}->is_empty($elem)) ? $self : $_; | ||
| 172 | } | ||
| 173 | |||
| 174 | ###################################################################### | ||
| 175 | |||
| 176 | =head3 _parent () | ||
| 177 | |||
| 178 | Returns the elements parent element object. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | ###################################################################### | ||
| 183 | |||
| 184 | sub _parent { | ||
| 185 | my $self = shift; | ||
| 186 | return $self->{parent}; | ||
| 187 | } | ||
| 188 | |||
| 189 | ###################################################################### | ||
| 190 | |||
| 191 | =head3 _get () | ||
| 192 | |||
| 193 | Returns the XML code taking the element it is called on as root. This | ||
| 194 | method works recursive. It checks every elements child list and | ||
| 195 | complains if it is not allowed by the DTD. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | ###################################################################### | ||
| 200 | |||
| 201 | sub _get { | ||
| 202 | my($self) = @_; | ||
| 203 | my $res = '<' . $self->{elem}; | ||
| 204 | local $_; | ||
| 205 | foreach $_ (keys(%{$self->{args}})) { | ||
| 206 | $res .= ' ' . $_ . '="' . $self->{args}->{$_} . '"'; | ||
| 207 | } | ||
| 208 | if($self->{dtd} and $self->{dtd}->is_empty($self->{elem})) { | ||
| 209 | $res .= ' />'; | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | $res .= '>'; | ||
| 213 | my @child; | ||
| 214 | foreach $_ (@{$self->{child}}) { | ||
| 215 | push @child, (ref($_) eq 'XML::XMLWriter::Element') ? $_->{elem} : '#PCDATA'; | ||
| 216 | } | ||
| 217 | $self->{dtd}->child_list_allowed($self->{elem},@child) or carp $self->{dtd}->errstr if($self->{dtd}); | ||
| 218 | foreach $_ (@{$self->{child}}) { | ||
| 219 | $res .= (ref($_) =~ m/^XML::XMLWriter::/) ? join("",$_->_get()) : $_; | ||
| 220 | } | ||
| 221 | $res .= '</' . $self->{elem} . '>'; | ||
| 222 | } | ||
| 223 | return $res; | ||
| 224 | } | ||
| 225 | |||
| 226 | ###################################################################### | ||
| 227 | |||
| 228 | =head3 _push_child (@childs) | ||
| 229 | |||
| 230 | Adds the elements of @childs to the objects child list. | ||
| 231 | Returns 1. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | ###################################################################### | ||
| 236 | |||
| 237 | sub _push_child { | ||
| 238 | my $self = shift; | ||
| 239 | push @{$self->{child}}, @_; | ||
| 240 | return 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | ###################################################################### | ||
| 244 | |||
| 245 | =head3 AUTOLOAD (\%arguments, $pcdata); | ||
| 246 | |||
| 247 | If you call an undefined method on the object this method is called | ||
| 248 | instead. | ||
| 249 | |||
| 250 | The name of the method that you tried to call will be taken as a tag | ||
| 251 | resp. element name, then C<_add> is called and returned, passing it | ||
| 252 | the name as first argument (and forwarding the other arguments). | ||
| 253 | |||
| 254 | For every tag that starts with I<_> you have to add one I<_> more | ||
| 255 | because one will be cutted of. That means that you've to call | ||
| 256 | C<__foobar(..)> to add a tag called C<_foobar>. | ||
| 257 | |||
| 258 | If you don't like that, you can always call C<_add('yourtag', ..)>. | ||
| 259 | |||
| 260 | Please see C<_add> for more information. | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | ###################################################################### | ||
| 265 | |||
| 266 | sub AUTOLOAD { | ||
| 267 | our $AUTOLOAD =~ /::([^:]*)$/; | ||
| 268 | return if($1 eq 'DESTROY'); | ||
| 269 | my $self = shift; | ||
| 270 | local $_; | ||
| 271 | $_ = $1; | ||
| 272 | s/^_//; | ||
| 273 | return $self->_add($_, @_); | ||
| 274 | } | ||
| 275 | |||
| 276 | ###################################################################### | ||
| 277 | |||
| 278 | return 1; | ||
| 279 | |||
| 280 | __END__ |