1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "saxemitter.hxx"
28 #include "emitcontext.hxx"
29 #include "saxattrlist.hxx"
30 
31 #include <rtl/strbuf.hxx>
32 #include <cppuhelper/exc_hlp.hxx>
33 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
34 
35 #if OSL_DEBUG_LEVEL > 1
36 #include <osl/file.hxx>
37 static osl::File* pStream = NULL;
38 static int nIndent = 0;
39 #endif
40 
41 using namespace com::sun::star;
42 
43 namespace pdfi
44 {
45 
SaxEmitter(const uno::Reference<xml::sax::XDocumentHandler> & xDocHdl)46 SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
47     m_xDocHdl( xDocHdl )
48 {
49     OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
50     try
51     {
52         m_xDocHdl->startDocument();
53     }
54     catch( xml::sax::SAXException& )
55     {
56 	}
57 #if OSL_DEBUG_LEVEL > 1
58     static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
59     if( pDir )
60     {
61         rtl::OUString aStr( rtl::OStringToOUString( pDir, RTL_TEXTENCODING_UTF8 ) );
62         rtl::OUString aFileURL;
63         osl_getFileURLFromSystemPath( aStr.pData, &aFileURL.pData );
64         rtl::OUStringBuffer aBuf( 256 );
65         aBuf.append( aFileURL );
66         aBuf.appendAscii( "/pdfimport.xml" );
67         pStream = new osl::File( aBuf.makeStringAndClear() );
68         if( pStream->open( OpenFlag_Write | OpenFlag_Create ) )
69         {
70             pStream->open( OpenFlag_Write );
71             pStream->setSize( 0 );
72         }
73     }
74     else
75         pStream = 0;
76 #endif
77 }
78 
~SaxEmitter()79 SaxEmitter::~SaxEmitter()
80 {
81     try
82     {
83         m_xDocHdl->endDocument();
84     }
85     catch( xml::sax::SAXException& )
86     {
87 	}
88 #if OSL_DEBUG_LEVEL > 1
89     if( pStream )
90     {
91         pStream->close();
92         delete pStream;
93         pStream = 0;
94     }
95 #endif
96 }
97 
beginTag(const char * pTag,const PropertyMap & rProperties)98 void SaxEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
99 {
100     rtl::OUString aTag = rtl::OUString::createFromAscii( pTag );
101     uno::Reference< xml::sax::XAttributeList > xAttr(
102         new SaxAttrList( rProperties ) );
103     try
104     {
105         m_xDocHdl->startElement( aTag, xAttr );
106     }
107     catch( xml::sax::SAXException& )
108     {
109     }
110 #if OSL_DEBUG_LEVEL > 1
111     if( pStream )
112     {
113         sal_uInt64 nWritten = 0;
114         for( int i = 0; i < nIndent; i++ )
115             pStream->write( "    ", 4, nWritten );
116 
117         rtl::OStringBuffer aBuf( 1024 );
118         aBuf.append( '<' );
119         aBuf.append( pTag );
120         for( PropertyMap::const_iterator it = rProperties.begin(); it != rProperties.end(); ++it )
121         {
122             aBuf.append( ' ' );
123             aBuf.append( rtl::OUStringToOString( it->first, RTL_TEXTENCODING_UTF8 ) );
124             aBuf.append( "=\"" );
125             aBuf.append( rtl::OUStringToOString( it->second, RTL_TEXTENCODING_UTF8 ) );
126             aBuf.append( "\"" );
127         }
128         aBuf.append( ">\n" );
129         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
130         nIndent++;
131     }
132 #endif
133 }
134 
write(const rtl::OUString & rText)135 void SaxEmitter::write( const rtl::OUString& rText )
136 {
137     try
138     {
139         m_xDocHdl->characters( rText );
140     }
141     catch( xml::sax::SAXException& )
142     {
143 	}
144 #if OSL_DEBUG_LEVEL > 1
145     if( pStream )
146     {
147         rtl::OString aStr( rtl::OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
148         sal_uInt64 nWritten = 0;
149         pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
150     }
151 #endif
152 }
153 
endTag(const char * pTag)154 void SaxEmitter::endTag( const char* pTag )
155 {
156     rtl::OUString aTag = rtl::OUString::createFromAscii( pTag );
157     try
158     {
159         m_xDocHdl->endElement( aTag );
160     }
161     catch( xml::sax::SAXException& )
162     {
163 	}
164 #if OSL_DEBUG_LEVEL > 1
165     if( pStream )
166     {
167         sal_uInt64 nWritten = 0;
168         for( int i = 0; i < nIndent; i++ )
169             pStream->write( "    ", 4, nWritten );
170 
171         rtl::OStringBuffer aBuf( 1024 );
172         aBuf.append( "</" );
173         aBuf.append( pTag );
174         aBuf.append( ">\n" );
175         pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
176         nIndent--;
177     }
178 #endif
179 }
180 
createSaxEmitter(const uno::Reference<xml::sax::XDocumentHandler> & xDocHdl)181 XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
182 {
183     return XmlEmitterSharedPtr(new SaxEmitter(xDocHdl));
184 }
185 
186 }
187