#
# Library functions for the ic_xml library
#
# File: ic_xml/lib/tdom_impl.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2004 ICEM CFD Engineering
#
# Author: Jason Aftosmis (jason@icemcfd.com)
#
# TODO:
#   o
#
# For tDOM documentaion, see:
#     http://www.tdom.org/dom.html
#     http://www.tdom.org/tdomcmd.html
#     http://www.tdom.org/domNode.html
#     http://www.tdom.org/expat.html
#
#---------------------------------------------------------------------
#

package require tdom

namespace eval tDomImpl {    
    
    namespace import ::IcXml::*

    proc err {text}   { return [ic_xml_err   "tDomImpl: $text"] }
    proc debug {text} { return [ic_xml_debug "tDomImpl: $text"] }
}


## Creates a new DOM document object with one element node with node name docElemName.
## The objVar controlls the memory handling as explained above.

proc tDomImpl::domCreate { root_element_name } {
    
    debug "dom createDocument $root_element_name"    
    set docObj [dom createDocument $root_element_name]    
    debug "gets $docObj"
    
    return $docObj
}


## 

proc tDomImpl::domRoot { docObj } {
    
    debug "$docObj documentElement"
    set rootNodeObj [$docObj documentElement]
    debug "gets $rootNodeObj"
    
    return $rootNodeObj
}


## 

proc tDomImpl::domOpen { xml_file } {
    
    debug "dom parse ... parsed-$xml_file"
    set domObj [dom parse -baseurl [tDOM::baseURL $xml_file] \
		    -externalentitycommand ::tDOM::extRefHandler \
		    -keepEmpties \
		    [tDOM::xmlReadFile $xml_file] ]
    debug "gets $domObj"
    
    return $domObj
}


## 

proc tDomImpl::domDelete { docObj } {
    
    debug "$docObj delete"    
    $docObj delete    
    debug "...done."

    return 0
}


## 

proc tDomImpl::domGetAsXml { docObj } {
    
    debug "$docObj asXML  -indent 5 -doctypeDeclaration 1"
    return [$docObj asXML -indent 5 -doctypeDeclaration 1]
}


## 

proc tDomImpl::domGetAsHtml { docObj {xsl_file ""} } {
    
    if {"" == $xsl_file} {
	debug "$docObj asHTML"
	return [$docObj asHTML]
    }
    
    if ![file exists $xsl_file] {
	
	err "could not find the given xsl file \"$xsl_file\""
	debug "$docObj asHTML"
	return [$docObj asHTML]	
    }
    
    debug "transforming xml data to html via xsl file \"xsl_file\""
    
    set fd [open $xsl_file]
    set xslData [read $fd]
    close $fd

    debug "dom parse xslData"
    set xslObj [dom parse $xslData]
    
    debug "$docObj xslt $xslObj"
    set xsltObj [$docObj xslt $xslObj]    
    $xslObj delete    

    debug "$xsltObj asHTML"
    set xsltData [$xsltObj asHTML]
    
    debug "$xsltObj delete"
    $xsltObj delete
    
    return $xsltData
}


## 

proc tDomImpl::domElementCreate { docObj element_name } {
    
    debug "$docObj createElement $element_name"    
    set newNodeObj [$docObj createElement $element_name]    
    debug "gets $newNodeObj"
    
    return $newNodeObj
}

## 

proc tDomImpl::domElementGet { parentObj element_name } {
    
    debug "getting child nodes of $parentObj, looking for $element_name"
    
    #element $element_name in $docObj..."
    
    set childObjs [$parentObj childNodes]
    foreach cObj $childObjs {
	set cName [$cObj nodeName]
	debug "\tgot name $cName"
	if [string equal $cName $element_name] {
	    debug "gets $cObj"
	    return $cObj
	}
    }
    
    debug "could not find element $element_name in $parentObj"
    
    return ""
}

##

proc tDomImpl::domTextNodeCreate { docObj text } {
    
    debug "$docObj createTextNode $text"
    set newNode [$docObj createTextNode $text]
    debug "gets $newNode"
    return $newNode
}


## 

proc tDomImpl::domElementDelete { nodeObj } {
    debug "$nodeObj delete"
    return [$nodeObj delete]
}


## 

proc tDomImpl::domElementSetValue { nodeObj val } {
    
    debug "$nodeObj nodeValue $val"
    return [$nodeObj nodeValue $val]
}


##

proc tDomImpl::domElementGetName { nodeObj } {
    
    debug "$nodeObj nodeName"
    set name [$nodeObj nodeName]
    debug "gets $name"
    return $name
}


## 

proc tDomImpl::domElementAddAttribute { nodeObj attName attVal } {
    
    debug "$nodeObj setAttribute $attName $attVal"
    set ret [$nodeObj setAttribute $attName $attVal]
    debug "gets $ret"
    return $ret
}

## 

proc tDomImpl::domElementGetAttribute { nodeObj attName } {
    # fixme
    debug "$nodeObj getAttribute $attName $attVal"    
    set att [$nodeObj getAttribute $attName $attVal]
    debug "gets $att"
    return $att
}


##

proc tDomImpl::domElementAppendChild { parentObj childObj } {
    
    debug "$parentObj appendChild $childObj"
    set ret [$parentObj appendChild $childObj]
    debug "gets $ret"
    return $ret
}

##

proc tDomImpl::domCloneElement { nodeObj } {
    
    debug "$nodeObj clone -deep"
    set ret [$nodeObj clone -deep]
    debug "gets $ret"
    return $ret
}
