use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.
the class CPU method execXMLCreationOpcodes.
private static void execXMLCreationOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
int i;
int j;
int k;
int l;
BXML<?> xmlVal;
switch(opcode) {
case InstructionCodes.NEWXMLELEMENT:
i = operands[0];
j = operands[1];
k = operands[2];
l = operands[3];
BXMLQName startTagName = (BXMLQName) sf.refRegs[j];
BXMLQName endTagName = (BXMLQName) sf.refRegs[k];
try {
sf.refRegs[i] = XMLUtils.createXMLElement(startTagName, endTagName, sf.stringRegs[l]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLCOMMENT:
i = operands[0];
j = operands[1];
try {
sf.refRegs[i] = XMLUtils.createXMLComment(sf.stringRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLTEXT:
i = operands[0];
j = operands[1];
try {
sf.refRegs[i] = XMLUtils.createXMLText(sf.stringRegs[j]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.NEWXMLPI:
i = operands[0];
j = operands[1];
k = operands[2];
try {
sf.refRegs[i] = XMLUtils.createXMLProcessingInstruction(sf.stringRegs[j], sf.stringRegs[k]);
} catch (Exception e) {
ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
handleError(ctx);
}
break;
case InstructionCodes.XMLSEQSTORE:
i = operands[0];
j = operands[1];
xmlVal = (BXML<?>) sf.refRegs[i];
BXML<?> child = (BXML<?>) sf.refRegs[j];
xmlVal.addChildren(child);
break;
case InstructionCodes.NEWXMLSEQ:
i = operands[0];
sf.refRegs[i] = new BXMLSequence();
break;
}
}
use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.
the class JSONUtils method convertToXML.
/**
* Converts given json object to the corresponding xml.
*
* @param json JSON object to get the corresponding xml
* @param attributePrefix String prefix used for attributes
* @param arrayEntryTag String used as the tag in the arrays
* @return BXML XML representation of the given json object
*/
@SuppressWarnings("rawtypes")
public static BXML convertToXML(BJSON json, String attributePrefix, String arrayEntryTag) {
BXML xml;
JsonNode jsonNode = json.value();
List<BXML> omElementArrayList = traverseTree(jsonNode, attributePrefix, arrayEntryTag);
if (omElementArrayList.size() == 1) {
xml = omElementArrayList.get(0);
} else {
// There is a multi rooted node and create xml sequence from it
BRefValueArray elementsSeq = new BRefValueArray();
int count = omElementArrayList.size();
for (int i = 0; i < count; i++) {
elementsSeq.add(i, omElementArrayList.get(i));
}
xml = new BXMLSequence(elementsSeq);
}
return xml;
}
use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.
the class XMLUtils method concatenate.
/**
* Concatenate two XML sequences and produce a single sequence.
*
* @param firstSeq First XML sequence
* @param secondSeq Second XML sequence
* @return Concatenated XML sequence
*/
public static BXML<?> concatenate(BXML<?> firstSeq, BXML<?> secondSeq) {
BRefValueArray concatSeq = new BRefValueArray();
int j = 0;
// Load the content fully before concat the two
firstSeq.build();
secondSeq.build();
// Add all the items in the first sequence
if (firstSeq.getNodeType() == XMLNodeType.SEQUENCE) {
BRefValueArray seq = ((BXMLSequence) firstSeq).value();
for (int i = 0; i < seq.size(); i++) {
concatSeq.add(j++, seq.get(i));
}
} else {
concatSeq.add(j++, firstSeq);
}
// Add all the items in the second sequence
if (secondSeq.getNodeType() == XMLNodeType.SEQUENCE) {
BRefValueArray seq = ((BXMLSequence) secondSeq).value();
for (int i = 0; i < seq.size(); i++) {
concatSeq.add(j++, seq.get(i));
}
} else {
concatSeq.add(j++, secondSeq);
}
return new BXMLSequence(concatSeq);
}
use of org.ballerinalang.model.values.BXMLSequence 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);
}
use of org.ballerinalang.model.values.BXMLSequence 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");
}
Aggregations