| 1 | package Bio::Phylo::NeXML::XML2JSON; | ||
| 2 | use XML::XML2JSON; | ||
| 3 | use base 'XML::XML2JSON'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Bio::Phylo::NeXML::XML2JSON - Helps convert NeXML to JSON, no serviceable parts inside | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | # As of 2017, the most recent version of XML::XML2JSON seems to have been | ||
| 14 | # abandoned with a bug in it. It assumes that hash keys are ordered, whereas | ||
| 15 | # more recent Perl versions randomize these. The upshot is that it starts | ||
| 16 | # peeling of hash keys from a data structure where it expects that the first | ||
| 17 | # key name will be a good name for an XML element (basically, a CURIE), and | ||
| 18 | # all subsequent keys will be attribute names (with a prefix, by default the | ||
| 19 | # '@' symbol). Because the hash keys are now randomized, this logic no longer | ||
| 20 | # works: sometimes it will try to instantiate an attribute as an element | ||
| 21 | # name, which will fail. To overcome this problem we are applying a patch here. | ||
| 22 | sub _obj2dom_patch { | ||
| 23 | my ( $Self, $Obj ) = @_; | ||
| 24 | my $Version = $Obj->{ $Self->{attribute_prefix} . 'version' } || $Obj->{'version'} || '1.0'; | ||
| 25 | my $Encoding = $Obj->{ $Self->{attribute_prefix} . 'encoding' } || $Obj->{'encoding'} || 'UTF-8'; | ||
| 26 | my $Dom = $XML::XML2JSON::XMLPARSER->createDocument( $Version, $Encoding ); | ||
| 27 | my $GotRoot = 0; | ||
| 28 | |||
| 29 | # patch: first filter out the element names, then the attributes. | ||
| 30 | my @attr = grep { /^[@]/ } keys %$Obj; | ||
| 31 | my @non_attr = grep { /^[^@]/ } keys %$Obj; | ||
| 32 | for my $Key ( @non_attr, @attr ) { | ||
| 33 | $Obj->{$Key} = "" unless defined($Obj->{$Key}); | ||
| 34 | my $RefType = ref( $Obj->{$Key} ); | ||
| 35 | warn "Value ref type for $Key is: $RefType (value seems to be $Obj->{$Key})" if $Self->{debug}; | ||
| 36 | my $Name = $Key; | ||
| 37 | |||
| 38 | # replace a "$" in the name with a ":" | ||
| 39 | $Name =~ s/([^^])\$/$1\:/; | ||
| 40 | if ( $RefType eq 'HASH' ) { | ||
| 41 | warn "Creating root element: $Name" if $Self->{debug}; | ||
| 42 | $GotRoot = 1; | ||
| 43 | my $Root = $Dom->createElement($Name); | ||
| 44 | $Dom->setDocumentElement($Root); | ||
| 45 | $Self->_process_element_hash( $Dom, $Root, $Obj->{$Key} ); | ||
| 46 | } | ||
| 47 | elsif ( !$RefType ) { | ||
| 48 | if ( $Obj->{$Key} ne '' ) { | ||
| 49 | unless ($GotRoot) { | ||
| 50 | my $Root; | ||
| 51 | eval { $Root = $Dom->createElement($Name) }; | ||
| 52 | if ( $@ ) { | ||
| 53 | die "Problem creating root element $Name: $@"; | ||
| 54 | } | ||
| 55 | $Dom->setDocumentElement($Root); | ||
| 56 | $Root->appendText( $Obj->{$Key} ); | ||
| 57 | $GotRoot = 1; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | warn "unknown reference: $RefType"; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return $Dom; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _process_element_hash_patch { | ||
| 69 | my ( $Self, $Dom, $Element, $Obj ) = @_; | ||
| 70 | |||
| 71 | # patch: first filter out the element names, then the attributes. Sort the | ||
| 72 | # element names in accordance with the NeXML schema. | ||
| 73 | my %order = ( | ||
| 74 | 'meta' => 1, | ||
| 75 | 'otus' => 2, | ||
| 76 | 'trees' => 3, | ||
| 77 | 'characters' => 3, | ||
| 78 | ); | ||
| 79 | my @attr = grep { /^[@]/ } keys %$Obj; | ||
| 80 | my @non_attr = map { $_->[0] } | ||
| 81 | sort { $a->[1] <=> $b->[1] } | ||
| 82 | map { $order{$_} ? [ $_, $order{$_} ] : [ $_, 4 ] } | ||
| 83 | grep { /^[^@]/ } keys %$Obj; | ||
| 84 | |||
| 85 | for my $Key ( @non_attr, @attr ) { | ||
| 86 | my $RefType = ref( $Obj->{$Key} ); | ||
| 87 | my $Name = $Key; | ||
| 88 | |||
| 89 | # replace a "$" in the name with a ":" | ||
| 90 | $Name =~ s/([^^])\$/$1\:/; | ||
| 91 | |||
| 92 | # true/false hacks | ||
| 93 | if ($RefType eq 'JSON::XS::Boolean') { | ||
| 94 | $RefType = ""; | ||
| 95 | $Obj->{$Key} = 1 if ("$Obj->{$Key}" eq 'true'); | ||
| 96 | $Obj->{$Key} = "" if ("$Obj->{$Key}" eq 'false'); | ||
| 97 | } | ||
| 98 | if ($RefType eq 'JSON::true') { | ||
| 99 | $RefType = ""; | ||
| 100 | $Obj->{$Key} = 1; | ||
| 101 | } | ||
| 102 | if ($RefType eq 'JSON::false') { | ||
| 103 | $RefType = ""; | ||
| 104 | $Obj->{$Key} = ""; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ( $RefType eq 'ARRAY' ) { | ||
| 108 | for my $ChildObj ( @{ $Obj->{$Key} } ) { | ||
| 109 | my $Child = $Dom->createElement($Name); | ||
| 110 | $Element->addChild($Child); | ||
| 111 | $Self->_process_element_hash( $Dom, $Child, $ChildObj ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | elsif ( $RefType eq 'HASH' ) { | ||
| 115 | my $Child = $Dom->createElement($Name); | ||
| 116 | $Element->addChild($Child); | ||
| 117 | $Self->_process_element_hash( $Dom, $Child, $Obj->{$Key} ); | ||
| 118 | } | ||
| 119 | elsif ( !$RefType ) { | ||
| 120 | if ( $Key eq $Self->{content_key} ) { | ||
| 121 | my $Value = defined($Obj->{$Key}) ? $Obj->{$Key} : q{}; | ||
| 122 | $Element->appendText( $Value ); | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | |||
| 126 | # remove the attribute prefix | ||
| 127 | my $AttributePrefix = $Self->{attribute_prefix}; | ||
| 128 | if ( $Name =~ /^\Q$AttributePrefix\E(.+)/ ) { | ||
| 129 | $Name = $1; | ||
| 130 | } | ||
| 131 | my $Value = defined($Obj->{$Key}) ? $Obj->{$Key} : q{}; | ||
| 132 | $Element->setAttribute( $Name, $Value ); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | # In addition, we patch the | ||
| 141 | # XML::XML2JSON::_init subroutine because it was emitting a debug message | ||
| 142 | # as a warning, which is ugly. | ||
| 143 | sub _init_patch { | ||
| 144 | my $Self = shift; | ||
| 145 | my %Args = @_; | ||
| 146 | my @Modules = qw(JSON::Syck JSON::XS JSON JSON::DWIW); | ||
| 147 | if ( $Args{module} ) { | ||
| 148 | my $OK = 0; | ||
| 149 | for my $Module ( @Modules ) { | ||
| 150 | $OK = 1 if $Module eq $Args{module}; | ||
| 151 | } | ||
| 152 | @Modules = ( $Args{module} ); | ||
| 153 | } | ||
| 154 | $Self->{_loaded_module} = ""; | ||
| 155 | for my $Module ( @Modules ) { | ||
| 156 | eval "use $Module (); 1;"; | ||
| 157 | unless ($@) { | ||
| 158 | $Self->{_loaded_module} = $Module; | ||
| 159 | last; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | # force arrays (this turns off array folding) | ||
| 164 | $Self->{force_array} = $Args{force_array} ? 1 : 0; | ||
| 165 | |||
| 166 | # use pretty printing when possible | ||
| 167 | $Self->{pretty} = $Args{pretty} ? 1 : 0; | ||
| 168 | |||
| 169 | # debug mode | ||
| 170 | $Self->{debug} = $Args{debug} ? 1 : 0; | ||
| 171 | |||
| 172 | # names | ||
| 173 | $Self->{attribute_prefix} = defined $Args{attribute_prefix} ? $Args{attribute_prefix} : '@'; | ||
| 174 | $Self->{content_key} = defined $Args{content_key} ? $Args{content_key} : '$t'; | ||
| 175 | |||
| 176 | # private_elements | ||
| 177 | $Self->{private_elements} = {}; | ||
| 178 | if ($Args{private_elements}) { | ||
| 179 | for my $private_element ( @{$Args{private_elements}} ) { | ||
| 180 | |||
| 181 | # this must account for the ":" to "$" switch | ||
| 182 | $private_element =~ s/([^^])\:/$1\$/; | ||
| 183 | $Self->{private_elements}->{$private_element} = 1; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # empty_elements | ||
| 188 | $Self->{empty_elements} = {}; | ||
| 189 | if ($Args{empty_elements}) { | ||
| 190 | for my $empty_element ( @{$Args{empty_elements}} ) { | ||
| 191 | |||
| 192 | # this must account for the ":" to "$" switch | ||
| 193 | $empty_element =~ s/([^^])\:/$1\$/; | ||
| 194 | $Self->{empty_elements}->{$empty_element} = 1; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | # private_attributes | ||
| 199 | $Self->{private_attributes} = {}; | ||
| 200 | if ($Args{private_attributes}) { | ||
| 201 | for my $private_attribute ( @{$Args{private_attributes}} ) { | ||
| 202 | |||
| 203 | # this must account for the attribute_prefix | ||
| 204 | $Self->{private_attributes}->{ $Self->{attribute_prefix} . $private_attribute } = 1; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | # Let's assume that the next version of XML::XML2JSON will fix these issues and just test | ||
| 211 | # for the current version, which appears to be the one that everybody has. | ||
| 212 | if ( $XML::XML2JSON::VERSION == 0.06 ) { | ||
| 213 | *obj2dom = \&_obj2dom_patch; | ||
| 214 | *_init = \&_init_patch; | ||
| 215 | *_process_element_hash = \&_process_element_hash_patch; | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; |