| 1 |
## ------------------------------------------------------------------------- |
| 2 |
## $Id: Encode.pm,v 1.5 2003/04/09 07:22:34 joko Exp $ |
| 3 |
## ------------------------------------------------------------------------- |
| 4 |
## $Log: Encode.pm,v $ |
| 5 |
## Revision 1.5 2003/04/09 07:22:34 joko |
| 6 |
## childObj2string now inside Encode.pm, renamed to 'twingle_reference' |
| 7 |
## |
| 8 |
## Revision 1.4 2003/03/27 15:17:07 joko |
| 9 |
## namespace fixes for Data::Mungle::* |
| 10 |
## |
| 11 |
## Revision 1.3 2003/02/20 20:48:36 joko |
| 12 |
## renamed methods |
| 13 |
## |
| 14 |
## Revision 1.2 2002/12/05 13:57:22 joko |
| 15 |
## + now able to export 'scalar2utf8' |
| 16 |
## + comments |
| 17 |
## |
| 18 |
## Revision 1.1 2002/11/29 04:49:20 joko |
| 19 |
## + initial check-in |
| 20 |
## |
| 21 |
## Revision 1.1 2002/10/10 03:26:00 cvsjoko |
| 22 |
## + new |
| 23 |
## ------------------------------------------------------------------------- |
| 24 |
|
| 25 |
|
| 26 |
package Data::Mungle::Transform::Encode; |
| 27 |
|
| 28 |
use strict; |
| 29 |
use warnings; |
| 30 |
|
| 31 |
require Exporter; |
| 32 |
our @ISA = qw( Exporter ); |
| 33 |
our @EXPORT_OK = qw( |
| 34 |
&latin_to_utf8 |
| 35 |
&latin_to_utf8_scalar |
| 36 |
&utf8_to_latin |
| 37 |
&utf8_to_latin_scalar |
| 38 |
&twingle_reference |
| 39 |
&decode_hex_nybbles |
| 40 |
); |
| 41 |
|
| 42 |
|
| 43 |
use Unicode::String qw(utf8 latin1); |
| 44 |
|
| 45 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
| 46 |
sub latin_to_utf8 { |
| 47 |
my $vref = shift; |
| 48 |
if (ref $vref eq 'HASH') { |
| 49 |
foreach (keys %{$vref}) { |
| 50 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
| 51 |
latin_to_utf8($vref->{$_}); |
| 52 |
} else { |
| 53 |
$vref->{$_} = latin_to_utf8_scalar($vref->{$_}); |
| 54 |
} |
| 55 |
} |
| 56 |
} elsif (ref $vref eq 'ARRAY') { |
| 57 |
foreach (@{$vref}) { |
| 58 |
if (ref $_ ne 'SCALAR') { |
| 59 |
latin_to_utf8($_); |
| 60 |
} else { |
| 61 |
$_ = latin_to_utf8_scalar($_); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
sub latin_to_utf8_scalar { |
| 69 |
my $scalar = shift; |
| 70 |
if ($scalar) { |
| 71 |
my $u = latin1($scalar); |
| 72 |
return $u->utf8; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
# TODO: refactor using Greg London's "Iterate" from CPAN |
| 77 |
sub utf8_to_latin { |
| 78 |
my $vref = shift; |
| 79 |
if (ref $vref eq 'HASH') { |
| 80 |
foreach (keys %{$vref}) { |
| 81 |
if ((ref $vref->{$_}) =~ m/ARRAY|HASH/) { |
| 82 |
utf8_to_latin($vref->{$_}); |
| 83 |
} else { |
| 84 |
$vref->{$_} = utf8_to_latin_scalar($vref->{$_}); |
| 85 |
} |
| 86 |
} |
| 87 |
} elsif (ref $vref eq 'ARRAY') { |
| 88 |
foreach (@{$vref}) { |
| 89 |
if (ref $_ ne 'SCALAR') { |
| 90 |
utf8_to_latin($_); |
| 91 |
} else { |
| 92 |
$_ = utf8_to_latin_scalar($_); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
sub utf8_to_latin_scalar { |
| 100 |
my $scalar = shift; |
| 101 |
if ($scalar) { |
| 102 |
my $u = utf8($scalar); |
| 103 |
return $u->latin1; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
# encodes object-references to serialized string representations |
| 109 |
# made up of: |
| 110 |
# - 'o_<classname>_<ref type>_<guid>'??? |
| 111 |
# - 'o_{guid}_{classname}'!!! |
| 112 |
|
| 113 |
# TODO: enhance further! |
| 114 |
# make it possible to twingle OID-, GUID- and/or other references |
| 115 |
|
| 116 |
sub twingle_reference { |
| 117 |
my $obj = shift; |
| 118 |
my $option = shift; |
| 119 |
my $string = 'n/a'; |
| 120 |
|
| 121 |
#if ($option == 1) { |
| 122 |
if ((my $classname = ref $obj) && (my $guid = $obj->{guid})) { |
| 123 |
$string = join('_', 'o', $guid, $classname); |
| 124 |
} |
| 125 |
#} |
| 126 |
|
| 127 |
return $string; |
| 128 |
} |
| 129 |
|
| 130 |
sub decode_hex_nybbles { |
| 131 |
my $data = shift; |
| 132 |
my @buf; |
| 133 |
for (my $i = 0; $i <= length($data); $i = $i + 2) { |
| 134 |
my $nybble = substr($data, $i, 2); |
| 135 |
push @buf, chr(hex($nybble)); |
| 136 |
} |
| 137 |
return join('', @buf); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
1; |
| 142 |
__END__ |