merge the formfield patch from ooo-build
[ooovba.git] / sdext / source / pdfimport / sax / emitcontext.cxx
blob07f418e0aaa5a7c0b6f5177fb110c4d89debce7c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: emitcontext.cxx,v $
11 * $Revision: 1.2 $
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;
48 #endif
50 using namespace com::sun::star;
52 namespace pdfi
55 SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
56 m_xDocHdl( xDocHdl )
58 OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
59 try
61 m_xDocHdl->startDocument();
63 catch( xml::sax::SAXException& )
65 #if 0
66 OSL_ENSURE( false,
67 rtl::OUStringToOString(
68 comphelper::anyToString( cppu::getCaughtException() ),
69 RTL_TEXTENCODING_UTF8 ).getStr() );
70 #endif
72 #if OSL_DEBUG_LEVEL > 1
73 static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
74 if( pDir )
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 );
89 else
90 pStream = 0;
91 #endif
94 SaxEmitter::~SaxEmitter()
96 try
98 m_xDocHdl->endDocument();
100 catch( xml::sax::SAXException& )
102 #if 0
103 OSL_ENSURE( false,
104 rtl::OUStringToOString(
105 comphelper::anyToString( cppu::getCaughtException() ),
106 RTL_TEXTENCODING_UTF8 ).getStr() );
107 #endif
109 #if OSL_DEBUG_LEVEL > 1
110 if( pStream )
112 pStream->close();
113 delete pStream;
114 pStream = 0;
116 #endif
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& )
130 #if 0
131 OSL_ENSURE( false,
132 rtl::OUStringToOString(
133 comphelper::anyToString( cppu::getCaughtException() ),
134 RTL_TEXTENCODING_UTF8 ).getStr() );
135 #endif
137 #if OSL_DEBUG_LEVEL > 1
138 if( pStream )
140 sal_uInt64 nWritten = 0;
141 for( int i = 0; i < nIndent; i++ )
142 pStream->write( " ", 4, nWritten );
144 rtl::OStringBuffer aBuf( 1024 );
145 aBuf.append( '<' );
146 aBuf.append( pTag );
147 for( PropertyMap::const_iterator it = rProperties.begin(); it != rProperties.end(); ++it )
149 aBuf.append( ' ' );
150 aBuf.append( rtl::OUStringToOString( it->first, RTL_TEXTENCODING_UTF8 ) );
151 aBuf.append( "=\"" );
152 aBuf.append( rtl::OUStringToOString( it->second, RTL_TEXTENCODING_UTF8 ) );
153 aBuf.append( "\"" );
155 aBuf.append( ">\n" );
156 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
157 nIndent++;
159 #endif
162 void SaxEmitter::write( const rtl::OUString& rText )
166 m_xDocHdl->characters( rText );
168 catch( xml::sax::SAXException& )
170 #if 0
171 OSL_ENSURE( false,
172 rtl::OUStringToOString(
173 comphelper::anyToString( cppu::getCaughtException() ),
174 RTL_TEXTENCODING_UTF8 ).getStr() );
175 #endif
177 #if OSL_DEBUG_LEVEL > 1
178 if( pStream )
180 rtl::OString aStr( rtl::OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
181 sal_uInt64 nWritten = 0;
182 pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
184 #endif
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& )
196 #if 0
197 OSL_ENSURE( false,
198 rtl::OUStringToOString(
199 comphelper::anyToString( cppu::getCaughtException() ),
200 RTL_TEXTENCODING_UTF8 ).getStr() );
201 #endif
203 #if OSL_DEBUG_LEVEL > 1
204 if( pStream )
206 sal_uInt64 nWritten = 0;
207 for( int i = 0; i < nIndent; i++ )
208 pStream->write( " ", 4, nWritten );
210 rtl::OStringBuffer aBuf( 1024 );
211 aBuf.append( "</" );
212 aBuf.append( pTag );
213 aBuf.append( ">\n" );
214 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
215 nIndent--;
217 #endif
220 XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
222 return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl));