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