Search in sources :

Example 1 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class PackageInfoWriter method writeAttributeInfo.

private static void writeAttributeInfo(DataOutputStream dataOutStream, AttributeInfo attributeInfo) throws IOException {
    AttributeInfo.Kind attributeKind = attributeInfo.getKind();
    dataOutStream.writeInt(attributeInfo.getAttributeNameIndex());
    switch(attributeKind) {
        case CODE_ATTRIBUTE:
            CodeAttributeInfo codeAttributeInfo = (CodeAttributeInfo) attributeInfo;
            dataOutStream.writeInt(codeAttributeInfo.codeAddrs);
            dataOutStream.writeShort(codeAttributeInfo.maxLongLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxDoubleLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxStringLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxIntLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxByteLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxRefLocalVars);
            dataOutStream.writeShort(codeAttributeInfo.maxLongRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxDoubleRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxStringRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxIntRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxByteRegs);
            dataOutStream.writeShort(codeAttributeInfo.maxRefRegs);
            break;
        case VARIABLE_TYPE_COUNT_ATTRIBUTE:
            VarTypeCountAttributeInfo varCountAttributeInfo = (VarTypeCountAttributeInfo) attributeInfo;
            dataOutStream.writeShort(varCountAttributeInfo.getMaxLongVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxDoubleVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxStringVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxIntVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxByteVars());
            dataOutStream.writeShort(varCountAttributeInfo.getMaxRefVars());
            break;
        case ERROR_TABLE:
            ErrorTableAttributeInfo errTable = (ErrorTableAttributeInfo) attributeInfo;
            ErrorTableEntry[] errorTableEntries = errTable.getErrorTableEntriesList().toArray(new ErrorTableEntry[0]);
            dataOutStream.writeShort(errorTableEntries.length);
            for (ErrorTableEntry errorTableEntry : errorTableEntries) {
                dataOutStream.writeInt(errorTableEntry.ipFrom);
                dataOutStream.writeInt(errorTableEntry.ipTo);
                dataOutStream.writeInt(errorTableEntry.ipTarget);
                dataOutStream.writeInt(errorTableEntry.priority);
                dataOutStream.writeInt(errorTableEntry.errorStructCPIndex);
            }
            break;
        case LOCAL_VARIABLES_ATTRIBUTE:
            LocalVariableAttributeInfo localVarAttrInfo = (LocalVariableAttributeInfo) attributeInfo;
            LocalVariableInfo[] localVarInfoArray = localVarAttrInfo.localVars.toArray(new LocalVariableInfo[0]);
            dataOutStream.writeShort(localVarInfoArray.length);
            for (LocalVariableInfo localVariableInfo : localVarInfoArray) {
                writeLocalVariableInfo(dataOutStream, localVariableInfo);
            }
            break;
        case LINE_NUMBER_TABLE_ATTRIBUTE:
            LineNumberTableAttributeInfo lnNoTblAttrInfo = (LineNumberTableAttributeInfo) attributeInfo;
            LineNumberInfo[] lineNumberInfoEntries = lnNoTblAttrInfo.getLineNumberInfoEntries();
            dataOutStream.writeShort(lineNumberInfoEntries.length);
            for (LineNumberInfo lineNumberInfo : lineNumberInfoEntries) {
                writeLineNumberInfo(dataOutStream, lineNumberInfo);
            }
            break;
        case DEFAULT_VALUE_ATTRIBUTE:
            DefaultValueAttributeInfo defaultValAttrInfo = (DefaultValueAttributeInfo) attributeInfo;
            writeDefaultValue(dataOutStream, defaultValAttrInfo.getDefaultValue());
            break;
        case PARAMETER_DEFAULTS_ATTRIBUTE:
            ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = (ParamDefaultValueAttributeInfo) attributeInfo;
            DefaultValue[] defaultValues = paramDefaultValAttrInfo.getDefaultValueInfo();
            dataOutStream.writeShort(defaultValues.length);
            for (DefaultValue defaultValue : defaultValues) {
                writeDefaultValue(dataOutStream, defaultValue);
            }
            break;
    }
// TODO Support other types of attributes
}
Also used : VarTypeCountAttributeInfo(org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo) LineNumberTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo) CodeAttributeInfo(org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) LineNumberTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) AttributeInfo(org.wso2.ballerinalang.programfile.attributes.AttributeInfo) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) VarTypeCountAttributeInfo(org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) CodeAttributeInfo(org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo)

