xmlevents.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env ruby
  2. #--
  3. # Copyright 2004 by Jim Weirich (jim@weirichhouse.org).
  4. # All rights reserved.
  5. # Permission is granted for use, copying, modification, distribution,
  6. # and distribution of modified versions of this work as long as the
  7. # above copyright notice is included.
  8. #++
  9. require 'builder/xmlmarkup'
  10. module Builder
  11. # Create a series of SAX-like XML events (e.g. start_tag, end_tag)
  12. # from the markup code. XmlEvent objects are used in a way similar
  13. # to XmlMarkup objects, except that a series of events are generated
  14. # and passed to a handler rather than generating character-based
  15. # markup.
  16. #
  17. # Usage:
  18. # xe = Builder::XmlEvents.new(hander)
  19. # xe.title("HI") # Sends start_tag/end_tag/text messages to the handler.
  20. #
  21. # Indentation may also be selected by providing value for the
  22. # indentation size and initial indentation level.
  23. #
  24. # xe = Builder::XmlEvents.new(handler, indent_size, initial_indent_level)
  25. #
  26. # == XML Event Handler
  27. #
  28. # The handler object must expect the following events.
  29. #
  30. # [<tt>start_tag(tag, attrs)</tt>]
  31. # Announces that a new tag has been found. +tag+ is the name of
  32. # the tag and +attrs+ is a hash of attributes for the tag.
  33. #
  34. # [<tt>end_tag(tag)</tt>]
  35. # Announces that an end tag for +tag+ has been found.
  36. #
  37. # [<tt>text(text)</tt>]
  38. # Announces that a string of characters (+text+) has been found.
  39. # A series of characters may be broken up into more than one
  40. # +text+ call, so the client cannot assume that a single
  41. # callback contains all the text data.
  42. #
  43. class XmlEvents < XmlMarkup
  44. def text!(text)
  45. @target.text(text)
  46. end
  47. def _start_tag(sym, attrs, end_too=false)
  48. @target.start_tag(sym, attrs)
  49. _end_tag(sym) if end_too
  50. end
  51. def _end_tag(sym)
  52. @target.end_tag(sym)
  53. end
  54. end
  55. end