| 1 | package GraphViz2::Parse::XML; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | |||
| 7 | our $VERSION = '2.50'; | ||
| 8 | |||
| 9 | use GraphViz2; | ||
| 10 | use Moo; | ||
| 11 | use XML::Tiny; | ||
| 12 | |||
| 13 | has graph => | ||
| 14 | ( | ||
| 15 | default => sub { | ||
| 16 | GraphViz2->new( | ||
| 17 | edge => {color => 'grey'}, | ||
| 18 | global => {directed => 1}, | ||
| 19 | graph => {rankdir => 'TB'}, | ||
| 20 | node => {color => 'blue', shape => 'oval'}, | ||
| 21 | ) | ||
| 22 | }, | ||
| 23 | is => 'rw', | ||
| 24 | #isa => 'GraphViz2', | ||
| 25 | required => 0, | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub create | ||
| 29 | { | ||
| 30 | my($self, %arg) = @_; | ||
| 31 | |||
| 32 | $self -> parse('', 0, XML::Tiny::parsefile($arg{file_name}) ); | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | |||
| 36 | } # End of create. | ||
| 37 | |||
| 38 | # ------------------------------------------------ | ||
| 39 | |||
| 40 | sub parse | ||
| 41 | { | ||
| 42 | my($self, $parent, $level, $araref) = @_; | ||
| 43 | |||
| 44 | for my $item (@$araref) | ||
| 45 | { | ||
| 46 | if ($$item{type} eq 'e') | ||
| 47 | { | ||
| 48 | $self -> graph -> add_node(name => $$item{name}); | ||
| 49 | $self -> graph -> add_edge(from => $parent, to => $$item{name}) if ($parent); | ||
| 50 | $self -> parse($$item{name}, $level + 1, $$item{content}); | ||
| 51 | } | ||
| 52 | else | ||
| 53 | { | ||
| 54 | $self -> graph -> add_node(name => $$item{content}, shape => 'rectangle'); | ||
| 55 | $self -> graph -> add_edge(from => $parent, to => $$item{content}) if ($parent); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | } # End of parse. | ||
| 60 | |||
| 61 | # ----------------------------------------------- | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | =pod | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | L<GraphViz2::Parse::XML> - Visualize XML as a graph | ||
| 70 | |||
| 71 | =head1 Synopsis | ||
| 72 | |||
| 73 | #!/usr/bin/env perl | ||
| 74 | |||
| 75 | use strict; | ||
| 76 | use warnings; | ||
| 77 | |||
| 78 | use File::Spec; | ||
| 79 | |||
| 80 | use GraphViz2; | ||
| 81 | use GraphViz2::Parse::XML; | ||
| 82 | |||
| 83 | my($graph) = GraphViz2 -> new | ||
| 84 | ( | ||
| 85 | edge => {color => 'grey'}, | ||
| 86 | global => {directed => 1}, | ||
| 87 | graph => {rankdir => 'TB'}, | ||
| 88 | node => {color => 'blue', shape => 'oval'}, | ||
| 89 | ); | ||
| 90 | my($g) = GraphViz2::Parse::XML -> new(graph => $graph); | ||
| 91 | |||
| 92 | $g -> create(file_name => File::Spec -> catfile('t', 'sample.xml') ); | ||
| 93 | |||
| 94 | my($format) = shift || 'svg'; | ||
| 95 | my($output_file) = shift || File::Spec -> catfile('html', "parse.xml.pp.$format"); | ||
| 96 | |||
| 97 | $graph -> run(format => $format, output_file => $output_file); | ||
| 98 | |||
| 99 | See scripts/parse.xml.pp.pl (L<GraphViz2/Scripts Shipped with this Module>). | ||
| 100 | |||
| 101 | =head1 Description | ||
| 102 | |||
| 103 | Takes an XML file and converts it into a graph, using the pure-Perl XML::Tiny. | ||
| 104 | |||
| 105 | You can write the result in any format supported by L<Graphviz|http://www.graphviz.org/>. | ||
| 106 | |||
| 107 | Here is the list of L<output formats|http://www.graphviz.org/content/output-formats>. | ||
| 108 | |||
| 109 | =head1 Constructor and Initialization | ||
| 110 | |||
| 111 | =head2 Calling new() | ||
| 112 | |||
| 113 | C<new()> is called as C<< my($obj) = GraphViz2::Parse::XML -> new(k1 => v1, k2 => v2, ...) >>. | ||
| 114 | |||
| 115 | It returns a new object of type C<GraphViz2::Parse::XML>. | ||
| 116 | |||
| 117 | Key-value pairs accepted in the parameter list: | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item o graph => $graphviz_object | ||
| 122 | |||
| 123 | This option specifies the GraphViz2 object to use. This allows you to configure it as desired. | ||
| 124 | |||
| 125 | The default is GraphViz2 -> new. The default attributes are the same as in the synopsis, above. | ||
| 126 | |||
| 127 | This key is optional. | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =head1 Methods | ||
| 132 | |||
| 133 | =head2 create(file_name => $file_name) | ||
| 134 | |||
| 135 | Creates the graph, which is accessible via the graph() method, or via the graph object you passed to new(). | ||
| 136 | |||
| 137 | Returns $self for method chaining. | ||
| 138 | |||
| 139 | $file_name is the name of an XML file. | ||
| 140 | |||
| 141 | =head2 graph() | ||
| 142 | |||
| 143 | Returns the graph object, either the one supplied to new() or the one created during the call to new(). | ||
| 144 | |||
| 145 | =head1 FAQ | ||
| 146 | |||
| 147 | See L<GraphViz2/FAQ> and L<GraphViz2/Scripts Shipped with this Module>. | ||
| 148 | |||
| 149 | =head1 Scripts Shipped with this Module | ||
| 150 | |||
| 151 | =head2 scripts/parse.xml.pp.pl | ||
| 152 | |||
| 153 | Demonstrates using L<XML::Tiny> to parse XML. | ||
| 154 | |||
| 155 | Inputs from ./t/sample.xml, and outputs to ./html/parse.xml.pp.svg by default. | ||
| 156 | |||
| 157 | =head1 Thanks | ||
| 158 | |||
| 159 | Many thanks are due to the people who chose to make L<Graphviz|http://www.graphviz.org/> Open Source. | ||
| 160 | |||
| 161 | And thanks to L<Leon Brocard|http://search.cpan.org/~lbrocard/>, who wrote L<GraphViz>, and kindly gave me co-maint of the module. | ||
| 162 | |||
| 163 | =head1 Author | ||
| 164 | |||
| 165 | L<GraphViz2> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2011. | ||
| 166 | |||
| 167 | Home page: L<http://savage.net.au/index.html>. | ||
| 168 | |||
| 169 | =head1 Copyright | ||
| 170 | |||
| 171 | Australian copyright (c) 2011, Ron Savage. | ||
| 172 | |||
| 173 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 174 | you can redistribute them and/or modify them under the terms of | ||
| 175 | The Perl License, a copy of which is available at: | ||
| 176 | http://dev.perl.org/licenses/ | ||
| 177 | |||
| 178 | =cut |