From 1d03a65e7a7c19acbf6644f60a7473934702a817 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 12 Apr 2012 11:07:10 -0400 Subject: [PATCH] Fieldmapper toXML repairs and additions 1. Be sure the append the current element to the document in progress 2. Use the class hint instead of the fully qualified class_name (e.g. Fielmapper::actor::user) to avoid XML errors: namespace error : Failed to parse QName 'Fieldmapper:' 3. Added support for additional options including "no_virt" which tells the routine to skip all virtual fields and "skip_fields" to support leaving specific fields out of the output. The main use case for skip fields is au => ['passwd'], but other examples might include large fields like bre => ['marc']. Signed-off-by: Bill Erickson Signed-off-by: Lebbeous Fogle-Weekley --- Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm index c7e7a25299..33050fd622 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm @@ -337,14 +337,23 @@ sub toXML { my $self = shift; return undef unless (ref $self); + my $opts = shift || {}; + my $no_virt = $$opts{no_virt}; # skip virtual fields + my $skip_fields = $$opts{skip_fields} || {}; # eg. {au => ['passwd']} + my @to_skip = @{$$skip_fields{$self->json_hint}} + if $$skip_fields{$self->json_hint}; + my $dom = XML::LibXML::Document->new; - my $root = $dom->createElement( $self->class_name ); + my $root = $dom->createElement( $self->json_hint ); $dom->setDocumentElement( $root ); - for my $f ($self->properties) { + my @field_names = $no_virt ? $self->real_fields : $self->properties; + + for my $f (@field_names) { next if ($f eq 'isnew'); next if ($f eq 'ischanged'); next if ($f eq 'isdeleted'); + next if (grep {$_ eq $f} @to_skip); my $value = $self->$f(); my $element = $dom->createElement( $f ); @@ -354,7 +363,7 @@ sub toXML { if (ref($value)) { # array for my $k (@$value) { if (blessed($k)) { - my $subdoc = $k->toXML; + my $subdoc = $k->toXML($opts); next unless $subdoc; my $subnode = $subdoc->documentElement; $dom->adoptNode($subnode); @@ -368,6 +377,8 @@ sub toXML { } else { $element->appendText($value); } + + $root->appendChild($element); } return $dom; -- 2.11.0