Search in sources :

Example 21 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class XMLUtils method parse.

/**
 * Create a XML sequence from string inputstream.
 *
 * @param xmlStream XML imput stream
 * @return  XML Sequence
 */
@SuppressWarnings("unchecked")
public static BXML<?> parse(InputStream xmlStream) {
    BRefValueArray elementsSeq = new BRefValueArray();
    OMDocument doc;
    try {
        doc = OMXMLBuilderFactory.createOMBuilder(xmlStream).getDocument();
        Iterator<OMNode> docChildItr = doc.getChildren();
        int i = 0;
        while (docChildItr.hasNext()) {
            elementsSeq.add(i++, new BXMLItem(docChildItr.next()));
        }
    } catch (DeferredParsingException e) {
        throw new BallerinaException(e.getCause().getMessage());
    } catch (Throwable e) {
        throw new BallerinaException("failed to create xml: " + e.getMessage());
    }
    return new BXMLSequence(elementsSeq);
}
Also used : DeferredParsingException(org.apache.axiom.om.DeferredParsingException) OMNode(org.apache.axiom.om.OMNode) BXMLItem(org.ballerinalang.model.values.BXMLItem) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) OMDocument(org.apache.axiom.om.OMDocument)

Example 22 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class MapInitializerExprTest method testComplexMapInit.

@Test
public void testComplexMapInit() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testComplexMapInit", new BValue[] {});
    Assert.assertTrue(returns[0] instanceof BMap<?, ?>);
    BMap<String, BValue> outerMap = (BMap<String, BValue>) returns[0];
    Assert.assertEquals(outerMap.get("name").stringValue(), "Supun");
    BValue adrsArray = outerMap.get("addressArray");
    Assert.assertTrue(adrsArray instanceof BRefValueArray);
    BRefValueArray addressArray = (BRefValueArray) adrsArray;
    BValue adrs1 = addressArray.get(0);
    Assert.assertTrue(adrs1 instanceof BMap<?, ?>);
    BValue address = ((BMap) adrs1).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Colombo");
    BValue adrs2 = addressArray.get(1);
    Assert.assertTrue(adrs2 instanceof BMap<?, ?>);
    address = ((BMap) adrs2).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Kandy");
    BValue adrs3 = addressArray.get(2);
    Assert.assertTrue(adrs3 instanceof BMap<?, ?>);
    address = ((BMap) adrs3).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Galle");
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BString(org.ballerinalang.model.values.BString) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 23 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class BNullValueTest method testArrayOfNulls.

@Test(description = "Test array of null values")
public void testArrayOfNulls() {
    BValue[] vals = BRunUtil.invoke(positiveCompileResult, "testArrayOfNulls", new BValue[] {});
    BRefValueArray nullArray = (BRefValueArray) vals[0];
    Assert.assertTrue(nullArray.get(0) instanceof BStruct);
    Assert.assertEquals(nullArray.get(1), null);
    Assert.assertEquals(nullArray.get(2), null);
    Assert.assertTrue(nullArray.get(3) instanceof BStruct);
    Assert.assertEquals(nullArray.get(4), null);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 24 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class XMLLiteralTest method testElementLiteralWithNamespaces.

@Test
public void testElementLiteralWithNamespaces() {
    BValue[] returns = BRunUtil.invoke(literalWithNamespacesResult, "testElementLiteralWithNamespaces");
    Assert.assertTrue(returns[0] instanceof BXML);
    Assert.assertEquals(returns[0].stringValue(), "<root xmlns=\"http://ballerina.com/\" xmlns:ns0=\"http://ballerina.com/a\" " + "xmlns:ns1=\"http://ballerina.com/c\" ns0:id=\"456\"><foo>123</foo>" + "<bar ns1:status=\"complete\"/></root>");
    Assert.assertTrue(returns[1] instanceof BXML);
    BXMLSequence seq = (BXMLSequence) returns[1];
    Assert.assertEquals(seq.stringValue(), "<foo xmlns=\"http://ballerina.com/\" " + "xmlns:ns0=\"http://ballerina.com/a\" xmlns:ns1=\"http://ballerina.com/c\">123</foo>" + "<bar xmlns=\"http://ballerina.com/\" xmlns:ns0=\"http://ballerina.com/a\" " + "xmlns:ns1=\"http://ballerina.com/c\" ns1:status=\"complete\"/>");
    BRefValueArray items = seq.value();
    Assert.assertEquals(items.size(), 2);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) Test(org.testng.annotations.Test)

Example 25 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class XMLLiteralTest method testElementLiteralWithTemplateChildren.

@Test
public void testElementLiteralWithTemplateChildren() {
    BValue[] returns = BRunUtil.invoke(result, "testElementLiteralWithTemplateChildren");
    Assert.assertTrue(returns[0] instanceof BXML);
    Assert.assertEquals(returns[0].stringValue(), "<root>hello aaa&lt;bbb good morning <fname>John</fname> " + "<lname>Doe</lname>. Have a nice day!<foo>123</foo><bar/></root>");
    Assert.assertTrue(returns[1] instanceof BXML);
    BXMLSequence seq = (BXMLSequence) returns[1];
    Assert.assertEquals(seq.stringValue(), "hello aaa<bbb good morning <fname>John</fname> <lname>Doe</lname>. " + "Have a nice day!<foo>123</foo><bar/>");
    BRefValueArray items = seq.value();
    Assert.assertEquals(items.size(), 7);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) Test(org.testng.annotations.Test)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3