$VERSION
=
"0.14"
;
my
$xp_field
;
my
$parent_field
;
BEGIN
{
$xp_field
= 11;
$parent_field
= 12;
}
sub
findnodes {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->findnodes(
$path
,
$dom
); }
sub
findnodes_as_string {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->findnodes_as_string(
$path
,
$dom
); }
sub
findnodes_as_strings {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->findnodes_as_strings(
$path
,
$dom
); }
sub
findvalue {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->findvalue(
$path
,
$dom
); }
sub
exists
{
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->
exists
(
$path
,
$dom
); }
sub
find {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->find(
$path
,
$dom
); }
sub
matches {
my
(
$dom
,
$path
)=
@_
;
return
$dom
->xp->matches(
$dom
,
$path
,
$dom
); }
sub
set_namespace {
my
$dom
=
shift
;
$dom
->xp->set_namespace(
@_
); }
sub
cmp {
return
$_
[1]->isa(
'XML::DOM::Document'
) ? 0 : 1; }
sub
getRootNode {
return
$_
[0]; }
sub
xp {
return
$_
[0]->[
$xp_field
] }
{
no
warnings;
sub
new
{
my
(
$class
) =
@_
;
my
$self
=
bless
[],
$class
;
$self
->[_Doc] =
$self
;
$self
->[_C] = new XML::DOM::NodeList;
$self
->[
$xp_field
]= XML::XPathEngine->new();
$self
;
}
}
sub
findnodes {
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->findnodes(
$path
,
$node
); }
sub
findnodes_as_string {
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->findnodes_as_string(
$path
,
$node
); }
sub
findvalue {
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->findvalue(
$path
,
$node
); }
sub
exists
{
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->
exists
(
$path
,
$node
); }
sub
find {
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->find(
$path
,
$node
); }
sub
matches {
my
(
$node
,
$path
)=
@_
;
return
$node
->xp->matches(
$node
->getOwnerDocument,
$path
,
$node
); }
sub
isCommentNode { 0 };
sub
isPINode { 0 };
sub
to_number {
return
XML::XPathEngine::Number->new(
shift
->string_value); }
sub
getParent {
return
$_
[0]->getParentNode; }
sub
getRootNode {
return
$_
[0]->getOwnerDocument; }
sub
xp {
return
$_
[0]->getOwnerDocument->xp; }
{
no
warnings;
sub
getAttributes
{
if
(
caller
(0)!~ m{^XML::XPathEngine}) {
return
undef
; }
else
{
my
@atts
= ();
return
wantarray
?
@atts
: \
@atts
; }
}
}
sub
cmp
{
my
(
$a
,
$b
)=
@_
;
return
0
if
(
$a
==
$b
);
return
-1
if
(
$a
->isAncestor(
$b
));
return
1
if
(
$b
->isAncestor(
$a
));
if
(
$a
->isa(
'XML::DOM::Attr'
) &&
$b
->isa(
'XML::DOM::Attr'
))
{
if
(
$a
->getParent ==
$b
->getParent)
{
return
$a
->getName cmp
$b
->getName }
else
{
return
$a
->getParent->cmp(
$b
->getParent); }
}
my
@a_pile
= (
$a
->ancestors_or_self);
my
@b_pile
= (
$b
->ancestors_or_self);
return
undef
unless
(
$a_pile
[-1] ==
$b_pile
[-1]);
my
$a_anc
=
pop
@a_pile
;
my
$b_anc
=
pop
@b_pile
;
while
(
$a_anc
==
$b_anc
)
{
$a_anc
=
pop
@a_pile
;
$b_anc
=
pop
@b_pile
;
}
my
(
$a_prev
,
$a_next
,
$b_prev
,
$b_next
)= (
$a_anc
,
$a_anc
,
$b_anc
,
$b_anc
);
while
()
{
$a_prev
=
$a_prev
->getPreviousSibling ||
return
( -1);
return
1
if
(
$a_prev
==
$b_next
);
$a_next
=
$a_next
->getNextSibling ||
return
( 1);
return
-1
if
(
$a_next
==
$b_prev
);
$b_prev
=
$b_prev
->getPreviousSibling ||
return
( 1);
return
-1
if
(
$b_prev
==
$a_next
);
$b_next
=
$b_next
->getNextSibling ||
return
( -1);
return
1
if
(
$b_next
==
$a_prev
);
}
}
sub
ancestors_or_self
{
my
$node
=
shift
;
my
@ancestors
= (
$node
);
while
(
$node
=
$node
->getParent)
{
push
@ancestors
,
$node
; }
return
@ancestors
;
}
sub
getNamespace
{
my
$node
=
shift
;
my
$prefix
=
shift
() ||
$node
->ns_prefix;
if
(
my
$expanded
=
$node
->get_namespace(
$prefix
))
{
return
XML::DOM::Namespace->new(
$prefix
,
$expanded
); }
else
{
return
XML::DOM::Namespace->new(
$prefix
,
''
); }
}
sub
getLocalName
{
my
$node
=
shift
;
(
my
$local
=
$node
->getName)=~ s{^[^:]*:}{};
return
$local
;
}
sub
ns_prefix
{
my
$node
=
shift
;
if
(
$node
->getName=~ m{^([^:]*):})
{
return
$1; }
else
{
return
(
'#default'
); } # should it be
''
?
}
BEGIN
);
sub
get_namespace
{
my
$node
=
shift
;
my
$prefix
=
defined
$_
[0] ?
shift
() :
$node
->ns_prefix;
if
(
$prefix
eq
"#default"
) {
$prefix
=
''
}
my
$ns_att
=
$prefix
?
"xmlns:$prefix"
:
"xmlns"
;
my
$expanded
=
$DEFAULT_NS
{
$prefix
} ||
$node
->inherit_att(
$ns_att
) ||
''
;
return
$expanded
;
}
}
sub
inherit_att
{
my
$node
=
shift
;
my
$att
=
shift
;
do
{
if
( (
$node
->getNodeType == ELEMENT_NODE) && (
$node
->getAttribute(
$att
)))
{
return
$node
->getAttribute(
$att
); }
}
while
(
$node
=
$node
->getParentNode);
return
undef
;
}
sub
getName {
return
$_
[0]->getTagName; }
{
no
warnings;
sub
getAttributes
{
$_
[0]->[_A] ||= XML::DOM::NamedNodeMap->new (
Doc
=>
$_
[0]->[_Doc],
Parent
=>
$_
[0]);
if
(
caller
(0)!~ m{^XML::XPathEngine})
{
return
$_
[0]->[_A];
}
else
{
my
$elt
=
shift
;
my
@atts
=
grep
{
ref
$_
eq
'XML::DOM::Attr'
}
values
%{
$elt
->[1]};
$_
->[
$parent_field
]=
$elt
foreach
(
@atts
);
return
wantarray
?
@atts
: \
@atts
;
}
}
}
sub
string_value
{
my
$self
=
shift
;
my
$string
=
''
;
foreach
my
$kid
(
$self
->getChildNodes)
{
if
(
$kid
->getNodeType == ELEMENT_NODE ||
$kid
->getNodeType == TEXT_NODE)
{
$string
.=
$kid
->string_value; }
}
return
$string
;
}
sub
inherit_att {
return
$_
[0]->getParent->inherit_att(
@_
); }
sub
getParent {
return
$_
[0]->[
$parent_field
]; }
sub
string_value {
return
$_
[0]->getValue; }
sub
getData {
return
$_
[0]->getValue; }
sub
string_value {
return
$_
[0]->getData; }
sub
isCommentNode { 1 };
sub
string_value {
return
$_
[0]->getData; }
sub
isPINode { 1 };
sub
isProcessingInstructionNode { 1 };
sub
string_value {
return
$_
[0]->getData; }
sub
value {
return
$_
[0]->getData; }
sub
new
{
my
(
$class
,
$prefix
,
$expanded
)=
@_
;
bless
{
prefix
=>
$prefix
,
expanded
=>
$expanded
},
$class
;
}
sub
isNamespaceNode { 1; }
sub
getPrefix {
$_
[0]->{prefix}; }
sub
getExpanded {
$_
[0]->{expanded}; }
sub
getValue {
$_
[0]->{expanded}; }
sub
getData {
$_
[0]->{expanded}; }
1;