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 <com/sun/star/xml/sax/SAXException.hpp>
28 #include <xmloff/xmlimp.hxx>
30 #if OSL_DEBUG_LEVEL > 0
31 #include <osl/file.hxx>
32 static osl::File
* pStream
= nullptr;
33 static int nIndent
= 0;
36 using namespace com::sun::star
;
41 SaxEmitter::SaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocHdl
) :
44 OSL_PRECOND(m_xDocHdl
.is(), "SaxEmitter(): invalid doc handler");
45 if (SvXMLImport
*pFastHandler
= dynamic_cast<SvXMLImport
*>(m_xDocHdl
.get()))
46 m_xDocHdl
.set( new SvXMLLegacyToFastDocHandler( pFastHandler
) );
49 m_xDocHdl
->startDocument();
51 catch( xml::sax::SAXException
& )
54 #if OSL_DEBUG_LEVEL > 0
55 static const char* pDir
= getenv( "DBG_PDFIMPORT_DIR" );
58 OUString
aStr( OStringToOUString( pDir
, RTL_TEXTENCODING_UTF8
) );
60 osl_getFileURLFromSystemPath( aStr
.pData
, &aFileURL
.pData
);
61 pStream
= new osl::File( aFileURL
+ "/pdfimport.xml" );
62 if( pStream
->open( osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
) )
64 pStream
->open( osl_File_OpenFlag_Write
);
65 pStream
->setSize( 0 );
73 SaxEmitter::~SaxEmitter()
77 m_xDocHdl
->endDocument();
79 catch( xml::sax::SAXException
& )
82 #if OSL_DEBUG_LEVEL > 0
92 void SaxEmitter::beginTag( const char* pTag
, const PropertyMap
& rProperties
)
94 OUString aTag
= OUString::createFromAscii( pTag
);
95 uno::Reference
< xml::sax::XAttributeList
> xAttr(
96 new SaxAttrList( rProperties
) );
99 m_xDocHdl
->startElement( aTag
, xAttr
);
101 catch( xml::sax::SAXException
& )
104 #if OSL_DEBUG_LEVEL > 0
108 sal_uInt64 nWritten
= 0;
109 for( int i
= 0; i
< nIndent
; i
++ )
110 pStream
->write( " ", 4, nWritten
);
112 OStringBuffer
aBuf( 1024 );
115 for( const auto& rProperty
: rProperties
)
118 aBuf
.append( OUStringToOString( rProperty
.first
, RTL_TEXTENCODING_UTF8
) );
119 aBuf
.append( "=\"" );
120 aBuf
.append( OUStringToOString( rProperty
.second
, RTL_TEXTENCODING_UTF8
) );
123 aBuf
.append( ">\n" );
124 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
129 void SaxEmitter::write( const OUString
& rText
)
133 m_xDocHdl
->characters( rText
);
135 catch( xml::sax::SAXException
& )
138 #if OSL_DEBUG_LEVEL > 0
141 OString
aStr( OUStringToOString( rText
, RTL_TEXTENCODING_UTF8
) );
142 sal_uInt64 nWritten
= 0;
143 pStream
->write( aStr
.getStr(), aStr
.getLength(), nWritten
);
148 void SaxEmitter::endTag( const char* pTag
)
150 OUString aTag
= OUString::createFromAscii( pTag
);
153 m_xDocHdl
->endElement( aTag
);
155 catch( xml::sax::SAXException
& )
158 #if OSL_DEBUG_LEVEL > 0
162 sal_uInt64 nWritten
= 0;
163 for( int i
= 0; i
< nIndent
; i
++ )
164 pStream
->write( " ", 4, nWritten
);
166 OString aBuf
= "</" + rtl::OStringView(pTag
) + ">\n";
167 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
172 XmlEmitterSharedPtr
createSaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocHdl
)
174 return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl
));
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */