indexing description: "Represents a platform configuration document" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_CONFIGURATION inherit P2P_XML_CACHE redefine initialize, document, validate end P2P_CREATORS_SHARED undefine out end create make_with_id, make_from_element, parse_from_string feature {NONE} -- Initialization make_with_id (a_peer_id: like peer_id) is -- Create new peer advertisement require Pid_valid: a_peer_id /= Void do initialize set_peer_id (a_peer_id) is_valid := True ensure Pid_set: peer_id.is_equal (a_peer_id) end feature -- Access peer_id: P2P_PEER_ID name: STRING description: STRING service_parameters: LINKED_LIST [TUPLE [P2P_MODULE_CLASS_ID, P2P_XML_CACHE]] has_service_parameter (a_module_class_id: P2P_MODULE_CLASS_ID): BOOLEAN is -- Does service parameter `a_module_class_id' exist? require Id_existent: a_module_class_id /= Void do goto_service_parameter (a_module_class_id) Result := not service_parameters.after end service_parameter (a_module_class_id: P2P_MODULE_CLASS_ID): P2P_XML_CACHE is -- First Parameter advertisement for service with given class id require Id_existent: a_module_class_id /= Void do goto_service_parameter (a_module_class_id) if not service_parameters.after then Result ?= service_parameters.item @ 2 end ensure Result_set: has_service_parameter (a_module_class_id) implies Result /= Void end is_reconfigured: BOOLEAN match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given element's value equal with `an_element_value' or is element present? do if element_peer_id.is_equal (an_element_name) then Result := does_element_match (peer_id, an_element_value) elseif element_name.is_equal (an_element_name) then Result := does_element_match (name, an_element_value) elseif element_description.is_equal (an_element_name) then Result := does_element_match (description, an_element_value) end end feature -- Element change set_peer_id (a_peer_id: like peer_id) is -- Set new peer id require Pid_valid: a_peer_id /= Void do peer_id := a_peer_id renew_document is_reconfigured := True ensure Pid_set: peer_id.is_equal (a_peer_id) Reconfigured: is_reconfigured end set_name (a_name: like name) is -- Set new name require Name_valid: a_name /= Void do name := a_name renew_document is_reconfigured := True ensure Name_set: name.is_equal (a_name) Reconfigured: is_reconfigured end set_description (a_description: like description) is -- Set new description require Description_valid: a_description /= Void do description := a_description renew_document is_reconfigured := True ensure Description_set: description.is_equal (a_description) Reconfigured: is_reconfigured end add_service_parameter (a_module_class_id: P2P_MODULE_CLASS_ID; a_parameter: P2P_XML_CACHE) is -- Add new service parameter for given module class require Mcid_valid: a_module_class_id /= Void Parameter_valid: a_parameter /= Void do service_parameters.extend ([a_module_class_id, a_parameter]) renew_document is_reconfigured := True ensure Parameter_set: service_parameters.has ([a_module_class_id, a_parameter]) Reconfigured: is_reconfigured end replace_service_parameter (a_module_class_id: P2P_MODULE_CLASS_ID; a_parameter: P2P_XML_CACHE) is -- Make sure `a_module_class_id' parameter has `a_parameter' attached require Mcid_valid: a_module_class_id /= Void Parameter_valid: a_parameter /= Void do goto_service_parameter (a_module_class_id) if not service_parameters.after then service_parameters.replace ([a_module_class_id, a_parameter]) else service_parameters.extend ([a_module_class_id, a_parameter]) end renew_document is_reconfigured := True ensure Parameter_set: service_parameters.has ([a_module_class_id, a_parameter]) Reconfigured: is_reconfigured end make_reconfigured is -- Mark advertisement as reconfigured do is_reconfigured := True ensure Reconfigured: is_reconfigured end feature -- Removal remove_service_parameter (a_module_class_id: P2P_MODULE_CLASS_ID) is -- Remove first found service parameter of given module class do goto_service_parameter (a_module_class_id) if not service_parameters.after then service_parameters.remove renew_document is_reconfigured := True end end feature -- Output document: P2P_XML_DOCUMENT is -- Create XML document local el_svc, el_parm_root: XM_ELEMENT mcid: P2P_MODULE_CLASS_ID parm: P2P_XML_CACHE parm_adv: P2P_ADVERTISEMENT cached: BOOLEAN do cached := cached_document /= Void -- create document Result := Precursor if not cached then Result.document.root_element.add_unqualified_attribute (attribute_type_name, attribute_type_value) -- element PID Result.create_root_child_element (element_peer_id, Result.namespace_empty) Result.create_content (Result.last_element, peer_id.out) -- element Name if name /= Void then Result.create_root_child_element (element_name, Result.namespace_empty) Result.create_content (Result.last_element, name) end -- element Desc if description /= Void then Result.create_root_child_element (element_description, Result.namespace_empty) Result.create_content (Result.last_element, description) end -- elements Svc from service_parameters.start until service_parameters.after loop mcid ?= service_parameters.item @ 1 parm ?= service_parameters.item @ 2 if mcid /= Void and parm /= Void then Result.create_root_child_element (element_service_parameters, Result.namespace_empty) el_svc := Result.last_element Result.create_child_element (el_svc, element_service_parameter_mcid, Result.namespace_empty) Result.create_content (Result.last_element, mcid.out) parm_adv ?= parm -- paste document's root element into Parm element: use a copy, because the elements are double linked if parm_adv /= Void then -- parameter is an advertisement Result.create_child_element (el_svc, element_service_parameter_parm, Result.namespace_empty) Result.add_child_element (Result.last_element, parm.document.document.root_element.deep_twin) else -- parameter is an arbitrary xml document el_parm_root := parm.document.document.root_element.deep_twin el_parm_root.set_name (element_service_parameter_parm) el_parm_root.set_namespace (Result.namespace_empty) Result.add_child_element (el_svc, el_parm_root) end end service_parameters.forth end end end feature {NONE} -- Implementation Element_peer_id: STRING is "PID" Element_name: STRING is "Name" Element_description: STRING is "Desc" Element_service_parameters: STRING is "Svc" Element_service_parameter_mcid: STRING is "MCID" Element_service_parameter_parm: STRING is "Parm" Type_peer_id: STRING is "jxta:JXTAID" Type_name: STRING is "xs:string" Type_description: STRING is "xs:anyType" Type_service_parameters: STRING is "jxta:serviceParams" Type_service_parameter_mcid: STRING is "jxta:JXTAID" Type_service_parameter_parm: STRING is "xs:anyType" Attribute_type_name: STRING is "type" Attribute_type_value: STRING is "jxta:PlatformConfig" root_element_name: STRING is "CP" attribute_handler (root_attribute: XM_ATTRIBUTE) -- No root attributes do end element_handler (element: XM_ELEMENT) is -- Handle root child element of parsed advertisement local el_mcid, el_parm: XM_ELEMENT parm: P2P_XML_CACHE mcid: P2P_MODULE_CLASS_ID do if element_peer_id.is_equal (element.name) then create peer_id.make_from_urn (element_text (element)) if not peer_id.is_valid then peer_id := Void end elseif element_name.is_equal (element.name) then name := trimmed (element.text) elseif element_description.is_equal (element.name) then description := (element.text) elseif element_service_parameters.is_equal (element.name) then el_mcid := element.element_by_name (element_service_parameter_mcid) el_parm := element.element_by_name (element_service_parameter_parm) if el_mcid /= Void and el_parm /= Void then create mcid.make_from_urn (element_text (el_mcid)) -- try to extract document from element: use copy, because elements are double linked parm := xml_document_creator.document_from_element (el_parm.deep_twin) if parm /= Void and parm.is_valid then service_parameters.extend ([mcid, parm]) end end end end validate is -- Validate advertisement and set `is_valid' do is_valid := is_valid and peer_id /= Void end goto_service_parameter (mcid: P2P_MODULE_CLASS_ID) is -- Goto service parameter with given module class id require Id_valid: mcid /= Void local current_id: P2P_MODULE_CLASS_ID found: BOOLEAN do from service_parameters.start until found or service_parameters.after loop current_id ?= service_parameters.item @ 1 if current_id /= Void and mcid.is_equal (current_id) then found := True else service_parameters.forth end end end initialize is -- Initialize object's data structure do Precursor create service_parameters.make service_parameters.compare_objects end invariant Parameters_existent: service_parameters /= Void end