PRANG.pm

filter: Statementstmt 100% 2 / 2 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 2 / 2 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« PRANG/Cookbook.pm ↑ Summary PRANG/Cookbook/Date.pm »
1  
2 package PRANG;
3  
4 9 our $VERSION = "0.11_03";
5  
6 9 1;
7  
8 __END__
9  
10 =head1 NAME
11  
12 PRANG - XML graph engine - XML to Moose objects and back!
13  
14 =head1 SYNOPSIS
15  
16 # step 1. define a common role for nodes in your XML language
17 package XML::Language::Node;
18 use Moose::Role;
19 sub xmlns { "http://example.com/language/1.0" }
20  
21 # step 2. define the root node(s) of your language
22 package XML::Language;
23 use Moose;
24 use PRANG::Graph;
25 sub root_element {
26 "envy"
27 };
28 has_attr 'laziness' =>
29 is => "ro",
30 isa => "Str",
31 ;
32 has_element 'lust' =>
33 is => "ro",
34 isa => "XML::Language::Lust",
35 ;
36 with 'PRANG::Graph', 'XML::Language::Node';
37  
38 # step 3. define further elements in your schema
39 package XML::Language::Lust;
40 use Moose;
41 use PRANG::Graph;
42 use PRANG::XMLSchema::Types;
43  
44 has_attr 'gluttony' =>
45 is => "ro",
46 isa => "PRANG::XMLSchema::byte",
47 ;
48 has_element 'sins' =>
49 is => "ro",
50 isa => "ArrayRef[XML::Language::Lust|Str]",
51 xml_nodeName => {
52 'lust' => 'XML::Language::Lust',
53 'anger' => 'Str',
54 },
55 ;
56 has_element 'greed' =>
57 is => "ro",
58 isa => "Bool",
59 ;
60 with 'XML::Language::Node';
61  
62 # step 4a. parse!
63 my $object = XML::Language->parse(<<XML);
64 <envy laziness="Very">
65 <lust gluttony="127">
66 <anger>You wouldn't like me when I'm angry</anger>
67 <lust>
68 <anger>You've done it now!</anger>
69 <greed />
70 </lust>
71 </lust>
72 </envy>
73 XML;
74  
75 # Parsing the above would give you the same structure as this:
76 XML::Language->new(
77 laziness => "Very",
78 lust => XML::Language::Lust->new(
79 gluttony => 127,
80 sins => [
81 "You wouldn't like me when I'm angry",
82 XML::Language::Lust->new(
83 sins => [ "You've done it now!" ],
84 greed => 1,
85 ),
86 ],
87 );
88  
89 # step 4b. emit!
90 $format = 1;
91 print $object->to_xml($format);
92  
93 =head1 DESCRIPTION
94  
95 PRANG is an B<XML Graph> engine, which provides B<post-schema
96 validation objects> (PSVO).
97  
98 It is designed for implementing XML languages for which a description
99 of the valid sets of XML documents is available - for example, a DTD,
100 W3C XML Schema or Relax specification. With PRANG (and, like
101 L<XML::Toolkit>), your class structure I<is> your XML Graph.
102  
103 XML namespaces are supported, and the module tries to make many XML
104 conventions as convenient as possible in the generated classes. This
105 includes XML data (elements with no attributes and textnode contents),
106 and presence elements (empty elements with no attributes which
107 indicate something). It also supports mixed and unprocessed portions
108 of the XML, and "pluggable" specifications.
109  
110 Currently, these must be manually constructed as in the example -
111 details on this are to be found on the L<PRANG::Graph::Meta::Element>
112 and L<PRANG::Graph::Meta::Attr> perldoc. There is also a cookbook of
113 examples - see L<PRANG::Cookbook>.
114  
115 However, eventually it should be possible to automatically process
116 schema documents to produce a class structure (see L</KNOWN
117 LIMITATIONS>).
118  
119 Once the L<PRANG::Graph> has been built, you can:
120  
121 =over
122  
123 =item B<marshall XML in>
124  
125 The L<PRANG::Marshaller> takes any well-formed document parsable by
126 L<XML::LibXML>, and constructs a corresponding set of Moose objects.
127  
128 A shortcut is available via the C<parse> method on the starting point
129 of the graph (indicated by using the role 'L<PRANG::Graph>').
130  
131 You can also parse documents which have multiple start nodes, by
132 defining a role which the concrete instances use.
133  
134 eg, for the example in the SYNOPSIS; define a role
135 'XML::Language::Family' - the root node will be parsed by the class
136 with a matching C<root_element> (and C<xmlns>) value.
137  
138 package XML::Language::Family;
139 use Moose::Role;
140 with 'PRANG::Graph';
141  
142 package XML::Language;
143 use Moose;
144 with 'XML::Language::Family';
145  
146 # later ...
147 my $marshaller = PRANG::Marshaller->get("XML::Language::Family");
148 my $object = $marshaller->parse($xml);
149  
150 B<note> the C<PRANG::Marshaller> API will probably go away in a future
151 release, once the "parse" role method is made to work correctly.
152  
153 =item B<marshall XML out>
154  
155 A L<PRANG::Graph> structure also has a C<to_xml> method, which emits
156 XML (optionally indented).
157  
158 =back
159  
160 =head1 Why "XML Graph"?
161  
162 The term B<XML Graph> is from the paper, "XML Graphs in Program
163 Analysis", Møller and Schwartzbach (2007).
164  
165 L<http://www.brics.dk/~amoeller/papers/xmlgraphs/>
166  
167 The difference between an B<Graph> and a B<Tree>, is that a Graph can
168 contain cycles, whereas a Tree cannot - there is only one correct way
169 to follow a tree, whereas there can be many correct ways to follow a
170 graph.
171  
172 So, XML documents are considered to be trees, and the mechanisms which
173 describe allowable forms for those trees XML graphs.
174  
175 They are graphs, because they can contain cycles - cycles in an XML
176 graph might point back to the same element (indicating an "any number
177 of this element" condition), or point to a different element closer to
178 the initial element (indicating an arbitrary level of nesting).
179  
180 =head1 KNOWN LIMITATIONS
181  
182 Support for these features will be considered as tuits allow. If you
183 can create a patch for any of these features which meets the coding
184 standards, they are very likely to be accepted. The authors will
185 provide guidance and/or assistance through this process, time and
186 patience permitting.
187  
188 =head2 Creating XML Graphs from Schema documents
189  
190 Validating/Parsing schema documents, and transforming those
191 to a L<PRANG::Graph> structure, could well be a valid approach to
192 address these issues and may be addressed by later releases and/or
193 modules which implement those XML languages using PRANG.
194  
195 =head2 Creating XML Graphs from example documents
196  
197 This is a bit more shonky an approach, but can be very useful for
198 I<ad-hoc> XML conventions for which no rigid definition can be found.
199 Currently, L<XML::Toolkit> is the best module for this.
200  
201 =head2 Validating Indeterminate Graphs
202  
203 It's possible that at a given point in time, a graph may be followed
204 in more than one direction, and the correct direction cannot be
205 determined based on the currently input token. However, few if any
206 XML languages are this indeterminate, so while many schema languages
207 may allow this to be specified, they should (hopefully) not correspond
208 to major standards.
209  
210 =head1 SOURCE, SUBMISSIONS, SUPPORT
211  
212 Source code is available from Catalyst:
213  
214 git://git.catalyst.net.nz/PRANG.git
215  
216 And Github:
217  
218 git://github.com/catalyst/PRANG.git
219  
220 Please see the file F<SubmittingPatches> for information on preferred
221 submission format.
222  
223 Suggested avenues for support:
224  
225 =over
226  
227 =item *
228  
229 Moose user's mailing list - see the L<Moose> perldoc for more
230 information. Please check with the latest release of PRANG - if there
231 is sufficient interest, a separate list may have been created.
232  
233 =item *
234  
235 Contact the author and ask either politely or commercially for help.
236  
237 =item *
238  
239 Log a ticket on L<http://rt.cpan.org/>
240  
241 =back
242  
243 =head1 AUTHOR AND LICENCE
244  
245 Development commissioned by NZ Registry Services, and carried out by
246 Catalyst IT - L<http://www.catalyst.net.nz/>
247  
248 Copyright 2009, 2010, NZ Registry Services. This module is licensed
249 under the Artistic License v2.0, which permits relicensing under other
250 Free Software licenses.
251  
252 =cut
« PRANG/Cookbook.pm ↑ Summary PRANG/Cookbook/Date.pm »