File Coverage

blib/lib/W3C/SOAP/Document/Node.pm
Criterion Covered Total %
statement 24 42 57.1
branch 0 12 0.0
condition 0 6 0.0
subroutine 8 11 72.7
pod n/a
total 32 71 45.0


line stmt bran cond sub pod time code
1             package W3C::SOAP::Document::Node;
2              
3             # Created on: 2012-05-26 19:04:19
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 1     1   1773 use Moose;
  1         2  
  1         8  
10 1     1   5069 use warnings;
  1         2  
  1         25  
11 1     1   4 use version;
  1         2  
  1         6  
12 1     1   58 use Carp;
  1         2  
  1         62  
13 1     1   5 use Scalar::Util;
  1         1  
  1         32  
14 1     1   5 use List::Util;
  1         1  
  1         47  
15 1     1   6 use Data::Dumper qw/Dumper/;
  1         7  
  1         39  
16 1     1   5 use English qw/ -no_match_vars /;
  1         1  
  1         7  
17              
18             our $VERSION = version->new('0.11');
19             $ENV{W3C_SOAP_NAME_STYLE} ||= 'perl';
20              
21             has node => (
22             is => 'rw',
23             isa => 'XML::LibXML::Node',
24             required => 1,
25             );
26             has parent_node => (
27             is => 'rw',
28             isa => 'Maybe[W3C::SOAP::Document::Node]',
29             predicate => 'has_parent_node',
30             weak_ref => 1,
31             );
32             has document => (
33             is => 'rw',
34             isa => 'W3C::SOAP::Document',
35             required => 1,
36             builder => '_document',
37             lazy => 1,
38             weak_ref => 1,
39             handles => {
40             xpc => 'xpc',
41             },
42             );
43             has name => (
44             is => 'rw',
45             isa => 'Maybe[Str]',
46             predicate => 'has_name',
47             builder => '_name',
48             lazy => 1,
49             );
50              
51             has perl_name => (
52             is => 'rw',
53             isa => 'Maybe[Str]',
54             predicate => 'has_perl_name',
55             builder => '_perl_name',
56             lazy => 1,
57             );
58              
59             has perl_names => (
60             is => 'ro',
61             isa => 'HashRef',
62             lazy => 1,
63             default => sub { return {} },
64             );
65              
66             around BUILDARGS => sub {
67             my ($orig, $class, @args) = @_;
68             my $args
69             = !@args ? {}
70             : @args == 1 ? $args[0]
71             : {@args};
72              
73             confess "If document is not specified parent_node must be defined!\n"
74             if !$args->{document} && !$args->{parent_node};
75              
76             return $class->$orig($args);
77             };
78              
79             sub _document {
80 0     0     my ($self) = shift;
81 0 0 0       confess "Lazybuild $self has both no parent_node nore document!\n" if !$self->has_parent_node || !defined $self->parent_node;
82 0 0         return $self->parent_node->isa('W3C::SOAP::Document') ? $self->parent_node : $self->parent_node->document;
83             }
84              
85             sub _name {
86 0     0     my ($self) = shift;
87 0           my $name = $self->node->getAttribute('name');
88 0 0         $name =~ s/\W/_/gxms if $name;
89 0           return $name;
90             }
91              
92             sub _perl_name
93             {
94 0     0     my ($self) = @_;
95 0           my $name = $self->name;
96              
97 0 0 0       if ( $name && ( $ENV{W3C_SOAP_NAME_STYLE} ne 'original' ) ) {
98              
99 0           $name =~ s/ (?<= [^A-Z_] ) ([A-Z]) /_$1/gxms;
100              
101             # the allowed characters in XML identifiers are not the same
102             # as those in Perl
103 0           $name =~ s/\W//g;
104 0           $name = lc $name;
105              
106             # horrid hack to dedupe elements Foo_Bar and Foo.Bar
107             # which are obviously stupid but allowed
108 0 0         if ( defined( my $parent = $self->parent_node() ) ) {
109 0 0         if ( exists $parent->perl_names()->{$name} ) {
110 0           $name .= '_' . $parent->perl_names()->{$name};
111             }
112 0           $parent->perl_names()->{$name}++;
113             }
114             }
115 0           return $name;
116             }
117              
118             1;
119              
120             __END__
121              
122             =head1 NAME
123              
124             W3C::SOAP::Document::Node - The super class for document nodes
125              
126             =head1 VERSION
127              
128             This documentation refers to W3C::SOAP::Document::Node version 0.11.
129              
130             =head1 SYNOPSIS
131              
132             use W3C::SOAP::Document::Node;
133              
134             # Brief but working code example(s) here showing the most common usage(s)
135             # This section will be as far as many users bother reading, so make it as
136             # educational and exemplary as possible.
137              
138              
139             =head1 DESCRIPTION
140              
141             Base class for modules extracting information about XML nodes.
142              
143             =head1 SUBROUTINES/METHODS
144              
145             =over 4
146              
147             =item C<perl_name ()>
148              
149             Converts the node's name (if it has one) from camel case to the Perl style
150             underscore separated words eg TagName -> tag_name.
151              
152             =back
153              
154             =head1 DIAGNOSTICS
155              
156             =head1 CONFIGURATION AND ENVIRONMENT
157              
158             =head1 DEPENDENCIES
159              
160             =head1 INCOMPATIBILITIES
161              
162             =head1 BUGS AND LIMITATIONS
163              
164             There are no known bugs in this module.
165              
166             Please report problems to Ivan Wills (ivan.wills@gmail.com).
167              
168             Patches are welcome.
169              
170             =head1 AUTHOR
171              
172             Ivan Wills - (ivan.wills@gmail.com)
173              
174             =head1 LICENSE AND COPYRIGHT
175              
176             Copyright (c) 2012 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
177             All rights reserved.
178              
179             This module is free software; you can redistribute it and/or modify it under
180             the same terms as Perl itself. See L<perlartistic>. This program is
181             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
182             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
183             PARTICULAR PURPOSE.
184              
185             =cut