Example 2 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class ParserUtils method createNewStruct.

/**
 * Create new struct.
 *
 * @param name   name of the struct
 * @param fields field definiton statements
 * @return {Function} function
 */
private static Struct createNewStruct(String name, List<BLangVariable> fields, String fileName) {
    Struct struct = new Struct(name);
    fields.forEach((field) -> {
        String defaultValue = null;
        if (field.getInitialExpression() != null) {
            defaultValue = ((BLangLiteral) field.getInitialExpression()).getValue().toString();
        }
        StructField structField = createNewStructField(field.getName().getValue(), field.getTypeNode().type.toString(), defaultValue);
        struct.addStructField(structField);
    });
    struct.setFileName(fileName);
    return struct;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) StructField(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.StructField) Struct(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct)

Example 3 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class CodeGenerator method getDefaultValueAttributeInfo.

private DefaultValueAttributeInfo getDefaultValueAttributeInfo(BLangLiteral literalExpr) {
    DefaultValue defaultValue = getDefaultValue(literalExpr);
    UTF8CPEntry defaultValueAttribUTF8CPEntry = new UTF8CPEntry(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE.toString());
    int defaultValueAttribNameIndex = currentPkgInfo.addCPEntry(defaultValueAttribUTF8CPEntry);
    return new DefaultValueAttributeInfo(defaultValueAttribNameIndex, defaultValue);
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 4 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project ballerina by ballerina-lang.

the class CodeGenerator method addParameterDefaultValues.

private void addParameterDefaultValues(BLangInvokableNode invokableNode, CallableUnitInfo callableUnitInfo) {
    int paramDefaultsAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE.value());
    ParamDefaultValueAttributeInfo paramDefaulValAttrInfo = new ParamDefaultValueAttributeInfo(paramDefaultsAttrNameIndex);
    // Only named parameters can have default values.
    for (BLangVariableDef param : invokableNode.defaultableParams) {
        DefaultValue defaultVal = getDefaultValue((BLangLiteral) param.var.expr);
        paramDefaulValAttrInfo.addParamDefaultValueInfo(defaultVal);
    }
    callableUnitInfo.addAttributeInfo(AttributeInfo.Kind.PARAMETER_DEFAULTS_ATTRIBUTE, paramDefaulValAttrInfo);
}
Also used : DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 5 with DefaultValue

use of org.wso2.ballerinalang.programfile.DefaultValue in project carbon-business-process by wso2.

the class HTRenderingApiImpl method getRenderingOutputElements.

/**
 * Function to retrieve output rendering elements
 *
 * @param taskIdentifier interested task identifier
 * @return set of output renderings wrapped within OutputType
 * @throws IllegalArgumentFault        error occured while retrieving renderings from task definition
 * @throws IOException                 If an error occurred while reading from the input source
 * @throws SAXException                If the xml content in the input source is invalid
 * @throws IllegalOperationFault
 * @throws IllegalAccessFault
 * @throws IllegalStateFault
 * @throws XPathExpressionException    If error occurred while xpath evaluation
 * @throws GetRenderingsFaultException If unable to find unique id for the wso2:output rendering element
 */
