# examplec.rb -- translated to ruby from Paolo Casarini's examplec.c
#
# Copyright (C) 2001 Paolo Casarini <paolo@casarini.org>
# Copyright (C) 2001 Tobias Peters <t-peters@berlios.de>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA.

require "gdome"

def isSpaceStr(string)
  /\s*/.match(string).to_s == string
end

def cleanSubTree (node)
  nl = node.childNodes
  i = nl.length - 1
  while(i >= 0)
    child = nl.item(i)
    if (child.kind_of?(Dom::Comment) ||
	(child.kind_of?(Dom::Text) && isSpaceStr(child.data)))
      node.removeChild(child)
    elsif (child.hasChildNodes?)
      cleanSubTree(child)
    end
    i -= 1
  end
end


# /* First I get a DOMImplementation reference */
domimpl = Dom::implementation

# /* I load a new document from the file name "exampleb.xml */
doc = domimpl.createDocFromURI("examplec.xml", Dom::LOAD_PARSING)

# /* I get reference to the root element of the document */
rootel = doc.documentElement

cleanSubTree(rootel)

domimpl.saveDocToFile(doc, "examplec_out.xml", Dom::SAVE_STANDARD)
