use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class RequestNativeFunctionSuccessTest method testSetXmlPayload.
@Test
public void testSetXmlPayload() {
BXMLItem value = new BXMLItem("<name>Ballerina</name>");
BValue[] inputArg = { value };
BValue[] returnVals = BRunUtil.invoke(result, "testSetXmlPayload", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertTrue(returnVals[0] instanceof BStruct);
BStruct entity = (BStruct) ((BStruct) returnVals[0]).getNativeData(MESSAGE_ENTITY);
// BXMLItem xmlValue = (BXMLItem) entity.getRefField(XML_DATA_INDEX);
BXML xmlValue = (BXML) EntityBodyHandler.getMessageDataSource(entity);
Assert.assertEquals(xmlValue.getTextValue().stringValue(), "Ballerina", "Payload is not set properly");
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class CPU method execBinaryOpCodes.
@SuppressWarnings("rawtypes")
private static void execBinaryOpCodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
int k;
switch(opcode) {
case InstructionCodes.IADD:
i = operands[0];
j = operands[1];
k = operands[2];
sf.longRegs[k] = sf.longRegs[i] + sf.longRegs[j];
break;
case InstructionCodes.FADD:
i = operands[0];
j = operands[1];
k = operands[2];
sf.doubleRegs[k] = sf.doubleRegs[i] + sf.doubleRegs[j];
break;
case InstructionCodes.SADD:
i = operands[0];
j = operands[1];
k = operands[2];
sf.stringRegs[k] = sf.stringRegs[i] + sf.stringRegs[j];
break;
case InstructionCodes.XMLADD:
i = operands[0];
j = operands[1];
k = operands[2];
BXML lhsXMLVal = (BXML) sf.refRegs[i];
BXML rhsXMLVal = (BXML) sf.refRegs[j];
if (lhsXMLVal == null || rhsXMLVal == null) {
handleNullRefError(ctx);
break;
}
// Here it is assumed that a refType addition can only be a xml-concat.
sf.refRegs[k] = XMLUtils.concatenate(lhsXMLVal, rhsXMLVal);
break;
case InstructionCodes.ISUB:
i = operands[0];
j = operands[1];
k = operands[2];
sf.longRegs[k] = sf.longRegs[i] - sf.longRegs[j];
break;
case InstructionCodes.FSUB:
i = operands[0];
j = operands[1];
k = operands[2];
sf.doubleRegs[k] = sf.doubleRegs[i] - sf.doubleRegs[j];
break;
case InstructionCodes.IMUL:
i = operands[0];
j = operands[1];
k = operands[2];
sf.longRegs[k] = sf.longRegs[i] * sf.longRegs[j];
break;
case InstructionCodes.FMUL:
i = operands[0];
j = operands[1];
k = operands[2];
sf.doubleRegs[k] = sf.doubleRegs[i] * sf.doubleRegs[j];
break;
case InstructionCodes.IDIV:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.longRegs[j] == 0) {
ctx.setError(BLangVMErrors.createError(ctx, " / by zero"));
handleError(ctx);
break;
}
sf.longRegs[k] = sf.longRegs[i] / sf.longRegs[j];
break;
case InstructionCodes.FDIV:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.doubleRegs[j] == 0) {
ctx.setError(BLangVMErrors.createError(ctx, " / by zero"));
handleError(ctx);
break;
}
sf.doubleRegs[k] = sf.doubleRegs[i] / sf.doubleRegs[j];
break;
case InstructionCodes.IMOD:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.longRegs[j] == 0) {
ctx.setError(BLangVMErrors.createError(ctx, " / by zero"));
handleError(ctx);
break;
}
sf.longRegs[k] = sf.longRegs[i] % sf.longRegs[j];
break;
case InstructionCodes.FMOD:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.doubleRegs[j] == 0) {
ctx.setError(BLangVMErrors.createError(ctx, " / by zero"));
handleError(ctx);
break;
}
sf.doubleRegs[k] = sf.doubleRegs[i] % sf.doubleRegs[j];
break;
case InstructionCodes.INEG:
i = operands[0];
j = operands[1];
sf.longRegs[j] = -sf.longRegs[i];
break;
case InstructionCodes.FNEG:
i = operands[0];
j = operands[1];
sf.doubleRegs[j] = -sf.doubleRegs[i];
break;
case InstructionCodes.BNOT:
i = operands[0];
j = operands[1];
sf.intRegs[j] = sf.intRegs[i] == 0 ? 1 : 0;
break;
case InstructionCodes.IEQ:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.longRegs[i] == sf.longRegs[j] ? 1 : 0;
break;
case InstructionCodes.FEQ:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.doubleRegs[i] == sf.doubleRegs[j] ? 1 : 0;
break;
case InstructionCodes.SEQ:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = StringUtils.isEqual(sf.stringRegs[i], sf.stringRegs[j]) ? 1 : 0;
break;
case InstructionCodes.BEQ:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.intRegs[i] == sf.intRegs[j] ? 1 : 0;
break;
case InstructionCodes.REQ:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.refRegs[i] == sf.refRegs[j] ? 1 : 0;
break;
case InstructionCodes.TEQ:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.refRegs[i] == null || sf.refRegs[j] == null) {
handleNullRefError(ctx);
}
sf.intRegs[k] = sf.refRegs[i].equals(sf.refRegs[j]) ? 1 : 0;
break;
case InstructionCodes.INE:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.longRegs[i] != sf.longRegs[j] ? 1 : 0;
break;
case InstructionCodes.FNE:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.doubleRegs[i] != sf.doubleRegs[j] ? 1 : 0;
break;
case InstructionCodes.SNE:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = !StringUtils.isEqual(sf.stringRegs[i], sf.stringRegs[j]) ? 1 : 0;
break;
case InstructionCodes.BNE:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.intRegs[i] != sf.intRegs[j] ? 1 : 0;
break;
case InstructionCodes.RNE:
i = operands[0];
j = operands[1];
k = operands[2];
sf.intRegs[k] = sf.refRegs[i] != sf.refRegs[j] ? 1 : 0;
break;
case InstructionCodes.TNE:
i = operands[0];
j = operands[1];
k = operands[2];
if (sf.refRegs[i] == null || sf.refRegs[j] == null) {
handleNullRefError(ctx);
}
sf.intRegs[k] = (!sf.refRegs[i].equals(sf.refRegs[j])) ? 1 : 0;
break;
default:
throw new UnsupportedOperationException();
}
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class CPU method execXMLCreationOpcodes.
private static void execXMLCreationOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
int k;
int l;
BXML<?> xmlVal;
switch(opcode) {
case InstructionCodes.NEWXMLELEMENT:
i = operands[0];
j = operands[1];
k = operands[2];
l = operands[3];
BXMLQName startTagName = (BXMLQName) sf.refRegs[j];
BXMLQName endTagName = (BXMLQName) sf.refRegs[k];
try {
sf.refRegs[i] = XMLUtils.createXMLElement(startTagName, endTagName, sf.stringRegs[l]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLCOMMENT:
i = operands[0];
j = operands[1];
try {
sf.refRegs[i] = XMLUtils.createXMLComment(sf.stringRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLTEXT:
i = operands[0];
j = operands[1];
try {
sf.refRegs[i] = XMLUtils.createXMLText(sf.stringRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLPI:
i = operands[0];
j = operands[1];
k = operands[2];
try {
sf.refRegs[i] = XMLUtils.createXMLProcessingInstruction(sf.stringRegs[j], sf.stringRegs[k]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.XMLSEQSTORE:
i = operands[0];
j = operands[1];
xmlVal = (BXML<?>) sf.refRegs[i];
BXML<?> child = (BXML<?>) sf.refRegs[j];
xmlVal.addChildren(child);
break;
case InstructionCodes.NEWXMLSEQ:
i = operands[0];
sf.refRegs[i] = new BXMLSequence();
break;
}
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class JSONUtils method convertToXML.
/**
* Converts given json object to the corresponding xml.
*
* @param json JSON object to get the corresponding xml
* @param attributePrefix String prefix used for attributes
* @param arrayEntryTag String used as the tag in the arrays
* @return BXML XML representation of the given json object
*/
@SuppressWarnings("rawtypes")
public static BXML convertToXML(BJSON json, String attributePrefix, String arrayEntryTag) {
BXML xml;
JsonNode jsonNode = json.value();
List<BXML> omElementArrayList = traverseTree(jsonNode, attributePrefix, arrayEntryTag);
if (omElementArrayList.size() == 1) {
xml = omElementArrayList.get(0);
} else {
// There is a multi rooted node and create xml sequence from it
BRefValueArray elementsSeq = new BRefValueArray();
int count = omElementArrayList.size();
for (int i = 0; i < count; i++) {
elementsSeq.add(i, omElementArrayList.get(i));
}
xml = new BXMLSequence(elementsSeq);
}
return xml;
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testSetChildrenWithDefaultNamespace.
@Test
public void testSetChildrenWithDefaultNamespace() {
BValue[] returns = BRunUtil.invoke(result, "testSetChildrenDefaultNamespace");
Assert.assertEquals(returns.length, 5);
Assert.assertTrue(returns[0] instanceof BXML);
Assert.assertEquals(returns[0].stringValue(), "<name xmlns=\"http://sample.com/test\"><fname>supun</fname>" + "<lname>setunga</lname><residency citizen=\"true\">true</residency></name>");
// is children seq is empty?
Assert.assertSame(returns[1].getClass(), BBoolean.class);
Assert.assertEquals(((BBoolean) returns[1]).booleanValue(), false);
// is children seq is singleton?
Assert.assertSame(returns[2].getClass(), BBoolean.class);
Assert.assertEquals(((BBoolean) returns[2]).booleanValue(), true);
// Check children
Assert.assertTrue(returns[3] instanceof BXML);
BRefValueArray children = ((BXMLSequence) returns[3]).value();
Assert.assertEquals(children.size(), 3);
Assert.assertEquals(children.get(0).stringValue(), "<fname xmlns=\"http://sample.com/test\">supun</fname>");
Assert.assertEquals(children.get(1).stringValue(), "<lname xmlns=\"http://sample.com/test\">setunga</lname>");
Assert.assertEquals(children.get(2).stringValue(), "<residency xmlns=\"http://sample.com/test\" citizen=\"true\">true</residency>");
// Check attribute value
Assert.assertSame(returns[4].getClass(), BString.class);
Assert.assertEquals(((BString) returns[4]).stringValue(), "true");
}
Aggregations