1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sdext.hxx"
30 
31 #include "saxemitter.hxx"
32 #include "emitcontext.hxx"
33 #include "saxattrlist.hxx"
34 
35 #include <rtl/strbuf.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
38 
39 #if OSL_DEBUG_LEVEL > 1
40 #include <osl/file.hxx>
41 static osl::File* pStream = NULL;
42 static int nIndent = 0;
43 #endif
44 
45 using namespace com::sun::star;
46 
47 namespace pdfi
48 {
49 
50 SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
51     m_xDocHdl( xDocHdl )
52 {
53     OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
54     try
55     {
56         m_xDocHdl->startDocument();
57     }
58     catch( xml::sax::SAXException& )
59     {
60 	}
61 #if OSL_DEBUG_LEVEL > 1
62     static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
63     if( pDir )
64     {
65         rtl::OUString aStr( rtl::OStringToOUString( pDir, RTL_TEXTENCODING_UTF8 ) );
66         rtl::OUString aFileURL;
67         osl_getFileURLFromSystemPath( aStr.pData, &aFileURL.pData );
68         rtl::OUStringBuffer aBuf( 256 );
69         aBuf.append( aFileURL );
70         aBuf.appendAscii( "/pdfimport.xml" );
71         pStream = new osl::File( aBuf.makeStringAndClear() );
72         if( pStream->open( OpenFlag_Write | OpenFlag_Create ) )
73         {
74             pStream->open( OpenFlag_Write );
75             pStream->setSize( 0 );
76         }
77     }
78     else
79         pStream = 0;
80 #endif
81 }
82 
83 SaxEmitter::~SaxEmitter()
84 {
85     try
86     {
87         m_xDocHdl->endDocument();
88     }
89     catch( xml::sax::SAXException& )
90     {
91 	}
92 #if OSL_DEBUG_LEVEL > 1
93     if( pStream )
94     {
95         pStream->close();
96         delete pStream;
97         pStream = 0;
98     }
99 #endif
100 }
101 
102 void SaxEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
103 {
104     rtl::OUString aTag = rtl::OUString::createFromAscii( pTag );
105     uno::Reference< xml::sax::XAttributeList > xAttr(
106         new SaxAttrList( rProperties ) );
107     try
108     {
109         m_xDocHdl->startElement( aTag, xAttr );
110     }
111     catch( xml::sax::SAXException& )
112     {
113     }
114 #if OSL_DEBUG_LEVEL > 1
115     if( pStream )
116     {
117         sal_uInt64 nWritten = 0;
118         for( int i = 0; i < nIndent; i++ )
119             pStream->write( "    ", 4, nWritten );
120 
121         rtl::OStringBuffer aBuf( 1024 );
122         aBuf.append( '<' );
123         aBuf.append( pTag );
124         for( PropertyMap::const_iterator it = rProperties.begin(); it != rProperties.end(); ++it )
125         {
126             aBuf.append( ' ' );
127             aBuf.append( rtl::OUStringToOString( it->first, RTL_TEXTENCODING_UTF8 ) );
128             aBuf.append( "=\"" );
129             aBuf.append( rtl::OUStringToOString( it->second, RTL_TEXTENCODING_UTF8 ) );
130             aBuf.append( "\"" );
131         }
132         aBuf.append( ">\n" );
133         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
134         nIndent++;
135     }
136 #endif
137 }
138 
139 void SaxEmitter::write( const rtl::OUString& rText )
140 {
141     try
142     {
143         m_xDocHdl->characters( rText );
144     }
145     catch( xml::sax::SAXException& )
146     {
147 	}
148 #if OSL_DEBUG_LEVEL > 1
149     if( pStream )
150     {
151         rtl::OString aStr( rtl::OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
152         sal_uInt64 nWritten = 0;
153         pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
154     }
155 #endif
156 }
157 
158 void SaxEmitter::endTag( const char* pTag )
159 {
160     rtl::OUString aTag = rtl::OUString::createFromAscii( pTag );
161     try
162     {
163         m_xDocHdl->endElement( aTag );
164     }
165     catch( xml::sax::SAXException& )
166     {
167 	}
168 #if OSL_DEBUG_LEVEL > 1
169     if( pStream )
170     {
171         sal_uInt64 nWritten = 0;
172         for( int i = 0; i < nIndent; i++ )
173             pStream->write( "    ", 4, nWritten );
174 
175         rtl::OStringBuffer aBuf( 1024 );
176         aBuf.append( "</" );
177         aBuf.append( pTag );
178         aBuf.append( ">\n" );
179         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
180         nIndent--;
181     }
182 #endif
183 }
184 
185 XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
186 {
187     return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl));
188 }
189 
190 }
191