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

Generated by: LCOV version 1.10