Rewrite/Schema.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 207 CC 14 SCAR 26.0
CC
14
Cov
0%
expandIncludes
30.0
repairSchemaHeader
20.0
transformForm
20.0
CRAP 13.5
1 # Copyrights 2008-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Rewrite. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Rewrite::Schema;
10 use vars '$VERSION';
11 $VERSION = '0.11';
12  
13 use base 'XML::Rewrite';
14  
15 use warnings;
16 use strict;
17  
18 use Log::Report 'xml-rewrite';
19  
20 use XML::Compile::Util qw/pack_type type_of_node :constants/;
21 use XML::LibXML ();
22 use File::Spec::Functions qw/catfile/;
23 use File::Basename qw/dirname/;
24  
25  
26 sub init($)
27 { my ($self, $args) = @_;
28 $self->SUPER::init($args);
29  
30 $self->{XRS_elemform} = $args->{element_form};
31 $self->{XRS_attrform} = $args->{attribute_form};
32 $self->{XRS_target} = $args->{target_namespace};
33 $self->{XRS_include} = $args->{expand_includes};
34  
35 $self->importDefinitions( [SCHEMA2001, XMLNS] );
36  
37 # Something must be produced for an annotation: it's required
38 $self->addHook( id => 'annotation'
39 , replace => sub { ($_[3] => XML::LibXML::Text->new('')) } )
40 if $args->{remove_annotations};
41  
42 $self->addHook( id => [ qw/key keyref unique/ ]
43 , replace => sub { ($_[3] => XML::LibXML::Text->new('')) } )
44 if $args->{remove_identity_constraints};
45  
46 $self;
47 }
48  
49  
50 sub repairXML($$$)
51 { my ($self, $type, $schema, $details) = @_;
52  
53 $self->SUPER::repairXML($type, $schema, $details);
54 $self->repairSchemaHeader($schema);
55 $self;
56 }
57  
58 sub repairSchemaHeader($)
59 { my ($self, $schema) = @_;
60 # we cannot interfere with the parser directly, so will need
61 # modify the XML tree.
62 if(my $elemform = $self->{XRS_elemform})
63 { $schema->removeAttribute('elementFormDefault');
64 $schema->setAttribute(elementFormDefault => $elemform);
65 }
66  
67 if(my $attrform = $self->{XRS_attrform})
68 { $schema->removeAttribute('attributeFormDefault');
69 $schema->setAttribute(elementFormDefault => $attrform);
70 }
71  
72 if(my $target = $self->{XRS_target})
73 { $schema->removeAttribute('targetNamespace');
74 $schema->setAttribute(targetNamespace => $target);
75 }
76 }
77  
78 sub transformData($$$)
79 { my ($self, $type, $data, $details) = @_;
80 $self->SUPER::transformData($type, $data, $details);
81 $self->transformForm($data);
82 $self->expandIncludes($data, $details) if $self->{XRS_include};
83 $self;
84 }
85  
86 sub transformForm($)
87 { my ($self, $data) = @_;
88  
89 if(my $elemform = $self->{XRS_elemform})
90 { $elemform eq 'unqualified' || $elemform eq 'qualified'
91 or error __x"element for must be qualied or unqualified, not {form}"
92 , form => $elemform;
93 $data->{elementFormDefault} = $elemform;
94 }
95  
96 if(my $attrform = $self->{XRS_attrform})
97 { $attrform eq 'unqualified' || $attrform eq 'qualified'
98 or error __x"attribute for must be qualied or unqualified, not {form}"
99 , form => $attrform;
100 $data->{attributeFormDefault} = $attrform;
101 }
102  
103 if(my $target = $self->{XRS_target})
104 { $data->{targetNamespace} = $target;
105 }
106  
107 $self;
108 }
109  
110 sub expandIncludes($$$)
111 { my ($self, $data, $details) = @_;
112 my $prefixes = $self->prefixes;
113 my $xsd = $prefixes->{&SCHEMA2001}{prefix};
114 $xsd .= '_' if length $xsd;
115  
116 my %included;
117 my $cho_in = $data->{"cho_${xsd}include"} or return;
118 my @cho_in = @$cho_in;
119 my @cho_out;
120 my @toplevel = @{$data->{"seq_${xsd}schemaTop"} || []};
121 foreach my $incl (@cho_in)
122 { my ($kind, $def) = %$incl;
123 if($kind ne "${xsd}include")
124 { push @cho_out, $incl;
125 next;
126 }
127  
128 my $basefn = $details->{filename};
129 defined $basefn
130 or error __x"includes need base filename";
131  
132 my $relfn = $def->{schemaLocation} or next;
133 my $inclfn = catfile dirname($basefn), $relfn;
134  
135 if($included{$inclfn}++)
136 { trace "include file $inclfn already seen";
137 next;
138 }
139  
140 my $doc = eval { XML::LibXML->new->parse_file($inclfn) };
141 if($@)
142 { notice __x"include file {fn} cannot be used", fn => $inclfn;
143 push @cho_out, $incl;
144 next;
145 }
146 trace "including {$inclfn}";
147  
148 my $xml = $doc->documentElement;
149 my $type = type_of_node $xml;
150 $type eq pack_type(SCHEMA2001, 'schema')
151 or error __x"file {fn} does not contain a schema", fn => $type;
152  
153 # gladly, the reader will use the same prefix table!
154 my $include = $self->reader($type)->($xml);
155  
156 # include location should be rewritten, but usually ok
157 unshift @cho_in, @{$include->{"cho_${xsd}include"} || []};
158 unshift @toplevel, @{$include->{"seq_${xsd}schemaTop"} || []};
159 }
160 $data->{"cho_${xsd}include"} = \@cho_out;
161 $data->{"seq_${xsd}schemaTop"} = \@toplevel;
162 }
163  
164 1;