Пример #1
0
def writeaccessorsheader(outputfile, propname, propmetadata):
    type = propmetadata['type']

    if type == 'array':
        writelistaccessorsheader(outputfile,
                                 jscom.propnametocppname(propname),
                                 jscom.javatypetocppname(propmetadata['items']['javaType']))
    elif jscom.propmetadatatypeisscalar(propmetadata):
        writescalaraccessorsheader(outputfile,
                                   jscom.propnametocppname(propname),
                                   jscom.propmetadatatocpptypename(propmetadata))
    else:
        writetakeownershipaccessorsheader(outputfile,
                                          jscom.propnametocppname(propname),
                                          jscom.propmetadatatocpptypename(propmetadata))
Пример #2
0
def writeaccessorsheader(outputfile, propname, propmetadata):
    type = propmetadata['type']

    if type == 'array':
        writelistaccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.javatypetocppname(propmetadata['items']['javaType']))
    elif jscom.propmetadatatypeisscalar(propmetadata):
        writescalaraccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.propmetadatatocpptypename(propmetadata))
    else:
        writetakeownershipaccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.propmetadatatocpptypename(propmetadata))
Пример #3
0
def writeaccessorsheader(outputfile, propname, propmetadata):
    type = propmetadata['type']

    if type == jscom.JSON_TYPE_ARRAY:
        writelistaccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.propmetadatatocpptypename(propmetadata['items']))
    elif jscom.propmetadatatypeisscalar(propmetadata):
        writescalaraccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.propmetadatatocpptypename(propmetadata))
    else:
        writetakeownershipaccessorsheader(
            outputfile, jscom.propnametocppname(propname),
            jscom.propmetadatatocpptypename(propmetadata))
Пример #4
0
def writenullstackedlistenerfieldimplementation(istate, propname):

    istate.outputfile().write(
        string.Template("""
            if (fNextItemName == "${propname}")
                fTarget->Set${cpppropname}Null();
        """).substitute({
            'propname': propname,
            'cpppropname': jscom.propnametocppname(propname)
        }))
Пример #5
0
def writestackedlistenerfieldimplementation(istate, propname,
                                            cppeventdataexpression):

    istate.outputfile().write(
        string.Template("""
            if (fNextItemName == "${propname}")
                fTarget->Set${cpppropname}(${cppeventdataexpression});
        """).substitute({
            'propname': propname,
            'cpppropname': jscom.propnametocppname(propname),
            'cppeventdataexpression': cppeventdataexpression
        }))
Пример #6
0
def writenullstackedlistenerfieldimplementation(
        istate,
        propname):

    istate.outputfile().write(
        string.Template("""
            if (fNextItemName == "${propname}")
                fTarget->Set${cpppropname}Null();
        """).substitute({
            'propname': propname,
            'cpppropname': jscom.propnametocppname(propname)
        }))
Пример #7
0
def writestackedlistenerfieldimplementation(
        istate,
        propname,
        cppeventdataexpression):

    istate.outputfile().write(
        string.Template("""
            if (fNextItemName == "${propname}")
                fTarget->Set${cpppropname}(${cppeventdataexpression});
        """).substitute({
            'propname': propname,
            'cpppropname': jscom.propnametocppname(propname),
            'cppeventdataexpression': cppeventdataexpression
        }))
