Search in sources :

Example 1 with OpcUaXmlDataTypeCodec

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

the class OpcUaXmlStreamDecoder method readStructArray.

@Override
public Object[] readStructArray(String field, NodeId dataTypeId) throws UaSerializationException {
    if (currentNode(field)) {
        Node node = currentNode;
        OpcUaXmlDataTypeCodec<?> codec = (OpcUaXmlDataTypeCodec<?>) context.getDataTypeManager().getCodec(OpcUaDefaultXmlEncoding.ENCODING_NAME, dataTypeId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + dataTypeId);
        }
        List<Object> values = new ArrayList<>();
        Node listNode = node.getFirstChild();
        NodeList children = listNode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            currentNode = children.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                values.add(readStruct(currentNode.getLocalName(), dataTypeId));
            }
        }
        try {
            Object array = Array.newInstance(codec.getType(), values.size());
            for (int i = 0; i < values.size(); i++) {
                Array.set(array, i, values.get(i));
            }
            return (Object[]) array;
        } finally {
            currentNode = node.getNextSibling();
        }
    } else {
        return null;
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) OpcUaXmlDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 2 with OpcUaXmlDataTypeCodec

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

the class OpcUaDefaultXmlEncoding method decode.

@Override
public Object decode(SerializationContext context, Object body, NodeId encodingId) {
    try {
        @SuppressWarnings("unchecked") OpcUaXmlDataTypeCodec<Object> codec = (OpcUaXmlDataTypeCodec<Object>) context.getDataTypeManager().getCodec(encodingId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered for encodingId=" + encodingId);
        }
        XmlElement xmlBody = (XmlElement) body;
        String xml = xmlBody.getFragmentOrEmpty();
        OpcUaXmlStreamDecoder reader = new OpcUaXmlStreamDecoder(context);
        reader.setInput(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8)));
        return reader.readStruct(null, codec);
    } catch (IOException | SAXException e) {
        throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) IOException(java.io.IOException) OpcUaXmlStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamDecoder) SAXException(org.xml.sax.SAXException)

Example 3 with OpcUaXmlDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec 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)

Example 4 with OpcUaXmlDataTypeCodec

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

the class OpcUaXmlStreamDecoder method readMessage.

@Override
public UaMessage readMessage(String field) throws UaSerializationException {
    if (currentNode(field)) {
        Node node = currentNode;
        String typeName = node.getLocalName();
        DataTypeCodec codec = context.getDataTypeManager().getCodec(Namespaces.OPC_UA_XSD, String.format("//xs:element[@name='%s']", typeName));
        if (codec instanceof OpcUaXmlDataTypeCodec<?>) {
            currentNode = node.getFirstChild();
            try {
                return (UaMessage) ((OpcUaXmlDataTypeCodec<?>) codec).decode(context, this);
            } finally {
                currentNode = node.getNextSibling();
            }
        } else {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + typeName);
        }
    } else {
        // TODO could be better if we passed Class<?> into method
        return null;
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) Node(org.w3c.dom.Node) OpcUaXmlDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) OpcUaXmlDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec) DataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.DataTypeCodec)

Example 5 with OpcUaXmlDataTypeCodec

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

the class OpcUaXmlStreamDecoder method readStruct.

@Override
public Object readStruct(String field, NodeId dataTypeId) throws UaSerializationException {
    if (currentNode(field)) {
        Node node = currentNode;
        OpcUaXmlDataTypeCodec<?> codec = (OpcUaXmlDataTypeCodec<?>) context.getDataTypeManager().getCodec(OpcUaDefaultXmlEncoding.ENCODING_NAME, dataTypeId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + dataTypeId);
        }
        try {
            currentNode = node.getFirstChild();
            return codec.decode(context, this);
        } finally {
            currentNode = node.getNextSibling();
        }
    } else {
        // TODO could be better if we passed Class<?> into method
        return null;
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) Node(org.w3c.dom.Node) OpcUaXmlDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec)

Aggregations

UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)5 OpcUaXmlDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec)5 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)3 Node (org.w3c.dom.Node)3 XmlElement (org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 OpcUaXmlStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamDecoder)1 OpcUaXmlStreamEncoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamEncoder)1 DataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.DataTypeCodec)1 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)1 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1