LCOV - code coverage report
Current view: top level - sdext/source/pdfimport/sax - emitcontext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 23 28 82.1 %
Date: 2015-06-13 12:38:46 Functions: 7 7 100.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 "saxemitter.hxx"
      22             : #include "emitcontext.hxx"
      23             : #include "saxattrlist.hxx"
      24             : 
      25             : #include <rtl/strbuf.hxx>
      26             : #include <osl/diagnose.h>
      27             : #include <cppuhelper/exc_hlp.hxx>
      28             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      29             : 
      30             : #if OSL_DEBUG_LEVEL > 1
      31             : #include <osl/file.hxx>
      32             : static osl::File* pStream = NULL;
      33             : static int nIndent = 0;
      34             : #endif
      35             : 
      36             : using namespace com::sun::star;
      37             : 
      38             : namespace pdfi
      39             : {
      40             : 
      41           2 : SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
      42           2 :     m_xDocHdl( xDocHdl )
      43             : {
      44             :     OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
      45             :     try
      46             :     {
      47           2 :         m_xDocHdl->startDocument();
      48             :     }
      49           0 :     catch( xml::sax::SAXException& )
      50             :     {
      51             :     }
      52             : #if OSL_DEBUG_LEVEL > 1
      53             :     static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
      54             :     if( pDir )
      55             :     {
      56             :         OUString aStr( OStringToOUString( pDir, RTL_TEXTENCODING_UTF8 ) );
      57             :         OUString aFileURL;
      58             :         osl_getFileURLFromSystemPath( aStr.pData, &aFileURL.pData );
      59             :         OUStringBuffer aBuf( 256 );
      60             :         aBuf.append( aFileURL );
      61             :         aBuf.appendAscii( "/pdfimport.xml" );
      62             :         pStream = new osl::File( aBuf.makeStringAndClear() );
      63             :         if( pStream->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) )
      64             :         {
      65             :             pStream->open( osl_File_OpenFlag_Write );
      66             :             pStream->setSize( 0 );
      67             :         }
      68             :     }
      69             :     else
      70             :         pStream = 0;
      71             : #endif
      72           2 : }
      73             : 
      74           6 : SaxEmitter::~SaxEmitter()
      75             : {
      76             :     try
      77             :     {
      78           2 :         m_xDocHdl->endDocument();
      79             :     }
      80           0 :     catch( xml::sax::SAXException& )
      81             :     {
      82             :     }
      83             : #if OSL_DEBUG_LEVEL > 1
      84             :     if( pStream )
      85             :     {
      86             :         pStream->close();
      87             :         delete pStream;
      88             :         pStream = 0;
      89             :     }
      90             : #endif
      91           4 : }
      92             : 
      93         141 : void SaxEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
      94             : {
      95         141 :     OUString aTag = OUString::createFromAscii( pTag );
      96             :     uno::Reference< xml::sax::XAttributeList > xAttr(
      97         282 :         new SaxAttrList( rProperties ) );
      98             :     try
      99             :     {
     100         141 :         m_xDocHdl->startElement( aTag, xAttr );
     101             :     }
     102           0 :     catch( xml::sax::SAXException& )
     103             :     {
     104         141 :     }
     105             : #if OSL_DEBUG_LEVEL > 1
     106             :     if( pStream )
     107             :     {
     108             :         sal_uInt64 nWritten = 0;
     109             :         for( int i = 0; i < nIndent; i++ )
     110             :             pStream->write( "    ", 4, nWritten );
     111             : 
     112             :         OStringBuffer aBuf( 1024 );
     113             :         aBuf.append( '<' );
     114             :         aBuf.append( pTag );
     115             :         for( PropertyMap::const_iterator it = rProperties.begin(); it != rProperties.end(); ++it )
     116             :         {
     117             :             aBuf.append( ' ' );
     118             :             aBuf.append( OUStringToOString( it->first, RTL_TEXTENCODING_UTF8 ) );
     119             :             aBuf.append( "=\"" );
     120             :             aBuf.append( OUStringToOString( it->second, RTL_TEXTENCODING_UTF8 ) );
     121             :             aBuf.append( "\"" );
     122             :         }
     123             :         aBuf.append( ">\n" );
     124             :         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
     125             :         nIndent++;
     126             :     }
     127             : #endif
     128         141 : }
     129             : 
     130          65 : void SaxEmitter::write( const OUString& rText )
     131             : {
     132             :     try
     133             :     {
     134          65 :         m_xDocHdl->characters( rText );
     135             :     }
     136           0 :     catch( xml::sax::SAXException& )
     137             :     {
     138             :     }
     139             : #if OSL_DEBUG_LEVEL > 1
     140             :     if( pStream )
     141             :     {
     142             :         OString aStr( OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
     143             :         sal_uInt64 nWritten = 0;
     144             :         pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
     145             :     }
     146             : #endif
     147          65 : }
     148             : 
     149         141 : void SaxEmitter::endTag( const char* pTag )
     150             : {
     151         141 :     OUString aTag = OUString::createFromAscii( pTag );
     152             :     try
     153             :     {
     154         141 :         m_xDocHdl->endElement( aTag );
     155             :     }
     156           0 :     catch( xml::sax::SAXException& )
     157             :     {
     158         141 :     }
     159             : #if OSL_DEBUG_LEVEL > 1
     160             :     if( pStream )
     161             :     {
     162             :         sal_uInt64 nWritten = 0;
     163             :         for( int i = 0; i < nIndent; i++ )
     164             :             pStream->write( "    ", 4, nWritten );
     165             : 
     166             :         OStringBuffer aBuf( 1024 );
     167             :         aBuf.append( "</" );
     168             :         aBuf.append( pTag );
     169             :         aBuf.append( ">\n" );
     170             :         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
     171             :         nIndent--;
     172             :     }
     173             : #endif
     174         141 : }
     175             : 
     176           2 : XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
     177             : {
     178           2 :     return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl));
     179             : }
     180             : 
     181             : }
     182             : 
     183             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11