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);
}
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);
}
}
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;
}
}
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);
}
}
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);
}
}
Aggregations