deprecated/buildtools/buildsystemtools/lib/XML/Handler/BuildDOM.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
package XML::Handler::BuildDOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
use XML::DOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
# TODO:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
# - add support for parameter entity references
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
# - expand API: insert Elements in the tree or stuff into DocType etc.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
sub new
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
    my ($class, %args) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
    bless \%args, $class;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
#-------- PerlSAX Handler methods ------------------------------
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
sub start_document # was Init
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
    # Define Document if it's not set & not obtainable from Element or DocType
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
    $self->{Document} ||= 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
	(defined $self->{Element} ? $self->{Element}->getOwnerDocument : undef)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
     || (defined $self->{DocType} ? $self->{DocType}->getOwnerDocument : undef)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
     || new XML::DOM::Document();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
    $self->{Element} ||= $self->{Document};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
    unless (defined $self->{DocType})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
	$self->{DocType} = $self->{Document}->getDoctype
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
	    if defined $self->{Document};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
	unless (defined $self->{Doctype})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
#?? should be $doc->createDocType for extensibility!
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
	    $self->{DocType} = new XML::DOM::DocumentType ($self->{Document});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
	    $self->{Document}->setDoctype ($self->{DocType});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
    # Prepare for document prolog
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
    $self->{InProlog} = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
    # We haven't passed the root element yet
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
    $self->{EndDoc} = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
sub end_document # was Final
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
    unless ($self->{SawDocType})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
	my $doctype = $self->{Document}->removeDoctype;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
	$doctype->dispose;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
#?? do we always want to destroy the Doctype?
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
    $self->{Document};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
sub characters # was Char
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
    my $self = $_[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
    my $str = $_[1]->{Data};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
    if ($self->{InCDATA} && $self->{KeepCDATA})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
	undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
	# Merge text with previous node if possible
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
	$self->{Element}->addCDATA ($str);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
	# Merge text with previous node if possible
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
	# Used to be:	$expat->{DOM_Element}->addText ($str);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
	if ($self->{LastText})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
	    $self->{LastText}->appendData ($str);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
	else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
	    $self->{LastText} = $self->{Document}->createTextNode ($str);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
	    $self->{Element}->appendChild ($self->{LastText});
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
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
sub start_element # was Start
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
    my $elem = $hash->{Name};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
    my $attr = $hash->{Attributes};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
    my $parent = $self->{Element};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
    my $doc = $self->{Document};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
    if ($parent == $doc)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
	# End of document prolog, i.e. start of first Element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
	$self->{InProlog} = 0;
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
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
    my $node = $doc->createElement ($elem);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
    $self->{Element} = $node;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
    $parent->appendChild ($node);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
    my $i = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
    my $n = scalar keys %$attr;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
    return unless $n;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
    if (exists $hash->{AttributeOrder})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
	my $defaulted = $hash->{Defaulted};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
	my @order = @{ $hash->{AttributeOrder} };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
	# Specified attributes
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
	for (my $i = 0; $i < $defaulted; $i++)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
	    my $a = $order[$i];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
	    my $att = $doc->createAttribute ($a, $attr->{$a}, 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
	    $node->setAttributeNode ($att);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
	# Defaulted attributes
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
	for (my $i = $defaulted; $i < @order; $i++)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
	    my $a = $order[$i];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
	    my $att = $doc->createAttribute ($elem, $attr->{$a}, 0);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
	    $node->setAttributeNode ($att);
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
    else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
	# We're assuming that all attributes were specified (1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
	for my $a (keys %$attr)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
	    my $att = $doc->createAttribute ($a, $attr->{$a}, 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
	    $node->setAttributeNode ($att);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
sub end_element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
    $self->{Element} = $self->{Element}->getParentNode;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
    # Check for end of root element
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
    $self->{EndDoc} = 1 if ($self->{Element} == $self->{Document});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
sub entity_reference # was Default
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
    my $self = $_[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
    my $name = $_[1]->{Name};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
    $self->{Element}->appendChild (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
			    $self->{Document}->createEntityReference ($name));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
sub start_cdata
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
    $self->{InCDATA} = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
sub end_cdata
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
    my $self = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
    $self->{InCDATA} = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
sub comment
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
    my $self = $_[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
    my $comment = $self->{Document}->createComment ($_[1]->{Data});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
    $self->{Element}->appendChild ($comment);
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 doctype_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
    $self->{DocType}->setParams ($hash->{Name}, $hash->{SystemId}, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
				 $hash->{PublicId}, $hash->{Internal});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
    $self->{SawDocType} = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
sub attlist_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
    $self->{DocType}->addAttDef ($hash->{ElementName},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
				 $hash->{AttributeName},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
				 $hash->{Type},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
				 $hash->{Default},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
				 $hash->{Fixed});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
sub xml_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
    $self->{Document}->setXMLDecl (new XML::DOM::XMLDecl ($self->{Document}, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
							  $hash->{Version},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
							  $hash->{Encoding},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
							  $hash->{Standalone}));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
sub entity_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
    # Parameter Entities names are passed starting with '%'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
    my $parameter = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
#?? parameter entities currently not supported by PerlSAX!
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
    $self->{DocType}->addEntity ($parameter, $hash->{Name}, $hash->{Value}, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
				 $hash->{SystemId}, $hash->{PublicId}, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
				 $hash->{Notation});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
# Unparsed is called when it encounters e.g:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
#   <!ENTITY logo SYSTEM "http://server/logo.gif" NDATA gif>
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
sub unparsed_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   248
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
    # same as regular ENTITY, as far as DOM is concerned
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
    $self->entity_decl ($hash);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
sub element_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
    $self->{DocType}->addElementDecl ($hash->{Name}, $hash->{Model});
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
sub notation_decl
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
    $self->{DocType}->addNotation ($hash->{Name}, $hash->{Base}, 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
				   $hash->{SystemId}, $hash->{PublicId});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
sub processing_instruction
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
    my ($self, $hash) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
    local $XML::DOM::IgnoreReadOnly = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
    undef $self->{LastText};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
    $self->{Element}->appendChild (new XML::DOM::ProcessingInstruction 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
			    ($self->{Document}, $hash->{Target}, $hash->{Data}));
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
return 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
__END__
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
=head1 NAME
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
XML::Handler::BuildDOM - PerlSAX handler that creates XML::DOM document structures
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
=head1 SYNOPSIS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
 use XML::Handler::BuildDOM;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
 use XML::Parser::PerlSAX;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
 my $handler = new XML::Handler::BuildDOM (KeepCDATA => 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
 my $parser = new XML::Parser::PerlSAX (Handler => $handler);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
 my $doc = $parser->parsefile ("file.xml");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
=head1 DESCRIPTION
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
XML::Handler::BuildDOM creates L<XML::DOM> document structures 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
(i.e. L<XML::DOM::Document>) from PerlSAX events.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
This class used to be called L<XML::PerlSAX::DOM> prior to libxml-enno 1.0.1.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
=head2 CONSTRUCTOR OPTIONS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   314
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   315
The XML::Handler::BuildDOM constructor supports the following options:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   316
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   317
=over 4
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   318
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   319
=item * KeepCDATA => 1 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   320
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   321
If set to 0 (default), CDATASections will be converted to regular text.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   322
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   323
=item * Document => $doc
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   324
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   325
If undefined, start_document will extract it from Element or DocType (if set),
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   326
otherwise it will create a new XML::DOM::Document.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   327
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   328
=item * Element => $elem
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   329
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   330
If undefined, it is set to Document. This will be the insertion point (or parent)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   331
for the nodes defined by the following callbacks.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   332
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   333
=item * DocType => $doctype
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   334
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   335
If undefined, start_document will extract it from Document (if possible).
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   336
Otherwise it adds a new XML::DOM::DocumentType to the Document.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   337
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   338
=back