From: Jan T. <de...@us...> - 2001-12-11 22:21:51
|
Update of /cvsroot/net-script/netscript2/src/perl/XML/DOM2 In directory usw-pr-cvs1:/tmp/cvs-serv3353 Added Files: Element.pm Log Message: * initial checkin --- NEW FILE: Element.pm --- #-------------------------------------------------------- # DOM Level 2 Implementation for Perl # Class Element # $Id: Element.pm,v 1.1 2001/12/11 22:21:49 derkork Exp $ # # DOM2 and all related materials, such as documentation, # are protected under the terms and conditions of the Artistic License. # (C) 2000-2001 by Jan Thomä, insOMnia # mailto: ko...@in... #-------------------------------------------------------- use strict; package XML::DOM2::Element; use base qw(XML::DOM2::Node); use vars qw($VERSION); use XML::DOM2::DOMException; use XML::DOM2::NamedNodeMap; #-------------------------------------------------------- # Globals #-------------------------------------------------------- $VERSION = '1.0'; #-------------------------------------------------------- # Methods #-------------------------------------------------------- #/** # Creates a new Element. # @return an instance of XML::DOM2::Element # @public #*/ sub new { my ($proto) = shift; my $class = ref( $proto ) || $proto;# get the Classname my $this = $proto -> SUPER::new(@_); $this -> { m_tagName } = ""; $this -> { m_attributes } = XML::DOM2::NamedNodeMap -> new(); return $this; } #/** # Returns the attributes of this Element. # @return an instance of XML::DOM2::NamedNodeMap which holds the attributes # of this element # @public #*/ sub attributes { my ($this) = @_; $this -> { m_attributes }; } #/** # Returns 0 if this Element has no attributes, and nonzero if it # has attributes. # @return 0 or nonzero. # @public #*/ sub hasAttributes { my ($this) = @_; $this -> attributes() -> length(); } #/** # Retrieves an attribute value by name. # @param a hash containing the following key-value-pairs # name - the name of the attribute to retrieve. # @return a scalar holding the attr value, or an empty string # if the attribute has no specified or default value. # @public #*/ sub getAttribute { my ($this, $paramsRef ) = @_; my $attribute = $this -> attributes() -> getNamedItem( $paramsRef ); if ( defined( $attribute) ) { return $attribute -> nodeValue(); } else { return ""; } } #/** # Returns the attribute value by local name and namespace URI. # @param a hash containing the following key-value-pairs. # namespaceURI - the namespace URI of the attribute to retrieve # localName - the local name of the attribute to retrieve. # @return the attr value as string or the empty string of that attribute # does not have a specified or default value. # @public #*/ sub getAttributeNS { my ($this, $paramsRef) = @_; my $attribute = $this -> attributes() -> getNamedItemNS( $paramsRef ); if ( defined( $attribute) ) { return $attribute -> nodeValue(); } else { return ""; } } #/** # Retrieves an attribute node by name. # @param a hash containing the following key-value-pairs: # name - the name (nodeName) of the attribute to retrieve # @return an instance of XML::DOM2::Attr or undef, if there is no such # attribute. # @public #*/ sub getAttributeNode { my ($this, $paramsRef) = @_; $this -> attributes() -> getNamedItem( $paramsRef ); } #/** # Retrieves an attribute node by local name and namespace URI. # @param a hash containing the following key-value-pairs: # namespaceURI - the namespace URI of the attribute to retrieve # localName - the local name of the attribute to retrieve. # @return an instance of XML::DOM2::Attr or undef, if there is no such # attribute #*/ sub getAttributeNodeNS { my ($this, $paramsRef) = @_; $this -> attributes() -> getNamedItemNS( $paramsRef ); } #/** # Returns a NodeList of all descendant Elements with a given tag name # in the order in which they are encountered in a preorder traversal of # the document tree. # @param a hash containing the following key-value-pairs: # name - the name of the tag to match on. the special value # "*" matches all tags. # @param (this is private, do not give a param here!) an instance of # XML::DOM2::NodeList holding the currently found elements. # @return an instance of XML::DOM2::NodeList holding all matching element # nodes. # @public # Note: This might be an expensive operation. # Note: You should not make any assumptions on the order of the elements # in the list. #*/ sub getElementsByTagName { my ($this, $paramRef, $listRef ) = @_; my $name = $paramRef -> { "name" }; # the root has no list yet, we produce one. if ( ! defined($listRef) ) { $listRef = XML::DOM2::NodeList -> new(); } # first search all own children. my $children = $this -> childNodes(); my $length -> $children -> length(); for ( 0..$length-1 ) { my $child = $children -> item ( { "index" => $_} ); if ( $child -> nodeType() == &ELEMENT_NODE() ) { # if an element is found add it to the list if ( $name eq "*" || $child -> nodeName() eq $name) { $listRef -> add( { "node" => $child } ); } # and traverse it. $child -> getElementsByTagName( $paramRef, $listRef ); } } # return the list. $listRef; } #/** # Returns a NodeList of all the elements with a given local name and namespace # URI in the order in which they are encountered in a preorder traversal of # the document tree. # @param a hash containing the following key-value-pairs: # namespaceURI - the namespace uri of the elements to match on. the # special value "*" matches all namespaces # localName - the local name of the elements to match on. the # special value "*" matches all local names. # @param (this is private, do not give any param here!) an instance of # XML::DOM2::NodeList containing the currently found elements. # @return an instance of XML::DOM2::NodeList containing the matching elements. # @public # Note: This operation might be expensive. # Note: You should not make any assumptions on the order of the elements # in the list. #*/ sub getElementsByTagNameNS { my ($this, $paramRef, $listRef ) = @_; my $namespaceURI = $paramRef -> { "namespaceURI" }; my $localName = $paramRef -> { "localName" }; # the root has no list yet, we produce one. if ( ! defined($listRef) ) { $listRef = XML::DOM2::NodeList -> new(); } # first search all own children. my $children = $this -> childNodes(); my $length -> $children -> length(); for ( 0..$length-1 ) { my $child = $children -> item ( { "index" => $_} ); if ( $child -> nodeType() == &ELEMENT_NODE() ) { # if an element is found add it to the list if ( ( $localName eq "*" || $child -> localName() eq $localName ) && ( $namespaceURI eq "*" || $child -> namespaceURI() eq $namespaceURI ) ) { $listRef -> add( { "node" => $child } ); } # and traverse it. $child -> getElementsByTagName( $paramRef, $listRef ); } } # return the list. $listRef; } #/** # Returns nonzero if an attribtue with the given name is specified on this # Element or has a default value, 0 otherwise. # @param a hash containing the following key-value-pairs: # name - the name of the attribute to look for. # @return 0 or nonzero # @public #*/. sub hasAttribute { my ($this, $paramsRef) = @_; my $attribute = $this -> attributes() -> getNamedItem( $paramsRef ); if ( defined($attribute) ) { $attribute -> specified(); } 0; } #/** # Returns nonzero when an attribute with a given local name and namespace URI # is specified on this element or has a default value. 0 otherwise. # @param a hash containing the following key-value-pairs: # namespaceURI - the namespace URI of the attribute to look for # localName - the local name of the attribute to look for # @return 0 or nonzero # @public #*/ sub hasAttributeNS { my ($this, $paramRef) = @_; my $attribute = $this -> attributes() -> getNamedItemNS( $paramRef ); if ( defined($attribute) ) { $attribute -> specified(); } 0; } #/** # Removes an attribute by name. If the removed attribute is known to # have a default value, an attribute immediately appears containing the # default value, as well as the corresponding namespace URI, local name # and prefix when applicable. # @param a hash containing the following key-value-pairs: # name - the name of the attribute to remove. # @public #*/ sub removeAttribute { my ($this, $paramRef ) = @_; $this -> attributes() -> removeNamedItem( $paramRef ); # FIXME: Implement default attributes. } #/** # Removes an attribute by local name and namespace URI. If the removed # attribute has a default value it is immediately replaced. # The replacing attribute has the same namespace URI and local name as # well as the original prefix. # @param a hash containing the following key-value-pairs: # namespaceURI - the namespace URI of the attribute to remove # localName - the local name of the attribute to remove # @public #*/ sub removeAttributeNS { my ($this, $paramRef) = @_; $this -> attributes() -> removeNamedItemNS( $paramRef ); # FIXME: implement default attributes } #/** # Removes the specified attribute. If the removed Attr has a default value # it is immediately replaced. The replacing attribute has the same namespace # URI and local name as well as the original prefix. # @param a hash containing the following key-value-pairs: # oldAttr - the Attr node to remove from the attribute list. # @return an instance of XML::DOM2::Attr - the removed attr node. # @public #*/ sub removeAttributeNode { my ($this, $paramRef) = @_; my $attr = $paramRef -> { "oldAttr" }; my $result = $this -> attributes() -> removeItem( { "node" => $attr} ); if ( defined( $result ) ) { $result; } else { my $exc = XML::DOM2::DOMException( { ErrCode => XML::DOM2::DOMException -> NOT_FOUND_ERR(), ErrDesc => "The attribute is no attribute of this element!" }); $exc -> raise(); } } #/** # Returns ELEMENT_NODE #*/ sub nodeType { &ELEMENT_NODE(); } |