OTX-Runtime for Java  
OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter Class Reference

Converts OTX Value to String and vice versa. More...

Static Public Member Functions

static Object String2Value (String stringLiteral, String targetDataType)
 Converts a String into a value of the given data type. More...
 
static String Value2String (Object value)
 Converts a value from an element of the API to a String literal. More...
 

Detailed Description

Converts OTX Value to String and vice versa.

Member Function Documentation

◆ String2Value()

static Object OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value ( String  stringLiteral,
String  targetDataType 
)
inlinestatic

Converts a String into a value of the given data type.

This method converts a String in OTL notation into a value of the given data type. The String for the data type is formatted in OTL notation and is therefore independent from the underlying software platform like C++, DotNet or Java. The data type String can be get via the related method or property of the related OTX element, e.g. ProcedureParameter.

Parameters
stringLiteralString literal formatted in OTL notation
targetDataTypeOTL notation of the data type which the string literal will be converted to. To get the data type the property DataType of the related element can be used, e.g. ProcedureParameter.
Returns
An OTX value object.
Exceptions
OpenTestSystem.Otx.Runtime.Api.Exceptions.InvalidLiteralExceptionThrown when a literal string cannot be converted to OTX Object.
IllegalArgumentExceptionThrown when either stringLiteral or targetDataType is null.

Example

