indexing description: "Represents an XML document with caching" license: "MIT license (see ../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" deferred class P2P_XML_CACHE inherit P2P_DOCUMENT P2P_STRING_UTILS redefine out end feature {NONE} -- Initialization parse_from_string (source: STRING) is -- Parse document from string do initialize create cached_document.parse_from_string (source) is_valid := cached_document.is_valid if is_valid then cached_document.handle_root_attributes (agent attribute_handler) if is_valid then cached_document.handle_elements (agent element_handler) validate end if not is_valid then clear_document end else clear_document end end make_from_element (source: XM_ELEMENT) is -- Create document from xml element do initialize create cached_document.make_from_root_element (source) is_valid := True cached_document.handle_root_attributes (agent attribute_handler) if is_valid then cached_document.handle_elements (agent element_handler) validate end if not is_valid then clear_document end end feature -- Access match (an_element_name, an_element_value: STRING): BOOLEAN is -- Is given elements value equal to `an_element_value'? require Element_name_valid: an_element_name /= Void deferred end feature -- Element change renew_document, clear_document is -- Clear document cache do cached_document := Void end feature -- Output out: STRING is -- String representation do Result := document.out end document: P2P_XML_DOCUMENT is -- Create XML document require Advertisement_valid: is_valid do if cached_document = Void then create cached_document.make_with_jxta_root (root_element_name) end Result := cached_document ensure Result_set: Result /= Void end feature {NONE} -- Implementation root_element_name: STRING is -- Root element name deferred end attribute_handler (root_attribute: XM_ATTRIBUTE) is -- Handle a root element attribute of a parsed document require Attribute_existent: root_attribute /= Void deferred end element_handler (child_element: XM_ELEMENT) is -- Handle a root child element of a parsed document require Element_existent: child_element /= Void deferred end validate is -- Validate document and set `is_valid' do -- The implementation here currently doesn't add any restrictions end cached_document: P2P_XML_DOCUMENT element_text (an_element: XM_ELEMENT): STRING is -- Concatenation of elements children text require Element_valid: an_element /= Void do Result := trimmed (an_element.text) if Result = Void then create Result.make_empty end ensure Result_set: Result /= Void end does_element_match (actual: ANY; other: STRING): BOOLEAN is -- If `other' non-void: does `actual' match `other' (allowing wildchars)? -- Else is `actual' void? local actout: STRING do if other /= Void then if actual /= Void then actout := actual.out -- compare both strings, allow wildchar * (at the beginning or the end of `other') if other.is_empty then -- both empty strings? Result := actout.is_empty elseif other.count = 1 and other.item (1).is_equal ('*') then -- `other' matches every string Result := True elseif other.item (1).is_equal ('*') and other.item (other.count).is_equal ('*') then -- does `actual' match *query*? Result := actout.has_substring (other.substring (2, other.count - 1)) elseif other.item (1).is_equal ('*') then -- does `actual' match *query? Result := (actout.count >= other.count - 1) and actout.substring (actout.count - other.count + 2, actout.count).is_equal (other.substring (2, other.count)) elseif other.item (other.count).is_equal ('*') then -- does `actual' match query*? Result := (actout.count >= other.count - 1) and actout.substring (1, other.count - 1).is_equal (other.substring (1, other.count - 1)) else -- are `actual' and `other' equal? Result := actout.is_equal (other) end else -- `other' doesn't match unexistent `actual' Result := False end else -- just check whether `actual' is existent or not Result := actual /= Void end end initialize is -- Initialize object's data structures do end end