Пример #8
0
def writestackedlistenertypedobjectimplementation(istate, schema):
    outfile = istate.outputfile()
    naming = istate.naming()
    subtypenaming = CppParserSubTypeNaming(schema, naming)

    outfile.write(
        string.Template("""
${subtype_cppstackedlistenerclassname}::${subtype_cppstackedlistenerclassname}(
    ${cppsupermainlistenerclassname}* mainListener,
    ${cppsuperstackedlistenerclassname}* parent)
    :
    ${cppsuperstackedlistenerclassname}(mainListener, parent)
{
    fTarget = new ${subtype_cppmodelclassname}();
}


${subtype_cppstackedlistenerclassname}::~${subtype_cppstackedlistenerclassname}()
{
}


${subtype_cppmodelclassname}*
${subtype_cppstackedlistenerclassname}::Target()
{
    return fTarget;
}


bool
${subtype_cppstackedlistenerclassname}::Handle(const BJsonEvent& event)
{
    switch (event.EventType()) {


        case B_JSON_ARRAY_END:
            HandleError(B_NOT_ALLOWED, JSON_EVENT_LISTENER_ANY_LINE, "illegal state - unexpected start of array");
            break;


        case B_JSON_OBJECT_NAME:
            fNextItemName = event.Content();
            break;


        case B_JSON_OBJECT_END:
        {
            bool status = Pop() && (ErrorStatus() == B_OK);
            delete this;
            return status;
        }

""").substitute(jscom.uniondicts(naming.todict(), subtypenaming.todict())))

    # now extract the fields from the schema that need to be fed in.

    writestackedlistenerfieldsimplementation(istate, schema,
                                             jscom.CPP_TYPE_STRING,
                                             'B_JSON_STRING',
                                             'new BString(event.Content())')

    writestackedlistenerfieldsimplementation(istate, schema,
                                             jscom.CPP_TYPE_BOOLEAN,
                                             'B_JSON_TRUE', 'true')

    writestackedlistenerfieldsimplementation(istate, schema,
                                             jscom.CPP_TYPE_BOOLEAN,
                                             'B_JSON_FALSE', 'false')

    outfile.write('        case B_JSON_NULL:\n')
    outfile.write('        {\n')

    for propname, propmetadata in schema['properties'].items():
        # TODO; deal with array case somehow.
        if 'array' != propmetadata['type']:
            writenullstackedlistenerfieldimplementation(istate, propname)
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # number type is a bit complex because it can either be a double or it can be an
    # integral value.

    outfile.write('        case B_JSON_NUMBER:\n')
    outfile.write('        {\n')

    for propname, propmetadata in schema['properties'].items():
        propcpptypename = jscom.propmetadatatocpptypename(propmetadata)
        if propcpptypename == jscom.CPP_TYPE_INTEGER:
            writestackedlistenerfieldimplementation(istate, propname,
                                                    'event.ContentInteger()')
        elif propcpptypename == jscom.CPP_TYPE_NUMBER:
            writestackedlistenerfieldimplementation(istate, propname,
                                                    'event.ContentDouble()')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # object type; could be a sub-type or otherwise just drop into a placebo consumer to keep the parse
    # structure working.  This would most likely be additional sub-objects that are additional to the
    # expected schema.

    outfile.write('        case B_JSON_OBJECT_START:\n')
    outfile.write('        {\n')

    objectifclausekeyword = 'if'

    for propname, propmetadata in schema['properties'].items():
        if propmetadata['type'] == jscom.JSON_TYPE_OBJECT:
            subtypenaming = CppParserSubTypeNaming(propmetadata, naming)

            outfile.write('            %s (fNextItemName == "%s") {\n' %
                          (objectifclausekeyword, propname))
            outfile.write(
                '                %s* nextListener = new %s(fMainListener, this);\n'
                % (subtypenaming.cppstackedlistenerclassname(),
                   subtypenaming.cppstackedlistenerclassname()))
            outfile.write(
                '                fTarget->Set%s(nextListener->Target());\n' %
                (subtypenaming.cppmodelclassname()))
            outfile.write('                Push(nextListener);\n')
            outfile.write('            }\n')

            objectifclausekeyword = 'else if'

    outfile.write('            %s (1 == 1) {\n' % objectifclausekeyword)
    outfile.write(
        '                %s* nextListener = new %s(fMainListener, this);\n' %
        (naming.cppgeneralobjectstackedlistenerclassname(),
         naming.cppgeneralobjectstackedlistenerclassname()))
    outfile.write('                Push(nextListener);\n')
    outfile.write('            }\n')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # array type; could be an array of objects or otherwise just drop into a placebo consumer to keep
    # the parse structure working.

    outfile.write('        case B_JSON_ARRAY_START:\n')
    outfile.write('        {\n')

    objectifclausekeyword = 'if'

    for propname, propmetadata in schema['properties'].items():
        if propmetadata['type'] == jscom.JSON_TYPE_ARRAY:
            subtypenaming = CppParserSubTypeNaming(propmetadata['items'],
                                                   naming)

            outfile.write('            %s (fNextItemName == "%s") {\n' %
                          (objectifclausekeyword, propname))
            outfile.write(
                '                %s* nextListener = new %s(fMainListener, this);\n'
                % (subtypenaming.cppstackedlistlistenerclassname(),
                   subtypenaming.cppstackedlistlistenerclassname()))
            outfile.write(
                '                fTarget->Set%s(nextListener->Target());\n' %
                (jscom.propnametocppname(propname)))
            outfile.write('                Push(nextListener);\n')
            outfile.write('            }\n')

            objectifclausekeyword = 'else if'

    outfile.write('            %s (1 == 1) {\n' % objectifclausekeyword)
    outfile.write(
        '                %s* nextListener = new %s(fMainListener, this);\n' %
        (naming.cppsuperstackedlistenerclassname(),
         naming.cppgeneralarraystackedlistenerclassname()))
    outfile.write('                Push(nextListener);\n')
    outfile.write('            }\n')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    outfile.write("""
    }


    return ErrorStatus() == B_OK;
}
""")
Пример #9
0
def writestackedlistenertypedobjectimplementation(istate, schema):
    outfile = istate.outputfile()
    naming = istate.naming();
    subtypenaming = CppParserSubTypeNaming(schema, naming)

    outfile.write(
        string.Template("""
${subtype_cppstackedlistenerclassname}::${subtype_cppstackedlistenerclassname}(
    ${cppsupermainlistenerclassname}* mainListener,
    ${cppsuperstackedlistenerclassname}* parent)
    :
    ${cppsuperstackedlistenerclassname}(mainListener, parent)
{
    fTarget = new ${subtype_cppmodelclassname}();
}


${subtype_cppstackedlistenerclassname}::~${subtype_cppstackedlistenerclassname}()
{
}


${subtype_cppmodelclassname}*
${subtype_cppstackedlistenerclassname}::Target()
{
    return fTarget;
}


bool
${subtype_cppstackedlistenerclassname}::Handle(const BJsonEvent& event)
{
    switch (event.EventType()) {


        case B_JSON_ARRAY_END:
            HandleError(B_NOT_ALLOWED, JSON_EVENT_LISTENER_ANY_LINE, "illegal state - unexpected start of array");
            break;


        case B_JSON_OBJECT_NAME:
            fNextItemName = event.Content();
            break;


        case B_JSON_OBJECT_END:
        {
            bool status = Pop() && (ErrorStatus() == B_OK);
            delete this;
            return status;
        }

""").substitute(jscom.uniondicts(
            naming.todict(),
            subtypenaming.todict())))

    # now extract the fields from the schema that need to be fed in.

    writestackedlistenerfieldsimplementation(
        istate, schema,
        jscom.CPP_TYPE_STRING, 'B_JSON_STRING', 'new BString(event.Content())')

    writestackedlistenerfieldsimplementation(
        istate, schema,
        jscom.CPP_TYPE_BOOLEAN, 'B_JSON_TRUE', 'true')

    writestackedlistenerfieldsimplementation(
        istate, schema,
        jscom.CPP_TYPE_BOOLEAN, 'B_JSON_FALSE', 'false')

    outfile.write('        case B_JSON_NULL:\n')
    outfile.write('        {\n')

    for propname, propmetadata in schema['properties'].items():
        # TODO; deal with array case somehow.
        if 'array' != propmetadata['type']:
            writenullstackedlistenerfieldimplementation(istate, propname)
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # number type is a bit complex because it can either be a double or it can be an
    # integral value.

    outfile.write('        case B_JSON_NUMBER:\n')
    outfile.write('        {\n')

    for propname, propmetadata in schema['properties'].items():
        propcpptypename = jscom.propmetadatatocpptypename(propmetadata)
        if propcpptypename == jscom.CPP_TYPE_INTEGER:
            writestackedlistenerfieldimplementation(istate, propname, 'event.ContentInteger()')
        elif propcpptypename == jscom.CPP_TYPE_NUMBER:
            writestackedlistenerfieldimplementation(istate, propname, 'event.ContentDouble()')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # object type; could be a sub-type or otherwise just drop into a placebo consumer to keep the parse
    # structure working.  This would most likely be additional sub-objects that are additional to the
    # expected schema.

    outfile.write('        case B_JSON_OBJECT_START:\n')
    outfile.write('        {\n')

    objectifclausekeyword = 'if'

    for propname, propmetadata in schema['properties'].items():
        if propmetadata['type'] == jscom.JSON_TYPE_OBJECT:
            subtypenaming = CppParserSubTypeNaming(propmetadata, naming)

            outfile.write('            %s (fNextItemName == "%s") {\n' % (objectifclausekeyword, propname))
            outfile.write('                %s* nextListener = new %s(fMainListener, this);\n' % (
                subtypenaming.cppstackedlistenerclassname(),
                subtypenaming.cppstackedlistenerclassname()))
            outfile.write('                fTarget->Set%s(nextListener->Target());\n' % (
                subtypenaming.cppmodelclassname()))
            outfile.write('                Push(nextListener);\n')
            outfile.write('            }\n')

            objectifclausekeyword = 'else if'

    outfile.write('            %s (1 == 1) {\n' % objectifclausekeyword)
    outfile.write('                %s* nextListener = new %s(fMainListener, this);\n' % (
        naming.cppgeneralobjectstackedlistenerclassname(),
        naming.cppgeneralobjectstackedlistenerclassname()))
    outfile.write('                Push(nextListener);\n')
    outfile.write('            }\n')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    # array type; could be an array of objects or otherwise just drop into a placebo consumer to keep
    # the parse structure working.

    outfile.write('        case B_JSON_ARRAY_START:\n')
    outfile.write('        {\n')

    objectifclausekeyword = 'if'

    for propname, propmetadata in schema['properties'].items():
        if propmetadata['type'] == jscom.JSON_TYPE_ARRAY:
            subtypenaming = CppParserSubTypeNaming(propmetadata['items'], naming)

            outfile.write('            %s (fNextItemName == "%s") {\n' % (objectifclausekeyword, propname))
            outfile.write('                %s* nextListener = new %s(fMainListener, this);\n' % (
                subtypenaming.cppstackedlistlistenerclassname(),
                subtypenaming.cppstackedlistlistenerclassname()))
            outfile.write('                fTarget->Set%s(nextListener->Target());\n' % (
                jscom.propnametocppname(propname)))
            outfile.write('                Push(nextListener);\n')
            outfile.write('            }\n')

            objectifclausekeyword = 'else if'

    outfile.write('            %s (1 == 1) {\n' % objectifclausekeyword)
    outfile.write('                %s* nextListener = new %s(fMainListener, this);\n' % (
        naming.cppsuperstackedlistenerclassname(),
        naming.cppgeneralarraystackedlistenerclassname()))
    outfile.write('                Push(nextListener);\n')
    outfile.write('            }\n')
    outfile.write('            fNextItemName.SetTo("");\n')
    outfile.write('            break;\n')
    outfile.write('        }\n')

    outfile.write("""
    }


    return ErrorStatus() == B_OK;
}
""")