| 1 | package PYX::XMLSchema::List; | ||||||||||
| 2 | |||||||||||
| 3 | # Pragmas. | ||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Modules. | ||||||||||
| 8 | 8 | ▶ | use Class::Utils qw(set_params); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 8 | ▶ | use List::Util qw(reduce); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 8 | ▶ | use PYX::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 8 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | # Constants. | ||||||||||
| 15 | 8 | Readonly::Scalar our $EMPTY_STR => q{}; | |||||||||
| 16 | 8 | Readonly::Scalar our $SPACE => q{ }; | |||||||||
| 17 | |||||||||||
| 18 | # Version. | ||||||||||
| 19 | 8 | our $VERSION = 0.04; | |||||||||
| 20 | |||||||||||
| 21 | # Constructor. | ||||||||||
| 22 | sub new { | ||||||||||
| 23 | 8 | ▶ | my ($class, @params) = @_; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 8 | my $self = bless {}, $class; | |||||||||
| 25 | |||||||||||
| 26 | # Output handler. | ||||||||||
| 27 | 8 | $self->{'output_handler'} = \*STDOUT; | |||||||||
| 28 | |||||||||||
| 29 | # Process params. | ||||||||||
| 30 | 8 | set_params($self, @params); | |||||||||
| 31 | |||||||||||
| 32 | # PYX::Parser object. | ||||||||||
| 33 | $self->{'_pyx_parser'} = PYX::Parser->new( | ||||||||||
| 34 | 'callbacks' => { | ||||||||||
| 35 | 'attribute' => \&_call_attribute, | ||||||||||
| 36 | 'final' => \&_call_final, | ||||||||||
| 37 | 'start_element' => \&_call_start_element, | ||||||||||
| 38 | }, | ||||||||||
| 39 | 'non_parser_options' => { | ||||||||||
| 40 | 'schemas' => {}, | ||||||||||
| 41 | }, | ||||||||||
| 42 | 6 | 'output_handler' => $self->{'output_handler'}, | |||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | # Object. | ||||||||||
| 46 | 6 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Parse pyx text or array of pyx text. | ||||||||||
| 50 | sub parse { | ||||||||||
| 51 | 3 | ▶ | my ($self, $pyx, $out) = @_; | ||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 3 | $self->{'_pyx_parser'}->parse($pyx, $out); | |||||||||
| 53 | 3 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | # Parse file with pyx text. | ||||||||||
| 57 | sub parse_file { | ||||||||||
| 58 | 3 | ▶ | my ($self, $file, $out) = @_; | ||||||||
|
Subroutine
parse_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 59 | 3 | $self->{'_pyx_parser'}->parse_file($file, $out); | |||||||||
| 60 | 3 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # Parse from handler. | ||||||||||
| 64 | sub parse_handler { | ||||||||||
| 65 | 5 | ▶ | my ($self, $input_file_handler, $out) = @_; | ||||||||
|
Subroutine
parse_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | 5 | $self->{'_pyx_parser'}->parse_handler($input_file_handler, $out); | |||||||||
| 67 | 5 | return; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # Reset parser. | ||||||||||
| 71 | sub reset { | ||||||||||
| 72 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 8 | $self->{'_pyx_parser'}->{'non_parser_options'}->{'schemas'} = {}; | |||||||||
| 74 | 8 | return; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # Gets statistics structure. | ||||||||||
| 78 | sub stats { | ||||||||||
| 79 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | my $schemas_hr = $self->{'_pyx_parser'}->{'non_parser_options'} | ||||||||||
| 81 | 2 | ->{'schemas'}; | |||||||||
| 82 | 2 | return $schemas_hr; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # Attribute callback. | ||||||||||
| 86 | sub _call_attribute { | ||||||||||
| 87 | 37 | ▶ | my ($pyx_parser_obj, $key, $val) = @_; | ||||||||
|
Subroutine
_call_attribute: called CC=4 SCAR=13.9
| |||||||||||
| 88 | 37 | my $schemas_hr = $pyx_parser_obj->{'non_parser_options'}->{'schemas'}; | |||||||||
| 89 | 37 | ▶ | if (my ($first, $sec) = _parse_schema($key)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | # Get URL for XML schema. | ||||||||||
| 92 | 28 | ▶ | if ($first eq 'xmlns') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 13 | my $schema = $sec; | |||||||||
| 94 | 13 | ▶ | if (! exists $schemas_hr->{$schema}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 10 | _init_struct($schemas_hr, $schema, $val); | |||||||||
| 96 | } else { | ||||||||||
| 97 | 3 | $schemas_hr->{$schema}->[0] = $val; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | # Add attribute to XML schema statistics. | ||||||||||
| 101 | } else { | ||||||||||
| 102 | 15 | my $schema = $first; | |||||||||
| 103 | 15 | _init_struct($schemas_hr, $schema); | |||||||||
| 104 | 15 | $schemas_hr->{$schema}->[1]->{'attr'}++; | |||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | 37 | return; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | # Finalize callback. | ||||||||||
| 111 | sub _call_final { | ||||||||||
| 112 | 11 | ▶ | my $pyx_parser_obj = shift; | ||||||||
|
Subroutine
_call_final: called CC=5 SCAR=16.1
| |||||||||||
| 113 | 11 | my $schemas_hr = $pyx_parser_obj->{'non_parser_options'}->{'schemas'}; | |||||||||
| 114 | 11 | my $out = $pyx_parser_obj->{'output_handler'}; | |||||||||
| 115 | 11 | ▶ | my $max_string = reduce { length($a) > length($b) ? $a : $b } keys %{$schemas_hr}; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 116 | 11 | ▶ | my $max_len = defined $max_string ? length $max_string : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 11 | foreach my $key (sort keys %{$schemas_hr}) { | |||||||||
| 118 | 18 | ▶ | printf {$out} "[ %-${max_len}s ] (E: %04d, A: %04d)%s\n", $key, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | $schemas_hr->{$key}->[1]->{'element'}, | ||||||||||
| 120 | $schemas_hr->{$key}->[1]->{'attr'}, | ||||||||||
| 121 | $schemas_hr->{$key}->[0] ne $EMPTY_STR | ||||||||||
| 122 | 18 | ? $SPACE.$schemas_hr->{$key}->[0] | |||||||||
| 123 | : $EMPTY_STR; | ||||||||||
| 124 | } | ||||||||||
| 125 | 11 | ▶ | if (! keys %{$schemas_hr}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 3 | print {$out} "No XML schemas.\n"; | |||||||||
| 127 | } | ||||||||||
| 128 | 11 | return; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | # Start of element callback. | ||||||||||
| 132 | sub _call_start_element { | ||||||||||
| 133 | 16 | ▶ | my ($pyx_parser_obj, $elem) = @_; | ||||||||
|
Subroutine
_call_start_element: called CC=2 SCAR=6.9
| |||||||||||
| 134 | 16 | my $schemas_hr = $pyx_parser_obj->{'non_parser_options'}->{'schemas'}; | |||||||||
| 135 | 16 | ▶ | if (defined(my $schema = _parse_schema($elem))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 8 | _init_struct($schemas_hr, $schema); | |||||||||
| 137 | 8 | $schemas_hr->{$schema}->[1]->{'element'}++; | |||||||||
| 138 | } | ||||||||||
| 139 | 16 | return; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | # Initialize XML schema structure. | ||||||||||
| 143 | sub _init_struct { | ||||||||||
| 144 | 33 | ▶ | my ($schemas_hr, $schema, $uri) = @_; | ||||||||
|
Subroutine
_init_struct: called CC=3 SCAR=11.0
| |||||||||||
| 145 | 33 | ▶ | if (! defined $uri) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 23 | $uri = $EMPTY_STR; | |||||||||
| 147 | } | ||||||||||
| 148 | 33 | ▶ | if (! exists $schemas_hr->{$schema}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 18 | $schemas_hr->{$schema} = [$uri, { | |||||||||
| 150 | 'attr' => 0, | ||||||||||
| 151 | 'element' => 0, | ||||||||||
| 152 | }]; | ||||||||||
| 153 | } | ||||||||||
| 154 | 33 | return; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | # Parse XML schema from string. | ||||||||||
| 158 | sub _parse_schema { | ||||||||||
| 159 | 53 | ▶ | my $string = shift; | ||||||||
|
Subroutine
_parse_schema: called CC=3 SCAR=11.0
| |||||||||||
| 160 | 53 | ▶ | if ($string =~ m/^(.+?):(.+)$/ms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 36 | ▶ | return wantarray ? ($1, $2) : $1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | } | ||||||||||
| 163 | 17 | return; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 8 | 1; | |||||||||
| 167 | |||||||||||
| 168 | __END__ | ||||||||||