SGML-Parser-OpenSP

 view release on metacpan or  search on metacpan

OpenSP.xs  view on Meta::CPAN


HV* SgmlParserOpenSP::location2hv(const SGMLApplication::Location l)
{
    HV* hv = newHV();

    hv_store(hv, "LineNumber", 10, uv_or_undef(l.lineNumber), HvvLineNumber);
    hv_store(hv, "ColumnNumber", 12, uv_or_undef(l.columnNumber), HvvColumnNumber);
    hv_store(hv, "ByteOffset", 10, uv_or_undef(l.byteOffset), HvvByteOffset);
    hv_store(hv, "EntityOffset", 12, uv_or_undef(l.entityOffset), HvvEntityOffset);
    hv_store(hv, "EntityName", 10, cs2sv(l.entityName), HvvEntityName);
    hv_store(hv, "FileName", 8, cs2sv(l.filename), HvvFileName);

    return hv;
}

HV* SgmlParserOpenSP::notation2hv(const SGMLApplication::Notation n)
{
    HV* hv = newHV();

    if (n.name.len > 0)
    {
        SV* sv = newRV_noinc((SV*)externalid2hv(n.externalId));
        hv_store(hv, "Name", 4, cs2sv(n.name), HvvName);
        hv_store(hv, "ExternalId", 10, sv, HvvExternalId);
    }

    return hv;
}

HV* SgmlParserOpenSP::externalid2hv(const SGMLApplication::ExternalId id)
{
    HV* hv = newHV();

    if (id.haveSystemId)
        hv_store(hv, "SystemId", 8, cs2sv(id.systemId), HvvSystemId);

    if (id.havePublicId)
        hv_store(hv, "PublicId", 8, cs2sv(id.publicId), HvvPublicId);

    if (id.haveGeneratedSystemId)
    {
        SV* sv = cs2sv(id.generatedSystemId);
        hv_store(hv, "GeneratedSystemId", 17, sv, HvvGeneratedSystemId);
    }

    return hv;
}

HV* SgmlParserOpenSP::entity2hv(const SGMLApplication::Entity e)
{
    HV* hv = newHV();

    hv_store(hv, "Name", 4, cs2sv(e.name), HvvName);

    // dataType
    switch (e.dataType)
    {
    case SGMLApplication::Entity::sgml:
        hv_store(hv, "DataType", 8, newSVpvn("sgml", 4), HvvDataType);
        break;
    case SGMLApplication::Entity::cdata:
        hv_store(hv, "DataType", 8, newSVpvn("cdata", 5), HvvDataType);
        break;
    case SGMLApplication::Entity::sdata:
        hv_store(hv, "DataType", 8, newSVpvn("sdata", 5), HvvDataType);
        break;
    case SGMLApplication::Entity::ndata:
        hv_store(hv, "DataType", 8, newSVpvn("ndata", 5), HvvDataType);
        break;
    case SGMLApplication::Entity::subdoc:
        hv_store(hv, "DataType", 8, newSVpvn("subdoc", 6), HvvDataType);
        break;
    case SGMLApplication::Entity::pi:
        hv_store(hv, "DataType", 8, newSVpvn("pi", 2), HvvDataType);
        break;
    }

    // declType
    switch (e.declType)
    {
    case SGMLApplication::Entity::general:
        hv_store(hv, "DeclType", 8, newSVpvn("general", 7), HvvDeclType);
        break;
    case SGMLApplication::Entity::parameter:
        hv_store(hv, "DeclType", 8, newSVpvn("parameter", 9), HvvDeclType);
        break;
    case SGMLApplication::Entity::doctype:
        hv_store(hv, "DeclType", 8, newSVpvn("doctype", 7), HvvDeclType);
        break;
    case SGMLApplication::Entity::linktype:
        hv_store(hv, "DeclType", 8, newSVpvn("linktype", 8), HvvDeclType);
        break;
    }

    if (e.isInternal)
    {
        hv_store(hv, "IsInternal", 10, newSViv(1), HvvIsInternal);
        hv_store(hv, "Text", 4, cs2sv(e.text), HvvText);
    }
    else
    {
        SV* sv1 = newRV_noinc((SV*)externalid2hv(e.externalId));
        SV* sv2 = newRV_noinc((SV*)attributes2hv(e.attributes, e.nAttributes));
        SV* sv3 = newRV_noinc((SV*)notation2hv(e.notation));
        
        hv_store(hv, "ExternalId", 10, sv1, HvvExternalId);
        hv_store(hv, "Attributes", 10, sv2, HvvAttributes);
        hv_store(hv, "Notation", 8, sv3, HvvNotation);
    }

    return hv;
}

