Search in sources :

Example 26 with BXMLItem

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;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) Blob(java.sql.Blob) SQLException(java.sql.SQLException) BJSON(org.ballerinalang.model.values.BJSON) BStructType(org.ballerinalang.model.types.BStructType) BFloatArray(org.ballerinalang.model.values.BFloatArray) BStringArray(org.ballerinalang.model.values.BStringArray) Array(java.sql.Array) BIntArray(org.ballerinalang.model.values.BIntArray) BNewArray(org.ballerinalang.model.values.BNewArray) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 27 with BXMLItem

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\"]}}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 28 with BXMLItem

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\"]}}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 29 with BXMLItem

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\"}}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 30 with BXMLItem

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\"]}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Aggregations

BXMLItem (org.ballerinalang.model.values.BXMLItem)56 Test (org.testng.annotations.Test)46 BValue (org.ballerinalang.model.values.BValue)42 BJSON (org.ballerinalang.model.values.BJSON)36 BString (org.ballerinalang.model.values.BString)26 OMNode (org.apache.axiom.om.OMNode)18 BXML (org.ballerinalang.model.values.BXML)17 BStruct (org.ballerinalang.model.values.BStruct)6 OMElement (org.apache.axiom.om.OMElement)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)4 ArrayList (java.util.ArrayList)3 OMText (org.apache.axiom.om.OMText)3 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)3 OMDocument (org.apache.axiom.om.OMDocument)2 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)2 BFloatArray (org.ballerinalang.model.values.BFloatArray)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2