1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: emitcontext.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "saxemitter.hxx"
36 #include "emitcontext.hxx"
37 #include "saxattrlist.hxx"
39 #include <rtl/strbuf.hxx>
40 #include <comphelper/anytostring.hxx>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
44 #if OSL_DEBUG_LEVEL > 1
45 #include <osl/file.hxx>
46 static osl::File
* pStream
= NULL
;
47 static int nIndent
= 0;
50 using namespace com::sun::star
;
55 SaxEmitter::SaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocHdl
) :
58 OSL_PRECOND(m_xDocHdl
.is(), "SaxEmitter(): invalid doc handler");
61 m_xDocHdl
->startDocument();
63 catch( xml::sax::SAXException
& )
67 rtl::OUStringToOString(
68 comphelper::anyToString( cppu::getCaughtException() ),
69 RTL_TEXTENCODING_UTF8
).getStr() );
72 #if OSL_DEBUG_LEVEL > 1
73 static const char* pDir
= getenv( "DBG_PDFIMPORT_DIR" );
76 rtl::OUString
aStr( rtl::OStringToOUString( pDir
, RTL_TEXTENCODING_UTF8
) );
77 rtl::OUString aFileURL
;
78 osl_getFileURLFromSystemPath( aStr
.pData
, &aFileURL
.pData
);
79 rtl::OUStringBuffer
aBuf( 256 );
80 aBuf
.append( aFileURL
);
81 aBuf
.appendAscii( "/pdfimport.xml" );
82 pStream
= new osl::File( aBuf
.makeStringAndClear() );
83 if( pStream
->open( OpenFlag_Write
| OpenFlag_Create
) )
85 pStream
->open( OpenFlag_Write
);
86 pStream
->setSize( 0 );
94 SaxEmitter::~SaxEmitter()
98 m_xDocHdl
->endDocument();
100 catch( xml::sax::SAXException
& )
104 rtl::OUStringToOString(
105 comphelper::anyToString( cppu::getCaughtException() ),
106 RTL_TEXTENCODING_UTF8
).getStr() );
109 #if OSL_DEBUG_LEVEL > 1
119 void SaxEmitter::beginTag( const char* pTag
, const PropertyMap
& rProperties
)
121 rtl::OUString aTag
= rtl::OUString::createFromAscii( pTag
);
122 uno::Reference
< xml::sax::XAttributeList
> xAttr(
123 new SaxAttrList( rProperties
) );
126 m_xDocHdl
->startElement( aTag
, xAttr
);
128 catch( xml::sax::SAXException
& )
132 rtl::OUStringToOString(
133 comphelper::anyToString( cppu::getCaughtException() ),
134 RTL_TEXTENCODING_UTF8
).getStr() );
137 #if OSL_DEBUG_LEVEL > 1
140 sal_uInt64 nWritten
= 0;
141 for( int i
= 0; i
< nIndent
; i
++ )
142 pStream
->write( " ", 4, nWritten
);
144 rtl::OStringBuffer
aBuf( 1024 );
147 for( PropertyMap::const_iterator it
= rProperties
.begin(); it
!= rProperties
.end(); ++it
)
150 aBuf
.append( rtl::OUStringToOString( it
->first
, RTL_TEXTENCODING_UTF8
) );
151 aBuf
.append( "=\"" );
152 aBuf
.append( rtl::OUStringToOString( it
->second
, RTL_TEXTENCODING_UTF8
) );
155 aBuf
.append( ">\n" );
156 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
162 void SaxEmitter::write( const rtl::OUString
& rText
)
166 m_xDocHdl
->characters( rText
);
168 catch( xml::sax::SAXException
& )
172 rtl::OUStringToOString(
173 comphelper::anyToString( cppu::getCaughtException() ),
174 RTL_TEXTENCODING_UTF8
).getStr() );
177 #if OSL_DEBUG_LEVEL > 1
180 rtl::OString
aStr( rtl::OUStringToOString( rText
, RTL_TEXTENCODING_UTF8
) );
181 sal_uInt64 nWritten
= 0;
182 pStream
->write( aStr
.getStr(), aStr
.getLength(), nWritten
);
187 void SaxEmitter::endTag( const char* pTag
)
189 rtl::OUString aTag
= rtl::OUString::createFromAscii( pTag
);
192 m_xDocHdl
->endElement( aTag
);
194 catch( xml::sax::SAXException
& )
198 rtl::OUStringToOString(
199 comphelper::anyToString( cppu::getCaughtException() ),
200 RTL_TEXTENCODING_UTF8
).getStr() );
203 #if OSL_DEBUG_LEVEL > 1
206 sal_uInt64 nWritten
= 0;
207 for( int i
= 0; i
< nIndent
; i
++ )
208 pStream
->write( " ", 4, nWritten
);
210 rtl::OStringBuffer
aBuf( 1024 );
213 aBuf
.append( ">\n" );
214 pStream
->write( aBuf
.getStr(), aBuf
.getLength(), nWritten
);
220 XmlEmitterSharedPtr
createSaxEmitter( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocHdl
)
222 return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl
));