Search in sources :

Example 1 with OpcUaXmlStreamEncoder

use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamEncoder in project milo by eclipse.

the class OpcUaDefaultXmlEncoding method encode.

@Override
public Object encode(SerializationContext context, Object struct, NodeId encodingId) {
    try {
        @SuppressWarnings("unchecked") OpcUaXmlDataTypeCodec<Object> codec = (OpcUaXmlDataTypeCodec<Object>) context.getDataTypeManager().getCodec(encodingId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_EncodingError, "no codec registered for encodingId=" + encodingId);
        }
        // We have to use writer.writeStruct() instead of codec.decode() because
        // XML-encoded structs are wrapped in a container element with the struct name.
        OpcUaXmlStreamEncoder writer = new OpcUaXmlStreamEncoder(context);
        writer.writeStruct(null, struct, codec);
        return new XmlElement(writer.getDocumentXml());
    } catch (ClassCastException e) {
        throw new UaSerializationException(StatusCodes.Bad_EncodingError, e);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) OpcUaXmlStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamEncoder) OpcUaXmlDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec) XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Aggregations

UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)1 OpcUaXmlStreamEncoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamEncoder)1 OpcUaXmlDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec)1 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)1 XmlElement (org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement)1