lib/openwfe/rest/xmlcodec.rb in openwferu-0.9.10.653 vs lib/openwfe/rest/xmlcodec.rb in openwferu-0.9.11

- old
+ new

@@ -1,8 +1,8 @@ # #-- -# Copyright (c) 2005-2007, John Mettraux, org +# Copyright (c) 2005-2007, John Mettraux, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # @@ -49,17 +49,17 @@ module OpenWFE # # Ugly XML codec for OpenWFE workitems # - class XmlCodec + module XmlCodec # # Takes as input some XML element and returns is decoded # (as an instance) # - def decode (xmlElt) + def XmlCodec.decode (xmlElt) return nil unless xmlElt if xmlElt.kind_of? String @@ -71,24 +71,23 @@ xmlElt = xmlElt.root end #puts "decode() xmlElt.name is >#{xmlElt.name}<" + return decode_session_id(xmlElt) if xmlElt.name == 'session' return decode_list(xmlElt) if xmlElt.name == STORES return decode_store(xmlElt) if xmlElt.name == STORE return decode_list(xmlElt) if xmlElt.name == HEADERS return decode_header(xmlElt) if xmlElt.name == HEADER return decode_launch_ok(xmlElt) if xmlElt.name == OK return decode_list(xmlElt) if xmlElt.name == HISTORY return decode_historyitem(xmlElt) if xmlElt.name == HISTORY_ITEM - return decode_list(xmlElt) \ - if xmlElt.name == FLOW_EXPRESSION_IDS - return decode_fei(xmlElt) \ - if xmlElt.name == FLOW_EXPRESSION_ID + return decode_list(xmlElt) if xmlElt.name == FLOW_EXPRESSION_IDS + return decode_fei(xmlElt) if xmlElt.name == FLOW_EXPRESSION_ID return decode_inflowworkitem(xmlElt) \ if xmlElt.name == IN_FLOW_WORKITEM return decode_launchitem(xmlElt) \ if xmlElt.name == LAUNCHITEM @@ -112,11 +111,11 @@ end # # Takes some OpenWFE Ruby instance and returns it as XML # - def encode (owfeData) + def XmlCodec.encode (owfeData) #puts "encode() #{owfeData.inspect}" return encode_launchitem(owfeData) \ if owfeData.kind_of? LaunchItem @@ -130,74 +129,91 @@ raise \ ArgumentError, \ "Cannot encode : "+owfeData.inspect() end + def XmlCodec.encode_workitem_as_header (in_flow_workitem, locked) + + e = REXML::Element.new HEADER + + e.add_attribute A_LAST_MODIFIED, "#{in_flow_workitem.last_modified}" + e.add_attribute A_LOCKED, locked + + e << XmlCodec::encode_fei(in_flow_workitem.fei) + e << XmlCodec::encode_attributes(in_flow_workitem) + + e + end + private # # DECODE # - def decode_list (xmlElt) + def XmlCodec.decode_session_id (xmlElt) + Integer(xmlElt.attributes['id']) + end + + def XmlCodec.decode_list (xmlElt) xmlElt.elements.collect { |elt| decode(elt) } end - def decode_launchable (xmlElt) + def XmlCodec.decode_launchable (xmlElt) launchable = Launchable.new() launchable.url = xmlElt.attributes[URL] launchable.engine_id = xmlElt.attributes[ENGINE_ID] launchable end - def decode_expression (xmlElt) + def XmlCodec.decode_expression (xmlElt) exp = Expression.new() #exp.id = decode(OpenWFE.first_element(xmlElt, "//"+FLOW_EXPRESSION_ID)) exp.id = decode(OpenWFE.first_element(xmlElt)) exp.apply_time = xmlElt.attributes[APPLY_TIME] exp.state = xmlElt.attributes[STATE] exp.state_since = xmlElt.attributes[STATE_SINCE] - return exp + exp end - def decode_store (xmlElt) + def XmlCodec.decode_store (xmlElt) store = Store.new() store.name = xmlElt.attributes[NAME] store.workitem_count = xmlElt.attributes[WORKITEM_COUNT] store.workitem_count = Integer(store.workitem_count) store.permissions = xmlElt.attributes[PERMISSIONS] - return store + store end - def decode_header (xmlElt) + def XmlCodec.decode_header (xmlElt) header = Header.new() header.last_modified = xmlElt.attributes[A_LAST_MODIFIED] header.locked = parse_boolean(xmlElt.attributes[A_LOCKED]) header.flow_expression_id = decode(OpenWFE.first_element(xmlElt, FLOW_EXPRESSION_ID)) header.attributes = decode(OpenWFE.first_element(xmlElt, ATTRIBUTES)) - return header + header end - def decode_fei (xmlElt) + def XmlCodec.decode_fei (xmlElt) fei = FlowExpressionId.new fei.owfe_version = xmlElt.attributes[OWFE_VERSION] fei.engine_id = xmlElt.attributes[ENGINE_ID] @@ -217,11 +233,11 @@ fei end - def decode_attribute (xmlElt) + def XmlCodec.decode_attribute (xmlElt) #puts "decodeAttribute() '#{xmlElt.name}' --> '#{xmlElt.text}'" # # atomic types @@ -271,17 +287,17 @@ ArgumentError, \ "Cannot decode <#{xmlElt.name}/> in \n"+\ OpenWFE.xmldoc_to_string(xmlElt.document()) end - def decode_xmldocument (xmlElt) + def XmlCodec.decode_xmldocument (xmlElt) s = Base64::decode64 xmlElt.text.strip REXML::Document.new s end - def decode_entry (xmlElt, map) + def XmlCodec.decode_entry (xmlElt, map) key = xmlElt.elements[1] val = xmlElt.elements[2] # @@ -299,16 +315,16 @@ #puts "decodeEntry() subject '#{val}'" if key == '__subject__' map[key] = val end - def parse_boolean (string) + def XmlCodec.parse_boolean (string) - return string.strip.downcase == 'true' + string.strip.downcase == 'true' end - def decode_historyitem (xmlElt) + def XmlCodec.decode_historyitem (xmlElt) hi = HistoryItem.new hi.author = xmlElt.attributes[A_AUTHOR] hi.date = xmlElt.attributes[A_DATE] @@ -318,25 +334,25 @@ hi.wfd_name = xmlElt.attributes[WORKFLOW_DEFINITION_NAME] hi.wfd_revision = xmlElt.attributes[WORKFLOW_DEFINITION_REVISION] hi.wf_instance_id = xmlElt.attributes[WORKFLOW_INSTANCE_ID] hi.expression_id = xmlElt.attributes[EXPRESSION_ID] - return hi + hi end - def decode_launch_ok (xmlElt) + def XmlCodec.decode_launch_ok (xmlElt) sFei = xmlElt.attributes[A_FLOW_ID] - return true if not sFei + return true unless sFei FlowExpressionId.to_fei(sFei) end - def decode_inflowworkitem (xmlElt) + def XmlCodec.decode_inflowworkitem (xmlElt) wi = InFlowWorkItem.new() wi.last_modified = xmlElt.attributes[A_LAST_MODIFIED] wi.attributes = decode(OpenWFE.first_element(xmlElt, ATTRIBUTES)) @@ -348,18 +364,17 @@ # TODO : decode filter wi.history = decode(OpenWFE.first_element(xmlElt, HISTORY)) - return wi + wi end + def XmlCodec.decode_launchitem (xmlElt) - def decode_launchitem (xmlElt) + li = LaunchItem.new - li = LaunchItem.new() - li.workflow_definition_url = xmlElt.attributes[WORKFLOW_DEFINITION_URL] li.attributes = decode(OpenWFE.first_element(xmlElt, ATTRIBUTES)) @@ -371,23 +386,28 @@ # # ENCODE # - def encode_item (item, elt) + def XmlCodec.encode_item (item, elt) elt.attributes[A_LAST_MODIFIED] = item.last_modified + elt << encode_attributes(item) + end + + def XmlCodec.encode_attributes (item) + eAttributes = REXML::Element.new(ATTRIBUTES) eAttributes << encode_attribute(item.attributes) - elt << eAttributes + eAttributes end - def encode_launchitem (launchitem) + def XmlCodec.encode_launchitem (launchitem) doc = REXML::Document.new() root = REXML::Element.new(LAUNCHITEM) @@ -406,11 +426,11 @@ OpenWFE.xmldoc_to_string(doc) end - def encode_inflowitem (item, elt) + def XmlCodec.encode_inflowitem (item, elt) encode_item(item, elt) elt.attributes[A_PARTICIPANT_NAME] = item.participant_name @@ -420,11 +440,11 @@ elt << eLastExpressionId end - def encode_inflowworkitem (item) + def XmlCodec.encode_inflowworkitem (item) doc = REXML::Document.new() root = REXML::Element.new(IN_FLOW_WORKITEM) @@ -438,15 +458,15 @@ doc << root s = OpenWFE.xmldoc_to_string(doc) #puts "encoded :\n#{s}" - return s + s end - def encode_history (item, elt) + def XmlCodec.encode_history (item, elt) eHistory = REXML::Element.new(HISTORY) item.history.each do |hi| @@ -466,11 +486,11 @@ elt << eHistory end - def encode_attribute (att) + def XmlCodec.encode_attribute (att) #puts "encodeAttribute() att.class is #{att.class}" return encode_atomicattribute(E_STRING, att) \ if att.kind_of?(String) @@ -503,18 +523,18 @@ #raise \ # ArgumentError, \ # "Cannot encode attribute of class '#{att.class}'" end - def encode_xmldocument (elt) + def XmlCodec.encode_xmldocument (elt) e = REXML::Element.new(E_XML_DOCUMENT) e.text = Base64::encode64(elt.to_s) e end - def encode_xmlattribute (elt) + def XmlCodec.encode_xmlattribute (elt) return elt if elt.name == E_RAW_XML # # else, wrap within <raw-xml>...</raw-xml> @@ -524,42 +544,42 @@ e end - def encode_base64attribute (att) + def XmlCodec.encode_base64attribute (att) e = REXML::Element.new(E_BASE64) e.text = att.content e end - def encode_atomicattribute (name, value) + def XmlCodec.encode_atomicattribute (name, value) elt = REXML::Element.new(name) #elt << REXML::Text.new(value.to_s()) elt.add_text(value.to_s()) elt end - def encode_listattribute (list) + def XmlCodec.encode_listattribute (list) elt = REXML::Element.new(E_LIST) list.each do |e| elt << encode_attribute(e) end - return elt + elt end - def encode_mapattribute (hash) + def XmlCodec.encode_mapattribute (hash) name = hash[MAP_TYPE] name = 'map' if name == nil elt = REXML::Element.new(name) @@ -576,15 +596,15 @@ eEntry << encode_attribute(val) elt << eEntry end - return elt + elt end - def encode_fei (fei) + def XmlCodec.encode_fei (fei) elt = REXML::Element.new(FLOW_EXPRESSION_ID) elt.attributes[OWFE_VERSION] = fei.owfe_version elt.attributes[ENGINE_ID] = fei.engine_id @@ -596,45 +616,35 @@ elt.attributes[WORKFLOW_INSTANCE_ID] = fei.workflow_instance_id elt.attributes[EXPRESSION_NAME] = fei.expression_name elt.attributes[EXPRESSION_ID] = fei.expression_id - return elt + elt end end - XMLCODEC = XmlCodec.new - # - # some kind of a singleton, rather a lonelyton - # - # Decodes any XML blurb produced by OpenWFE[ja] into a Ruby instance. - # - def OpenWFE.xml_decode (xmlelt) - XMLCODEC.decode(xmlelt) - end - - # - # Encodes to XML (OpenWFE dialect ;) ) workitems and launch items. - # - def OpenWFE.xml_encode (owfedata) - XMLCODEC.encode(owfedata) - end - - # # Just turns some XML to a String (if decl is set to false, no # XML declaration will be printed). # def OpenWFE.xmldoc_to_string (xml, decl=true) - s = "" + #return xml if xml.is_a?(String) - if decl and xml[0].kind_of? REXML::XMLDecl + if decl and (not xml[0].is_a?(REXML::XMLDecl)) xml << REXML::XMLDecl.new() end + s = "" xml.write(s, 0) return s + end + + # + # An alias for OpenWFE::xmldoc_to_string() + # + def OpenWFE.xml_to_s (xml, decl=true) + OpenWFE::xmldoc_to_string(xml, decl) end end