package XML::DOM::Lite::Serializer;

use XML::DOM::Lite::Constants qw(:all);

sub new {
    my $class = shift;
    return bless { }, $class;
}

sub serializeToString {
    my ($self, $node) = @_;
    unless (ref $self) {
        $self = __PACKAGE__->new;
    }
    if ($node->nodeType == DOCUMENT_NODE) {
        $node = $node->firstChild;
    }

    $self->{_indent_level} = 0 unless defined $self->{_indent_level};

    my $out = "";
    if ($node->nodeType == ELEMENT_NODE) {
        $out .= "\n".$self->_mkIndent()."<".$node->tagName;
        foreach my $att (@{$node->attributes}) {
            $out .= " $att->{nodeName}=\"".$att->{nodeValue}."\"";
        }
        if ($node->childNodes->length) {
            $out .= ">";
            $self->{_indent_level}++;
            my $contains_element_childs = 0; #added by Andi, is a boolean value that indicates whether a node has element child nodes
            foreach my $n (@{$node->childNodes}) {      
                $out .= $self->serializeToString($n);
                if ($n->nodeType == ELEMENT_NODE) {
                	$contains_element_childs = 1; #indicates that a node contains child element nodes
                }
            }
            $self->{_indent_level}--;
            if ($contains_element_childs == 1) { #added by Andi
            	$out .= "\n".$self->_mkIndent()."</".$node->tagName.">";
            } #added by Andi
            else {
            	$out .= "</".$node->tagName.">"; #added by Andi
            } #added by Andi
        } else {
            $out .= " />";
        }
    }
    elsif ($node->nodeType == TEXT_NODE) {
        #$out .= "\n".$self->_mkIndent().$node->nodeValue; #original line
        $out .= $node->nodeValue; #changed by Andi
    }
    elsif ($node->nodeType == COMMENT_NODE) {
        $out .= "\n".$self->_mkIndent()."<!--".$node->nodeValue."-->";
    }

    return $out;
}

sub _mkIndent {
    my ($self) = @_;
    return (" " x (2 * $self->{_indent_level}));
}
1;
