Пример #1
0
 void Handle(char c)
 {
     switch(state)
     {
     case State::Default:
         ReadDefault(c);
         break;
     case State::ReadTagName:
         ReadTagName(c);
         break;
     case State::SkipTag:
         SkipTag(c);
         break;
     case State::ReadTag:
         ReadTag(c);
         break;
     case State::ReadAttribute:
         ReadAttribute(c);
         break;
     case State::SkipAttribute:
         SkipAttribute(c);
         break;
     case State::SkipComment:
         SkipComment(c);
         break;
     }
 }
//----------------------------------------------------------------------------
int medHTMLTemplateParserBlock::PreParseTag( wxString *inputTemplate, int &parsingPos )
//----------------------------------------------------------------------------
{
    int templateSize=inputTemplate->size();
    wxString tagName;

    // OPENING TAG
    if (SubStringCompare(inputTemplate,MED_HMTL_TAG_OPENING,parsingPos))
    {
        int substitutionType;

        int subPos;

        parsingPos+=Strlen(MED_HMTL_TAG_OPENING);
        //cheking the opening tag type
        if (SubStringCompare(inputTemplate,MED_HTML_TAG_VARIABLE,parsingPos))
        {
            substitutionType=MED_HTML_SUBSTITUTION_VARIABLE;
            parsingPos+=Strlen(MED_HTML_TAG_VARIABLE);
        }
        else if (SubStringCompare(inputTemplate,MED_HTML_TAG_LOOP,parsingPos))
        {
            substitutionType=MED_HTML_SUBSTITUTION_BLOCK;
            parsingPos+=Strlen(MED_HTML_TAG_LOOP);
        }
        else if (SubStringCompare(inputTemplate,MED_HTML_TAG_IF,parsingPos))
        {
            substitutionType=MED_HTML_SUBSTITUTION_BLOCK;
            parsingPos+=Strlen(MED_HTML_TAG_IF);
        }
        else if (SubStringCompare(inputTemplate,MED_HTML_TAG_ELSE,parsingPos))
        {
            //if I find a else tag I update local variables and continue parsing inside this block
            if (m_BlockType!=MED_HTML_TEMPLATE_IF)
                mafLogMessage("medHTMLTemplateParserBlock: [MAFElse] found inside a non [MAFIf] block ");
            parsingPos+=Strlen(MED_HTML_TAG_ELSE);

            ReadTagName(inputTemplate,parsingPos,tagName);

            int comp=tagName.Cmp(m_BlockName);

            if (comp!=0)
                mafLogMessage(" ELSE medHTMLTemplateParserBlock: warning wrong closing tag");
            else m_IfChars=m_PreParsedHTML.size();

            //return because Else tag has not a tag name
            return true;
        }
        else
            mafLogMessage("medHTMLTemplateParserBlock: Invalid TAG Type");

        ReadTagName(inputTemplate,parsingPos,tagName);
        subPos=AddSubstitution(&tagName,substitutionType);



        //Parsing sub tags
        if (subPos>=0 && (substitutionType==MED_HTML_TEMPLATE_IF || substitutionType==MED_HTML_TEMPLATE_LOOP))
        {
            int tablePos=m_Substitutions[subPos].SubsTablePos;
            int blockPos=m_SubstitutionTable[tablePos].Pos;

            if (m_SubstitutionTable[tablePos].Type==MED_HTML_SUBSTITUTION_BLOCK)
                m_SubBlocks[blockPos]->PreParse(inputTemplate,parsingPos);
            else if (m_SubstitutionTable[tablePos].Type==MED_HTML_SUBSTITUTION_BLOCK_ARRAY)
            {
                int actualParsingPos=parsingPos;
                for (int i=0; i<m_LoopsNumber; i++)
                {
                    //Every sub block must be parsed, each loop parsing pos will be increased so we need to
                    //move the parsing pos back in order to parse the same text to each loop
                    parsingPos=actualParsingPos;
                    int x=m_SubBlocksArray[blockPos].size();
                    medHTMLTemplateParserBlock *blockPointer=m_SubBlocksArray[blockPos][i];
                    blockPointer->PreParse(inputTemplate,parsingPos);
                }
            }
            else if (m_SubstitutionTable[tablePos].Type=MED_HTML_SUBSTITUTION_FORWARD_UP)
            {
                mafLogMessage("Tag Name already used");
                return false;
            }
        }

    }
    //CLOSING TAG
    else if (SubStringCompare(inputTemplate,MED_HTML_TAG_CLOSING,parsingPos))
    {
        parsingPos+=Strlen(MED_HTML_TAG_CLOSING);

        if (SubStringCompare(inputTemplate,MED_HTML_TAG_LOOP,parsingPos))
        {
            parsingPos+=Strlen(MED_HTML_TAG_LOOP);
            if (m_BlockType!=MED_HTML_TEMPLATE_LOOP)
                mafLogMessage("medHTMLTemplateParserBlock: Invalid closing TAG Type");
        }
        else if (SubStringCompare(inputTemplate,MED_HTML_TAG_IF,parsingPos))
        {
            parsingPos+=Strlen(MED_HTML_TAG_IF);
            if (m_BlockType!=MED_HTML_TEMPLATE_IF)
                mafLogMessage("medHTMLTemplateParserBlock: Invalid closing TAG Type");
            //if there is not an ELSE TAG the if char are all chars now
            if (m_IfChars==0)
                m_IfChars=m_PreParsedHTML.size();
        }
        else
            mafLogMessage("medHTMLTemplateParserBlock: Invalid TAG Type");

        ReadTagName(inputTemplate,parsingPos,tagName);


        int comp=tagName.Cmp(m_BlockName);

        if (comp!=0)
            mafLogMessage("medHTMLTemplateParserBlock: warning wrong closing tag");

        return comp;
    }
    else
    {
        //there is a '[' char but there is not a MAF tag
        //in this case the input must be copied to pre parsed string
        //we add '[' char to m_PreParsedHTML and increase parsingPos
        //in order to avoid a non ending loop in the caller function
        m_PreParsedHTML.Append("[");
        parsingPos++;
    }

    //continue parsing
    return true;
}