1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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;
38 using namespace com::sun::star
;
43 SaxEmitter::SaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& 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
) );
51 m_xDocHdl
->startDocument();
53 catch( xml::sax::SAXException
& )
56 #if OSL_DEBUG_LEVEL > 0
57 static const char* pDir
= getenv( "DBG_PDFIMPORT_DIR" );
60 OUString
aStr( OStringToOUString( pDir
, RTL_TEXTENCODING_UTF8
) );
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 );
78 SaxEmitter::~SaxEmitter()
82 m_xDocHdl
->endDocument();
84 catch( xml::sax::SAXException
& )
87 #if OSL_DEBUG_LEVEL > 0
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
112 sal_uInt64 nWritten
= 0;
113 for( int i
= 0; i
< nIndent
; i
++ )
114 pStream
->write( " ", 4, nWritten
);
116 OStringBuffer
aBuf( 1024 );
119 for( PropertyMap::const_iterator it
= rProperties
.begin(); it
!= rProperties
.end(); ++it
)
122 aBuf
.append( OUStringToOString( it
->first
, RTL_TEXTENCODING_UTF8
) );
123 aBuf
.append( "=\"" );
124 aBuf
.append( OUStringToOString( it
->second
, RTL_TEXTENCODING_UTF8
) );
127 aBuf
.append( ">\n" );
128 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
134 void SaxEmitter::write( const OUString
& rText
)
138 m_xDocHdl
->characters( rText
);
140 catch( xml::sax::SAXException
& )
143 #if OSL_DEBUG_LEVEL > 0
146 OString
aStr( OUStringToOString( rText
, RTL_TEXTENCODING_UTF8
) );
147 sal_uInt64 nWritten
= 0;
148 pStream
->write( aStr
.getStr(), aStr
.getLength(), nWritten
);
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
166 sal_uInt64 nWritten
= 0;
167 for( int i
= 0; i
< nIndent
; i
++ )
168 pStream
->write( " ", 4, nWritten
);
170 OStringBuffer
aBuf( 1024 );
173 aBuf
.append( ">\n" );
174 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
180 XmlEmitterSharedPtr
createSaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocHdl
)
182 return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl
));
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */