1 /*************************************************************************** 2 * This file is part of the Lime Report project * 3 * Copyright (C) 2015 by Alexander Arin * 4 * arin_a@bk.ru * 5 * * 6 ** GNU General Public License Usage ** 7 * * 8 * This library is free software: you can redistribute it and/or modify * 9 * it under the terms of the GNU General Public License as published by * 10 * the Free Software Foundation, either version 3 of the License, or * 11 * (at your option) any later version. * 12 * You should have received a copy of the GNU General Public License * 13 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 14 * * 15 ** GNU Lesser General Public License ** 16 * * 17 * This library is free software: you can redistribute it and/or modify * 18 * it under the terms of the GNU Lesser General Public License as * 19 * published by the Free Software Foundation, either version 3 of the * 20 * License, or (at your option) any later version. * 21 * You should have received a copy of the GNU Lesser General Public * 22 * License along with this library. * 23 * If not, see <http://www.gnu.org/licenses/>. * 24 * * 25 * This library is distributed in the hope that it will be useful, * 26 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 28 * GNU General Public License for more details. * 29 ****************************************************************************/ 30 #ifndef LRSIMPLETAGPARSER_H 31 #define LRSIMPLETAGPARSER_H 32 33 #include <QVector> 34 #include <QString> 35 #include <QStringList> 36 37 namespace LimeReport{ 38 39 class Tag{ 40 public: Tag(QString text,int beginPos,int endPos)41 Tag(QString text,int beginPos,int endPos) 42 :m_tagText(text),m_beginPos(beginPos),m_endPos(endPos){} Tag()43 Tag():m_tagText(""),m_beginPos(-1),m_endPos(-1){} isValid()44 bool isValid(){return (!m_tagText.isEmpty())&&(m_beginPos>0)&&(m_endPos>0);} tagText()45 QString tagText() const {return m_tagText;} begin()46 int begin() const {return m_beginPos;} end()47 int end() const {return m_endPos;} 48 private: 49 QString m_tagText; 50 int m_beginPos; 51 int m_endPos; 52 }; 53 54 class Symb{ 55 public: Symb(QString text,int pos)56 Symb(QString text, int pos):m_text(text),m_pos(pos){} Symb()57 Symb():m_text(""),m_pos(-1){} isValid()58 bool isValid(){return (!m_text.isEmpty())&&(m_pos>0);} isTag()59 bool isTag(){return isValid()&&m_text.at(0)=='<';} text()60 QString text(){return m_text;} pos()61 int pos(){return m_pos;} 62 private: 63 QString m_text; 64 int m_pos; 65 }; 66 67 struct TagDiff{ 68 enum Direction { 69 Inner=0, 70 Outer=1 71 }; 72 Tag* tag; 73 Direction direction; 74 }; 75 76 class HtmlContext 77 { 78 public: 79 HtmlContext(QString html); 80 ~HtmlContext(); 81 static QString extractWord(QString text,int index); 82 static QVector<TagDiff> tagVectDiff(QVector<Tag*> source, QVector<Tag*> dest); 83 static bool isVectorEqual(QVector<Tag*> source, QVector<Tag*> dest); 84 void fillTagVector(QString html); 85 QString extendTextByTags(QString text, int pos); 86 QVector<Tag *> tagsAt(int pos); 87 Symb symbAt(int pos); 88 void clearTags(); 89 void clearSymbs(); 90 private: 91 static QString parseTag(QVector<Tag*>& storage,QString text,int& curPos, bool createTag=true); 92 void parseSymbs(QString text); 93 void initSymbPatterns(); 94 private: 95 QVector<Tag*> m_tags; 96 QVector<Symb*> m_symbs; 97 QStringList m_symbPatterns; 98 }; 99 } 100 #endif // LRSIMPLETAGPARSER_H 101