- Timestamp:
- 08/16/08 09:30:42 (5 years ago)
- Location:
- lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM
- Files:
-
- 2 modified
-
LibXML.pm (modified) (4 diffs)
-
LibXML/ContextNode.pm (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML.pm
r17687 r17689 12 12 is => 'rw', 13 13 isa => 'MooseX::DOM::LibXML::ContextNode', 14 coerce => 1, 14 15 ); 15 16 … … 23 24 no Moose; 24 25 26 sub BUILD { 27 my $self = shift; 28 $self->node->register_namespaces( $self->namespaces ); 29 $self; 30 } 31 25 32 sub init_meta { 26 33 # Only MooseX::DOM knows the true caller, so we expect it to … … 33 40 'MooseX::DOM::Meta::LibXML' 34 41 ); 35 }36 37 sub BUILDARGS {38 my ($self, %args) = @_;39 40 my $namespaces = $args{namespaces} || {};41 my $node = delete $args{node};42 if ($node) {43 if (! ref $node) {44 $node = MooseX::DOM::LibXML::ContextNode->new(45 node => XML::LibXML->new->parse_string($node)->documentElement,46 namespaces => $namespaces47 );48 } elsif ($node->isa('XML::LibXML::Element')) {49 $node = MooseX::DOM::LibXML::ContextNode->new(50 node => $node,51 namespaces => $namespaces52 );53 } else {54 confess "Don't know how to handle $node";55 }56 57 $args{node} = $node;58 }59 60 return { %args };61 42 } 62 43 … … 108 89 # attributes => { attr1 => $val1, attr2 => $val2 } 109 90 91 92 # Find the meta attribute for 'node', and add type constraints 93 110 94 $tag = $args{tag} if $args{tag}; 111 95 my $attrs = $args{attributes}; 112 96 113 97 my $meta = $caller->meta; 98 my $node_attr = $meta->get_attribute('node'); 99 return () unless $node_attr; 100 101 114 102 $meta->dom_root( { tag => $tag, attributes => $attrs } ); 115 103 -
lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML/ContextNode.pm
r17247 r17689 3 3 package MooseX::DOM::LibXML::ContextNode; 4 4 use Moose; 5 use Moose::Util::TypeConstraints; 5 6 use Class::Inspector; 6 7 use XML::LibXML; 7 8 use XML::LibXML::XPathContext; 9 10 coerce 'MooseX::DOM::LibXML::ContextNode' 11 => from 'Str' 12 => via { 13 MooseX::DOM::LibXML::ContextNode->new( 14 node => XML::LibXML->new->parse_string($_)->documentElement 15 ) 16 } 17 ; 8 18 9 19 has 'node' => ( … … 22 32 is => 'rw', 23 33 isa => 'XML::LibXML::XPathContext', 24 required => 1,25 34 handles => [ qw(findnodes findvalue) ], 26 35 ); … … 30 39 no Moose; 31 40 32 sub BUILD ARGS{33 my ($self, %args) = @_;41 sub BUILD { 42 my ($self, $args) = @_; 34 43 35 my $xc = XML::LibXML::XPathContext->new($args{node}); 36 while (my ($prefix, $uri) = each %{ $args{namespaces} || {} }) { 44 my $xc = XML::LibXML::XPathContext->new($self->node); 45 $self->context($xc); 46 $self->register_namespaces( $args->{namespaces} ) if $args->{namespaces}; 47 $self; 48 } 49 50 sub register_namespaces { 51 my ($self, $namespaces) = @_; 52 53 my $xc = $self->context; 54 while (my ($prefix, $uri) = each %{ $namespaces || {} }) { 37 55 next if defined $xc->lookupNs($uri); 38 56 39 57 $xc->registerNs($prefix, $uri); 40 58 } 41 42 return { %args, context => $xc };43 59 } 44 60 61 45 62 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)