Changeset 17333

Show
Ignore:
Timestamp:
08/10/08 23:20:23 (5 years ago)
Author:
daisuke
Message:

automatically generate root node

Location:
lang/perl/MooseX-DOM/trunk/lib/MooseX
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM.pm

    r17304 r17333  
    5555 
    5656  no Moose; 
     57  no MoooseX::DOM; 
    5758 
    5859  my $obj = MyObject->new(node => <<EOXML); 
     
    7374=head1 DECLARATION 
    7475 
    75 =head2 has_dom_root $tag 
     76=head2 has_dom_root $name[, %opts] 
    7677 
    77 Specifies that the given XML have the specified tag 
     78Specifies that the given XML have the specified tag. This specification is 
     79also used when creating new root node for creating the underlying XML 
     80 
     81  has_dom_root $name => ( 
     82    # attributes => { ... } 
     83  ); 
    7884 
    7985=head2 has_dom_attr $name[, %opts] 
  • lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML.pm

    r17304 r17333  
    1010    is => 'rw', 
    1111    isa => 'MooseX::DOM::LibXML::ContextNode', 
    12     required => 1, 
    1312); 
    1413 
     
    4140            confess "Don't know how to handle $node"; 
    4241        } 
    43     } 
    44  
    45     return  { %args, node => $node }; 
    46 } 
    47  
    48 { 
     42 
     43        $args{node} = $node; 
     44    } 
     45 
     46    return  { %args }; 
     47} 
     48 
     49BOOTSTRAP: { 
    4950    my $subname = sub { join('::', $_[1] || __PACKAGE__, $_[0]) }; 
    5051    my $subassign = sub { 
     
    5657    my $textfilter = sub { 
    5758        my $self = shift; 
    58         return map { $_->textContent } @_; 
     59        return map { blessed $_ && $_->can('textContent') ? $_->textContent : $_ } @_; 
    5960    }; 
    6061 
     
    8485    # Used only in has_dom_children, to create a list of element nodes from 
    8586    # list of text 
    86     my $text2elements =Class::MOP::subname($subname->('text2elements') => sub { 
     87    my $text2elements = Class::MOP::subname($subname->('text2elements') => sub { 
    8788        my($self, %args) = @_; 
    8889 
     
    113114    my %exports = ( 
    114115        has_dom_root => sub { 
    115             return Class::MOP::subname($subname->('has_dom_attr') => sub ($;%) { 
     116            return Class::MOP::subname($subname->('has_dom_root') => sub ($;%) { 
    116117                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 }; 
    118127 
    119128                my $assert_root = sub { 
     
    125134                }; 
    126135 
    127                 my $meta = $caller->meta; 
    128136                $meta->add_around_method_modifier(new => sub { 
    129137                    my $next = shift; 
     
    148156                    $name = $args{accessor}; 
    149157                } 
     158 
     159                $caller->meta->{'%!dom_attributes'}->{$name} = 1; 
    150160                my $method = $subname->($name, $caller); 
    151161                $subassign->($method => sub { 
     
    171181                    $name = $args{accessor}; 
    172182                } 
     183                $caller->meta->{'%!dom_attributes'}->{$name} = 1; 
    173184                my $method = $subname->($name, $caller); 
    174185 
     
    182193                        $node->getChildrenByTagName($tagname) 
    183194                    ; 
    184  
    185195                    if (@_) { 
    186196                        $create->($self, children => \@children, values => \@_, namespace => $namespace, tag => $tagname); 
     
    204214                } 
    205215 
     216                my $meta = $caller->meta; 
     217                $meta->{'%!dom_attributes'}->{$name} = 1; 
    206218                my $method = $subname->($name, $caller); 
    207219                $subassign->($method => sub { 
     
    209221 
    210222                    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                    } 
    216231 
    217232                    if (@_) { 
     233                        $self->__create_root_node(); 
    218234                        $create->( $self, child => $child, value => $_[0], namespace => $namespace, tag => $tagname); 
    219235                    } 
     
    231247    }); 
    232248    sub export_dsl { 
    233         goto &$export; 
     249        goto &$export if $export; 
    234250    } 
    235251 
     
    254270        } 
    255271    } 
    256  
     272} 
     273 
     274sub 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; 
    257284} 
    258285 
     
    262289} 
    263290 
     291sub __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 
    264308 
    2653091;