HV* SgmlParserOpenSP::attributes2hv(const SGMLApplication::Attribute* attrs, size_t n)
{
    HV* hv = newHV();

    for (unsigned int i = 0; i < n; ++i)
    {
        HV* a = attribute2hv(attrs[i]);
        hv_store(a, "Index", 5, newSViv(i), HvvIndex);
        hv_store_ent(hv, sv_2mortal(cs2sv(attrs[i].name)), newRV_noinc((SV*)a), 0);
    }

    return hv;
}

HV* SgmlParserOpenSP::attribute2hv(const SGMLApplication::Attribute a)
{
    HV* hv = newHV();

    // Name => ...
    hv_store(hv, "Name", 4, cs2sv(a.name), HvvName);

    // Type => ...
    if (a.type == SGMLApplication::Attribute::cdata)
    {
        AV* av = newAV();

        for (unsigned int i = 0; i < a.nCdataChunks; ++i)
        {
            HV* cc = newHV();

            if (a.cdataChunks[i].isSdata)
            {
                SV* sv = cs2sv(a.cdataChunks[i].entityName);

                // redundant?
                hv_store(cc, "IsSdata", 7, newSViv(1), HvvIsSdata);
                hv_store(cc, "EntityName", 10, sv, HvvEntityName);
            }
            else if (a.cdataChunks[i].isNonSgml)
            {
                SV* sv = newSViv(a.cdataChunks[i].nonSgmlChar);
                
                // redundant?
                hv_store(cc, "IsNonSgml", 9, newSViv(1), HvvIsNonSgml);
                hv_store(cc, "NonSgmlChar", 11, sv, HvvNonSgmlChar);
            }

            hv_store(cc, "Data", 4, cs2sv(a.cdataChunks[i].data), HvvData);

            av_push(av, newRV_noinc((SV*)cc));
        }

        hv_store(hv, "Type", 4, newSVpvn("cdata", 5), HvvType);
        hv_store(hv, "CdataChunks", 11, newRV_noinc((SV*)av), HvvCdataChunks);

    }
    else if (a.type == SGMLApplication::Attribute::tokenized)
    {
        AV* entities = newAV();

        hv_store(hv, "Type", 4, newSVpvn("tokenized", 9), HvvType);
        hv_store(hv, "Tokens", 6, cs2sv(a.tokens), HvvTokens);
        hv_store(hv, "IsGroup", 7, newSViv((int)a.isGroup), HvvIsGroup);
        hv_store(hv, "IsId", 4, newSViv((int)a.isId), HvvIsId);

        for (unsigned int i = 0; i < a.nEntities; ++i)
        {
            av_push(entities, newRV_noinc((SV*)entity2hv(a.entities[i])));
        }

        SV* sv1 = newRV_noinc((SV*)notation2hv(a.notation));
        SV* sv2 = newRV_noinc((SV*)entities);
        
        hv_store(hv, "Notation", 8, sv1, HvvNotation);
        hv_store(hv, "Entities", 8, sv2, HvvEntities);
    }
    else if (a.type == SGMLApplication::Attribute::implied)
    {
        hv_store(hv, "Type", 4, newSVpvn("implied", 7), HvvType);
    }
    else if (a.type == SGMLApplication::Attribute::invalid)
    {
        hv_store(hv, "Type", 4, newSVpvn("invalid", 7), HvvType);
    }

    if (a.type == SGMLApplication::Attribute::cdata ||
        a.type == SGMLApplication::Attribute::tokenized)
    {
        switch (a.defaulted)
        {
        case SGMLApplication::Attribute::specified:
            hv_store(hv, "Defaulted", 9, newSVpvn("specified", 9), HvvDefaulted);
            break;
        case SGMLApplication::Attribute::definition:
            hv_store(hv, "Defaulted", 9, newSVpvn("definition", 10), HvvDefaulted);
            break;
        case SGMLApplication::Attribute::current:
            hv_store(hv, "Defaulted", 9, newSVpvn("current", 7), HvvDefaulted);
            break;
        }
    }

    return hv;
}

///////////////////////////////////////////////////////////////////////////
// ...
///////////////////////////////////////////////////////////////////////////

bool SgmlParserOpenSP::_hv_fetch_SvTRUE(HV* hv, const char* key, const I32 klen)
{
    SV** svp = hv_fetch(hv, key, klen, 0);
    return (svp && SvTRUE(*svp));
}

