int XMLDocument::ParseDocument() { char Ch; SkipWhiteSpaces(); while (currentChar!='\0' && !hasError) { XMLNodeParser * Node=new XMLNodeParser(this); Ch=GetCh(); if (Ch=='<') { NextChar(); if (Node->ParseNode()) { if (Node->type!=Type_Unknown) Childs.push_back(Node); SkipWhiteSpaces(); } else { delete Node; break; } } else { hasError=true; Error="Document should consist only from nodes rounded with <name /> or <name></name> tags"; break; } } return (int)hasError; }
/* ================ Lexer::ReadToken ================ */ const Token *Lexer::ReadToken( void ) { if ( tokPos == -1 ) tokPos = 0; else if ( tokIsUnread ) { tokIsUnread = false; return &token; } while ( bufPos < bufSize ) { // Single line comments if ( !lineComment.IsEmpty() && String::CmpPrefix((const char *)(buffer+bufPos), lineComment.c_str()) == 0 ) { if ( !(flags & LEXER_FULL_LINES) && tokPos > 0 ) { FinishToken(); return &token; } bufPos += 2; if ( tokPos > 0 ) { SkipRestOfLine(); return &token; } SkipRestOfLine(); continue; } // Multiline comments else if ( !blockComment[0].IsEmpty() && String::CmpPrefix((const char *)(buffer+bufPos), blockComment[0].c_str()) == 0 ) { if ( !(flags & LEXER_FULL_LINES) && tokPos > 0 ) { FinishToken(); return &token; } bufPos += 2; int l = line; SkipToCommentEnd(); // If changed line during comment, finish the line. if ( (flags & LEXER_FULL_LINES) && l != line && tokPos > 0 ) { FinishToken(); return &token; } continue; } switch( buffer[bufPos] ) { // Ignore whitespaces case ' ': case '\t': SkipWhiteSpaces(); if ( flags & LEXER_FULL_LINES ) AddToToken( ' ' ); else if ( tokPos > 0 ) { FinishToken(); return &token; } continue; // Newlines case '\n': case '\r': if ( !tokPos ) { FinishLine(); continue; } FinishLine(); return &token; // Quoted tokens case '\"': case '\'': if ( flags & LEXER_FULL_LINES ) { ParseQuoted(); continue; } if ( tokPos > 0 ) { FinishToken(); return &token; } ParseQuoted(); return &token; default: // Check for single char tokens if ( (flags & LEXER_FULL_LINES) == 0 && !singleTokenChars.IsEmpty() ) { char c = buffer[bufPos]; const char *chars = singleTokenChars.c_str(); int num = singleTokenChars.Length(); for( int i=0; i<num; i++ ) { if ( c != chars[i] ) continue; if ( tokPos == 0 ) { AddToToken( buffer[bufPos] ); bufPos++; } FinishToken(); return &token; } } // Add this char to the token AddToToken( buffer[bufPos] ); bufPos++; continue; } } if ( tokPos > 0 ) { FinishToken(); return &token; } line = 0; return NULL; }
wyBool LexHelper::GetObjectType(wyInt32 *obj) { wyChar obj_type[512] = {0}; if(!GetWordLex(obj_type, 512)) return wyFalse; if(strnicmp(obj_type, "OR", 2) == 0) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; else { if(strnicmp(obj_type, "REPLACE", 7) == 0) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } else return wyFalse; } } if(strnicmp(obj_type, "ALGORITHM=", 10) == 0) { if(strlen(obj_type) == 10) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } else if(strnicmp(obj_type, "ALGORITHM", 9) == 0) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; else { if(strlen(obj_type) == 1) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } } m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } if(strnicmp(obj_type, "DEFINER=", 8) == 0) { if(strlen(obj_type) == 8) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } else if(strnicmp(obj_type, "DEFINER", 7) == 0) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; else { if(strlen(obj_type) == 1) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } } m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) return wyFalse; } if(strnicmp(obj_type, "SQL",3) == 0) { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) // get SECURITY key word for views return wyFalse; else { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type,512)) // get SQL SECURITY parameter for views return wyFalse; else { m_curpos = SkipWhiteSpaces(); if(!GetWordLex(obj_type, 512)) // get probable Object type if above keywords are used return wyFalse; } } } if((stricmp(obj_type , "Table") == 0)) *obj = OBJ_TABLE; else if(stricmp(obj_type , "View") == 0) *obj = OBJ_VIEW; else if(stricmp(obj_type , "Database") == 0) *obj = OBJ_DATABASE; else if(stricmp(obj_type , "Procedure") == 0) *obj = OBJ_PROCEDURE; else if(stricmp(obj_type , "Function") == 0) *obj = OBJ_FUNCTION; else if(stricmp(obj_type , "Trigger") == 0) *obj = OBJ_TRIGGER; else if(stricmp(obj_type , "Event") == 0) *obj = OBJ_EVENT; else return wyFalse; return wyTrue; }
wyInt32 LexHelper::StartLex(wyChar * buffer, wyUInt32 dwcurpos, OPDETAILS *opdetails, wyBool isuse) { wyChar curr; wyChar table[SIZE_512] = {0}; wyChar totable[SIZE_512] = {0}; wyBool bop = wyFalse, btype = wyFalse, btable = wyFalse, bif = wyFalse, btotable = wyFalse, bnot = wyFalse; wyInt32 oper = 0, obj = 0; const wyChar* tempsubstr; m_buffer = buffer; m_curpos = 0; if(!m_buffer) return -1; while(1) { curr = *(m_buffer + m_curpos); if(curr == C_NULL) break; m_curpos = SkipWhiteSpaces(); // Get words. // The function will make a node depending upon of keywords or function name. if(bop == wyFalse) { if(GetOperationType(&oper, isuse) == wyFalse) break; bop = wyTrue; if(isuse == wyTrue) btype = wyTrue; } else if(btype == wyFalse) { if(GetObjectType(&obj) == wyFalse) break; btype = wyTrue; } else if(!btable) { if(!GetWordLex(table, SIZE_512)) break; if(stricmp(table, "if")== 0) { bif = wyTrue; } else if(stricmp(table, "not") == 0 && bif == wyTrue) { bnot = wyTrue; } else if((stricmp(table, "exists")!= 0)|| bif == wyFalse) { btable = wyTrue; m_all = wyTrue; } } if(m_all == wyTrue && oper == OPER_RENAME && obj == OBJ_TABLE) { if(btotable == wyFalse) { btotable = wyTrue; continue; } else { if(!GetWordLex(totable, SIZE_512)) break; if(stricmp(totable, "TO")== 0) continue; wyString strto(totable); wyString dbto, tblto; wyInt32 pos; if(strto.GetLength() == 0) break; pos = strto.Find(".", 0); if(pos != -1) { dbto.SetAs(strto.Substr(0, pos)); if(dbto.GetCharAt(0) == '`') { dbto.Erase(0, 1); dbto.Strip(1); } } else dbto.SetAs(""); tblto.SetAs(strto.Substr(pos + 1, strto.GetLength())); if(tblto.GetCharAt(0) == '`') { tblto.Erase(0,1); tblto.Strip(1); } strncpy(opdetails->totable, tblto.GetString(), SIZE_512-1); strncpy(opdetails->todb, dbto.GetString(), SIZE_512-1); } } if(btable) break; } if(!m_all) { return 1; } else { opdetails->op_type =(OPERTYPE)oper; opdetails->obj_type =(OBJTYPE)obj; wyString str, db, tbl; wyInt32 position; str.SetAs(table); if(str.GetLength() == 0) return 1; position = str.Find(".", 0); if(position != -1) { db.SetAs(str.Substr(0, position)); if(db.GetCharAt(0) == '`') { db.Erase(0, 1); db.Strip(1); } } else { position = -1; db.SetAs(""); } if((tempsubstr = str.Substr(position + 1, str.GetLength()))) { tbl.SetAs(tempsubstr); } if(tbl.GetCharAt(0) == '`') { tbl.Erase(0,1); tbl.Strip(1); } strncpy(opdetails->table, tbl.GetString(), SIZE_512-1); strncpy(opdetails->db, db.GetString(), SIZE_512-1); } return 0; }
CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, bool bStartTag) { m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (IsEOF()) { return nullptr; } CFX_ByteString tag_name, tag_space; bool bEndTag; GetTagName(tag_space, tag_name, bEndTag, bStartTag); if (tag_name.IsEmpty() || bEndTag) { return nullptr; } CXML_Element* pElement = new CXML_Element; pElement->m_pParent = pParent; pElement->SetTag(tag_space.AsStringC(), tag_name.AsStringC()); do { CFX_ByteString attr_space, attr_name; while (m_dwIndex < m_dwBufferSize) { SkipWhiteSpaces(); if (IsEOF()) { break; } if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) { break; } GetName(attr_space, attr_name); SkipWhiteSpaces(); if (IsEOF()) { break; } if (m_pBuffer[m_dwIndex] != '=') { break; } m_dwIndex++; SkipWhiteSpaces(); if (IsEOF()) { break; } CFX_WideString attr_value; GetAttrValue(attr_value); pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value); } m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (m_dwIndex < m_dwBufferSize || IsEOF()) { break; } } while (ReadNextBlock()); SkipWhiteSpaces(); if (IsEOF()) { return pElement; } uint8_t ch = m_pBuffer[m_dwIndex++]; if (ch == '/') { m_dwIndex++; m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; return pElement; } if (ch != '>') { m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; delete pElement; return nullptr; } SkipWhiteSpaces(); if (IsEOF()) { return pElement; } CFX_UTF8Decoder decoder; CFX_WideTextBuf content; bool bCDATA = false; int32_t iState = 0; do { while (m_dwIndex < m_dwBufferSize) { ch = m_pBuffer[m_dwIndex++]; switch (iState) { case 0: if (ch == '<') { iState = 1; } else if (ch == '&') { decoder.ClearStatus(); decoder.AppendChar(GetCharRef()); } else { decoder.Input(ch); } break; case 1: if (ch == '!') { iState = 2; } else if (ch == '?') { SkipLiterals("?>"); SkipWhiteSpaces(); iState = 0; } else if (ch == '/') { CFX_ByteString space, name; GetName(space, name); SkipWhiteSpaces(); m_dwIndex++; iState = 10; } else { content << decoder.GetResult(); CFX_WideString dataStr = content.MakeString(); if (!bCDATA && !m_bSaveSpaceChars) { dataStr.TrimRight(L" \t\r\n"); } InsertContentSegment(bCDATA, dataStr.AsStringC(), pElement); content.Clear(); decoder.Clear(); bCDATA = false; iState = 0; m_dwIndex--; CXML_Element* pSubElement = ParseElement(pElement, true); if (!pSubElement) { break; } pSubElement->m_pParent = pElement; pElement->m_Children.push_back( {CXML_Element::Element, pSubElement}); SkipWhiteSpaces(); } break; case 2: if (ch == '[') { SkipLiterals("]]>"); } else if (ch == '-') { m_dwIndex++; SkipLiterals("-->"); } else { SkipLiterals(">"); } decoder.Clear(); SkipWhiteSpaces(); iState = 0; break; } if (iState == 10) { break; } } m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) { break; } } while (ReadNextBlock()); content << decoder.GetResult(); CFX_WideString dataStr = content.MakeString(); if (!m_bSaveSpaceChars) { dataStr.TrimRight(L" \t\r\n"); } InsertContentSegment(bCDATA, dataStr.AsStringC(), pElement); content.Clear(); decoder.Clear(); bCDATA = false; return pElement; }
CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, FX_BOOL bStartTag) { m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (IsEOF()) { return NULL; } CFX_ByteString tag_name, tag_space; FX_BOOL bEndTag; GetTagName(tag_space, tag_name, bEndTag, bStartTag); if (tag_name.IsEmpty() || bEndTag) { return NULL; } CXML_Element* pElement; pElement = FX_NEW CXML_Element; if (pElement) { pElement->m_pParent = pParent; pElement->SetTag(tag_space, tag_name); } if (!pElement) { return NULL; } do { CFX_ByteString attr_space, attr_name; while (m_dwIndex < m_dwBufferSize) { SkipWhiteSpaces(); if (IsEOF()) { break; } if (!g_FXCRT_XML_IsNameIntro(m_pBuffer[m_dwIndex])) { break; } GetName(attr_space, attr_name); SkipWhiteSpaces(); if (IsEOF()) { break; } if (m_pBuffer[m_dwIndex] != '=') { break; } m_dwIndex ++; SkipWhiteSpaces(); if (IsEOF()) { break; } CFX_WideString attr_value; GetAttrValue(attr_value); pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value); } m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (m_dwIndex < m_dwBufferSize || IsEOF()) { break; } } while (ReadNextBlock()); SkipWhiteSpaces(); if (IsEOF()) { return pElement; } FX_BYTE ch = m_pBuffer[m_dwIndex ++]; if (ch == '/') { m_dwIndex ++; m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; return pElement; } if (ch != '>') { m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; delete pElement; return NULL; } SkipWhiteSpaces(); if (IsEOF()) { return pElement; } CFX_UTF8Decoder decoder; CFX_WideTextBuf content; FX_BOOL bCDATA = FALSE; FX_INT32 iState = 0; do { while (m_dwIndex < m_dwBufferSize) { ch = m_pBuffer[m_dwIndex ++]; switch (iState) { case 0: if (ch == '<') { iState = 1; } else if (ch == '&') { decoder.ClearStatus(); decoder.AppendChar(GetCharRef()); } else { decoder.Input(ch); } break; case 1: if (ch == '!') { iState = 2; } else if (ch == '?') { SkipLiterals(FX_BSTRC("?>")); SkipWhiteSpaces(); iState = 0; } else if (ch == '/') { CFX_ByteString space, name; GetName(space, name); SkipWhiteSpaces(); m_dwIndex ++; iState = 10; } else { content << decoder.GetResult(); CFX_WideString dataStr = content.GetWideString(); if (!bCDATA && !m_bSaveSpaceChars) { dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n"); } InsertContentSegment(bCDATA, dataStr, pElement); content.Clear(); decoder.Clear(); bCDATA = FALSE; iState = 0; m_dwIndex --; CXML_Element* pSubElement = ParseElement(pElement, TRUE); if (pSubElement == NULL) { break; } pSubElement->m_pParent = pElement; pElement->m_Children.Add((FX_LPVOID)CXML_Element::Element); pElement->m_Children.Add(pSubElement); SkipWhiteSpaces(); } break; case 2: if (ch == '[') { SkipLiterals(FX_BSTRC("]]>")); } else if (ch == '-') { m_dwIndex ++; SkipLiterals(FX_BSTRC("-->")); } else { SkipLiterals(FX_BSTRC(">")); } decoder.Clear(); SkipWhiteSpaces(); iState = 0; break; } if (iState == 10) { break; } } m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (iState == 10 || m_dwIndex < m_dwBufferSize || IsEOF()) { break; } } while (ReadNextBlock()); content << decoder.GetResult(); CFX_WideString dataStr = content.GetWideString(); if (!m_bSaveSpaceChars) { dataStr.TrimRight((FX_LPCWSTR)L" \t\r\n"); } InsertContentSegment(bCDATA, dataStr, pElement); content.Clear(); decoder.Clear(); bCDATA = FALSE; return pElement; }