LCOV - code coverage report
Current view: top level - i18npool/source/localedata - saxparser.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 90 145 62.1 %
Date: 2015-06-13 12:38:46 Functions: 21 33 63.6 %
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             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "sal/config.h"
      21             : 
      22             : #include <cstdlib>
      23             : #include <iostream>
      24             : #include <stdio.h>
      25             : #include <string.h>
      26             : #include <stack>
      27             : 
      28             : #include "sal/main.h"
      29             : 
      30             : #include <com/sun/star/lang/XComponent.hpp>
      31             : 
      32             : #include <com/sun/star/xml/sax/SAXParseException.hpp>
      33             : #include <com/sun/star/xml/sax/Parser.hpp>
      34             : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
      35             : 
      36             : #include <com/sun/star/io/XOutputStream.hpp>
      37             : #include <com/sun/star/io/XActiveDataSource.hpp>
      38             : 
      39             : #include <cppuhelper/bootstrap.hxx>
      40             : #include <cppuhelper/implbase1.hxx>
      41             : #include <cppuhelper/implbase3.hxx>
      42             : 
      43             : #include <osl/diagnose.h>
      44             : 
      45             : #include "LocaleNode.hxx"
      46             : 
      47             : using namespace ::std;
      48             : using namespace ::cppu;
      49             : using namespace ::com::sun::star::uno;
      50             : using namespace ::com::sun::star::lang;
      51             : using namespace ::com::sun::star::xml::sax;
      52             : using namespace ::com::sun::star::io;
      53             : 
      54             : 
      55             : 
      56             : 
      57             : 
      58             : 
      59             : /************
      60             :  * Sequence of bytes -> InputStream
      61             :  ************/
      62         490 : class OInputStream : public WeakImplHelper1 < XInputStream >
      63             : {
      64             : public:
      65         245 :     explicit OInputStream( const Sequence< sal_Int8 >&seq )
      66             :         : nPos(0)
      67         245 :         , m_seq(seq)
      68         245 :     {}
      69             : 
      70             : public:
      71         580 :     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
      72             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
      73             :         {
      74         580 :             nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
      75         490 :                 m_seq.getLength() - nPos :
      76        1070 :                 nBytesToRead;
      77         580 :             aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
      78         580 :             nPos += nBytesToRead;
      79         580 :             return nBytesToRead;
      80             :         }
      81         580 :     virtual sal_Int32 SAL_CALL readSomeBytes(
      82             :         ::com::sun::star::uno::Sequence< sal_Int8 >& aData,
      83             :         sal_Int32 nMaxBytesToRead )
      84             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
      85             :         {
      86         580 :             return readBytes( aData, nMaxBytesToRead );
      87             :         }
      88           0 :     virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
      89             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
      90             :         {
      91             :             // not implemented
      92           0 :         }
      93           0 :     virtual sal_Int32 SAL_CALL available(  )
      94             :         throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
      95             :         {
      96           0 :             return m_seq.getLength() - nPos;
      97             :         }
      98           0 :     virtual void SAL_CALL closeInput(  )
      99             :         throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
     100             :         {
     101             :             // not needed
     102           0 :         }
     103             :     sal_Int32 nPos;
     104             :     Sequence< sal_Int8> m_seq;
     105             : };
     106             : 
     107             : 
     108             : // Helper : create an input stream from a file
     109             : 
     110         245 : Reference< XInputStream > createStreamFromFile(
     111             :     const char *pcFile )
     112             : {
     113         245 :     Reference<  XInputStream >  r;
     114             : 
     115         245 :     FILE *f = fopen( pcFile , "rb" );
     116             : 
     117         245 :     if (!f)
     118             :     {
     119           0 :         fprintf(stderr, "failure opening %s\n", pcFile);
     120           0 :         return r;
     121             :     }
     122             : 
     123         245 :     if (fseek( f , 0 , SEEK_END ) == -1)
     124             :     {
     125           0 :         fprintf(stderr, "failure fseeking %s\n", pcFile);
     126           0 :         fclose(f);
     127           0 :         return r;
     128             :     }
     129             : 
     130         245 :     long nLength = ftell( f );
     131         245 :     if (nLength == -1)
     132             :     {
     133           0 :         fprintf(stderr, "failure ftelling %s\n", pcFile);
     134           0 :         fclose(f);
     135           0 :         return r;
     136             :     }
     137             : 
     138         245 :     if (fseek( f , 0 , SEEK_SET ) == -1)
     139             :     {
     140           0 :         fprintf(stderr, "failure fseeking %s\n", pcFile);
     141           0 :         fclose(f);
     142           0 :         return r;
     143             :     }
     144             : 
     145         490 :     Sequence<sal_Int8> seqIn(nLength);
     146         245 :     if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
     147         245 :         r = Reference< XInputStream > ( new OInputStream( seqIn ) );
     148             :     else
     149           0 :         fprintf(stderr, "failure reading %s\n", pcFile);
     150         245 :     fclose( f );
     151         245 :     return r;
     152             : }
     153             : 
     154             : 
     155             : class TestDocumentHandler :
     156             :     public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
     157             : {
     158             : public:
     159         245 :     TestDocumentHandler(const char* locale, const char* outFile )
     160             :         : rootNode(0)
     161             :         , nError(0)
     162         245 :         , of(outFile, locale)
     163             :     {
     164         245 :         strncpy( theLocale, locale, sizeof(theLocale) );
     165         245 :         theLocale[sizeof(theLocale)-1] = 0;
     166         245 :     }
     167             : 
     168         490 :     virtual ~TestDocumentHandler(  )
     169         490 :     {
     170         245 :         of.closeOutput();
     171         245 :         delete rootNode;
     172         490 :     }
     173             : 
     174             : 
     175             : public: // Error handler
     176           0 :     virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
     177             :     {
     178           0 :         ++nError;
     179           0 :         printf( "Error !\n" );
     180             :         throw  SAXException(
     181             :             "error from error handler",
     182             :             Reference < XInterface >() ,
     183           0 :             aSAXParseException );
     184             :     }
     185           0 :     virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
     186             :     {
     187           0 :         ++nError;
     188           0 :         printf( "Fatal Error !\n" );
     189           0 :     }
     190           0 :     virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
     191             :     {
     192           0 :         printf( "Warning !\n" );
     193           0 :     }
     194             : 
     195             : 
     196             : public: // ExtendedDocumentHandler
     197             : 
     198             : 
     199             : 
     200             :     stack<LocaleNode *> currentNode ;
     201             :     LocaleNode * rootNode;
     202             : 
     203         245 :     virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
     204             :     {
     205         245 :     printf( "parsing document %s started\n", theLocale);
     206         245 :     of.writeAsciiString("#include <sal/types.h>\n\n\n");
     207         245 :     of.writeAsciiString("#include <stdio.h>\n\n");
     208         245 :     of.writeAsciiString("extern \"C\" {\n\n");
     209         245 :     }
     210             : 
     211         245 :     virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
     212             :     {
     213         245 :         if (rootNode)
     214             :         {
     215         245 :             rootNode->generateCode(of);
     216         245 :             int err = rootNode->getError();
     217         245 :             if (err)
     218             :             {
     219           0 :                 printf( "Error: in data for %s: %d\n", theLocale, err);
     220           0 :                 nError += err;
     221             :             }
     222             :         }
     223             :         else
     224             :         {
     225           0 :             ++nError;
     226           0 :             printf( "Error: no data for %s\n", theLocale);
     227             :         }
     228         245 :         printf( "parsing document %s finished\n", theLocale);
     229             : 
     230         245 :         of.writeAsciiString("} // extern \"C\"\n\n");
     231         245 :         of.closeOutput();
     232         245 :     }
     233             : 
     234       51550 :     virtual void SAL_CALL startElement(const OUString& aName,
     235             :                               const Reference< XAttributeList > & xAttribs)
     236             :         throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     237             :     {
     238             : 
     239       51550 :         LocaleNode * l =  LocaleNode::createNode (aName, xAttribs);
     240       51550 :         if (!currentNode.empty() ) {
     241       51305 :             LocaleNode * ln = currentNode.top();
     242       51305 :             ln->addChild(l);
     243             :         } else {
     244         245 :             rootNode = l;
     245             :         }
     246       51550 :         currentNode.push (l);
     247       51550 :     }
     248             : 
     249             : 
     250       51550 :     virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     251             :     {
     252       51550 :         currentNode.pop();
     253       51550 :     }
     254             : 
     255      167581 :     virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     256             :     {
     257             : 
     258      167581 :         LocaleNode * l = currentNode.top();
     259      167581 :         l->setValue (aChars);
     260      167581 :     }
     261             : 
     262           0 :     virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     263             :     {
     264           0 :     }
     265             : 
     266           0 :     virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     267             :     {
     268             :         // ignored
     269           0 :     }
     270             : 
     271         245 :     virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
     272             :         throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     273             :     {
     274             :         // ignored
     275         245 :     }
     276             : 
     277           0 :     virtual InputSource SAL_CALL resolveEntity(
     278             :         const OUString& sPublicId,
     279             :         const OUString& sSystemId)
     280             :         throw (RuntimeException, std::exception) SAL_OVERRIDE
     281             :     {
     282           0 :         InputSource source;
     283           0 :         source.sSystemId = sSystemId;
     284           0 :         source.sPublicId = sPublicId;
     285             : 
     286           0 :         source.aInputStream = createStreamFromFile(
     287           0 :             OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
     288             : 
     289           0 :         return source;
     290             :     }
     291             : 
     292           0 :     virtual void SAL_CALL startCDATA() throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     293             :     {
     294           0 :     }
     295           0 :     virtual void SAL_CALL endCDATA() throw (RuntimeException, std::exception) SAL_OVERRIDE
     296             :     {
     297           0 :     }
     298         426 :     virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     299             :     {
     300         426 :     }
     301        3007 :     virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
     302             :     {
     303        3007 :     }
     304             : 
     305           0 :     virtual void SAL_CALL allowLineBreak() throw (SAXException, RuntimeException, std::exception ) SAL_OVERRIDE
     306             :     {
     307             : 
     308           0 :     }
     309             : 
     310             : public:
     311             :     int nError;
     312             :     sal_Char theLocale[50];
     313             :     OFileWriter of;
     314             : };
     315             : 
     316             : 
     317             : 
     318             : 
     319             : 
     320         490 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
     321             : {
     322             :     try {
     323         245 :         if( argc < 4) {
     324           0 :             printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv[0] );
     325           0 :             exit( 1 );
     326             :         }
     327             : 
     328             :         Reference< XComponentContext > xContext(
     329         245 :             defaultBootstrap_InitialComponentContext());
     330             : 
     331             : 
     332             :         // parser demo
     333             :         // read xml from a file and count elements
     334             : 
     335         490 :         Reference< XParser > rParser = Parser::create(xContext);
     336             : 
     337         245 :         int nError = 0;
     338             :         // create and connect the document handler to the parser
     339         245 :         TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
     340             : 
     341         490 :         Reference < XDocumentHandler >  rDocHandler( static_cast<XDocumentHandler *>(pDocHandler) );
     342         490 :         Reference< XEntityResolver > rEntityResolver( static_cast<XEntityResolver *>(pDocHandler) );
     343             : 
     344         245 :         rParser->setDocumentHandler( rDocHandler );
     345         245 :         rParser->setEntityResolver( rEntityResolver );
     346             : 
     347             :         // create the input stream
     348         490 :         InputSource source;
     349         245 :         source.aInputStream = createStreamFromFile( argv[2] );
     350         245 :         source.sSystemId    = OUString::createFromAscii( argv[2] );
     351             : 
     352             :         // start parsing
     353         245 :         rParser->parseStream( source );
     354             : 
     355         245 :         nError = pDocHandler->nError;
     356             :         css::uno::Reference<css::lang::XComponent>(
     357         245 :             xContext, css::uno::UNO_QUERY_THROW)->dispose();
     358         490 :         return nError;
     359           0 :     } catch (css::uno::Exception & e) {
     360           0 :         std::cerr << "ERROR: " << e.Message << '\n';
     361           0 :         return EXIT_FAILURE;
     362             :     }
     363         735 : }
     364             : 
     365             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11