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));
}
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);
}
}
}
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");
}
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>");
}
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;
}
}
Aggregations