deprecated/buildtools/buildsystemtools/lib/XML/XQL/DOM.pm
author lorewang
Wed, 01 Dec 2010 16:05:36 +0800
changeset 715 e0739b8406dd
parent 655 3f65fd25dfd4
permissions -rw-r--r--
Specify extenal tool with path
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# Copyright (c) 1998 Enno Derksen
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
# All rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# This program is free software; you can redistribute it and/or modify it
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# under the same terms as Perl itself.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
############################################################################
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
# Functions added to the XML::DOM implementation for XQL support
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
# NOTE: This code is a bad example of how to use XML::DOM.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
# I'm accessing internal (private) data members for a little gain in performance.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
# When the internal DOM implementation changes, this code will no longer work.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
# But since I maintain XML::DOM, it's easy for me to keep them in sync.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
# Regular users are adviced to use the XML::DOM API as described in the 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
# documentation.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
package XML::XQL::DOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
BEGIN
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
    require XML::DOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
    # import constant field definitions, e.g. _Doc
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
    import XML::DOM::Node qw{ :Fields };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
package XML::DOM::Node;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
sub xql
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
    # Odd number of args, assume first is XQL expression without 'Expr' key
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
    unshift @_, 'Expr' if (@_ % 2 == 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
    my $query = new XML::XQL::Query (@_);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
    my @result = $query->solve ($self);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
    $query->dispose;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
    @result;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
sub xql_sortKey
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
    my $key = $_[0]->[_SortKey];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
    return $key if defined $key;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
    $key = XML::XQL::createSortKey ($_[0]->[_Parent]->xql_sortKey, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
				    $_[0]->xql_childIndex, 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
#print "xql_sortKey $_[0] ind=" . $_[0]->xql_childIndex . " key=$key str=" . XML::XQL::keyStr($key) . "\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
    $_[0]->[_SortKey] = $key;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
# Find previous sibling that is not a text node with ignorable whitespace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
sub xql_prevNonWS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
    my $parent = $self->[_Parent];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
    return unless $parent;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
    for (my $i = $parent->getChildIndex ($self) - 1; $i >= 0; $i--)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
	my $node = $parent->getChildAtIndex ($i);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
	return $node unless $node->xql_isIgnorableWS;	# skip whitespace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
    undef;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
# True if it's a Text node with just whitespace and xml::space != "preserve"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
sub xql_isIgnorableWS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
    0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
# Whether the node should preserve whitespace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
# It should if it has attribute xml:space="preserve"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
sub xql_preserveSpace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
    $_[0]->[_Parent]->xql_preserveSpace;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
sub xql_element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
#?? I wonder which implemention is used for e.g. DOM::Text, since XML::XQL::Node also has an implementation
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
    [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    87
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    88
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
sub xql_document
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
    $_[0]->[_Doc];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
sub xql_node
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
    my $kids = $_[0]->[_C];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
    if (defined $kids)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
	# Must copy the list or else we return a blessed reference
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
	# (which causes trouble later on)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
	my @list = @$kids;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
	return \@list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
    [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
#?? implement something to support NamedNodeMaps in DocumentType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
sub xql_childIndex
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
    $_[0]->[_Parent]->getChildIndex ($_[0]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
#?? implement something to support NamedNodeMaps in DocumentType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
sub xql_childCount
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
    my $ch = $_[0]->[_C];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
    defined $ch ? scalar(@$ch) : 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
sub xql_parent
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
    $_[0]->[_Parent];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
sub xql_DOM_nodeType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
    $_[0]->getNodeType;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
sub xql_nodeType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
    $_[0]->getNodeType;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
# As it appears in the XML document
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
sub xql_xmlString
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
    $_[0]->toString;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
package XML::DOM::Element;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
sub xql_attribute
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
    my ($node, $attrName) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
    if (defined $attrName)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
	my $attr = $node->getAttributeNode ($attrName);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
	defined ($attr) ? [ $attr ] : [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
	defined $node->[_A] ? $node->[_A]->getValues : [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
# Used by XML::XQL::Union::genSortKey to generate sort keys
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
# Returns the maximum of the number of children and the number of Attr nodes.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
sub xql_childCount
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
    my $n = scalar @{$_[0]->[_C]};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
    my $m = defined $_[0]->[_A] ? $_[0]->[_A]->getLength : 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
    return $n > $m ? $n : $m;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
sub xql_element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
    my ($node, $elem) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
    my @list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
    if (defined $elem)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
	    push @list, $kid if $kid->isElementNode && $kid->[_TagName] eq $elem;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
	    push @list, $kid if $kid->isElementNode;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
    \@list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   188
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   189
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   190
sub xql_nodeName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
    $_[0]->[_TagName];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
sub xql_baseName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
    my $name = $_[0]->[_TagName];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
    $name =~ s/^\w*://;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
    $name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
sub xql_prefix
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
    my $name = $_[0]->[_TagName];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
    $name =~ /([^:]+):/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
    $1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
sub xql_rawText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
    my ($self, $recurse) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
    $recurse = 1 unless defined $recurse;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
    my $text = "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
    for my $kid (@{$self->xql_node})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
	my $type = $kid->xql_nodeType;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
	# type=1: element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
	# type=3: text (Text, CDATASection, EntityReference)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
	if (($type == 1 && $recurse) || $type == 3)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
	    $text .= $kid->xql_rawText ($recurse);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
    $text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
sub xql_text
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
    my ($self, $recurse) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
    $recurse = 1 unless defined $recurse;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
    my $j = -1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
    my @text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
    my $last_was_text = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
    # Collect text blocks. Consecutive blocks of Text, CDataSection and 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
    # EntityReference nodes should be merged without stripping and without
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
    # putting spaces in between.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
    for my $kid (@{$self->xql_node})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
	my $type = $kid->xql_nodeType;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
	if ($type == 1)	    # 1: element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   248
	    if ($recurse)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
	    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
		$text[++$j] = $kid->xql_text ($recurse);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
	    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
	    $last_was_text = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
	elsif ($type == 3)  # 3: text (Text, CDATASection, EntityReference)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
	    ++$j unless $last_was_text;		# next text block
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
	    $text[$j] .= $kid->getData;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
	    $last_was_text = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
	else	# e.g. Comment
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
	    $last_was_text = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   264
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   265
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   266
    # trim whitespace and remove empty blocks
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
    my $i = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
    my $n = @text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
    while ($i < $n)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
	# similar to XML::XQL::trimSpace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
	$text[$i] =~ s/^\s+//;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
	$text[$i] =~ s/\s+$//;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
	if ($text[$i] eq "")
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
	    splice (@text, $i, 1);	# remove empty block
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
	    $n--;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
	else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
	    $i++;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
    join (" ", @text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   286
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   287
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   288
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
# Returns a list of text blocks for this Element.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
# A text block is a concatenation of consecutive text-containing nodes (i.e.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
# Text, CDATASection or EntityReference nodes.)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
# For each text block a reference to an array is returned with the following
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
# 3 items:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
#  [0] index of first node of the text block
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
#  [1] index of last node of the text block
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
#  [2] concatenation of the raw text (of the nodes in this text block)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
# The text blocks are returned in reverse order for the convenience of
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
# the routines that want to modify the text blocks.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
sub xql_rawTextBlocks
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
    my ($self) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
    my @result;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
    my $curr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
    my $prevWasText = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
    my $kids = $self->[_C];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
    my $n = @$kids;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
    for (my $i = 0; $i < $n; $i++)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
	my $node = $kids->[$i];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
	# 3: text (Text, CDATASection, EntityReference)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   314
	if ($node->xql_nodeType == 3)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   315
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   316
	    if ($prevWasText)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   317
	    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   318
		$curr->[1] = $i;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   319
		$curr->[2] .= $node->getData;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   320
	    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   321
	    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   322
	    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   323
		$curr = [$i, $i, $node->getData];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   324
		unshift @result, $curr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   325
		$prevWasText = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   326
	    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   327
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   328
	else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   329
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   330
	    $prevWasText = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   331
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   332
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   333
    @result;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   334
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   335
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   336
sub xql_replaceBlockWithText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   337
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   338
    my ($self, $start, $end, $text) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   339
    for (my $i = $end; $i > $start; $i--)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   340
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   341
	# dispose of the old nodes
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   342
	$self->removeChild ($self->[_C]->[$i])->dispose;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   343
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   344
    my $node = $self->[_C]->[$start];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   345
    my $newNode = $self->[_Doc]->createTextNode ($text);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   346
    $self->replaceChild ($newNode, $node)->dispose;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   347
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   348
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   349
sub xql_setValue
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   350
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   351
    my ($self, $str) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   352
    # Remove all children
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   353
    for my $kid (@{$self->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   354
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   355
	$self->removeChild ($kid);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   356
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   357
    # Add a (single) text node
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   358
    $self->appendChild ($self->[_Doc]->createTextNode ($str));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   359
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   360
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   361
sub xql_value
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   362
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   363
    XML::XQL::elementValue ($_[0]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   364
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   365
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   366
sub xql_preserveSpace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   367
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   368
    # attribute value should be "preserve" (1), "default" (0) or "" (ask parent)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   369
    my $space = $_[0]->getAttribute ("xml:space");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   370
    $space eq "" ? $_[0]->[_Parent]->xql_preserveSpace : ($space eq "preserve");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   371
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   372
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   373
package XML::DOM::Attr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   374
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   375
sub xql_sortKey
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   376
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   377
    my $key = $_[0]->[_SortKey];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   378
    return $key if defined $key;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   379
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   380
    $_[0]->[_SortKey] = XML::XQL::createSortKey ($_[0]->xql_parent->xql_sortKey, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   381
						$_[0]->xql_childIndex, 0);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   382
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   383
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   384
sub xql_nodeName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   385
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   386
    $_[0]->getNodeName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   387
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   388
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   389
sub xql_text
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   390
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   391
    XML::XQL::trimSpace ($_[0]->getValue);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   392
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   393
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   394
sub xql_rawText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   395
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   396
    $_[0]->getValue;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   397
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   398
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   399
sub xql_value
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   400
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   401
    XML::XQL::attrValue ($_[0]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   402
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   403
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   404
sub xql_setValue
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   405
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   406
    $_[0]->setValue ($_[1]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   407
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   408
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   409
sub xql_baseName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   410
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   411
    my $name = $_[0]->getNodeName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   412
    $name =~ s/^\w*://;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   413
    $name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   414
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   415
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   416
sub xql_prefix
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   417
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   418
    my $name = $_[0]->getNodeName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   419
    $name =~ s/:\w*$//;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   420
    $name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   421
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   422
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   423
sub xql_parent
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   424
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   425
    $_[0]->[_UsedIn]->{''}->{Parent};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   426
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   427
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   428
sub xql_childIndex
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   429
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   430
    my $map = $_[0]->[_UsedIn];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   431
    $map ? $map->getChildIndex ($_[0]) : 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   432
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   433
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   434
package XML::DOM::Text;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   435
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   436
sub xql_rawText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   437
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   438
    $_[0]->[_Data];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   439
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   440
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   441
sub xql_text
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   442
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   443
    XML::XQL::trimSpace ($_[0]->[_Data]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   444
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   445
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   446
sub xql_setValue
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   447
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   448
    $_[0]->setData ($_[1]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   449
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   450
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   451
sub xql_isIgnorableWS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   452
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   453
    $_[0]->[_Data] =~ /^\s*$/ &&
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   454
    !$_[0]->xql_preserveSpace;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   455
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   456
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   457
package XML::DOM::CDATASection;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   458
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   459
sub xql_rawText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   460
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   461
    $_[0]->[_Data];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   462
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   463
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   464
sub xql_text
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   465
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   466
    XML::XQL::trimSpace ($_[0]->[_Data]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   467
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   468
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   469
sub xql_setValue
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   470
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   471
    $_[0]->setData ($_[1]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   472
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   473
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   474
sub xql_nodeType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   475
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   476
    3;	# it contains text, so XQL spec states it's a text node
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   477
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   478
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   479
package XML::DOM::EntityReference;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   480
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   481
BEGIN
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   482
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   483
    # import constant field definitions, e.g. _Data
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   484
    import XML::DOM::CharacterData qw{ :Fields };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   485
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   486
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   487
sub xql_text
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   488
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   489
    $_[0]->getData;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   490
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   491
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   492
sub xql_rawText
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   493
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   494
    XML::XQL::trimSpace ($_[0]->[_Data]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   495
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   496
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   497
sub xql_setValue
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   498
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   499
    $_[0]->setData ($_[1]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   500
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   501
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   502
sub xql_nodeType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   503
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   504
    3;	# it contains text, so XQL spec states it's a text node
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   505
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   506
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   507
package XML::DOM::Document;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   508
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   509
BEGIN
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   510
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   511
    # import constant field definitions, e.g. _TagName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   512
    import XML::DOM::Element qw{ :Fields };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   513
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   514
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   515
sub xql_sortKey
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   516
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   517
    "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   518
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   519
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   520
sub xql_element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   521
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   522
    my ($node, $elem) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   523
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   524
    my @list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   525
    if (defined $elem)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   526
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   527
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   528
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   529
	    push @list, $kid if $kid->isElementNode && $kid->[_TagName] eq $elem;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   530
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   531
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   532
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   533
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   534
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   535
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   536
	    push @list, $kid if $kid->isElementNode;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   537
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   538
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   539
    \@list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   540
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   541
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   542
sub xql_parent
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   543
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   544
    undef;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   545
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   546
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   547
# By default the elements in a document don't preserve whitespace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   548
sub xql_preserveSpace
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   549
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   550
    0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   551
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   552
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   553
package XML::DOM::DocumentFragment;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   554
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   555
BEGIN
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   556
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   557
    # import constant field definitions, e.g. _TagName
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   558
    import XML::DOM::Element qw{ :Fields };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   559
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   560
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   561
sub xql_element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   562
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   563
    my ($node, $elemName) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   564
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   565
    my @list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   566
    if (defined $elemName)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   567
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   568
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   569
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   570
	    push @list, $kid if $kid->isElementNode && $kid->[_TagName] eq $elemName;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   571
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   572
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   573
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   574
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   575
	for my $kid (@{$node->[_C]})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   576
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   577
	    push @list, $kid if $kid->isElementNode;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   578
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   579
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   580
    \@list;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   581
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   582
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   583
sub xql_parent
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   584
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   585
    undef;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   586
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   587
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   588
1; # module loaded successfuly
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   589
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   590
__END__
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   591
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   592
=head1 NAME
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   593
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   594
XML::XQL::DOM - Adds XQL support to XML::DOM nodes
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   595
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   596
=head1 SYNOPSIS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   597
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   598
 use XML::XQL;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   599
 use XML::XQL::DOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   600
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   601
 $parser = new XML::DOM::Parser;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   602
 $doc = $parser->parsefile ("file.xml");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   603
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   604
 # Return all elements with tagName='title' under the root element 'book'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   605
 $query = new XML::XQL::Query (Expr => "book/title");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   606
 @result = $query->solve ($doc);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   607
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   608
 # Or (to save some typing)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   609
 @result = XML::XQL::solve ("book/title", $doc);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   610
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   611
 # Or (see XML::DOM::Node)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   612
 @result = $doc->xql ("book/title");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   613
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   614
=head1 DESCRIPTION
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   615
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   616
XML::XQL::DOM adds methods to L<XML::DOM> nodes to support XQL queries
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   617
on XML::DOM document structures.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   618
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   619
See L<XML::XQL> and L<XML::XQL::Query> for more details.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   620
L<XML::DOM::Node> describes the B<xql()> method.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   621
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   622