blib/lib/W3C/SOAP/Utils.pm

filter: Statementstmt 100% 69 / 69 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 99.0% 100 / 101 CC 7 SCAR 5.7
CC
7
Cov
99%
xml_error
3.0
split_ns
3.0
normalise_ns
2.0
CRAP 1.8
« blib/lib/W3C/SOAP/Header.pm ↑ Summary blib/lib/W3C/SOAP/WSDL/Document/Port.pm »
1 package W3C::SOAP::Utils;
2  
3 # Created on: 2012-06-01 12:15:15
4 # Create by: dev
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use W3C::SOAP::WSDL::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use W3C::SOAP::WSDL::Meta::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 Moose::Exporter->setup_import_methods(
21 as_is => [qw/split_ns xml_error normalise_ns cmp_ns ns2module/],
22 with_meta => ['operation'],
23 );
24  
25 8 our $VERSION = 0.13;
26  
27 sub split_ns {
28 2829 ▶ my ($tag) = @_;
Subroutine split_ns: called CC=3 SCAR=11.0
Pod documented
29 2829 ▶ confess "No XML tag passed to split!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 1 2828 2829
30 2828 my ($ns, $name) = split /:/xms, $tag, 2;
31 2828 ▶ return $name ? ($ns, $name) : ('', $ns);
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 2822 6 2828
32 }
33  
34 sub normalise_ns {
35 633 ▶ my ($ns) = @_;
Subroutine normalise_ns: called CC=2 SCAR=6.9
Pod documented
36  
37 633 my $uri = URI->new($ns);
38  
39 633 ▶ if ( $uri->can('host') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("host")) 231 402 633
40 231 $uri->host(lc $uri->host);
41 }
42  
43 633 return "$uri";
44 }
45  
46 sub ns2module {
47 12 ▶ my ($ns) = @_;
Subroutine ns2module: called CC=2 SCAR=6.9
Pod documented
48  
49 12 my $uri = URI->new($ns);
50  
51 # URI's which have a host an a path are converted Java style name spacing
52 12 ▶ if ( $uri->can('host') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("host")) 10 2 12
53 my $module
54 = join '::',
55 27 reverse map { ucfirst $_}
56 27 map { lc $_ }
57 10 map { s/\W/_/gxms; $_ } ## no critic
58 split /[.]/xms, $uri->host;
59 $module .= join '::',
60 10 map { s/\W/_/gxms; $_ } ## no critic
61 split m{/}xms, $uri->path;
62 10 return $module;
63 }
64  
65 # other URI's are just made safe as a perl module name.
66 2 $ns =~ s{://}{::}xms;
67 2 $ns =~ s{([^:]:)([^:])}{$1:$2}gxms;
68 2 $ns =~ s{[^\w:]+}{_}gxms;
69  
70 2 return $ns;
71 }
72  
73 sub cmp_ns {
74 18 ▶ my ($ns1, $ns2) = @_;
Subroutine cmp_ns: called CC=1 SCAR=0.0
Pod documented
75  
76 18 return normalise_ns($ns1) eq normalise_ns($ns2);
77 }
78  
79 sub xml_error {
80 2 ▶ my ($node) = @_;
Subroutine xml_error: called CC=3 SCAR=11.0
Pod documented
81 2 my @lines = split /\r?\n/xms, $node->toString;
82 2 my $indent = '';
83 2 ▶ if ( $lines[-1] =~ /^(\s+)/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[-1] =~ /^(\s+)/msx) 1 1 2
84 1 $indent = $1;
85 }
86 2 my $error = $indent . $node->toString."\n at ";
87 2 ▶ $error .= "line - ".$node->line_number.' ' if $node->line_number;
Branch: 1 branch
BranchTrueFalseTotal
if $node->line_number 2 0 2
88 2 $error .= "path - ".$node->nodePath;
89  
90 2 return $error;
91 }
92  
93 8 1;
94  
95 __END__
96  
97 =head1 NAME
98  
99 W3C::SOAP::Utils - Utility functions to be used with C<W3C::SOAP> modules
100  
101 =head1 VERSION
102  
103 This documentation refers to W3C::SOAP::Utils version 0.13.
104  
105 =head1 SYNOPSIS
106  
107 use W3C::SOAP::Utils;
108  
109 # splits tags with an optional XML namespace prefix
110 my ($namespace, $tag) = split_ns('xs:thing');
111 # $namespace = xs
112 # $tag = thing
113  
114 =head1 DESCRIPTION
115  
116 Utility Functions
117  
118 =head1 SUBROUTINES
119  
120 =over 4
121  
122 =item C<split_ns ($name)>
123  
124 Splits an XML tag's namespace from the tag name
125  
126 =item C<normalise_ns ($ns)>
127  
128 Creates a normalized XML name space string (ie lower cases the host part of
129 the name space)
130  
131 =item C<ns2module ($ns)>
132  
133 Takes the XML namespace C<$ns> and coverts it to a module name, if it is a
134 "normal" URI the module name is got by reversing the order of the domain
135 parts and joining that with any directory parts (setting default Perl module
136 capitalization along the way)
137  
138 eg http://www.example.com/some/path => Com::Example::Www::Some::Path
139  
140 If the URI doesn't have a host part then URI is split on the non-word
141 characters and similarly rejoined
142  
143 eg uri:thing.other/unknown => Uri::Thing::Other::Unknown
144  
145 =item C<cmp_ns ($ns1, $ns2)>
146  
147 Compare two namespaces (with normalized host parts lower cased)
148  
149 =item C<xml_error ($xml_node)>
150  
151 Pretty format the C<$xml_node> for an error message
152  
153 =back
154  
155 =head1 MOOSE HELPERS
156  
157 =over 4
158  
159 =item C<operation ($name, %optisns)>
160  
161 See L<W3C::SOAP::WSDL::Utils> for details using it from this module is deprecated
162  
163 =back
164  
165 =head1 DIAGNOSTICS
166  
167 =head1 CONFIGURATION AND ENVIRONMENT
168  
169 =head1 DEPENDENCIES
170  
171 =head1 INCOMPATIBILITIES
172  
173 =head1 BUGS AND LIMITATIONS
174  
175 There are no known bugs in this module.
176  
177 Please report problems to Ivan Wills - (ivan.wills@gmail.com)
178  
179 Patches are welcome.
180  
181 =head1 AUTHOR
182  
183 Ivan Wills - (ivan.wills@gmail.com)
184  
185 =head1 LICENSE AND COPYRIGHT
186  
187 Copyright (c) 2012 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
188 All rights reserved.
189  
190 This module is free software; you can redistribute it and/or modify it under
191 the same terms as Perl itself. See L<perlartistic>. This program is
192 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
193 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
194 PARTICULAR PURPOSE.
195  
196 =cut
« blib/lib/W3C/SOAP/Header.pm ↑ Summary blib/lib/W3C/SOAP/WSDL/Document/Port.pm »