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 9     9   48 use strict;
  9         13  
  9         297  
4 9     9   35 use warnings;
  9         11  
  9         355  
5              
6 9     9   37 use vars qw($VERSION);
  9         12  
  9         406  
7             BEGIN {
8 9     9   203 $VERSION='2.29'; # version template
9             }
10 9     9   33 no warnings 'uninitialized';
  9         25  
  9         309  
11 9     9   36 use Carp;
  9         24  
  9         477  
12 9     9   43 use URI;
  9         19  
  9         222  
13 9     9   34 use Treex::PML::Schema::Constants;
  9         14  
  9         809  
14 9     9   3428 use Encode;
  9         66613  
  9         760  
15              
16 9     9   55 use base qw(Treex::PML::Schema::XMLNode);
  9         13  
  9         7608  
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         80 $self=$self->{-parent} while $self->{-parent};
24 16         41 return $self;
25             }
26              
27             sub simplify {
28 16     16 1 51 my ($import,$opts)=@_;
29 16         93 my $target = $import->schema;
30 16   50     88 my $base_url = $target->{URL}||'';
31 16         111 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     53 $parent->get_decl_type == PML_SCHEMA_DECL and $opts->{no_import});
      66        
      33        
35 16 50       48 die "Missing 'schema' attribute on element in $base_url!" unless $import->{schema};
36              
37 16   50     45 $opts->{schemas}||={};
38 16         164 my $url = URI->new(Encode::encode_utf8($import->{schema}));
39              
40             my $schema = ref($target)->new({
41 48         110 (map { ($_=>$opts->{$_}) } qw(schemas use_resources validate)),
42             filename => $url,
43             base_url => $base_url,
44             imported => 1,
45             (map {
46 16 100       991 exists($import->{$_}) ? ( $_ => $import->{$_} ) : ()
  48         194  
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     710 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       12 if (ref $schema->{type}) {
56 3   100     18 $parent->{type}||={};
57 3         7 foreach my $name (keys(%{$schema->{type}})) {
  3         15  
58 16 50       45 unless (exists $parent->{type}{$name}) {
59 16         62 $parent->{type}{$name}=$parent->copy_decl($schema->{type}{$name});
60             }
61             }
62             }
63             } else {
64 13         36 my $name = $import->{type};
65             # print STDERR "IMPORTING $name\n";
66 13 50       34 if (ref($schema->{type})) {
67 13         52 $import->_import_type($parent,$schema,$name);
68             }
69             }
70 16 100 33     147 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       11 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         26 my $name = $import->{template};
84 13 50       35 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     147 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         13 $parent->{root} = $parent->copy_decl($schema->{root});
95             }
96 16         59 return $schema;
97             }
98              
99             sub _import_type {
100 13     13   37 my ($self,$target,$src_schema, $name) = @_;
101 13 50       46 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         25 my $type = $src_schema->{type}{$name};
105 13         39 my %referred = ($name => $type);
106 13         85 $src_schema->_get_referred_types($type,\%referred);
107 13         81 foreach my $n (keys %referred) {
108 22 100       62 unless (exists $target->{type}{$n}) {
109 18         85 $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__