Class: Xampl::XMLText

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr/xml-text.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXMLText

Returns a new instance of XMLText.



6
7
8
9
10
11
12
# File 'lib/xamplr/xml-text.rb', line 6

def initialize
  @namespaces = []
  @prefix_ns_map = {}
  @first_text = nil
  @text = nil
  @ns = nil
end

Instance Attribute Details

#first_textObject

Returns the value of attribute first_text.



4
5
6
# File 'lib/xamplr/xml-text.rb', line 4

def first_text
  @first_text
end

#namespacesObject

Returns the value of attribute namespaces.



4
5
6
# File 'lib/xamplr/xml-text.rb', line 4

def namespaces
  @namespaces
end

#nsObject

Returns the value of attribute ns.



4
5
6
# File 'lib/xamplr/xml-text.rb', line 4

def ns
  @ns
end

#prefix_ns_mapObject

Returns the value of attribute prefix_ns_map.



4
5
6
# File 'lib/xamplr/xml-text.rb', line 4

def prefix_ns_map
  @prefix_ns_map
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/xamplr/xml-text.rb', line 4

def text
  @text
end

Instance Method Details

#build(pp) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xamplr/xml-text.rb', line 77

def build(pp)
  @namespaces = {}
  @first_text = nil
  @text = nil

  depth = start_element(pp, 0)

  while true
    raise XamplException.new("unexpected end of document") if pp.endDocument?

    case pp.nextEvent
    when Xampl_PP::START_DOCUMENT
      raise XamplException.new("unexpected start of document")
    when Xampl_PP::END_DOCUMENT
      raise XamplException.new("unexpected end of document")
    when Xampl_PP::START_ELEMENT
      depth = start_element(pp, depth)
    when Xampl_PP::END_ELEMENT
      depth = end_element(pp, depth)
      break if depth <= 0
    when Xampl_PP::TEXT, Xampl_PP::CDATA_SECTION, Xampl_PP::ENTITY_REF
      @text << pp.text
      #when Xampl_PP::IGNORABLE_WHITESPACE
      #when Xampl_PP::PROCESSING_INSTRUCTION
      #when Xampl_PP::COMMENT
      #when Xampl_PP::DOCTYPE
    end
  end

  @namespaces = @namespaces.keys

  @prefix_ns_map.sort.each do |prefix, ns|
    @first_text << " xmlns:" << prefix << "='" << ns << "'"
  end
  @first_text << @text
  @text = @first_text
  @first_text = nil
end

#end_element(pp, depth) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/xamplr/xml-text.rb', line 59

def end_element(pp, depth)
  depth -= 1

  @text << "</"
  @text << pp.prefix << ":" if pp.prefix
  @text << pp.name << ">"

  return depth
end

#start_element(pp, depth) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xamplr/xml-text.rb', line 14

def start_element(pp, depth)
  @namespaces = {}
  @namespaces[pp.namespace] = pp.namespace

  depth += 1

  if @first_text then
    @text << "<" << pp.qname

    pp.attributeCount.times do |i|
      @text << " " << pp.attributeQName(i) << "='" << pp.attributeValue(i) << "'"

      prefix = pp.attribute_prefix(i)
      if prefix then
        @prefix_ns_map[prefix] = pp.attribute_namespace(i)
      end
    end
  else
    @first_text = "<" << pp.qname

    pp.attributeCount.times do |i|
      @first_text << " " << pp.attributeQName(i) << "='" << pp.attributeValue(i) << "'"

      prefix = pp.attribute_prefix(i)
      if prefix then
        @prefix_ns_map[prefix] = pp.attribute_namespace(i)
      end
    end
    @text = ""
  end

  prefix = pp.prefix
  if prefix then
    @prefix_ns_map[prefix] = pp.namespace
  end

  if @ns != pp.namespace then
    @text << " xmlns='" << pp.namespace << "'"
  end

  @text << ">"

  return depth
end

#to_sObject



69
70
71
# File 'lib/xamplr/xml-text.rb', line 69

def to_s
  @text
end

#to_xml(xml_printer = nil) ⇒ Object



73
74
75
# File 'lib/xamplr/xml-text.rb', line 73

def to_xml(xml_printer=nil)
  @text
end