private OutputType getRenderingOutputElements(URI taskIdentifier) throws IllegalArgumentFault, IOException, SAXException, IllegalOperationFault, IllegalAccessFault, IllegalStateFault, XPathExpressionException, GetRenderingsFaultException {
    QName renderingType = new QName(htRenderingNS, "output", "wso2");
    String outputRenderings = (String) taskOps.getRendering(taskIdentifier, renderingType);
    // create output element
    OutputType renderingOutputs = null;
    // check availability of output renderings
    if (outputRenderings != null && outputRenderings.length() > 0) {
        // parse output renderings
        Element outputRenderingsElement = DOMUtils.stringToDOM(outputRenderings);
        // retrieve output rendering elements
        NodeList outputElementList = outputRenderingsElement.getElementsByTagNameNS(htRenderingNS, "element");
        if (outputElementList != null && outputElementList.getLength() > 0) {
            int outputElementNum = outputElementList.getLength();
            OutputElementType[] outputElements = new OutputElementType[outputElementNum];
            // TODO get task output message from the cache
            // (if not in the cache) retrieve saved output using HumanTaskClientAPI
            String savedOutputMsg = (String) taskOps.getOutput(taskIdentifier, null);
            // Element to hold parsed saved output message
            Element savedOutputElement = null;
            if (savedOutputMsg != null && savedOutputMsg.length() > 0) {
                savedOutputElement = DOMUtils.stringToDOM(savedOutputMsg);
            }
            for (int i = 0; i < outputElementNum; i++) {
                Element tempElement = (Element) outputElementList.item(i);
                if (tempElement.hasAttribute("id")) {
                    // Retrieve element data
                    String elementID = tempElement.getAttribute("id");
                    String label = tempElement.getElementsByTagNameNS(htRenderingNS, "label").item(0).getTextContent();
                    String xpath = tempElement.getElementsByTagNameNS(htRenderingNS, "xpath").item(0).getTextContent();
                    String defaultValue = tempElement.getElementsByTagNameNS(htRenderingNS, "default").item(0).getTextContent();
                    // set the readOnly attribute if Exists, default false
                    String readOnly = "false";
                    if (tempElement.hasAttribute("readOnly")) {
                        readOnly = tempElement.getAttribute("readOnly");
                    }
                    // set element data in the response message
                    outputElements[i] = new OutputElementType();
                    // set ID
                    outputElements[i].setId(elementID);
                    // set label
                    outputElements[i].setLabel(label);
                    // set xpath
                    outputElements[i].setXpath(xpath);
                    // set value
                    Element valueElement = (Element) tempElement.getElementsByTagNameNS(htRenderingNS, "value").item(0);
                    outputElements[i].setValue(createOutRenderElementValue(valueElement));
                    if (readOnly.equals("true")) {
                        outputElements[i].setReadOnly(true);
                    } else {
                        outputElements[i].setReadOnly(false);
                    }
                    if (savedOutputElement != null) {
                        // resolve default value
                        String savedOutMessageValue = evaluateXPath(xpath, savedOutputElement, outputRenderingsElement.getOwnerDocument());
                        if (savedOutMessageValue == null) {
                            outputElements[i].set_default(defaultValue);
                        } else {
                            outputElements[i].set_default(savedOutMessageValue);
                        }
                    } else {
                        // add default value specified in the HT rendering definition
                        outputElements[i].set_default(defaultValue);
                    }
                } else {
                    // no unique id for the element
                    log.error("Unable to find unique id for the wso2:output rendering element");
                    throw new GetRenderingsFaultException("Unable to find unique id for the wso2:output rendering element");
                }
            }
            renderingOutputs = new OutputType();
            renderingOutputs.setElement(outputElements);
        }
    }
    return renderingOutputs;
}
Also used : OutputElementType(org.wso2.carbon.humantask.rendering.api.OutputElementType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) GetRenderingsFaultException(org.wso2.carbon.humantask.rendering.api.GetRenderingsFaultException) OutputType(org.wso2.carbon.humantask.rendering.api.OutputType)

Aggregations

BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)4 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)4 StructDoc (org.ballerinalang.docgen.model.StructDoc)2 DefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo)2 UTF8CPEntry (org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 Struct (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Struct)1 StructField (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.StructField)1 Field (org.ballerinalang.docgen.model.Field)1 Page (org.ballerinalang.docgen.model.Page)1 Test (org.testng.annotations.Test)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)1 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1