# test-dtd.rb -- translated to ruby from Paolo Casarini's test-dtd.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 dot
  print(".")
  $stdout.flush
end

def test_dtd (doc)
  ret = true
  dt = doc.doctype

  # /* Test DocumentType.name */
  str = dt.name
  if (str == nil)
    $stderr.print("\nDocumentType.name, NULL")
    ret = false
  elsif (ret && (str != "TEST-DTD"))
    $stderr.print("\nDocumentType.name, wrong")
  end
  dot

  # /* Test DocumentType.publicId */
  str = dt.publicId
  if (str != nil)
    $stderr.print("\nDocumentType.publicId, wrong")
    ret = false
  end
  dot

  # /* Test DocumentType.systemId */
  str = dt.systemId
  if (str == nil)
    $stderr.print("\nDocumentType.systemId, NULL")
    ret = false
  elsif (ret && (str != "test-dtd.dtd"))
    $stderr.print("\nDocumentType.systemId, wrong")
    ret = false
  end
  dot

  # /* Test DocumentType.internalSubset */
  str = dt.internalSubset
  if (str == nil)
    $stderr.print("\nDocumentType.internalSubset, NULL")
    ret = false
  elsif (ret && (str != "<!DOCTYPE TEST-DTD SYSTEM \"test-dtd.dtd\">"))
    $stderr.print("\nDocumentType.internalSubset, wrong")
    ret = false
  end
  dot

  # /* Test DocumentType.entities */
  ents = dt.entities
  if (ents == nil)
    $stderr.print("\nDocumentType.entities, NULL")
    ret = false
  elsif (ret && (ents.length != 5))
    $stderr.print("\nDocumentType.entities, wrong length")
    ret = false
  end
  dot

  # /* Test DocumentType.notations */
  nots = dt.notations
  if (nots == nil)
    $stderr.print("\nDocumentType.notations, NULL")
    ret = false
  elsif (ret && (nots.length != 5))
    $stderr.print("\nDocumentType.notations, wrong length")
    ret = false
  end
  dot

  print ("test_dtd\n")

  # /* Test Parsed Entity */
  ent = ents.getNamedItem ("FOO1")
  if (ent == nil)
    $stderr.print("\nNamedNodeMap.getNamedItem(\"FOO1\"), NULL")
    ret = false
  elsif (ret && (ent.nodeName != "FOO1"))
    $stderr.print("\nEntity.nodeName(), (parsed) FOO1 wrong nodeName")
    ret = false
  elsif (ret && (ent.publicId != nil))
    $stderr.print("\nEntity.publicId(), (parsed) FOO1 NOT NULL")
    ret = false
  elsif (ret && (ent.systemId != nil))
    $stderr.print("\nEntity.systemId(), (parsed) FOO1 NOT NULL")
    ret = false
  elsif (ret && (ent.notationName != nil))
    $stderr.print("\nEntity.notationName(), (parsed) FOO1 NOT NULL")
    ret = false
  end
  dot


  # /* Test Unparsed Entity */
  ent = ents.getNamedItem("FOO2")
  if (ent == nil)
    $stderr.print("\nNamedNodeMap.getNamedItem(\"FOO2\"), NULL")
    ret = false
  elsif (ret && (ent.nodeName != "FOO2"))
    $stderr.print("\nEntity.nodeName(), (unparsed) FOO2 wrong")
    ret = false
  elsif (ret && (ent.publicId != nil))
    $stderr.print("\nEntity.publicId(), (unparsed) FOO2 wrong")
    ret = false
  elsif (ret && (ent.systemId != "file.type2"))
    $stderr.print("\nEntity.systemId(), (unparsed) FOO2 wrong")
    ret = false
  elsif (ret && (ent.notationName != "type2"))
    $stderr.print("\nEntity.notationName(), (unparsed) FOO2 wrong")
    ret = false
  end
  dot

  print("test_entity\n")

  # /* Test Notation */
  nota = nots.getNamedItem("type1")
  if (nota == nil)
    $stderr.print("\nNamedNodeMap.getNamedItem(\"type1\"), nil")
    ret = false
  elsif (ret && (nota.nodeName != "type1"))
    $stderr.print("\nNotation.nodeName(), wrong nodeName")
    ret = false
  elsif (ret && (nota.publicId != nil))
    $stderr.print("\nNotation.publicId(), NOT nil")
    ret = false
  elsif (ret && (nota.systemId != "program1"))
    $stderr.print("\nNotation.systemId(), wrong")
    ret = false
  end
  dot

  print("test_notation\n")
  ret
end

domImpl = Dom::implementation

domdoc = domImpl.createDocFromURI("test-dtd.xml", Dom::LOAD_VALIDATING)
if (domdoc == nil)
    $stderr.print("DOImplementation.createDocFromURI: failed\n" +
		  "   test-dtd.xml not found\n")
else
  ret = test_dtd(domdoc)
end
