my
$XMLParser
= LaTeXML::Common::XML::Parser->new();
sub
new {
my
(
$class
,
$key
,
%data
) =
@_
;
return
bless
{
key
=>
$key
,
%data
},
$class
; }
sub
key {
my
(
$entry
) =
@_
;
return
$$entry
{key}; }
sub
getAttributes {
my
(
$self
) =
@_
;
return
keys
%$self
; }
sub
hasValue {
my
(
$self
,
$attr
) =
@_
;
return
exists
$$self
{
$attr
}; }
sub
getValue {
my
(
$self
,
$attr
) =
@_
;
return
decodeValue(
$$self
{
$attr
}); }
sub
setValues {
my
(
$self
,
%avpairs
) =
@_
;
foreach
my
$attr
(
keys
%avpairs
) {
my
$value
= encodeValue(
$avpairs
{
$attr
});
if
(!
defined
$value
) {
if
(
defined
$$self
{
$attr
}) {
delete
$$self
{
$attr
}; } }
elsif
((!
defined
$$self
{
$attr
}) || (
$$self
{
$attr
} ne
$value
)) {
$$self
{
$attr
} =
$value
; } }
return
; }
sub
pushValues {
my
(
$self
,
$attr
,
@values
) =
@_
;
my
$list
=
$$self
{
$attr
};
foreach
my
$value
(
@values
) {
push
(
@$list
, encodeValue(
$value
))
if
defined
$value
; }
return
; }
sub
pushNew {
my
(
$self
,
$attr
,
@values
) =
@_
;
my
$list
=
$$self
{
$attr
};
foreach
my
$value
(
@values
) {
my
$value
= encodeValue(
$value
);
push
(
@$list
,
$value
)
if
(
defined
$value
) && !
grep
{
$_
eq
$value
}
@$list
; }
return
; }
sub
noteAssociation {
my
(
$self
,
@keys
) =
@_
;
my
$hash
=
$self
;
while
(
@keys
) {
my
$key
=
shift
(
@keys
);
if
(
defined
$$hash
{
$key
}) {
$hash
=
$$hash
{
$key
}; }
else
{
$hash
=
$$hash
{
$key
} = (
@keys
? {} : 1); } }
return
; }
sub
show {
my
(
$self
) =
@_
;
my
$string
=
"ObjectDB Entry for: $$self{key}\n"
;
foreach
my
$attr
(
grep
{
$_
ne
'key'
}
keys
%{
$self
}) {
my
$label
=
sprintf
(
' %16s : '
,
$attr
);
my
$value
= showvalue(
$self
->getValue(
$attr
));
my
$line
;
eval
{
local
$LaTeXML::IGNORE_ERRORS
= 1;
$line
= wrap(
$label
, (
' '
x 20),
$value
); };
$string
.= (
defined
$line
?
$line
:
$label
.
$value
) .
"\n"
; }
return
$string
; }
sub
showvalue {
my
(
$value
) =
@_
;
if
((
ref
$value
) =~ /^XML::/) {
return
$value
->toString(1); }
elsif
(
ref
$value
eq
'HASH'
) {
return
"{"
.
join
(
', '
,
map
{
"$_=>"
. showvalue(
$$value
{
$_
}) }
keys
%$value
) .
"}"
; }
elsif
(
ref
$value
eq
'ARRAY'
) {
return
"["
.
join
(
', '
,
map
{ showvalue(
$_
) }
@$value
) .
"]"
; }
else
{
return
"$value"
; } }
sub
encodeValue {
my
(
$value
) =
@_
;
my
$ref
=
ref
$value
;
if
(!
defined
$value
) {
return
$value
; }
elsif
(!
$ref
) {
return
$value
; }
elsif
(
$ref
=~ /^XML::/) {
return
"XML::"
.
$value
->cloneNode(1)->toString; }
elsif
(
$ref
eq
'ARRAY'
) {
return
[
map
{ encodeValue(
$_
) }
@$value
]; }
elsif
(
$ref
eq
'HASH'
) {
my
%h
=
map
{ (
$_
=> encodeValue(
$$value
{
$_
})) }
keys
%$value
;
return
\
%h
; }
else
{
return
$value
; } }
sub
decodeValue {
my
(
$value
) =
@_
;
my
$ref
=
ref
$value
;
if
(!
defined
$value
) {
return
$value
; }
elsif
(
$value
=~ /^XML::/) {
return
$XMLParser
->parseChunk(
substr
(
$value
, 5)); }
elsif
(!
$ref
) {
return
$value
; }
elsif
(
$ref
eq
'ARRAY'
) {
return
[
map
{ decodeValue(
$_
) }
@$value
]; }
elsif
(
$ref
eq
'HASH'
) {
my
%h
=
map
{ (
$_
=> decodeValue(
$$value
{
$_
})) }
keys
%$value
;
return
\
%h
; }
else
{
return
$value
; } }
1;