use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class TableIterator method generateNext.
@Override
public BStruct generateNext() {
BStruct bStruct = new BStruct(type);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
int blobRegIndex = -1;
int index = 0;
try {
BStructType.StructField[] structFields = type.getStructFields();
for (BStructType.StructField sf : structFields) {
BType type = sf.getFieldType();
++index;
switch(type.getTag()) {
case TypeTags.INT_TAG:
long iValue = rs.getInt(index);
bStruct.setIntField(++longRegIndex, iValue);
break;
case TypeTags.STRING_TAG:
String sValue = rs.getString(index);
bStruct.setStringField(++stringRegIndex, sValue);
break;
case TypeTags.FLOAT_TAG:
double dalue = rs.getDouble(index);
bStruct.setFloatField(++doubleRegIndex, dalue);
break;
case TypeTags.BOOLEAN_TAG:
boolean boolValue = rs.getBoolean(index);
bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
break;
case TypeTags.JSON_TAG:
String jsonValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BJSON(jsonValue));
break;
case TypeTags.XML_TAG:
String xmlValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BXMLItem(xmlValue));
break;
case TypeTags.BLOB_TAG:
Blob blobValue = rs.getBlob(index);
bStruct.setBlobField(++blobRegIndex, blobValue.getBytes(1L, (int) blobValue.length()));
break;
case TypeTags.ARRAY_TAG:
Array arrayValue = rs.getArray(index);
bStruct.setRefField(++refRegIndex, getDataArray(arrayValue));
break;
}
}
} catch (SQLException e) {
throw new BallerinaException("error in generating next row of data :" + e.getMessage());
}
return bStruct;
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonWithComplexObject.
@Test
public void testToJsonWithComplexObject() {
String xmlStr = "<bookStore><storeName>foo</storeName><postalCode>94</postalCode><isOpen>true</isOpen><address>" + "<street>foo</street><city>94</city><country>true</country></address><codes><item>4</item>" + "<item>8</item><item>9</item></codes></bookStore>\n";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"bookStore\":{\"storeName\":\"foo\",\"postalCode\":\"94\"," + "\"isOpen\":\"true\",\"address\":{\"street\":\"foo\",\"city\":\"94\",\"country\":\"true\"}," + "\"codes\":{\"item\":[\"4\",\"8\",\"9\"]}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonWithDifferentAttributeTag.
@Test
public void testToJsonWithDifferentAttributeTag() {
String xmlStr = "<bookStore status = \"online\" id = \"5\"><storeName>foo</storeName><postalCode>94" + "</postalCode><isOpen>true</isOpen><address><street>foo</street>" + "<city code = \"A\" reg = \"C\">94</city><country>true</country></address>" + "<codes quality=\"b\" type = \"0\"><item>4</item><item>8</item><item>9</item></codes></bookStore>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSONWithOptions", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"bookStore\":{\"#status\":\"online\",\"#id\":\"5\"," + "\"storeName\":\"foo\",\"postalCode\":\"94\",\"isOpen\":\"true\",\"address\":{\"street\":\"foo\"," + "\"country\":\"true\",\"city\":{\"#code\":\"A\",\"#reg\":\"C\",\"#text\":\"94\"}}," + "\"codes\":{\"#quality\":\"b\",\"#type\":\"0\",\"item\":[\"4\",\"8\",\"9\"]}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJSONWithAttributeNamespacesAndNoPreserveNamespace.
@Test
public void testToJSONWithAttributeNamespacesAndNoPreserveNamespace() {
String xmlStr = "<ns0:bookStore xmlns:ns0=\"http://sample0.com/test\" status=\"online\" " + "ns0:id = \"10\"><ns0:storeName>foo</ns0:storeName><ns0:isOpen>true</ns0:isOpen><ns2:address " + "xmlns:ns2=\"http://sample2.com/test\" status=\"online\" ns0:id = \"10\" ns2:code= \"test\">" + "srilanka</ns2:address></ns0:bookStore>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSONWithoutNamespace", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"bookStore\":{\"@status\":\"online\",\"@id\":\"10\"," + "\"storeName\":\"foo\",\"isOpen\":\"true\",\"address\":{\"@status\":\"online\",\"@id\":\"10\"," + "\"@code\":\"test\",\"#text\":\"srilanka\"}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonWithArray.
@Test
public void testToJsonWithArray() {
String xmlStr = "<books><item>book1</item><item>book2</item><item>book3</item></books>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"books\":{\"item\":[\"book1\",\"book2\",\"book3\"]}}");
}
Aggregations