Changeset 4436

Show
Ignore:
Timestamp:
Sat Feb 4 19:52:49 2006
Author:
mugur
Message:

implemented ticket:1617 - Add ability to extract first paragraph of a story

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/campsite/implementation/parser/parser/actions.h

    r4406 r4436  
    752 752     int modifier;       // print modifier  
    753 753     int image;          // image number for printing image attributes  
      754     int m_nParagraphNumber; // for string type of fields: if greater than 0 will  
      755                             // print the paragraph identified by number "m_nParagraphNumber"  
    754 756     CCParser cparser;   // article content parser  
    755 757  
     
    765 767     //      const char* field - table field  
    766 768     int DateField(const char* table, const char* field);  
      769      
      770     // IsPEntity: returns true if it finds a <P> HTML entity at the given position  
      771     // Parameters:  
      772     //      string::const_iterator& p_rcoCurrent - the position in the string where to search  
      773     //          for the <P> HTML entity  
      774     //      const string::const_iterator& p_rcoEnd - the end of the string  
      775     static bool IsPEntity(string::const_iterator& p_rcoCurrent,  
      776                           const string::const_iterator& p_rcoEnd);  
      777  
      778     // IsBREntity: returns true if it finds a <BR> HTML entity at the given position  
      779     // Parameters:  
      780     //      string::const_iterator& p_rcoCurrent - the position in the string where to search  
      781     //          for the <BR> HTML entity  
      782     //      const string::const_iterator& p_rcoEnd - the end of the string  
      783     static bool IsBREntity(string::const_iterator& p_rcoCurrent,  
      784                            const string::const_iterator& p_rcoEnd);  
    767 785  
    768 786 public:  
     
    774 792     //      string f = "" - format (for date type attributes)  
    775 793     CActPrint(const string& a, int m, const string& t = string(""), bool st = false,  
    776                 const string& f = string(""), int i = 1) throw(InvalidModifier)  
    777           : attr(a), type(t), strictType(st), format(f), modifier(m), image(i)  
      794               const string& f = string(""), int i = 1, int p = 0) throw(InvalidModifier)  
      795     : attr(a), type(t), strictType(st), format(f), modifier(m), image(i), m_nParagraphNumber(p)  
    778 796     {  
    779 797         if (!s_coModifiers.validModifier(m))  
    798 816     // validModifier: return true if modifier is valid; false otherwise  
    799 817     static bool validModifier(int m) { return s_coModifiers.validModifier(m); }  
      818  
      819     static bool isParagraphStart(string::const_iterator& p_rcoCurrent,  
      820                                  const string::const_iterator& p_rcoEnd,  
      821                                  string::const_iterator& p_rcoParagraphStart);  
      822  
      823     // printParagraph: prints only the paragraph identifier by the number "p_nParagraphNumber"  
      824     //      to the output stream  
      825     // Parameters:  
      826     //      const string& p_rcoText - the text to be printed  
      827     //      sockstream& p_rcoStream - output stream  
      828     //      int p_nParagraphNumber - the number of the paragraph to be printed  
      829     static void printParagraph(const string& p_rcoText, sockstream& p_rcoStream,  
      830                                int p_nParagraphNumber);  
    800 831 };  
    801 832  
  • trunk/campsite/implementation/parser/parser/parser.cpp

    r4406 r4436  
    1077 1077     if (case_comp(attrIdentifier, "wday_name") == 0)  
    1078 1078         format = "%W";  
    1079       al.insert(al.end(), new CActPrint(attrAttribute, st->id(), type, strictType, format, image));  
      1079     int nParagraphNumber = 0;  
      1080     if (attrType->first->dataType() == CMS_DT_STRING)  
      1081     {  
      1082         l = lex.getLexem();  
      1083         DEBUGLexem("print", l);  
      1084         CheckForEOF(l, PERR_EOS_MISSING);  
      1085         if (l->res() != CMS_LEX_END_STATEMENT)  
      1086         {  
      1087             CheckForAtom(l);  
      1088             if (case_comp(l->atom()->identifier(), "firstParagraph") != 0)  
      1089             {  
      1090                 SetPError(parse_err, PERR_INVALID_ATTRIBUTE, MODE_PARSE,  
      1091                           "FirstParagraph", lex.prevLine(), lex.prevColumn());  
      1092                 WaitForStatementEnd(true);  
      1093                 return 0;  
      1094             }  
      1095             nParagraphNumber = 1;  
      1096         }  
      1097     }  
      1098     al.insert(al.end(), new CActPrint(attrAttribute, st->id(), type, strictType, format, image,  
      1099                                      nParagraphNumber));  
    1080 1100     if (l->res() != CMS_LEX_END_STATEMENT)  
    1081 1101         WaitForStatementEnd(true);  
  • trunk/campsite/implementation/parser/parser/actions.cpp

    r4403 r4436  
    1481 1481 }  
    1482 1482  
      1483 // IsPEntity: returns true if it finds a <P> HTML entity at the given position  
      1484 // Parameters:  
      1485 //      string::const_iterator& p_rcoCurrent - the position in the string where to search  
      1486 //          for the <P> HTML entity  
      1487 //      const string::const_iterator& p_rcoEnd - the end of the string  
      1488 bool CActPrint::IsPEntity(string::const_iterator& p_rcoCurrent,  
      1489                           const string::const_iterator& p_rcoEnd)  
      1490 {  
      1491     if (*p_rcoCurrent != '<')  
      1492     {  
      1493         return false;  
      1494     }  
      1495     do {  
      1496         ++p_rcoCurrent;  
      1497     } while(p_rcoCurrent != p_rcoEnd && *p_rcoCurrent >= 0 && *p_rcoCurrent <= ' ');  
      1498     if (p_rcoCurrent == p_rcoEnd)  
      1499     {  
      1500         return false;  
      1501     }  
      1502     char chFirstChar = *p_rcoCurrent;  
      1503     char chSecondChar = *(++p_rcoCurrent);  
      1504     if (p_rcoCurrent == p_rcoEnd)  
      1505     {  
      1506         return false;  
      1507     }  
      1508     if (tolower(chFirstChar) == 'p' && (chSecondChar == '/' || chSecondChar == '>'  
      1509            || (chSecondChar >= 0 && chSecondChar <= ' ')))  
      1510     {  
      1511         for (; p_rcoCurrent != p_rcoEnd && *p_rcoCurrent != '>'; ++p_rcoCurrent);  
      1512         return true;  
      1513     }  
      1514     return false;  
      1515 }  
      1516  
      1517 // IsBREntity: returns true if it finds a <BR> HTML entity at the given position  
      1518 // Parameters:  
      1519 //      string::const_iterator& p_rcoCurrent - the position in the string where to search  
      1520 //          for the <BR> HTML entity  
      1521 //      const string::const_iterator& p_rcoEnd - the end of the string  
      1522 bool CActPrint::IsBREntity(string::const_iterator& p_rcoCurrent,  
      1523                            const string::const_iterator& p_rcoEnd)  
      1524 {  
      1525     if (*p_rcoCurrent != '<')  
      1526     {  
      1527         return false;  
      1528     }  
      1529     do {  
      1530         ++p_rcoCurrent;  
      1531     } while(p_rcoCurrent != p_rcoEnd && *p_rcoCurrent >= 0 && *p_rcoCurrent <= ' ');  
      1532     if (p_rcoCurrent == p_rcoEnd)  
      1533     {  
      1534         return false;  
      1535     }  
      1536     char chFirstChar = *p_rcoCurrent;  
      1537     char chSecondChar = *(++p_rcoCurrent);  
      1538     if (p_rcoCurrent == p_rcoEnd)  
      1539     {  
      1540         return false;  
      1541     }  
      1542     char chThirdChar = *(++p_rcoCurrent);  
      1543     if (tolower(chFirstChar) == 'b' && tolower(chSecondChar) == 'r'  
      1544            && (chThirdChar == '/' || chThirdChar == '>'  
      1545            || (chThirdChar >= 0 && chThirdChar <= ' ')))  
      1546     {  
      1547         for (; p_rcoCurrent != p_rcoEnd && *p_rcoCurrent != '>'; ++p_rcoCurrent);  
      1548         return true;  
      1549     }  
      1550     return false;  
      1551 }  
      1552  
    1483 1553 // takeAction: performs the action  
    1484 1554 // Parametes:  
     
    1831 1901             return RES_OK;  
    1832 1902         table = string("X") + row[0];  
    1833           int blob;  
    1834           blob = BlobField(table.c_str(), attr.c_str());  
      1903         int blob = BlobField(table.c_str(), attr.c_str());  
    1835 1904         coQuery = string("select ") + attr + " from " + table + " where NrArticle = " + row[1]  
    1836 1905                   + " and IdLanguage = " + row[2];  
     
    1845 1914             cparser.setDebug(*m_coDebug);  
    1846 1915             cparser.reset(row2[0], lengths[0]);  
    1847               cparser.parse(c, fs, &m_coSql, c.StartSubtitle(), c.AllSubtitles(), true);  
      1916             if (m_nParagraphNumber <= 0)  
      1917             {  
      1918                 cparser.parse(c, fs, &m_coSql, c.StartSubtitle(), c.AllSubtitles(), true);  
      1919             }  
      1920             else  
      1921             {  
      1922                 ostringstream coOut;  
      1923                 cparser.parse(c, coOut, &m_coSql, c.StartSubtitle(), c.AllSubtitles(), true);  
      1924                 printParagraph(coOut.str(), fs, m_nParagraphNumber);  
      1925             }  
    1848 1926         }  
    1849 1927         else if (DateField(table.c_str(), attr.c_str()) == 0 && format != "")  
    1870 1948 }  
    1871 1949  
      1950 bool CActPrint::isParagraphStart(string::const_iterator& p_rcoCurrent,  
      1951                                  const string::const_iterator& p_rcoEnd,  
      1952                                  string::const_iterator& p_rcoParagraphStart)  
      1953 {  
      1954     if (*p_rcoCurrent != '<')  
      1955     {  
      1956         return false;  
      1957     }  
      1958     string::const_iterator p_rcoMyCurrent = p_rcoCurrent;  
      1959     if (CActPrint::IsPEntity(p_rcoMyCurrent, p_rcoEnd))  
      1960     {  
      1961         p_rcoParagraphStart = p_rcoCurrent;  
      1962         p_rcoCurrent = p_rcoMyCurrent;  
      1963         return true;  
      1964     }  
      1965     p_rcoMyCurrent = p_rcoCurrent;  
      1966     if (CActPrint::IsBREntity(p_rcoMyCurrent, p_rcoEnd))  
      1967     {  
      1968         for (; p_rcoMyCurrent != p_rcoEnd && *p_rcoMyCurrent != '<'; ++p_rcoMyCurrent);  
      1969         if (CActPrint::IsBREntity(p_rcoMyCurrent, p_rcoEnd))  
      1970         {  
      1971             p_rcoParagraphStart = p_rcoCurrent;  
      1972             p_rcoCurrent = p_rcoMyCurrent;  
      1973             return true;  
      1974         }  
      1975     }  
      1976     return false;  
      1977 }  
      1978  
      1979 // printParagraph: prints only the paragraph identifier by the number "p_nParagraphNumber"  
      1980 //      to the output stream  
      1981 // Parameters:  
      1982 //      const string& p_rcoText - the text to be printed  
      1983 //      sockstream& p_rcoStream - output stream  
      1984 //      int p_nParagraphNumber - the number of the paragraph to be printed  
      1985 void CActPrint::printParagraph(const string& p_rcoText, sockstream& p_rcoStream,  
      1986                                int p_nParagraphNumber)  
      1987 {  
      1988     if (p_nParagraphNumber <= 0)  
      1989     {  
      1990         p_rcoStream << p_rcoText;  
      1991         return;  
      1992     }  
      1993     int nCurrentParagraph = 1;  
      1994     string::const_iterator coParagraphStart = p_rcoText.begin();  
      1995     string::const_iterator coNextParagraphStart = p_rcoText.begin();  
      1996     string::const_iterator coFoundParagraph;  
      1997     string::const_iterator coCurrent = p_rcoText.begin();  
      1998     do {  
      1999         for (; coCurrent != p_rcoText.end() && *coCurrent != '<'; ++coCurrent);  
      2000         if (coCurrent == p_rcoText.end()  
      2001                      || CActPrint::isParagraphStart(coCurrent, p_rcoText.end(),  
      2002                 coFoundParagraph))  
      2003         {  
      2004             coParagraphStart = coNextParagraphStart;  
      2005             coNextParagraphStart = coCurrent == p_rcoText.end() ?  
      2006                     p_rcoText.end() : coFoundParagraph;  
      2007             nCurrentParagraph++;  
      2008         }  
      2009         else  
      2010         {  
      2011             ++coCurrent;  
      2012         }  
      2013     } while (coCurrent != p_rcoText.end() && nCurrentParagraph <= p_nParagraphNumber);  
      2014     if (coParagraphStart != coNextParagraphStart)  
      2015     {  
      2016         p_rcoStream << p_rcoText.substr(distance(p_rcoText.begin(), coParagraphStart),  
      2017                                         distance(coParagraphStart, coNextParagraphStart));  
      2018     }  
      2019 }  
      2020  
    1872 2021 CIfModifiers::CIfModifiers()  
    1873 2022 {