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

Generated by: LCOV version 1.10