PRANG/Graph.pm

filter: Statementstmt 87.5% 70 / 80 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.4% 23 / 26 Podpod 100% 2 / 2 totaltotal 84.4% 98 / 116 CC 4 SCAR 2.4
CC
4
Cov
85%
has_attr
2.0
has_element
2.0
__ANON__
2.0
CRAP 1.3
« PRANG/Graph/Node.pm ↑ Summary PRANG/Cookbook/Library.pm »
1  
2 package PRANG::Graph;
3  
4 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use PRANG;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use PRANG::Graph::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use PRANG::Graph::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use PRANG::Graph::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use PRANG::Graph::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use PRANG::Graph::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use PRANG::Graph::Choice;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use PRANG::Graph::Quantity;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use PRANG::Graph::Meta::Attr;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use PRANG::Graph::Meta::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use MooseX::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use PRANG::Marshaller;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 sub has_attr {
24 47 ▶ my ( $meta, $name, %options ) = @_;
Subroutine has_attr: called CC=2 SCAR=7.0
Pod documented
25 47 ▶ my $traits_a = $options{traits} ||= [];
Condition50%1/2
$options{"traits"} ||= []
l!l
047
MC/DC0%0/1
$options{"traits"} ||= []
$options{"traits"}
Truth table50%1/2
A
A= $options{"traits"}
Aresult
11
00
26 47 push @$traits_a, "PRANG::Attr";
27 47 $meta->add_attribute(
28 $name,
29 %options,
30 );
31 }
32 sub has_element {
33 134 ▶ my ( $meta, $name, %options ) = @_;
Subroutine has_element: called CC=2 SCAR=7.0
Pod documented
34 134 ▶ my $traits_a = $options{traits} ||= [];
Condition50%1/2
$options{"traits"} ||= []
l!l
0134
MC/DC0%0/1
$options{"traits"} ||= []
$options{"traits"}
Truth table50%1/2
A
A= $options{"traits"}
Aresult
11
00
35 134 push @$traits_a, "PRANG::Element";
36 134 $meta->add_attribute(
37 $name,
38 %options,
39 );
40 }
41  
42 9 Moose::Exporter->setup_import_methods(
43 with_meta => [ qw(has_attr has_element) ],
44 metaclass_roles => [qw(PRANG::Graph::Meta::Class)],
45 );
46  
47 9 requires 'xmlns';
48 9 requires 'root_element';
49  
50 9 ▶method marshaller($inv:) { #returns PRANG::Marshaller {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.0
51 1 ▶ if ( ref $inv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inv) 1 0 1
52 1 $inv = ref $inv;
53 }
54 1 PRANG::Marshaller->get( $inv );
55 9 }
56  
57 9 ▶method parse($class: Str $xml) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
58 0 my $instance = $class->marshaller->parse( xml => $xml );
59 0 return $instance;
60 9 }
61  
62 9 ▶method parse_file($class: Str $filename) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
63 0 my $instance = $class->marshaller->parse( filename => $filename );
64 0 return $instance;
65 9 }
66  
67 9 ▶method parse_fh($class: GlobRef $fh) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
68 0 my $instance = $class->marshaller->parse( fh => $fh );
69 0 return $instance;
70 9 }
71  
72 9 ▶method to_xml(Int $format = 0) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.2
73 1 my $marshaller = $self->marshaller;
74 0 $marshaller->to_xml( $self, $format );
75 9 }
76  
77 9 1;
78  
79 =head1 NAME
80  
81 PRANG::Graph - XML mapping by peppering Moose attributes
82  
83 =head1 SYNOPSIS
84  
85 # declaring a /language/
86 package My::XML::Language;
87 use Moose;
88 use PRANG::Graph;
89 sub xmlns { "some:urn" }
90 sub root_element { "Root" }
91 with 'PRANG::Graph';
92 has_element "data" =>
93 is => "ro",
94 isa => "My::XML::Language::Node",
95 ;
96  
97 # declaring a /node/ in a language
98 package My::XML::Language::Node;
99 use Moose;
100 use PRANG::Graph;
101 has_attr "count" =>
102 is => "ro",
103 isa => "Num",
104 ;
105 has_element "text" =>
106 is => "ro",
107 isa => "Str",
108 xml_nodeName => "",
109 ;
110  
111 package main;
112 # example document for the above.
113 my $xml = q[<Root xmlns="some:urn"><data count="2">blah</data></Root>];
114  
115 # loading XML to data structures
116 my $parsed = My::XML::Language->parse($xml);
117  
118 # alternatives
119 $parsed = My::XML::Language->parse_file($filename);
120 $parsed = My::XML::Language->parse_fh($fh);
121  
122 # converting back to XML
123 print $parsed->to_xml;
124  
125 =head1 DESCRIPTION
126  
127 PRANG::Graph allows you to mark attributes on your L<Moose> classes as
128 corresponding to XML attributes and child elements. This allows your
129 class structure to function as an I<XML graph> (a generalized form of
130 an specification for the shape of an XML document; ie, what nodes and
131 attributes are allowed at which point).
132  
133 =head2 PRANG::Graph for document types
134  
135 If a class implements a document type, that is, it is a valid root
136 node for your language, it B<must> both C<use> and implement the
137 C<PRANG::Graph> role, and implement the required functions C<xmlns>
138 and C<root_element>;
139  
140 package XML::Language;
141 use Moose;
142 use PRANG::Graph;
143 sub xmlns { }
144 sub root_element { "rootNode" }
145 with 'PRANG::Graph';
146  
147 If no URL is returned by the C<xmlns> function, XML namespaces are not
148 permitted in the input documents.
149  
150 =head2 PRANG::Graph for element types
151  
152 If a class implements an element, that is, a node which appears
153 somewhere other than the root node of your language, it B<must> C<use>
154 the C<PRANG::Graph> package. Well, actually, it must apply the PRANG
155 class trait, but using the package will also define the useful
156 functions C<has_attr> and C<has_element>, so most users will want
157 that.
158  
159 The minimum is;
160  
161 package XML::Language::SomeElement;
162 use PRANG::Graph;
163  
164 Note, the name of the node is not defined in the class itself, it is
165 defined in the class which includes it, using the C<has_element>
166 method. See L<PRANG::Graph::Meta::Element> for more information.
167  
168 =head2 PRANG::Graph for multi-root document types
169  
170 If your language has multiple valid root elements, then you must
171 define one document type for each valid root element. These must all
172 implement a particular role, which should bundle the C<PRANG::Graph>
173 role. The common role should not define C<root_element>, but probably
174 should define C<xmlns> in most cases:
175  
176 eg,
177  
178 package XML::Language::Family;
179 use Moose::Role;
180 sub xmlns { }
181 with 'PRANG::Graph';
182  
183 package XML::Language::One;
184 use Moose;
185 use PRANG::Graph;
186 sub root_element { "one" }
187 with 'XML::Language::Family';
188  
189 package XML::Language::Two;
190 use Moose;
191 use PRANG::Graph;
192 sub root_element { "two" }
193 with 'XML::Language::Family';
194  
195 =head2 PRANG::Graph for plug-in element types
196  
197 Normally, the details on the node name of elements is in the class
198 which includes those elements, not the target classes. However, it is
199 also possible to refer to roles instead of classes. The first time
200 the parser encounters this, it sees which loaded classes implement
201 that role, and then builds the map from element name to class.
202  
203 The classes should implement the C<PRANG::Graph> role, effectively
204 defining a document type.
205  
206 The effect of specifying a role as a type is to immediate search all
207 I<loaded packages> to see which consume the specified role.
208  
209 Given the example in the previous section, if you used:
210  
211 has_element 'some_attr' =>
212 is => "ro",
213 isa => "XML::Language::Family",
214 ;
215  
216 It would be the same as:
217  
218 has_element 'some_attr' =>
219 is => "ro",
220 isa => "XML::Language::One|XML::Language::Two",
221 xml_nodeName => {
222 one => "XML::Language::One",
223 two => "XML::Language::Two",
224 },
225 ;
226  
227 Otherwise, the requirements are the same as for regular element
228 definition as described in L<PRANG::Graph::Meta::Element>. In
229 particular, two types cannot share the same C<xmlns> I<and>
230 C<root_element>; that would be ambiguous.
231  
232 =head1 EXPORTS
233  
234 These exports are delivered to the class which says C<use
235 PRANG::Graph;>
236  
237 =head2 has_attr
238  
239 This is the same as declaring an attribute using the regular Moose
240 C<has> keyword, but adds the C<PRANG::Attr> trait, declared in
241 L<PRANG::Graph::Meta::Attr> - the attribute will behave just like a
242 regular Moose attribute, but the marshalling machinery will convert it
243 to and from a particular XML attribute (or even multiple attributes).
244 See L<PRANG::Graph::Meta::Attr> for more information.
245  
246 =head2 has_element
247  
248 Same as C<has_attr>, except it adds the C<PRANG::Element> trait,
249 indicating to the marshalling machinery to emit an XML node or
250 sequence of nodes. See L<PRANG::Graph::Meta::Element>.
251  
252 =head1 METHODS
253  
254 These methods are defined in, or required classes which implement the
255 C<PRANG::Graph> I<role>. In general, that means they are a document
256 type.
257  
258 =head2 B<parse($class: Str $xml) returns Object>
259  
260 Parse an XML string according to the PRANG Graph and return the built
261 object. Throws exceptions on error.
262  
263 By example, this is:
264  
265 my $object = $class->parse($xml);
266  
267 =head2 B<to_xml(PRANG::Graph $object: Int $format = 0) returns Str>
268  
269 Converts an object to an XML string. The returned XML will include
270 the XML declaration and so on.
271 Output will be indented by LibXML2 if the C<format> parameter is set
272 to 1. The default is 0 (do not indent). See
273 L<XML::LibXML::Document/toString> for other valid values.
274  
275 By example, this is:
276  
277 my $xml = $object->to_xml;
278  
279 =head2 B<xmlns(Moose::Meta::Class $class:) returns Maybe[Str]>
280  
281 This is a B<required class method> which returns the XML namespace of
282 this document type. This is used for emitting, and when parsing the
283 input namespace must generally be either unset or match this XML
284 namespace.
285  
286 If you are not using namespaces, just define the method in your class,
287 and return a false value.
288  
289 =head2 B<root_element(Moose::Meta::Class $class:) returns Str>
290  
291 This is a B<required class method> which returns the XML name of the
292 element which corresponds to this document type.
293  
294 =head1 HOW IT WORKS
295  
296 These details are not important for regular use of PRANG, however if
297 you can understand this you will grok the module much more quickly.
298  
299 This class applies a I<trait> to your classes' metaclass. This means,
300 that when you C<use PRANG::Graph>, there is an implied;
301  
302 use Moose -traits => ["PRANG"];
303  
304 Which is something like;
305  
306 PRANG::Graph::Meta::Class->meta->apply(__PACKAGE__->meta);
307  
308 That sets up the metaclass to be capable of being used by the
309 marshalling machinery. This machinery expects Moose attributes which
310 have the C<PRANG::Element> or C<PRANG::Attr> traits applied to connect
311 XML attributes and elements to object attributes. These are in turn
312 implemented by the L<PRANG::Graph::Meta::Attr> and
313 L<PRANG::Graph::Meta::Element> classes.
314  
315 Applying the L<PRANG::Graph> role happens separately, and delivers a
316 separate set of super-powers. It is roughly equivalent to;
317  
318 PRANG::Graph->meta->apply(__PACKAGE__);
319  
320 So, the key difference between these two aspects are the source
321 package, and the destination meta-object; in one, it is the class, in
322 the other, the metaclass.
323  
324 =head1 SEE ALSO
325  
326 L<PRANG>, L<PRANG::Graph::Meta::Class>, L<PRANG::Graph::Meta::Attr>,
327 L<PRANG::Graph::Meta::Element>, L<PRANG::Graph::Node>
328  
329 =head1 AUTHOR AND LICENCE
330  
331 Development commissioned by NZ Registry Services, and carried out by
332 Catalyst IT - L<http://www.catalyst.net.nz/>
333  
334 Copyright 2009, 2010, NZ Registry Services. This module is licensed
335 under the Artistic License v2.0, which permits relicensing under other
336 Free Software licenses.
337  
338 =cut
339  
« PRANG/Graph/Node.pm ↑ Summary PRANG/Cookbook/Library.pm »