Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sdext / source / pdfimport / sax / emitcontext.cxx
blobf21b17e60c883a29f365412de452d582dc261be4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <saxemitter.hxx>
22 #include "emitcontext.hxx"
23 #include "saxattrlist.hxx"
25 #include <rtl/strbuf.hxx>
26 #include <osl/diagnose.h>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <com/sun/star/xml/sax/SAXException.hpp>
29 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
30 #include <xmloff/xmlimp.hxx>
32 #if OSL_DEBUG_LEVEL > 0
33 #include <osl/file.hxx>
34 static osl::File* pStream = nullptr;
35 static int nIndent = 0;
36 #endif
38 using namespace com::sun::star;
40 namespace pdfi
43 SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
44 m_xDocHdl( xDocHdl )
46 OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
47 if (SvXMLImport *pFastHandler = dynamic_cast<SvXMLImport*>(m_xDocHdl.get()))
48 m_xDocHdl.set( new SvXMLLegacyToFastDocHandler( pFastHandler ) );
49 try
51 m_xDocHdl->startDocument();
53 catch( xml::sax::SAXException& )
56 #if OSL_DEBUG_LEVEL > 0
57 static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
58 if( pDir )
60 OUString aStr( OStringToOUString( pDir, RTL_TEXTENCODING_UTF8 ) );
61 OUString aFileURL;
62 osl_getFileURLFromSystemPath( aStr.pData, &aFileURL.pData );
63 OUStringBuffer aBuf( 256 );
64 aBuf.append( aFileURL );
65 aBuf.append( "/pdfimport.xml" );
66 pStream = new osl::File( aBuf.makeStringAndClear() );
67 if( pStream->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) )
69 pStream->open( osl_File_OpenFlag_Write );
70 pStream->setSize( 0 );
73 else
74 pStream = nullptr;
75 #endif
78 SaxEmitter::~SaxEmitter()
80 try
82 m_xDocHdl->endDocument();
84 catch( xml::sax::SAXException& )
87 #if OSL_DEBUG_LEVEL > 0
88 if( pStream )
90 pStream->close();
91 delete pStream;
92 pStream = nullptr;
94 #endif
97 void SaxEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
99 OUString aTag = OUString::createFromAscii( pTag );
100 uno::Reference< xml::sax::XAttributeList > xAttr(
101 new SaxAttrList( rProperties ) );
104 m_xDocHdl->startElement( aTag, xAttr );
106 catch( xml::sax::SAXException& )
109 #if OSL_DEBUG_LEVEL > 0
110 if( pStream )
112 sal_uInt64 nWritten = 0;
113 for( int i = 0; i < nIndent; i++ )
114 pStream->write( " ", 4, nWritten );
116 OStringBuffer aBuf( 1024 );
117 aBuf.append( '<' );
118 aBuf.append( pTag );
119 for( PropertyMap::const_iterator it = rProperties.begin(); it != rProperties.end(); ++it )
121 aBuf.append( ' ' );
122 aBuf.append( OUStringToOString( it->first, RTL_TEXTENCODING_UTF8 ) );
123 aBuf.append( "=\"" );
124 aBuf.append( OUStringToOString( it->second, RTL_TEXTENCODING_UTF8 ) );
125 aBuf.append( "\"" );
127 aBuf.append( ">\n" );
128 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
129 nIndent++;
131 #endif
134 void SaxEmitter::write( const OUString& rText )
138 m_xDocHdl->characters( rText );
140 catch( xml::sax::SAXException& )
143 #if OSL_DEBUG_LEVEL > 0
144 if( pStream )
146 OString aStr( OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
147 sal_uInt64 nWritten = 0;
148 pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
150 #endif
153 void SaxEmitter::endTag( const char* pTag )
155 OUString aTag = OUString::createFromAscii( pTag );
158 m_xDocHdl->endElement( aTag );
160 catch( xml::sax::SAXException& )
163 #if OSL_DEBUG_LEVEL > 0
164 if( pStream )
166 sal_uInt64 nWritten = 0;
167 for( int i = 0; i < nIndent; i++ )
168 pStream->write( " ", 4, nWritten );
170 OStringBuffer aBuf( 1024 );
171 aBuf.append( "</" );
172 aBuf.append( pTag );
173 aBuf.append( ">\n" );
174 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
175 nIndent--;
177 #endif
180 XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
182 return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl));
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */