File Coverage

blib/lib/Treex/PML/Schema/Import.pm
Criterion Covered Total %
statement 67 77 87.0
branch 17 30 56.6
condition 23 44 52.2
subroutine 13 15 86.6
pod 3 4 75.0
total 123 170 72.3


line stmt bran cond sub pod time code
1             package Treex::PML::Schema::Import;
2              
3 8     8   54 use strict;
  8         15  
  8         293  
4 8     8   34 use warnings;
  8         18  
  8         438  
5              
6 8     8   66 use vars qw($VERSION);
  8         19  
  8         426  
7             BEGIN {
8 8     8   162 $VERSION='2.28'; # version template
9             }
10 8     8   94 no warnings 'uninitialized';
  8         30  
  8         408  
11 8     8   49 use Carp;
  8         15  
  8         564  
12 8     8   68 use URI;
  8         14  
  8         264  
13 8     8   37 use Treex::PML::Schema::Constants;
  8         16  
  8         1093  
14 8     8   3489 use Encode;
  8         74132  
  8         948  
15              
16 8     8   71 use base qw(Treex::PML::Schema::XMLNode);
  8         18  
  8         9570  
17              
18 0     0 1 0 sub get_decl_type { return(PML_IMPORT_DECL); }
19 0     0 1 0 sub get_decl_type_str { return('import'); }
20              
21             sub schema {
22 16     16 0 43 my ($self)=@_;
23 16         85 $self=$self->{-parent} while $self->{-parent};
24 16         45 return $self;
25             }
26              
27             sub simplify {
28 16     16 1 50 my ($import,$opts)=@_;
29 16         69 my $target = $import->schema;
30 16   50     87 my $base_url = $target->{URL}||'';
31 16         137 my $parent = $import->{-parent}; # FIXME: for templates
32             return if
33             ($parent->get_decl_type == PML_TEMPLATE_DECL and $opts->{no_template_import} or
34 16 50 66     66 $parent->get_decl_type == PML_SCHEMA_DECL and $opts->{no_import});
      66        
      33        
35 16 50       54 die "Missing 'schema' attribute on element in $base_url!" unless $import->{schema};
36              
37 16   50     51 $opts->{schemas}||={};
38 16         202 my $url = URI->new(Encode::encode_utf8($import->{schema}));
39              
40             my $schema = ref($target)->new({
41 48         134 (map { ($_=>$opts->{$_}) } qw(schemas use_resources validate)),
42             filename => $url,
43             base_url => $base_url,
44             imported => 1,
45             (map {
46 16 100       1201 exists($import->{$_}) ? ( $_ => $import->{$_} ) : ()
  48         218  
47             } qw(revision minimal_revision maximal_revision)),
48             revision_error => "Error importing schema %f to $base_url - revision mismatch: %e"
49             });
50 16 100 33     792 if ((!exists($import->{type}) and
      33        
      66        
51             !exists($import->{template}) and
52             !exists($import->{root})
53             ) or defined($import->{type}) and $import->{type} eq '*') {
54             # print STDERR "IMPORTING *\n";
55 3 50       13 if (ref $schema->{type}) {
56 3   100     15 $parent->{type}||={};
57 3         7 foreach my $name (keys(%{$schema->{type}})) {
  3         19  
58 16 50       77 unless (exists $parent->{type}{$name}) {
59 16         73 $parent->{type}{$name}=$parent->copy_decl($schema->{type}{$name});
60             }
61             }
62             }
63             } else {
64 13         34 my $name = $import->{type};
65             # print STDERR "IMPORTING $name\n";
66 13 50       48 if (ref($schema->{type})) {
67 13         53 $import->_import_type($parent,$schema,$name);
68             }
69             }
70 16 100 33     236 if ((!exists($import->{type}) and
      33        
      66        
71             !exists($import->{template}) and
72             !exists($import->{root})
73             ) or defined($import->{template}) and $import->{template} eq '*') {
74 3 50       28 if (ref $schema->{template}) {
75 0   0     0 $parent->{template}||={};
76 0         0 foreach my $name (keys(%{$schema->{template}})) {
  0         0  
77 0 0       0 unless (exists $parent->{template}{$name}) {
78 0         0 $parent->{template}{$name}=$parent->copy_decl($schema->{template}{$name});
79             }
80             }
81             }
82             } else {
83 13         34 my $name = $import->{template};
84 13 50       48 if (ref($schema->{template})) {
85 0 0       0 unless (exists $parent->{template}{$name}) {
86 0         0 $parent->{template}{$name}=$parent->copy_decl($schema->{template}{$name});
87             }
88             }
89             }
90 16 50 100     164 if (((!exists($import->{type}) and
      66        
      33        
91             !exists($import->{template}) and
92             !exists($import->{root})
93             ) or defined($import->{root}) and $import->{root} eq '1') and !exists($parent->{root}) and $schema->{root}) {
94 3         27 $parent->{root} = $parent->copy_decl($schema->{root});
95             }
96 16         75 return $schema;
97             }
98              
99             sub _import_type {
100 13     13   46 my ($self,$target,$src_schema, $name) = @_;
101 13 50       57 unless (exists $src_schema->{type}{$name}) {
102 0         0 croak "Cannot import type '$name' from '$src_schema->{URL}' to '$target->{URL}': type not declared in the source schema\n";
103             }
104 13         33 my $type = $src_schema->{type}{$name};
105 13         49 my %referred = ($name => $type);
106 13         66 $src_schema->_get_referred_types($type,\%referred);
107 13         103 foreach my $n (keys %referred) {
108 22 100       91 unless (exists $target->{type}{$n}) {
109 18         90 $target->{type}{$n}=$target->copy_decl($referred{$n});
110             } else {
111             # print STDERR "already there\n";
112             }
113             }
114             }
115              
116              
117             1;
118             __END__