Search in sources :

Example 1 with XmlElement

use of org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement in project milo by eclipse.

the class XmlElementSerializationTest method testXmlElementRoundTrip.

@Test(dataProvider = "XmlElementProvider", description = "XmlElement is round-trip serializable.")
public void testXmlElementRoundTrip(XmlElement element) throws Exception {
    writer.writeXmlElement(element);
    XmlElement decoded = reader.readXmlElement();
    assertEquals(decoded, element);
}
Also used : XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) Test(org.testng.annotations.Test)

Example 2 with XmlElement

use of org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement in project milo by eclipse.

the class JsonStructureCodec method opcUaToMemberTypeScalar.

@Override
protected JsonElement opcUaToMemberTypeScalar(String name, Object value, String typeName) {
    if (value == null) {
        return JsonNull.INSTANCE;
    } else if (value instanceof Number) {
        if (value instanceof UByte) {
            return new JsonPrimitive(((UByte) value).shortValue());
        } else if (value instanceof UShort) {
            return new JsonPrimitive(((UShort) value).intValue());
        } else if (value instanceof UInteger) {
            return new JsonPrimitive(((UInteger) value).longValue());
        } else if (value instanceof ULong) {
            return new JsonPrimitive(((ULong) value).toBigInteger());
        } else {
            return new JsonPrimitive((Number) value);
        }
    } else if (value instanceof Boolean) {
        return new JsonPrimitive((Boolean) value);
    } else if (value instanceof String) {
        return new JsonPrimitive((String) value);
    } else if (value instanceof Character) {
        return new JsonPrimitive((Character) value);
    } else if (value instanceof JsonElement) {
        return (JsonElement) value;
    } else if (value instanceof DateTime) {
        return new JsonPrimitive(((DateTime) value).getUtcTime());
    } else if (value instanceof UUID) {
        return new JsonPrimitive(value.toString());
    } else if (value instanceof LocalizedText) {
        return gson.toJsonTree(value);
    } else if (value instanceof QualifiedName) {
        return gson.toJsonTree(value);
    } else if (value instanceof ByteString) {
        ByteString byteString = (ByteString) value;
        byte[] bs = byteString.bytesOrEmpty();
        JsonArray array = new JsonArray();
        for (Byte b : bs) {
            array.add(new JsonPrimitive(b));
        }
        return array;
    } else if (value instanceof XmlElement) {
        String fragment = ((XmlElement) value).getFragment();
        return fragment != null ? new JsonPrimitive(fragment) : JsonNull.INSTANCE;
    } else if (value instanceof NodeId) {
        String nodeId = ((NodeId) value).toParseableString();
        return new JsonPrimitive(nodeId);
    } else if (value instanceof ExpandedNodeId) {
        String xNodeId = ((ExpandedNodeId) value).toParseableString();
        return new JsonPrimitive(xNodeId);
    } else if (value instanceof StatusCode) {
        long code = ((StatusCode) value).getValue();
        return new JsonPrimitive(code);
    } else {
        throw new RuntimeException("could not create JsonElement for value: " + value);
    }
}
Also used : ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) JsonPrimitive(com.google.gson.JsonPrimitive) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) Unsigned.ulong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ulong) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) JsonArray(com.google.gson.JsonArray) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) JsonElement(com.google.gson.JsonElement) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) UUID(java.util.UUID)

Example 3 with XmlElement

use of org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement in project milo by eclipse.

the class OpcUaBinaryStreamDecoder method readXmlElementArray.

@Override
public XmlElement[] readXmlElementArray(String field) throws UaSerializationException {
    int length = readInt32();
    if (length == -1) {
        return null;
    } else {
        checkArrayLength(length);
        XmlElement[] values = new XmlElement[length];
        for (int i = 0; i < length; i++) {
            values[i] = readXmlElement(field);
        }
        return values;
    }
}
Also used : XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 4 with XmlElement

use of org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement in project milo by eclipse.

the class OpcUaBinaryStreamDecoder method readExtensionObject.

public ExtensionObject readExtensionObject() throws UaSerializationException {
    NodeId encodingTypeId = readNodeId();
    int encoding = buffer.readByte();
    if (encoding == 0) {
        return new ExtensionObject(ByteString.NULL_VALUE, encodingTypeId);
    } else if (encoding == 1) {
        ByteString byteString = readByteString();
        return new ExtensionObject(byteString, encodingTypeId);
    } else if (encoding == 2) {
        XmlElement xmlElement = readXmlElement();
        return new ExtensionObject(xmlElement, encodingTypeId);
    } else {
        throw new UaSerializationException(StatusCodes.Bad_DecodingError, "unknown ExtensionObject encoding: " + encoding);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 5 with XmlElement

use of org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement 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)

Aggregations

XmlElement (org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement)10 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)5 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)5 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)5 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)3 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)3 OpcUaXmlDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaXmlDataTypeCodec)2 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 UUID (java.util.UUID)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 OpcUaXmlStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamDecoder)1