void SgmlParserOpenSP::_hv_fetch_pk_setOption(HV* hv, const char* key, const I32 klen,
                            ParserEventGeneratorKit& pk,
                            const enum ParserEventGeneratorKit::OptionWithArg o)
{
    SV** svp = hv_fetch(hv, key, klen, 0);
    SV* rv;

    if (!svp || !*svp)
        return;

    // character string
    if (SvPOK(*svp))
    {
        pk.setOption(o, SvPV_nolen(*svp));
        return;
    }

    if (!SvROK(*svp))
        return;

    rv = SvRV(*svp);

    if (!rv)
        return;

    if (!(SvTYPE(rv) == SVt_PVAV))
        return;

    // array reference
    AV* av = (AV*)rv;
    I32 len = av_len(av);

OpenSP.xs  view on Meta::CPAN

    if (!handler_can("appinfo"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    if (!e.none)
    {
        hv_store(hv, "None", 4, newSViv(0), HvvNone);
        hv_store(hv, "String", 6, cs2sv(e.string), HvvString);
    }
    else
    {
        hv_store(hv, "None", 4, newSViv(1), HvvNone);
    }

    dispatchEvent("appinfo", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::pi
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::pi(const PiEvent& e)
{
    if (!handler_can("processing_instruction"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    hv_store(hv, "EntityName", 10, cs2sv(e.entityName), HvvEntityName);
    hv_store(hv, "Data", 4, cs2sv(e.data), HvvData);
    dispatchEvent("processing_instruction", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::startElement
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::startElement(const StartElementEvent& e)
{
    if (!handler_can("start_element"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();
    SV* sv = newRV_noinc((SV*)attributes2hv(e.attributes, e.nAttributes));

    hv_store(hv, "Name", 4, cs2sv(e.gi), HvvName);
    hv_store(hv, "Attributes", 10, sv, HvvAttributes);

    switch (e.contentType)
    {
    case SGMLApplication::StartElementEvent::empty:
        hv_store(hv, "ContentType", 11, newSVpvn("empty", 5), HvvContentType);
        break;
    case SGMLApplication::StartElementEvent::cdata:
        hv_store(hv, "ContentType", 11, newSVpvn("cdata", 5), HvvContentType);
        break;
    case SGMLApplication::StartElementEvent::rcdata:
        hv_store(hv, "ContentType", 11, newSVpvn("rcdata", 6), HvvContentType);
        break;
    case SGMLApplication::StartElementEvent::mixed:
        hv_store(hv, "ContentType", 11, newSVpvn("mixed", 5), HvvContentType);
        break;
    case SGMLApplication::StartElementEvent::element:
        hv_store(hv, "ContentType", 11, newSVpvn("element", 7), HvvContentType);
        break;
    }

    hv_store(hv, "Included", 8, newSViv(e.included ? 1 : 0), HvvIncluded);

    dispatchEvent("start_element", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::endElement
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::endElement(const EndElementEvent& e)
{
    if (!handler_can("end_element"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    hv_store(hv, "Name", 4, cs2sv(e.gi), HvvName);

    dispatchEvent("end_element", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::data
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::data(const DataEvent& e)
{
    if (!handler_can("data"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    hv_store(hv, "Data", 4, cs2sv(e.data), HvvData);

    dispatchEvent("data", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::sdata
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::sdata(const SdataEvent& e)
{
    if (!handler_can("sdata"))
        return;

    updatePosition(e.pos);

OpenSP.xs  view on Meta::CPAN

///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::generalEntity(const GeneralEntityEvent& e)
{
    if (!handler_can("general_entity"))
        return;

    HV* hv = newHV();

    hv_store(hv, "Entity", 6, newRV_noinc((SV*)entity2hv(e.entity)), HvvEntity);

    dispatchEvent("general_entity", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::commentDecl
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::commentDecl(const CommentDeclEvent& e)
{
    if (!handler_can("comment_decl"))
        return;

    updatePosition(e.pos);

    AV* av = newAV();
    HV* hv = newHV();

    for (unsigned int i = 0; i < e.nComments; ++i)
    {
        HV* comment = newHV();
        hv_store(comment, "Comment", 7, cs2sv(e.comments[i]), HvvComment);
        hv_store(comment, "Separator", 9, cs2sv(e.seps[i]), HvvSeparator);
        av_push(av, newRV_noinc((SV*)comment));
    }

    hv_store(hv, "Comments", 8, newRV_noinc((SV*)av), HvvComments);

    dispatchEvent("comment_decl", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::markedSectionStart
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::markedSectionStart(const MarkedSectionStartEvent& e)
{
    if (!handler_can("marked_section_start"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();
    AV* av = newAV();

    switch (e.status)
    {
    case SGMLApplication::MarkedSectionStartEvent::include:
        hv_store(hv, "Status", 6, newSVpvn("include", 7), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionStartEvent::rcdata:
        hv_store(hv, "Status", 6, newSVpvn("rcdata", 6), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionStartEvent::cdata:
        hv_store(hv, "Status", 6, newSVpvn("cdata", 5), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionStartEvent::ignore:
        hv_store(hv, "Status", 6, newSVpvn("ignore", 6), HvvStatus);
        break;
    }

    for (unsigned int i = 0; i < e.nParams; ++i)
    {
        HV* param = newHV();

        switch (e.params[i].type)
        {
        case SGMLApplication::MarkedSectionStartEvent::Param::temp:
            hv_store(param, "Type", 6, newSVpvn("temp", 4), HvvType);
            break;
        case SGMLApplication::MarkedSectionStartEvent::Param::include:
            hv_store(param, "Type", 6, newSVpvn("include", 7), HvvType);
            break;
        case SGMLApplication::MarkedSectionStartEvent::Param::rcdata:
            hv_store(param, "Type", 6, newSVpvn("rcdata", 6), HvvType);
            break;
        case SGMLApplication::MarkedSectionStartEvent::Param::cdata:
            hv_store(param, "Type", 6, newSVpvn("cdata", 5), HvvType);
            break;
        case SGMLApplication::MarkedSectionStartEvent::Param::ignore:
            hv_store(param, "Type", 6, newSVpvn("ignore", 6), HvvType);
            break;
        case SGMLApplication::MarkedSectionStartEvent::Param::entityRef:
            hv_store(param, "Type", 6, newSVpvn("entityRef", 9), HvvType);
            hv_store(param, "EntityName", 10, cs2sv(e.params[i].entityName), HvvEntityName);
            break;
        }

        av_push(av, newRV_noinc((SV*)av));
    }

    hv_store(hv, "Params", 6, newRV_noinc((SV*)av), HvvParams);

    dispatchEvent("marked_section_start", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::markedSectionEnd
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::markedSectionEnd(const MarkedSectionEndEvent& e)
{
    if (!handler_can("marked_section_end"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    switch (e.status)
    {
    case SGMLApplication::MarkedSectionEndEvent::include:
        hv_store(hv, "Status", 6, newSVpvn("include", 7), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionEndEvent::rcdata:
        hv_store(hv, "Status", 6, newSVpvn("rcdata", 6), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionEndEvent::cdata:
        hv_store(hv, "Status", 6, newSVpvn("cdata", 5), HvvStatus);
        break;
    case SGMLApplication::MarkedSectionEndEvent::ignore:
        hv_store(hv, "Status", 6, newSVpvn("ignore", 6), HvvStatus);
        break;
    }

    dispatchEvent("marked_section_end", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::ignoredChars
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::ignoredChars(const IgnoredCharsEvent& e)
{
    if (!handler_can("ignored_chars"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    hv_store(hv, "Data", 4, cs2sv(e.data), HvvData);

    dispatchEvent("ignored_chars", hv);
}

///////////////////////////////////////////////////////////////////////////
// SgmlParserOpenSP::error
///////////////////////////////////////////////////////////////////////////

void SgmlParserOpenSP::error(const ErrorEvent& e)
{
    if (!handler_can("error"))
        return;

    updatePosition(e.pos);

    HV* hv = newHV();

    hv_store(hv, "Message", 7, cs2sv(e.message), HvvMessage);

    switch (e.type)
    {
    case SGMLApplication::ErrorEvent::quantity:
       hv_store(hv, "Type", 4, newSVpvn("quantity", 8), HvvType);
       break;
    case SGMLApplication::ErrorEvent::idref:
       hv_store(hv, "Type", 4, newSVpvn("idref", 5), HvvType);
       break;
    case SGMLApplication::ErrorEvent::capacity:
       hv_store(hv, "Type", 4, newSVpvn("capacity", 8), HvvType);
       break;
    case SGMLApplication::ErrorEvent::otherError:
       hv_store(hv, "Type", 4, newSVpvn("otherError", 10), HvvType);
       break;
    case SGMLApplication::ErrorEvent::warning:
       hv_store(hv, "Type", 4, newSVpvn("warning", 7), HvvType);
       break;
    case SGMLApplication::ErrorEvent::info:



( run in 0.762 second using v1.01-cache-2.11-cpan-39bf76dae61 )