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