/tools/Ruby/lib/ruby/1.8/rexml/instruction.rb

http://github.com/agross/netopenspace · Ruby · 70 lines · 44 code · 8 blank · 18 comment · 4 complexity · 4e016f54de8d692f44a33b21637b9fe6 MD5 · raw file

  1. require "rexml/child"
  2. require "rexml/source"
  3. module REXML
  4. # Represents an XML Instruction; IE, <? ... ?>
  5. # TODO: Add parent arg (3rd arg) to constructor
  6. class Instruction < Child
  7. START = '<\?'
  8. STOP = '\?>'
  9. # target is the "name" of the Instruction; IE, the "tag" in <?tag ...?>
  10. # content is everything else.
  11. attr_accessor :target, :content
  12. # Constructs a new Instruction
  13. # @param target can be one of a number of things. If String, then
  14. # the target of this instruction is set to this. If an Instruction,
  15. # then the Instruction is shallowly cloned (target and content are
  16. # copied). If a Source, then the source is scanned and parsed for
  17. # an Instruction declaration.
  18. # @param content Must be either a String, or a Parent. Can only
  19. # be a Parent if the target argument is a Source. Otherwise, this
  20. # String is set as the content of this instruction.
  21. def initialize(target, content=nil)
  22. if target.kind_of? String
  23. super()
  24. @target = target
  25. @content = content
  26. elsif target.kind_of? Instruction
  27. super(content)
  28. @target = target.target
  29. @content = target.content
  30. end
  31. @content.strip! if @content
  32. end
  33. def clone
  34. Instruction.new self
  35. end
  36. # == DEPRECATED
  37. # See the rexml/formatters package
  38. #
  39. def write writer, indent=-1, transitive=false, ie_hack=false
  40. Kernel.warn( "#{self.class.name}.write is deprecated" )
  41. indent(writer, indent)
  42. writer << START.sub(/\\/u, '')
  43. writer << @target
  44. writer << ' '
  45. writer << @content
  46. writer << STOP.sub(/\\/u, '')
  47. end
  48. # @return true if other is an Instruction, and the content and target
  49. # of the other matches the target and content of this object.
  50. def ==( other )
  51. other.kind_of? Instruction and
  52. other.target == @target and
  53. other.content == @content
  54. end
  55. def node_type
  56. :processing_instruction
  57. end
  58. def inspect
  59. "<?p-i #{target} ...?>"
  60. end
  61. end
  62. end