String2Value("1", "Integer"); String2Value("\"Hello World!"", "String"); String2Value("true", "Boolean") String2Value("1.23", "Float"); String2Value("&123456789ABCDEF0", "ByteField"); String2Value("{"Value1", "Value2", "Value3"}", "List<String>"); String2Value("{"Key1":"Value1", "Key2":"Value2", "Key3":"Value3"}", "Map<String, String>"); String2Value("{StringElement="Hello", IntegerElement=123, FloatElement=457.0}", "StructurePackage1.Document1.StructureSignature1<String, Integer, Float>"); // structure defined in OTX String2Value("IntegerElement", "EnumerationElementPackage1.Document1.EnumerationSignature1"); // enumeration defined in OTX String2Value("{"Value1", "Value2", "Value3"}", parameter.DataType);

◆ Value2String()

static String OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.Value2String ( Object  value)
inlinestatic

Converts a value from an element of the API to a String literal.

This method converts a value from an element of the API like a ProcedureParameter to a String literal. The String literal is formatted in OTL notation and is therefore independent from the underlying software platform like C++, DotNet or Java.

Parameters
valueAn arbitrary value of an OTX object e.g. ProcedureParameter.
Returns
String literal in OTL notation.
Exceptions
IllegalArgumentExceptionThrown when value is null.

Example

// Pseudo-Code example to convert values to string and vice versa
// ==============================================================
// Please note that the exact syntax of C++, DotNet and Java is different!
void main()
{
// ...
// Set and Get datatype List
// Assume the parameter[0] is a ProcedureInParameter of data type List of String (List<String>)
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[0], "{\"Value1\", \"Value2\", \"Value3\"}");
// ...
// ...
// Set and Get datatype Map
// Assume the parameter[1] is a ProcedureInParameter of data type Map with key of type String and value of type String (Map<String, String>)
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[1], "{\"Key1\":\"Value1\", \"Key2\":\"Value2\", \"Key3\":\"Value3\"}");
// ...
// ...
// Set and Get datatype ByteField
// Assume the parameter[2] is a ProcedureInParameter of data type ByteField
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[2], "&121212");
// ...
// Set and Get datatype ResourceLocation
// Assume the parameter[3] is a ProcedureInParameter of data type ResourceLocation
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[3], "@ResourceLocation: \"ID_1\"/\"path/to/resource/file\"");
// ...
}
// Set value to a parameter using string literal
// and Get the newly added value back as a string literal
String SetParameterValue(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureParameter parameter, String stringLiteral)
{
if (parameter is null)
{
return String.Empty;
}
try
{
// The string literal will be converted to an OTX Data Types Supported at the API
Object value = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(stringLiteral, parameter.DataType);
{
(parameter as OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter).Value = value;
}
{
(parameter as OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInOutParameter).Value = value;
}
// Get value from the parameter and convert it back to a string literal
// This should be nearly the same as the new string literal set to the Parameter
}
{
throw e;
}
}
OpenTestSystem.Otx.Runtime.Api.DataTypes.Structure CreatePersonStructureWithValueConverter()
{
// Structure: Person{Name, Age}
// Name: String
// Age: Integer
// Structure Person is locate inside Document1 in Package1
String structureDataType = "Package1.Document1.Person<String, Integer>";
String structureLiteral = "{Name=\"Chris\",Age=25}";
// Use ValueConverter.String2Value to convert the string literal
// then cast the result from Object back to OpenTestSystem.Otx.Runtime.Api.DataTypes.Structure
return person1;
}
void ChangePersonStructure(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// Structure: Person{Name, Age}
// Name: String
// Age: Integer
// Structure Person is locate inside Document1 in Package1
// Assume that the given inParameter datatype is Person Structure
// and default value is Person{Name="Chris",Age=25}
// Get StrutureElements from Structure chris
// This Collection should contains only 2 values (Name and Age StructureElement)
ReadOnlyCollection<OpenTestSystem.Otx.Runtime.Api.DataTypes.StructureElement> chrisInfoCollection = chris.Elements;
String newNameLiteral = "\"Christopher\"";
String newAgeLiteral = "40";
foreach (OpenTestSystem.Otx.Runtime.Api.DataTypes.StructureElement info in chrisInfoCollection)
{
// Create new StructureElement Name
if (info.Name == "Name")
{
Object newNameValue = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(newNameLiteral, info.Type);
info.Name
, newNameValue
, info.Type
);
newChrisInfoCollection.Add(nameStructureElement);
}
// Create new StructureElement Age
else if (info.Name == "Age")
{
Object newAgeValue = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(newAgeLiteral, info.Type);
info.Name
, newAgeValue
, info.Type
);
newChrisInfoCollection.Add(ageStructureElement);
}
}
// Create new Chris Structure with the new Information
chris.Signature
, newChrisInfoCollection
);
}
OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement CreateColorEnumerationWithValueConverter()
{
// EnumerationSignature name: Color
// EnumerationElements in Color: Red, Green, Blue
// EnumerationSignature Color is locate inside Document1 in Package1
String enumerationDataType = "Package1.Document1.Color";
String enumerationLiteral = "Blue";
// Use ValueConverter.String2Value to convert the string literal
// then cast the result from Object back to OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement
// This will only work after the Otx Runtime Api loaded a PTX contains EnumerationSignature Color like above
OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement color1 = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(enumerationLiteral, enumerationDataType) as OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement; // On success, this will return Color EnumerationElement Blue
return color1;
}
void ChangeColorEnumerationElement(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// EnumerationSignature name: Color
// EnumerationElements in Color: Red, Green, Blue
// EnumerationSignature Color is locate inside Document1 in Package1
// Assume that the given inParameter datatype is EnumerationElement from Color EnumerationSignature
// and the default Color is Red
OpenTestSystem.Otx.Runtime.Api.Otx.Signatures.IEnumerationSignature colorEnumeration = redColor.Enumeration;
// Get all Color EnumerationElements
List<OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement> colors = colorEnumeration.Elements;
// Get Blue Color EnumerationElement
{
if (color.Name == "Blue")
{
blueColor = color;
break;
}
}
// Set Blue Color into inParameter
inParameter.Value = blueColor;
}
void ChangeLocationPropertyOfResourceLocation(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// Assume that the given inParameter datatype is ResourceLocation
// and the default ID is "ID_1" and Location is "current/path/to/resource"
// Update ResourceLocation
// Since this is a reference to the ResourceLocaiton inside inParameter
// , the ResourceLocation's Location should also be updated, inParameter.Value does not need to be set again
resourceLocation.Location = "new/path/to/resource";
}
void CreateNewResourceLocationWithResourceLocationConstructor(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
String id = "Logo";
String location = "/home/chris/pictures/myLogo.ico";
// Assume that the given inParameter datatype is ResourceLocation
inParameter.Value = logoResource;
}
Represents OTX Enumeration DataType.
Definition: EnumerationElement.java:19
Represents the data type ResourceLocation.
Definition: ResourceLocation.java:6
Represents OTX StructureElement DataType.
Definition: StructureElement.java:6
Represents OTX Structure DataType.
Definition: Structure.java:13
Converts OTX Value to String and vice versa.
Definition: ValueConverter.java:17
static Object String2Value(String stringLiteral, String targetDataType)
Converts a String into a value of the given data type.
Definition: ValueConverter.java:58
static String Value2String(Object value)
Converts a value from an element of the API to a String literal.
Definition: ValueConverter.java:139
Thrown when a literal string cannot be converted to OTX Object.
Definition: InvalidLiteralException.java:6
Represents InOutParameter of a Procedure.
Definition: IProcedureInOutParameter.java:8
Represents InParameter of a Procedure.
Definition: IProcedureInParameter.java:8
Represents base class for InParameter, OutParameter and InOutParameter of a Procedure.
Definition: IProcedureParameter.java:8
Package contains all supported data types.
Definition: BlackBox.java:1
Package containing exceptions.
Definition: ConnectionStateException.java:1
Package for browsing at OTX data structure.
Definition: IContextVariable.java:1
Package containing the programming interface for browsing and execution of OTX procedures in own appl...
Definition: ApiConstants.java:1
Package containing all objects for browsing and execution of OTX procedures.
Definition: opentestsystem.otx.runtime2.api/src/main/java/opentestsystem/otx/runtime/package-info.java:4
Package containing all objects which are standardized according to ISO 13209 (OTX)
Package containing all objects related to testing inside automotive industry.

The documentation for this class was generated from the following file: