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