Search in sources :

Example 1 with BXMLQName

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

the class CPU method execXMLOpcodes.

@SuppressWarnings("rawtypes")
private static void execXMLOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    int k;
    int localNameIndex;
    int uriIndex;
    int prefixIndex;
    BXML<?> xmlVal;
    BXMLQName xmlQName;
    switch(opcode) {
        case InstructionCodes.XMLATTRSTORE:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            xmlVal = (BXML) sf.refRegs[i];
            if (xmlVal == null) {
                handleNullRefError(ctx);
                break;
            }
            xmlQName = (BXMLQName) sf.refRegs[j];
            if (xmlQName == null) {
                handleNullRefError(ctx);
                break;
            }
            xmlVal.setAttribute(xmlQName.getLocalName(), xmlQName.getUri(), xmlQName.getPrefix(), sf.stringRegs[k]);
            break;
        case InstructionCodes.XMLATTRLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            xmlVal = (BXML) sf.refRegs[i];
            if (xmlVal == null) {
                handleNullRefError(ctx);
                break;
            }
            xmlQName = (BXMLQName) sf.refRegs[j];
            if (xmlQName == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.stringRegs[k] = xmlVal.getAttribute(xmlQName.getLocalName(), xmlQName.getUri(), xmlQName.getPrefix());
            break;
        case InstructionCodes.XML2XMLATTRS:
            i = operands[0];
            j = operands[1];
            xmlVal = (BXML) sf.refRegs[i];
            if (xmlVal == null) {
                sf.refRegs[j] = null;
                break;
            }
            sf.refRegs[j] = new BXMLAttributes(xmlVal);
            break;
        case InstructionCodes.S2QNAME:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            String qNameStr = sf.stringRegs[i];
            int parenEndIndex = qNameStr.indexOf('}');
            if (qNameStr.startsWith("{") && parenEndIndex > 0) {
                sf.stringRegs[j] = qNameStr.substring(parenEndIndex + 1, qNameStr.length());
                sf.stringRegs[k] = qNameStr.substring(1, parenEndIndex);
            } else {
                sf.stringRegs[j] = qNameStr;
                sf.stringRegs[k] = STRING_NULL_VALUE;
            }
            break;
        case InstructionCodes.NEWQNAME:
            localNameIndex = operands[0];
            uriIndex = operands[1];
            prefixIndex = operands[2];
            i = operands[3];
            String localname = sf.stringRegs[localNameIndex];
            localname = StringEscapeUtils.escapeXml11(localname);
            String prefix = sf.stringRegs[prefixIndex];
            prefix = StringEscapeUtils.escapeXml11(prefix);
            sf.refRegs[i] = new BXMLQName(localname, sf.stringRegs[uriIndex], prefix);
            break;
        case InstructionCodes.XMLSEQLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            xmlVal = (BXML) sf.refRegs[i];
            if (xmlVal == null) {
                handleNullRefError(ctx);
                break;
            }
            long index = sf.longRegs[j];
            sf.refRegs[k] = xmlVal.getItem(index);
            break;
        case InstructionCodes.XMLLOAD:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            xmlVal = (BXML<?>) sf.refRegs[i];
            if (xmlVal == null) {
                handleNullRefError(ctx);
                break;
            }
            String qname = sf.stringRegs[j];
            sf.refRegs[k] = xmlVal.children(qname);
            break;
        case InstructionCodes.XMLLOADALL:
            i = operands[0];
            j = operands[1];
            xmlVal = (BXML<?>) sf.refRegs[i];
            if (xmlVal == null) {
                handleNullRefError(ctx);
                break;
            }
            sf.refRegs[j] = xmlVal.children();
            break;
        case InstructionCodes.NEWXMLELEMENT:
        case InstructionCodes.NEWXMLCOMMENT:
        case InstructionCodes.NEWXMLTEXT:
        case InstructionCodes.NEWXMLPI:
        case InstructionCodes.XMLSEQSTORE:
        case InstructionCodes.NEWXMLSEQ:
            execXMLCreationOpcodes(ctx, sf, opcode, operands);
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : BXMLQName(org.ballerinalang.model.values.BXMLQName) BXMLAttributes(org.ballerinalang.model.values.BXMLAttributes) BString(org.ballerinalang.model.values.BString)

Example 2 with BXMLQName

use of org.ballerinalang.model.values.BXMLQName 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;
    }
}
Also used : BXMLQName(org.ballerinalang.model.values.BXMLQName) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with BXMLQName

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

the class XMLUtils method createXMLElement.

/**
 * Create an element type BXML.
 *
 * @param startTagName Name of the start tag of the element
 * @param endTagName Name of the end tag of element
 * @param defaultNsUri Default namespace URI
 * @return BXML Element type BXML
 */
public static BXML<?> createXMLElement(BXMLQName startTagName, BXMLQName endTagName, String defaultNsUri) {
    if (!StringUtils.isEqual(startTagName.getLocalName(), endTagName.getLocalName()) || !StringUtils.isEqual(startTagName.getUri(), endTagName.getUri()) || !StringUtils.isEqual(startTagName.getPrefix(), endTagName.getPrefix())) {
        throw new BallerinaException("start and end tag names mismatch: '" + startTagName + "' and '" + endTagName + "'");
    }
    // Validate whether the tag names are XML supported qualified names, according to the XML recommendation.
    XMLValidationUtils.validateXMLQName(startTagName);
    String nsUri = startTagName.getUri();
    OMElement omElement;
    if (defaultNsUri == null) {
        defaultNsUri = XMLConstants.NULL_NS_URI;
    }
    String prefix = startTagName.getPrefix() == null ? XMLConstants.DEFAULT_NS_PREFIX : startTagName.getPrefix();
    if (nsUri == null) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), defaultNsUri, prefix);
    } else if (nsUri.isEmpty()) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), nsUri, prefix);
    } else if (nsUri.equals(defaultNsUri)) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), defaultNsUri, prefix);
    } else {
        QName qname = getQName(startTagName.getLocalName(), nsUri, prefix);
        omElement = OM_FACTORY.createOMElement(qname);
        if (!defaultNsUri.isEmpty()) {
            omElement.declareDefaultNamespace(defaultNsUri);
        }
    }
    return new BXMLItem(omElement);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BXMLQName(org.ballerinalang.model.values.BXMLQName) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BXMLQName (org.ballerinalang.model.values.BXMLQName)3 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 BString (org.ballerinalang.model.values.BString)1 BXML (org.ballerinalang.model.values.BXML)1 BXMLAttributes (org.ballerinalang.model.values.BXMLAttributes)1 BXMLItem (org.ballerinalang.model.values.BXMLItem)1 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)1