use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class Strip method execute.
@Override
public void execute(Context ctx) {
BValue result = null;
try {
// Accessing Parameters.
BXML value = (BXML) ctx.getRefArgument(0);
result = value.strip();
} catch (Throwable e) {
ErrorHandler.handleXMLException(OPERATION, e);
}
// Setting output value.
ctx.setReturnValues(result);
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class ToJSON method execute.
@Override
public void execute(Context ctx) {
BJSON json = null;
try {
// Accessing Parameters
BXML xml = (BXML) ctx.getRefArgument(0);
BStruct optionsStruct = ((BStruct) ctx.getRefArgument(1));
String attributePrefix = optionsStruct.getStringField(0);
Boolean preserveNamespaces = optionsStruct.getBooleanField(0) == 1;
// Converting to json
json = XMLUtils.convertToJSON(xml, attributePrefix, preserveNamespaces);
} catch (Throwable e) {
ErrorHandler.handleXMLException("convert xml to json", e);
}
ctx.setReturnValues(json);
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class EntityBodyHandler method constructXmlDataSource.
/**
* Construct XMl data source from the underneath byte channel which is associated with the entity struct.
*
* @param entityStruct Represent an entity struct
* @return BXML data source which is kept in memory
*/
public static BXML constructXmlDataSource(BStruct entityStruct) {
try {
Channel byteChannel = getByteChannel(entityStruct);
if (byteChannel == null) {
throw new BallerinaIOException("Empty xml payload");
}
BXML xmlContent = XMLUtils.parse(byteChannel.getInputStream());
byteChannel.close();
return xmlContent;
} catch (IOException e) {
throw new BallerinaIOException("Error occurred while closing the channel", e);
}
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class GetXml method execute.
@Override
public void execute(Context context) {
BXML result;
try {
BStruct entityStruct = (BStruct) context.getRefArgument(FIRST_PARAMETER_INDEX);
MessageDataSource dataSource = EntityBodyHandler.getMessageDataSource(entityStruct);
if (dataSource != null) {
if (dataSource instanceof BXML) {
result = (BXML) dataSource;
} else {
// else, build the XML from the string representation of the payload.
result = XMLUtils.parse(dataSource.getMessageAsString());
}
} else {
result = EntityBodyHandler.constructXmlDataSource(entityStruct);
EntityBodyHandler.addMessageDataSource(entityStruct, result);
// Set byte channel to null, once the message data source has been constructed
entityStruct.addNativeData(ENTITY_BYTE_CHANNEL, null);
}
context.setReturnValues(result);
} catch (Throwable e) {
context.setReturnValues(MimeUtil.createEntityError(context, "Error occurred while retrieving xml data from entity : " + e.getMessage()));
}
}
use of org.ballerinalang.model.values.BXML in project ballerina by ballerina-lang.
the class RequestNativeFunctionSuccessTest method testGetXmlPayload.
@Test
public void testGetXmlPayload() {
BStruct inRequest = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, reqStruct);
BStruct entity = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageMime, entityStruct);
BStruct mediaType = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageMime, mediaTypeStruct);
String payload = "<name>ballerina</name>";
MimeUtil.setContentType(mediaType, entity, APPLICATION_XML);
entity.addNativeData(ENTITY_BYTE_CHANNEL, EntityBodyHandler.getEntityWrapper(payload));
inRequest.addNativeData(MESSAGE_ENTITY, entity);
inRequest.addNativeData(IS_BODY_BYTE_CHANNEL_ALREADY_SET, true);
BValue[] inputArg = { inRequest };
BValue[] returnVals = BRunUtil.invoke(result, "testGetXmlPayload", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertEquals(((BXML) returnVals[0]).getTextValue().stringValue(), "ballerina");
}
Aggregations