Search in sources :

Example 46 with BRefValueArray

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

the class ParseHeader method execute.

@Override
public void execute(Context context) {
    String errMsg;
    try {
        String headerValue = context.getStringArgument(0);
        if (headerValue.contains(COMMA)) {
            headerValue = headerValue.substring(0, headerValue.indexOf(COMMA));
        }
        // Set value and param map
        String value = headerValue.trim();
        if (headerValue.contains(SEMICOLON)) {
            value = HeaderUtil.getHeaderValue(value);
        }
        BRefValueArray contentTuple = new BRefValueArray(parseHeaderTupleType);
        contentTuple.add(0, new BString(value));
        contentTuple.add(1, HeaderUtil.getParamMap(headerValue));
        context.setReturnValues(contentTuple);
        return;
    } catch (BLangNullReferenceException ex) {
        errMsg = PARSER_ERROR + "header value cannot be null";
    } catch (BallerinaException ex) {
        errMsg = PARSER_ERROR + ex.getMessage();
    }
    // set parse error
    context.setReturnValues(MimeUtil.getParserError(context, errMsg));
}
Also used : BString(org.ballerinalang.model.values.BString) BLangNullReferenceException(org.ballerinalang.util.exceptions.BLangNullReferenceException) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 47 with BRefValueArray

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

the class ConnectionAction method serializeMultiparts.

/**
 * Serlaize multipart entity body. If an array of body parts exist, encode body parts else serialize body content
 * if it exist as a byte channel.
 *
 * @param responseMessage          Response message that needs to be sent out.
 * @param boundaryString           Boundary string that should be used in encoding body parts
 * @param outboundRespStatusFuture Represent the future events and results of connectors
 * @param entityStruct             Represent the entity that holds the actual body
 */
private void serializeMultiparts(HTTPCarbonMessage responseMessage, String boundaryString, HttpResponseFuture outboundRespStatusFuture, BStruct entityStruct) {
    BRefValueArray bodyParts = EntityBodyHandler.getBodyPartArray(entityStruct);
    if (bodyParts != null && bodyParts.size() > 0) {
        MultipartDataSource multipartDataSource = new MultipartDataSource(entityStruct, boundaryString);
        serializeMsgDataSource(responseMessage, multipartDataSource, outboundRespStatusFuture, entityStruct);
    } else {
        OutputStream messageOutputStream = getOutputStream(responseMessage, outboundRespStatusFuture);
        try {
            EntityBodyHandler.writeByteChannelToOutputStream(entityStruct, messageOutputStream);
        } catch (IOException e) {
            throw new BallerinaException("Error occurred while serializing byte channel content : " + e.getMessage());
        } finally {
            HttpUtil.closeMessageOutputStream(messageOutputStream);
        }
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) OutputStream(java.io.OutputStream) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) IOException(java.io.IOException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 48 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray 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");
}
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 49 with BRefValueArray

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

the class XMLNativeFunctionTest method testCopy.

@Test
public void testCopy() {
    BValue[] returns = BRunUtil.invoke(result, "testCopy");
    Assert.assertEquals(returns.length, 4);
    Assert.assertTrue(returns[0] instanceof BXMLItem);
    Assert.assertEquals(returns[0].stringValue(), "<ns0:name xmlns:ns0=\"http://sample.com/test\"><newFname>" + "supun-new</newFname><newMname>thilina-new</newMname><newLname>setunga-new</newLname></ns0:name>");
    // Check children of the copied xml
    Assert.assertTrue(returns[3] instanceof BXML);
    BRefValueArray children = ((BXMLSequence) ((BXML) returns[0]).children()).value();
    Assert.assertEquals(children.size(), 3);
    Assert.assertEquals(children.get(0).stringValue(), "<newFname>supun-new</newFname>");
    Assert.assertEquals(children.get(1).stringValue(), "<newMname>thilina-new</newMname>");
    Assert.assertEquals(children.get(2).stringValue(), "<newLname>setunga-new</newLname>");
    // 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 of the original xml
    Assert.assertTrue(returns[3] instanceof BXML);
    BRefValueArray originalChildren = ((BXMLSequence) returns[3]).value();
    Assert.assertEquals(originalChildren.size(), 2);
    Assert.assertEquals(originalChildren.get(0).stringValue(), "<fname xmlns:ns0=\"http://sample.com/test\">supun</fname>");
    Assert.assertEquals(originalChildren.get(1).stringValue(), "<lname xmlns:ns0=\"http://sample.com/test\">setunga</lname>");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) 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 50 with BRefValueArray

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

the class JSONUtils method jsonNodeToBArray.

/**
 * Convert a JSON node to an array.
 *
 * @param arrayNode JSON to convert
 * @param targetArrayType Type of the target array
 * @return If the provided JSON is of array type, this method will return a {@link BArrayType} containing the values
 *         of the JSON array. Otherwise the method will throw a {@link BallerinaException}.
 */
@SuppressWarnings("rawtypes")
private static BNewArray jsonNodeToBArray(JsonNode arrayNode, BArrayType targetArrayType) {
    if (!arrayNode.isArray()) {
        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.ARRAY), getTypeName(arrayNode));
    }
    BType elementType = targetArrayType.getElementType();
    BRefValueArray refValueArray;
    switch(elementType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToBIntArray(arrayNode);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToBFloatArray(arrayNode);
        case TypeTags.STRING_TAG:
            return jsonNodeToBStringArray(arrayNode);
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBBooleanArray(arrayNode);
        case TypeTags.ANY_TAG:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                refValueArray.add(i, (BRefType) getBValue(element));
            }
            return refValueArray;
        default:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                if (elementType.getTag() == TypeTags.MAP_TAG) {
                    refValueArray.add(i, jsonNodeToBMap(element, (BMapType) elementType));
                } else if (elementType instanceof BStructType) {
                    refValueArray.add(i, convertJSONNodeToStruct(element, (BStructType) elementType));
                } else if (elementType instanceof BArrayType) {
                    refValueArray.add(i, jsonNodeToBArray(element, (BArrayType) elementType));
                } else {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, elementType, getTypeName(element));
                }
            }
            return refValueArray;
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BMapType(org.ballerinalang.model.types.BMapType) BArrayType(org.ballerinalang.model.types.BArrayType) BType(org.ballerinalang.model.types.BType) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

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