otupy.encoders.xml.XMLEncoder
- class XMLEncoder
Bases:
EncoderXML Encoder
This class implements the Encoder interface for the XML format. It leverages the intermediary dictionary representation.
The XMLEncoder can be used to create an OpenC2 stack in Consumer and Producer.
Methods
Decode an OpenC2 message
Encode an OpenC2 object
Create an object from dictionary
Encoder name
Convert object to dictionary
Attributes
The root of the XML message.
The label that is used to identify this Encoder in OpenC2 messages.
is_binary- OpenC2Root = 'OpenC2Msg'
The root of the XML message.
- static decode(msg, msgtype=None)
Decode an OpenC2 message
This method is used to create an otupy object of type msgtype from a xml record. The otupy class msgtype corresponding to the xml record msg must be explicitly provided, since parsing and automatically inferring the msgtype is not currently implemented.
The implementation leverages the intermediary dictionary representation and it is therefore agnostic of otupy classes. Note that the real OpenC2 message is wrapped in an additional object to satisfy XML syntax requirements (see encode()).
- Parameters:
msg – The xml record to decode.
msgtype – The otupy class to convert the xml to.
- Returns:
An msgtype class initialized according to the xml content.
- static encode(obj)
Encode an OpenC2 object
This method is used to encode an OpenC2 object, which usually is a Command or Message. The implementation leverages the intermediary dictionary representation and it is therefore agnostic of otupy classes. Attribute types are not used.
Since XML strictly requires a single root, additional nesting of the the OpenC2 message is necessary. The message is arbitrary enclosed in an object.
- Parameters:
obj – A valid otupy object.
- Returns:
A string with the xml representation of the obj.
- encoder_type = 'xml'
The label that is used to identify this Encoder in OpenC2 messages.
- static fromdict(clstype, dic)
Create an object from dictionary
This is an internal method to create an otupy object from a dictionary. The dictionary must be compliant with the OpenC2 syntax rules. Derived classes are expected to create this intermediate representation and use this method in the encode method. It is necessary to provide the class definition of the otupy object to be instantiated.
- Parameters:
clstype – The class definition that must be used to instantiate the object.
dic – The dictionary with the OpenC2 description.
- Returns:
An instance of clstype initialized with the data in the dic.
- classmethod getName()
Encoder name
This method MUST be implemented to return the name of the encoding format. The name should be highly representative, possible using official terminology (e.g.: json, xml).
- Returns:
The class name.
- static todict(obj)
Convert object to dictionary
This is an internal method to convert an otupy object into a dictionary. The dictionary is structured according to OpenC2 syntax.
This method should only be invoked by derived classes to get the intermediary representation of otupy objects. It will likely be used in the implementation of the decode method. :param obj: The otupy object to convert into a dictionary. :return: A dictionary compliant with the OpenC2 syntax rules.