Class: Xampl::FromXML
- Inherits:
-
Object
- Object
- Xampl::FromXML
- Defined in:
- lib/xamplr/from-xml.rb
Constant Summary collapse
- @@by_tag =
{}
- @@by_ns_tag =
{}
Instance Attribute Summary collapse
-
#checkWellFormed ⇒ Object
readonly
Returns the value of attribute checkWellFormed.
-
#is_realising ⇒ Object
readonly
Returns the value of attribute is_realising.
-
#tokenise_content ⇒ Object
readonly
Returns the value of attribute tokenise_content.
Class Method Summary collapse
- .register(tag, ns_tag, klass) ⇒ Object
- .registered(name) ⇒ Object
- .reset_registry ⇒ Object
- .tokenise_string(str, strip = true) ⇒ Object
Instance Method Summary collapse
- #attributeCount ⇒ Object
- #attributeName(i) ⇒ Object
- #attributeNamespace(i) ⇒ Object
- #attributeValue(i) ⇒ Object
- #build_attribute_arrays ⇒ Object
- #column ⇒ Object
- #current_node_type ⇒ Object
- #depth ⇒ Object
- #find_the_first_element ⇒ Object
-
#initialize(recovering = false) ⇒ FromXML
constructor
A new instance of FromXML.
- #line ⇒ Object
-
#next_reader_event ⇒ Object
def describe_current_element_type() case @reader.node_type when LibXML::XML::Reader::TYPE_ATTRIBUTE puts “ATTRIBUTE” when LibXML::XML::Reader::TYPE_DOCUMENT puts “DOCUMENT” when LibXML::XML::Reader::TYPE_ELEMENT attribute_count = @reader.attribute_count puts “ELEMENT #{ @reader.local_name }, ns: #{ @reader.namespace_uri }, #attributes: #{ attribute_count }, depth: #{ @reader.depth }” puts “ FAKING END ELEMENT” if @faking_an_end_element when LibXML::XML::Reader::TYPE_END_ELEMENT puts “END ELEMENT” when LibXML::XML::Reader::TYPE_TEXT puts “TEXT [[#{ @reader.read_string }]]” when LibXML::XML::Reader::TYPE_CDATA puts “CDATA [[#{ @reader.read_string }]]” when LibXML::XML::Reader::TYPE_SIGNIFICANT_WHITESPACE puts “SIGNIFICANT white space [[#{ @reader.read_string }]]” when LibXML::XML::Reader::TYPE_ENTITY_REFERENCE puts “entity ref” when LibXML::XML::Reader::TYPE_WHITESPACE puts “whitespace” when LibXML::XML::Reader::TYPE_PROCESSING_INSTRUCTION puts “processing instruction” when LibXML::XML::Reader::TYPE_COMMENT puts “comment” when LibXML::XML::Reader::TYPE_DOCUMENT_TYPE puts “doc type”.
- #parse(filename, tokenise_content = true, is_realising = false) ⇒ Object
- #parse_element(parent = nil, target = nil) ⇒ Object
- #parse_string(string, tokenise_content = true, is_realising = false, target = nil) ⇒ Object
- #realise_string(string, tokenise_content = true, target = nil) ⇒ Object
- #resolve(name) ⇒ Object
- #setup_parse(filename, tokenise_content = true, is_realising = false) ⇒ Object
- #setup_parse_string(string, tokenise_content = true, is_realising = false) ⇒ Object
- #start_element? ⇒ Boolean
- #whitespace? ⇒ Boolean
Constructor Details
#initialize(recovering = false) ⇒ FromXML
Returns a new instance of FromXML.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/xamplr/from-xml.rb', line 16 def initialize(recovering=false) @recovering = recovering @attribute_name = Array.new(32) @attribute_namespace = Array.new(32) @attribute_value = Array.new(32) @insert_end_element = false @faking_an_end_element = false @just_opened_an_element = false end |
Instance Attribute Details
#checkWellFormed ⇒ Object (readonly)
Returns the value of attribute checkWellFormed.
7 8 9 |
# File 'lib/xamplr/from-xml.rb', line 7 def checkWellFormed @checkWellFormed end |
#is_realising ⇒ Object (readonly)
Returns the value of attribute is_realising.
8 9 10 |
# File 'lib/xamplr/from-xml.rb', line 8 def is_realising @is_realising end |
#tokenise_content ⇒ Object (readonly)
Returns the value of attribute tokenise_content.
9 10 11 |
# File 'lib/xamplr/from-xml.rb', line 9 def tokenise_content @tokenise_content end |
Class Method Details
.register(tag, ns_tag, klass) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/xamplr/from-xml.rb', line 33 def FromXML.register(tag, ns_tag, klass) @@by_ns_tag[ns_tag] = [ klass ] a = @@by_tag[tag] if (nil == a) then @@by_tag[tag] = [ klass ] else found = false a.each { | thing | found = found | (thing == klass) } a << klass unless found end end |
.registered(name) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/xamplr/from-xml.rb', line 45 def FromXML.registered(name) klass = @@by_ns_tag[name] klass = @@by_tag[name] unless klass klass = [] unless klass return klass end |
.reset_registry ⇒ Object
28 29 30 31 |
# File 'lib/xamplr/from-xml.rb', line 28 def FromXML.reset_registry @@by_tag = {} @@by_ns_tag = {} end |
.tokenise_string(str, strip = true) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/xamplr/from-xml.rb', line 116 def FromXML.tokenise_string(str, strip=true) return nil unless str str.strip! if strip str.gsub!(/[ \n\r\t][ \n\r\t]*/, " ") return str end |
Instance Method Details
#attributeCount ⇒ Object
410 411 412 |
# File 'lib/xamplr/from-xml.rb', line 410 def attributeCount return @attribute_name.length end |
#attributeName(i) ⇒ Object
414 415 416 |
# File 'lib/xamplr/from-xml.rb', line 414 def attributeName(i) return @attribute_name[i] end |
#attributeNamespace(i) ⇒ Object
418 419 420 |
# File 'lib/xamplr/from-xml.rb', line 418 def attributeNamespace(i) return @attribute_namespace[i] end |
#attributeValue(i) ⇒ Object
422 423 424 |
# File 'lib/xamplr/from-xml.rb', line 422 def attributeValue(i) return @attribute_value[i] end |
#build_attribute_arrays ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/xamplr/from-xml.rb', line 384 def build_attribute_arrays @attribute_name.clear @attribute_namespace.clear @attribute_value.clear return unless LibXML::XML::Reader::TYPE_ELEMENT == current_node_type if @reader.has_attributes? then attribute_count = @reader.attribute_count @reader.move_to_first_attribute attribute_count.times do | i | if @reader.namespace_declaration? then @reader.move_to_next_attribute next end @attribute_name << @reader.local_name @attribute_namespace << @reader.namespace_uri @attribute_value << @reader.value @reader.move_to_next_attribute end end end |
#column ⇒ Object
434 435 436 |
# File 'lib/xamplr/from-xml.rb', line 434 def column return @reader.column_number end |
#current_node_type ⇒ Object
291 292 293 294 295 296 297 |
# File 'lib/xamplr/from-xml.rb', line 291 def current_node_type if @faking_an_end_element then LibXML::XML::Reader::TYPE_END_ELEMENT else @reader.node_type end end |
#depth ⇒ Object
426 427 428 |
# File 'lib/xamplr/from-xml.rb', line 426 def depth return @reader.depth end |
#find_the_first_element ⇒ Object
376 377 378 379 380 381 382 |
# File 'lib/xamplr/from-xml.rb', line 376 def find_the_first_element while true do break if start_element? break unless next_reader_event end @just_opened_an_element = start_element? end |
#line ⇒ Object
430 431 432 |
# File 'lib/xamplr/from-xml.rb', line 430 def line return @reader.line_number end |
#next_reader_event ⇒ Object
def describe_current_element_type()
case @reader.node_type
when LibXML::XML::Reader::TYPE_ATTRIBUTE
puts "ATTRIBUTE"
when LibXML::XML::Reader::TYPE_DOCUMENT
puts "DOCUMENT"
when LibXML::XML::Reader::TYPE_ELEMENT
attribute_count = @reader.attribute_count
puts "ELEMENT #{ @reader.local_name }, ns: #{ @reader.namespace_uri }, #attributes: #{ attribute_count }, depth: #{ @reader.depth }"
puts " FAKING END ELEMENT" if @faking_an_end_element
when LibXML::XML::Reader::TYPE_END_ELEMENT
puts "END ELEMENT"
when LibXML::XML::Reader::TYPE_TEXT
puts "TEXT [[#{ @reader.read_string }]]"
when LibXML::XML::Reader::TYPE_CDATA
puts "CDATA [[#{ @reader.read_string }]]"
when LibXML::XML::Reader::TYPE_SIGNIFICANT_WHITESPACE
puts "SIGNIFICANT white space [[#{ @reader.read_string }]]"
when LibXML::XML::Reader::TYPE_ENTITY_REFERENCE
puts "entity ref"
when LibXML::XML::Reader::TYPE_WHITESPACE
puts "whitespace"
when LibXML::XML::Reader::TYPE_PROCESSING_INSTRUCTION
puts "processing instruction"
when LibXML::XML::Reader::TYPE_COMMENT
puts "comment"
when LibXML::XML::Reader::TYPE_DOCUMENT_TYPE
puts "doc type"
when LibXML::XML::Reader::TYPE_XML_DECLARATION
puts "xml decl"
when LibXML::XML::Reader::TYPE_NONE
puts "NONE!!"
when LibXML::XML::Reader::TYPE_NOTATION
puts "notifiation"
when LibXML::XML::Reader::TYPE_DOCUMENT_FRAGMENT
puts "doc fragment"
when LibXML::XML::Reader::TYPE_ENTITY
puts "entity"
when LibXML::XML::Reader::TYPE_END_ENTITY
puts "end entity"
else
puts "UNKNOWN: #{@reader.node_type}"
end
end
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/xamplr/from-xml.rb', line 347 def next_reader_event if @insert_end_element then @faking_an_end_element = true @insert_end_element = false return end @faking_an_end_element = false #describe_current_element_type okay = @reader.read @just_opened_an_element = start_element? @insert_end_element = (@just_opened_an_element and @reader.empty_element?) #describe_current_element_type okay end |
#parse(filename, tokenise_content = true, is_realising = false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/xamplr/from-xml.rb', line 88 def parse(filename, tokenise_content=true, is_realising=false) begin setup_parse(filename, tokenise_content, is_realising) element, ignore = parse_element return element rescue Exception => e puts "trouble parsing file: '#{filename}'" puts "Exception: #{e}" raise end end |
#parse_element(parent = nil, target = nil) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/xamplr/from-xml.rb', line 123 def parse_element(parent=nil, target=nil) find_the_first_element return unless start_element? namespace = @reader.namespace_uri name = @reader.local_name existing_element = nil element = nil requires_caching = false build_attribute_arrays if ((nil != namespace) and (0 < namespace.size)) then klass_name = "{#{namespace}}#{name}" klasses = FromXML.registered(klass_name) if (0 == klasses.size) then xml_text = XMLText.new xml_text.build(self) xml_text = parent.note_adding_text_content(xml_text, @is_realising) parent.add_content(xml_text, @tokenise_content) if xml_text return xml_text, false end if (1 < klasses.size) then raise XamplException.new("there is more than one '#{name}' tag in namespace '#{namespace}'\nplease report this error") end else klasses = FromXML.registered(name) if (0 == klasses.size) then raise XamplException.new("do not recognise tag '#{name}' (no namespace specified)") end if (1 < klasses.size) then raise XamplException.new("there is more than one '#{name}' tag (no namespace specified)") end end unless @is_realising then @attribute_value.size.times do |i| FromXML.tokenise_string @attribute_value[i] end end if target then element = target target.load_needed = false target = nil element.init_attributes(@attribute_name, @attribute_namespace, @attribute_value) element.note_attributes_initialised(@is_realising) else if klasses[0].persisted? then @attribute_name.each_index do |i| if @attribute_name[i] == klasses[0].persisted?.to_s then existing_element = Xampl.find_known(klasses[0], @attribute_value[i]) if existing_element then # so we've found the element. Now what??? We can do several # reasonable things: # # 1) continue parsing into the found element # 2) simply return the found element # 3) replace the found element with the new element # # The first one is dubious, so we won't. # The second and third option both make complete sense # # We are going to do the second # # BTW, 'existing element' means a representation of this element already in memory # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # puts "FOUND AN EXISTING THING... #{ klasses[0] } #{ @attribute_value[i] }" # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # caller(0).each { | trace | puts " #{trace}"} # existing_element.reset_contents # element = existing_element # existing_element = nil # puts "#{File.basename(__FILE__)} #{__LINE__} EXISTING ELEMENT: #{ existing_element }" # puts "#{File.basename(__FILE__)} #{__LINE__} WOW, must handle the existing element correctly" element = existing_element #TODO -- IS THIS RIGHT???????????????????????? end unless element then element = klasses[0].new requires_caching = @recovering # puts "#{File.basename(__FILE__)} #{__LINE__} WOW, what about recovering????" #TODO -- IS THIS RIGHT???????????????????????? requires_caching = true #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! unless @recovering then element.force_load if parent end element.note_created(@is_realising) end break end end end unless element then element = klasses[0].new element.note_created(@is_realising) end element.note_initialise_attributes_with(@attribute_name, @attribute_namespace, @attribute_value, @is_realising) element.init_attributes(@attribute_name, @attribute_namespace, @attribute_value) element.note_attributes_initialised(@is_realising) if requires_caching and element and element.persist_required then Xampl.cache(element) end #element = element.note_add_to_parent(parent, @is_realising) #element.append_to(parent) if parent end while next_reader_event case current_node_type =begin TODO -- can these ever happen? when START_DOCUMENT return element if @recovering return existing_element || element when END_DOCUMENT return element if @recovering return existing_element || element =end when LibXML::XML::Reader::TYPE_ELEMENT child, ignore_child = parse_element(element) unless ignore_child then case child when XamplObject then child = child.note_add_to_parent(element, @is_realising) if child child = element.note_add_child(child, @is_realising) if element child.append_to(element) if element and child when XMLText then puts "UNRECOGNISED Well-formed XML: #{child.to_s[0..25]}..." else puts "WHAT IS THIS??? #{child.class.name}" end end when LibXML::XML::Reader::TYPE_END_ELEMENT element = element.note_closed(@is_realising) return element if @recovering return existing_element || element when LibXML::XML::Reader::TYPE_TEXT, LibXML::XML::Reader::TYPE_CDATA, LibXML::XML::Reader::TYPE_SIGNIFICANT_WHITESPACE, LibXML::XML::Reader::TYPE_ENTITY_REFERENCE if element.has_mixed_content then text = @reader.read_string the_text = element.note_adding_text_content(text, @is_realising) element << the_text else text = @reader.read_string the_text = element.note_adding_text_content(text, @is_realising) element.add_content(the_text, false) end else end end return element if @recovering return existing_element || element end |
#parse_string(string, tokenise_content = true, is_realising = false, target = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/xamplr/from-xml.rb', line 104 def parse_string(string, tokenise_content=true, is_realising=false, target=nil) begin setup_parse_string(string, tokenise_content, is_realising) element, ignore = parse_element(nil, target) return element rescue Exception => e puts "trouble parsing string: '#{string}'" puts "Exception: #{e}" raise end end |
#realise_string(string, tokenise_content = true, target = nil) ⇒ Object
100 101 102 |
# File 'lib/xamplr/from-xml.rb', line 100 def realise_string(string, tokenise_content=true, target=nil) return parse_string(string, tokenise_content, true, target) end |
#resolve(name) ⇒ Object
52 53 54 55 |
# File 'lib/xamplr/from-xml.rb', line 52 def resolve(name) #TODO -- ??? return name end |
#setup_parse(filename, tokenise_content = true, is_realising = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/xamplr/from-xml.rb', line 57 def setup_parse(filename, tokenise_content=true, is_realising=false) @resolver = self @is_realising = is_realising @tokenise_content = tokenise_content @reader = LibXML::XML::Reader.file(filename, :options => LibXML::XML::Parser::Options::NOENT | LibXML::XML::Parser::Options::NONET | LibXML::XML::Parser::Options::NOCDATA | LibXML::XML::Parser::Options::DTDATTR | LibXML::XML::Parser::Options::COMPACT) #TODO CLOSE THIS THING!! end |
#setup_parse_string(string, tokenise_content = true, is_realising = false) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/xamplr/from-xml.rb', line 72 def setup_parse_string(string, tokenise_content=true, is_realising=false) @resolver = self @is_realising = is_realising @tokenise_content = tokenise_content # setInput(string) @reader = LibXML::XML::Reader.string(string, :options => LibXML::XML::Parser::Options::NOENT | LibXML::XML::Parser::Options::NONET | LibXML::XML::Parser::Options::NOCDATA | LibXML::XML::Parser::Options::DTDATTR | LibXML::XML::Parser::Options::COMPACT) #TODO CLOSE THIS THING!! end |
#start_element? ⇒ Boolean
368 369 370 |
# File 'lib/xamplr/from-xml.rb', line 368 def start_element? current_node_type == LibXML::XML::Reader::TYPE_ELEMENT end |
#whitespace? ⇒ Boolean
372 373 374 |
# File 'lib/xamplr/from-xml.rb', line 372 def whitespace? current_note_type == LibXML::XML::Reader::TYPE_WHITESPACE end |