Changeset 17333
- Timestamp:
- 08/10/08 23:20:23 (5 years ago)
- Location:
- lang/perl/MooseX-DOM/trunk/lib/MooseX
- Files:
-
- 2 modified
-
DOM.pm (modified) (2 diffs)
-
DOM/LibXML.pm (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM.pm
r17304 r17333 55 55 56 56 no Moose; 57 no MoooseX::DOM; 57 58 58 59 my $obj = MyObject->new(node => <<EOXML); … … 73 74 =head1 DECLARATION 74 75 75 =head2 has_dom_root $ tag76 =head2 has_dom_root $name[, %opts] 76 77 77 Specifies that the given XML have the specified tag 78 Specifies that the given XML have the specified tag. This specification is 79 also used when creating new root node for creating the underlying XML 80 81 has_dom_root $name => ( 82 # attributes => { ... } 83 ); 78 84 79 85 =head2 has_dom_attr $name[, %opts] -
lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML.pm
r17304 r17333 10 10 is => 'rw', 11 11 isa => 'MooseX::DOM::LibXML::ContextNode', 12 required => 1,13 12 ); 14 13 … … 41 40 confess "Don't know how to handle $node"; 42 41 } 43 } 44 45 return { %args, node => $node }; 46 } 47 48 { 42 43 $args{node} = $node; 44 } 45 46 return { %args }; 47 } 48 49 BOOTSTRAP: { 49 50 my $subname = sub { join('::', $_[1] || __PACKAGE__, $_[0]) }; 50 51 my $subassign = sub { … … 56 57 my $textfilter = sub { 57 58 my $self = shift; 58 return map { $_->textContent} @_;59 return map { blessed $_ && $_->can('textContent') ? $_->textContent : $_ } @_; 59 60 }; 60 61 … … 84 85 # Used only in has_dom_children, to create a list of element nodes from 85 86 # list of text 86 my $text2elements = Class::MOP::subname($subname->('text2elements') => sub {87 my $text2elements = Class::MOP::subname($subname->('text2elements') => sub { 87 88 my($self, %args) = @_; 88 89 … … 113 114 my %exports = ( 114 115 has_dom_root => sub { 115 return Class::MOP::subname($subname->('has_dom_ attr') => sub ($;%) {116 return Class::MOP::subname($subname->('has_dom_root') => sub ($;%) { 116 117 my $caller = caller(); 117 my $tag = shift; 118 my ($tag, %args) = @_; 119 # tag => $tag 120 # attributes => { attr1 => $val1, attr2 => $val2 } 121 122 $tag = $args{tag} if $args{tag}; 123 my $attrs = $args{attributes}; 124 125 my $meta = $caller->meta; 126 $meta->{'$!dom_root'} = { tag => $tag, attributes => $attrs }; 118 127 119 128 my $assert_root = sub { … … 125 134 }; 126 135 127 my $meta = $caller->meta;128 136 $meta->add_around_method_modifier(new => sub { 129 137 my $next = shift; … … 148 156 $name = $args{accessor}; 149 157 } 158 159 $caller->meta->{'%!dom_attributes'}->{$name} = 1; 150 160 my $method = $subname->($name, $caller); 151 161 $subassign->($method => sub { … … 171 181 $name = $args{accessor}; 172 182 } 183 $caller->meta->{'%!dom_attributes'}->{$name} = 1; 173 184 my $method = $subname->($name, $caller); 174 185 … … 182 193 $node->getChildrenByTagName($tagname) 183 194 ; 184 185 195 if (@_) { 186 196 $create->($self, children => \@children, values => \@_, namespace => $namespace, tag => $tagname); … … 204 214 } 205 215 216 my $meta = $caller->meta; 217 $meta->{'%!dom_attributes'}->{$name} = 1; 206 218 my $method = $subname->($name, $caller); 207 219 $subassign->($method => sub { … … 209 221 210 222 my $node = $self->node; 211 my $nsuri = $self->namespaces->{ $namespace }; 212 my ($child) = ($nsuri) ? 213 $node->getChildrenByTagNameNS($nsuri, $tagname): 214 $node->getChildrenByTagName($tagname) 215 ; 223 my $child; 224 if ($node) { 225 my $nsuri = $self->namespaces->{ $namespace }; 226 ($child) = ($nsuri) ? 227 $node->getChildrenByTagNameNS($nsuri, $tagname): 228 $node->getChildrenByTagName($tagname) 229 ; 230 } 216 231 217 232 if (@_) { 233 $self->__create_root_node(); 218 234 $create->( $self, child => $child, value => $_[0], namespace => $namespace, tag => $tagname); 219 235 } … … 231 247 }); 232 248 sub export_dsl { 233 goto &$export ;249 goto &$export if $export; 234 250 } 235 251 … … 254 270 } 255 271 } 256 272 } 273 274 sub BUILD { 275 my ($self, $args) = @_; 276 277 if (my $attrs = $self->meta->{'%!dom_attributes'}) { 278 foreach my $attr (keys %$attrs) { 279 next unless defined $args->{$attr}; 280 $self->$attr( $args->{ $attr } ); 281 } 282 } 283 return $self; 257 284 } 258 285 … … 262 289 } 263 290 291 sub __create_root_node { 292 my $self = shift; 293 return if $self->node; 294 295 my $root = $self->meta->{'$!dom_root'}; 296 confess "No root node defined" unless $root; 297 298 my $tag = $root->{tag}; 299 my $attrs = $root->{attributes}; 300 my $doc = XML::LibXML::Document->new( '1.0' => 'UTF-8' ); 301 my $node = $doc->createElement($tag); 302 while (my($name, $value) = each %$attrs) { 303 $node->setAttribute($name, $value); 304 } 305 $self->node( MooseX::DOM::LibXML::ContextNode->new(node => $node) ); 306 } 307 264 308 265 309 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)