LCOV - code coverage report
Current view: top level - helpcompiler/source - BasCodeTagger.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 122 0.8 %
Date: 2015-06-13 12:38:46 Functions: 2 13 15.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <BasCodeTagger.hxx>
      11             : 
      12           0 : LibXmlTreeWalker::LibXmlTreeWalker( xmlDocPtr doc )
      13             : {
      14           0 :     if ( doc == NULL )
      15           0 :         throw BasicCodeTagger::NULL_DOCUMENT;
      16           0 :     m_pCurrentNode = xmlDocGetRootElement( doc );
      17           0 :     if ( m_pCurrentNode == NULL )
      18           0 :         throw BasicCodeTagger::EMPTY_DOCUMENT;
      19           0 :     else if ( m_pCurrentNode->xmlChildrenNode != NULL )
      20           0 :         m_Queue.push_back( m_pCurrentNode->xmlChildrenNode );
      21           0 :     nextNode();
      22           0 : }
      23             : 
      24           0 : void LibXmlTreeWalker::nextNode()
      25             : {
      26             : 
      27             :       //next node
      28           0 :     if ( m_pCurrentNode->next == NULL )
      29             :     {
      30           0 :         m_pCurrentNode = m_Queue.front();
      31           0 :         m_Queue.pop_front();
      32             :     }
      33             :     else
      34           0 :         m_pCurrentNode = m_pCurrentNode->next;
      35             :     //queue chiledren if they exist
      36           0 :     if ( m_pCurrentNode->xmlChildrenNode != NULL )
      37           0 :         m_Queue.push_back( m_pCurrentNode->xmlChildrenNode );
      38           0 : }
      39             : 
      40           0 : void LibXmlTreeWalker::ignoreCurrNodesChildren()
      41             : {
      42           0 :     if ( m_pCurrentNode->xmlChildrenNode != NULL )
      43           0 :           m_Queue.pop_back();
      44           0 : }
      45             : 
      46           0 : bool LibXmlTreeWalker::end()
      47             : {
      48           0 :     return m_pCurrentNode->next == NULL && m_Queue.empty();
      49             : }
      50             : 
      51             : 
      52             : 
      53             : 
      54           0 : BasicCodeTagger::BasicCodeTagger( xmlDocPtr rootDoc ):
      55           0 :     m_Highlighter(HIGHLIGHT_BASIC)
      56             : {
      57           0 :       if ( rootDoc == NULL )
      58           0 :           throw NULL_DOCUMENT;
      59           0 :       m_pDocument = rootDoc;
      60           0 :     m_pXmlTreeWalker = NULL;
      61           0 :     m_bTaggingCompleted = false;
      62             : 
      63           0 : }
      64             : 
      65           0 : BasicCodeTagger::~BasicCodeTagger()
      66             : {
      67           0 :     if ( m_pXmlTreeWalker != NULL )
      68           0 :       delete m_pXmlTreeWalker;
      69           0 : }
      70             : //!Gathers all the <bascode> tag nodes from xml tree.
      71             : /*!
      72             :  *    Assumes m_pDocument is valid. Handles m_pXmlTreeWalker and m_BasicCodeContainerTags members.
      73             :  */
      74           0 : void BasicCodeTagger::getBasicCodeContainerNodes()
      75             : {
      76             :       xmlNodePtr currentNode;
      77             : 
      78           0 :     m_BasicCodeContainerTags.clear();
      79             : 
      80           0 :     if ( m_pXmlTreeWalker != NULL )
      81           0 :       delete m_pXmlTreeWalker;
      82           0 :     m_pXmlTreeWalker = new LibXmlTreeWalker( m_pDocument );
      83             : 
      84           0 :     currentNode = m_pXmlTreeWalker->currentNode();
      85           0 :     if ( !( xmlStrcmp( currentNode->name, reinterpret_cast<const xmlChar*>("bascode") ) ) )
      86             :     { //Found <bascode>
      87           0 :         m_BasicCodeContainerTags.push_back( currentNode ); //it goes to the end of the list
      88             :     }
      89           0 :     while ( !m_pXmlTreeWalker->end() )
      90             :     {
      91           0 :           m_pXmlTreeWalker->nextNode();
      92           0 :         if ( !( xmlStrcmp( m_pXmlTreeWalker->currentNode()->name, reinterpret_cast<const xmlChar*>("bascode") ) ) )
      93             :         { //Found <bascode>
      94           0 :             m_BasicCodeContainerTags.push_back( m_pXmlTreeWalker->currentNode() ); //it goes to the end of the list
      95           0 :             m_pXmlTreeWalker->ignoreCurrNodesChildren();
      96             :         }
      97             :     }
      98           0 : }
      99             : 
     100             : //! Extracts Basic Codes contained in <bascode> tags.
     101             : /*!
     102             :  *  For each <bascode> this method iterates through it's <paragraph> tags and "inserts" <item> tags according
     103             :  *  to the Basic code syntax found in that paragraph.
     104             :  */
     105           0 : void BasicCodeTagger::tagBasCodeParagraphs()
     106             : {
     107             :     //helper variables
     108             :     xmlNodePtr currBascodeNode;
     109             :     xmlNodePtr currParagraph;
     110           0 :     while ( !m_BasicCodeContainerTags.empty() )
     111             :     {
     112           0 :         currBascodeNode = m_BasicCodeContainerTags.front();
     113           0 :         currParagraph = currBascodeNode->xmlChildrenNode; //first <paragraph>
     114           0 :         while ( currParagraph != NULL )
     115             :         {
     116           0 :             tagParagraph( currParagraph );
     117           0 :             currParagraph=currParagraph->next;
     118             :         }
     119           0 :         m_BasicCodeContainerTags.pop_front(); //next element
     120             :     }
     121           0 : }
     122             : 
     123             : //! Used by tagBasCodeParagraphs(). It does the work on the current paragraph containing Basic code.
     124           0 : void BasicCodeTagger::tagParagraph( xmlNodePtr paragraph )
     125             : {
     126             :     //1. get paragraph text
     127             :     xmlChar* codeSnippet;
     128           0 :     codeSnippet = xmlNodeListGetString( m_pDocument, paragraph->xmlChildrenNode, 1 );
     129           0 :     if ( codeSnippet == NULL )
     130             :     {
     131           0 :         return; //no text, nothing more to do here
     132             :     }
     133             :     //2. delete every child from paragraph (except attributes)
     134           0 :     xmlNodePtr curNode = paragraph->xmlChildrenNode;
     135             :     xmlNodePtr sibling;
     136           0 :     while ( curNode != NULL )
     137             :     {
     138           0 :         sibling = curNode->next;
     139           0 :         xmlUnlinkNode( curNode );
     140           0 :         xmlFreeNode( curNode );
     141           0 :         curNode = sibling;
     142             :     }
     143             : 
     144             :     //3. create new paragraph content
     145             :     OUString strLine( reinterpret_cast<const sal_Char*>(codeSnippet),
     146           0 :                                 strlen(reinterpret_cast<const char*>(codeSnippet)),
     147           0 :                                 RTL_TEXTENCODING_UTF8 );
     148           0 :     std::vector<HighlightPortion> portions;
     149           0 :     m_Highlighter.getHighlightPortions( strLine, portions );
     150           0 :     for (std::vector<HighlightPortion>::iterator i(portions.begin());
     151           0 :          i != portions.end(); ++i)
     152             :     {
     153           0 :         OString sToken(OUStringToOString(strLine.copy(i->nBegin, i->nEnd-i->nBegin), RTL_TEXTENCODING_UTF8));
     154           0 :         xmlNodePtr text = xmlNewText(reinterpret_cast<const xmlChar*>(sToken.getStr()));
     155           0 :         if ( i->tokenType != TT_WHITESPACE )
     156             :         {
     157           0 :             xmlChar* typeStr = getTypeString( i->tokenType );
     158           0 :             curNode = xmlNewTextChild( paragraph, 0, reinterpret_cast<xmlChar const *>("item"), 0 );
     159           0 :             xmlNewProp( curNode, reinterpret_cast<xmlChar const *>("type"), typeStr );
     160           0 :             xmlAddChild( curNode, text );
     161           0 :             xmlFree( typeStr );
     162             :         }
     163             :         else
     164           0 :             xmlAddChild( paragraph, text );
     165           0 :     }
     166           0 :     xmlFree( codeSnippet );
     167             : }
     168             : 
     169             : //! Manages tagging process.
     170             : /*!
     171             :  *    This is the "main" function of BasicCodeTagger.
     172             :  */
     173           0 : void BasicCodeTagger::tagBasicCodes()
     174             : {
     175           0 :       if ( m_bTaggingCompleted )
     176           0 :         return;
     177             :     //gather <bascode> nodes
     178             :     try
     179             :     {
     180           0 :         getBasicCodeContainerNodes();
     181             :     }
     182           0 :     catch (TaggerException &ex)
     183             :     {
     184           0 :           std::cout << "BasCodeTagger error occurred. Error code:" << ex << std::endl;
     185             :     }
     186             : 
     187             :     //tag basic code paragraphs in <bascode> tag
     188           0 :     tagBasCodeParagraphs();
     189           0 :     m_bTaggingCompleted = true;
     190             : }
     191             : 
     192             : //! Converts SyntaxHighlighter's TokenTypes enum to a type string for <item type=... >
     193           0 : xmlChar* BasicCodeTagger::getTypeString( TokenTypes tokenType )
     194             : {
     195             :     const char* str;
     196           0 :     switch ( tokenType )
     197             :     {
     198             :         case TT_UNKNOWN :
     199           0 :             str = "unknown";
     200           0 :             break;
     201             :         case TT_IDENTIFIER :
     202           0 :             str = "identifier";
     203           0 :             break;
     204             :         case TT_WHITESPACE :
     205           0 :             str = "whitespace";
     206           0 :             break;
     207             :         case TT_NUMBER :
     208           0 :             str = "number";
     209           0 :             break;
     210             :         case TT_STRING :
     211           0 :             str = "string";
     212           0 :             break;
     213             :         case TT_EOL :
     214           0 :             str = "eol";
     215           0 :             break;
     216             :         case TT_COMMENT :
     217           0 :             str = "comment";
     218           0 :             break;
     219             :         case TT_ERROR :
     220           0 :             str = "error";
     221           0 :             break;
     222             :         case TT_OPERATOR :
     223           0 :             str = "operator";
     224           0 :             break;
     225             :         case TT_KEYWORDS :
     226           0 :             str = "keyword";
     227           0 :             break;
     228             :         case TT_PARAMETER :
     229           0 :             str = "parameter";
     230           0 :             break;
     231             :         default :
     232           0 :             str = "unknown";
     233           0 :             break;
     234             :     }
     235           0 :     return xmlCharStrdup( str );
     236         336 : }
     237             : 
     238             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11