indexing description: "XML document extracting tools" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_XML_DOCUMENT_CREATOR feature {NONE} -- Implementation document_from_element (an_element: XM_ELEMENT): P2P_XML_CACHE is -- Extract a document from an XML tree with root `an_element'. -- Return Void if creation of an object was not successful. require Root_valid: an_element /= Void local attr: XM_ATTRIBUTE children: DS_LIST [XM_ELEMENT] child: XM_ELEMENT do -- detect type by element's name Result := create_specific_type (an_element.name, an_element) if Result = Void then -- detect type by element's attribute `type' attr := an_element.attribute_by_name ("type") if attr /= Void then Result := create_specific_type (strip_namespace (attr.value), an_element) end end children := an_element.elements if Result = Void and children.count = 1 then -- detect type by element's single child name child := children.first Result := create_specific_type (child.name, child) if Result = Void then -- detect type by element's single child attribute `type' attr := child.attribute_by_name ("type") if attr /= Void then Result := create_specific_type (strip_namespace (attr.value), child) end end end if Result = Void then -- create a general xml document create {P2P_UNKNOWN_XML_DOCUMENT} Result.make_from_element (an_element) end -- check for valid document if not Result.is_valid then Result := Void end end advertisement_from_element (an_element: XM_ELEMENT): P2P_ADVERTISEMENT is -- Try to extract a document from an XML tree with root `an_element'. -- Return either Void when not a valid advertisement, an unknown advertisement when -- de type is unknown, or a real advertisement on successful advertisement detection. require Root_valid: an_element /= Void local document: P2P_XML_CACHE unknown: P2P_UNKNOWN_XML_DOCUMENT do document := document_from_element (an_element) if document /= Void then Result ?= document if Result = Void then unknown ?= document if unknown /= Void then create {P2P_UNKNOWN_ADVERTISEMENT} Result.make_from_document (unknown) end end end end Rootname_peer: STRING is "PA" Rootname_group: STRING is "PGA" Rootname_module_class: STRING is "MCA" Rootname_module_specification: STRING is "MSA" Rootname_module_implementation: STRING is "MIA" Rootname_pipe: STRING is "PipeAdvertisement" Rootname_transport_tcp: STRING is "TCPTransportAdvertisement" Rootname_platform_config: STRING is "CP" Rootname_route: STRING is "RA" Rootname_accesspoint: STRING is "APA" Rootname_rendezvous: STRING is "RdvAdvertisement" Rootname_rendezvous_propagate: STRING is "RendezVousPropagateMessage" Rootname_rendezvous_configuration: STRING is "RdvConfig" create_specific_type (a_name: STRING; an_element: XM_ELEMENT): P2P_XML_CACHE is -- Detect specific type from `a_name' and create the corresponding document with root `an_element' require Name_valid: a_name /= Void Root_element_valid: an_element /= Void do -- advertisements if Rootname_peer.is_equal (a_name) then create {P2P_PEER_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_group.is_equal (a_name) then create {P2P_PEERGROUP_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_module_class.is_equal (a_name) then create {P2P_MODULE_CLASS_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_module_specification.is_equal (a_name) then create {P2P_MODULE_SPECIFICATION_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_module_implementation.is_equal (a_name) then create {P2P_MODULE_IMPLEMENTATION_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_pipe.is_equal (a_name) then create {P2P_PIPE_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_transport_tcp.is_equal (a_name) then create {P2P_TCP_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_route.is_equal (a_name) then create {P2P_ROUTE_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_accesspoint.is_equal (a_name) then create {P2P_ACCESSPOINT_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) elseif Rootname_rendezvous.is_equal (a_name) then create {P2P_RENDEZVOUS_ADVERTISEMENT} Result.make_from_element (an_element.deep_twin) -- other xml documents elseif Rootname_platform_config.is_equal (a_name) then create {P2P_CONFIGURATION} Result.make_from_element (an_element.deep_twin) elseif Rootname_rendezvous_propagate.is_equal (a_name) then create {P2P_RENDEZVOUS_PROPAGATE} Result.make_from_element (an_element.deep_twin) elseif Rootname_rendezvous_configuration.is_equal (a_name) then create {P2P_RENDEZVOUS_CONFIGURATION} Result.make_from_element (an_element.deep_twin) end end strip_namespace (a_value: STRING): STRING is -- Return a string equal to `a_value', but without namespace require Value_valid: a_value /= Void do Result := a_value.twin if Result.substring (1, 5).is_equal ("jxta:") then Result.remove_head (5) end ensure Result_set: Result /= Void and Result /= a_value end end