LibreOffice Module sdext (master) 1
emitcontext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21#include <saxemitter.hxx>
22#include "emitcontext.hxx"
23#include "saxattrlist.hxx"
24
25#include <rtl/strbuf.hxx>
26#include <osl/diagnose.h>
27#include <com/sun/star/xml/sax/SAXException.hpp>
28#include <xmloff/xmlimp.hxx>
29
30#if OSL_DEBUG_LEVEL > 0
31#include <osl/file.hxx>
32static osl::File* pStream = nullptr;
33static int nIndent = 0;
34#endif
35
36using namespace com::sun::star;
37
38namespace pdfi
39{
40
41SaxEmitter::SaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl ) :
42 m_xDocHdl( xDocHdl )
43{
44 OSL_PRECOND(m_xDocHdl.is(), "SaxEmitter(): invalid doc handler");
45 if (SvXMLImport *pFastHandler = dynamic_cast<SvXMLImport*>(m_xDocHdl.get()))
46 m_xDocHdl.set( new SvXMLLegacyToFastDocHandler( pFastHandler ) );
47 try
48 {
49 m_xDocHdl->startDocument();
50 }
51 catch( xml::sax::SAXException& )
52 {
53 }
54#if OSL_DEBUG_LEVEL > 0
55 static const char* pDir = getenv( "DBG_PDFIMPORT_DIR" );
56 if( pDir )
57 {
58 OUString aStr( OStringToOUString( pDir, RTL_TEXTENCODING_UTF8 ) );
59 OUString aFileURL;
60 osl_getFileURLFromSystemPath( aStr.pData, &aFileURL.pData );
61 pStream = new osl::File( aFileURL + "/pdfimport.xml" );
62 if( pStream->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) )
63 {
64 pStream->open( osl_File_OpenFlag_Write );
65 pStream->setSize( 0 );
66 }
67 }
68 else
69 pStream = nullptr;
70#endif
71}
72
74{
75 try
76 {
77 m_xDocHdl->endDocument();
78 }
79 catch( xml::sax::SAXException& )
80 {
81 }
82#if OSL_DEBUG_LEVEL > 0
83 if( pStream )
84 {
85 pStream->close();
86 delete pStream;
87 pStream = nullptr;
88 }
89#endif
90}
91
92void SaxEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
93{
94 OUString aTag = OUString::createFromAscii( pTag );
95 uno::Reference< xml::sax::XAttributeList > xAttr(
96 new SaxAttrList( rProperties ) );
97 try
98 {
99 m_xDocHdl->startElement( aTag, xAttr );
100 }
101 catch( xml::sax::SAXException& )
102 {
103 }
104#if OSL_DEBUG_LEVEL > 0
105 if( !pStream )
106 return;
107
108 sal_uInt64 nWritten = 0;
109 for( int i = 0; i < nIndent; i++ )
110 pStream->write( " ", 4, nWritten );
111
112 OStringBuffer aBuf( 1024 );
113 aBuf.append( OString::Concat("<") + pTag );
114 for( const auto& rProperty : rProperties )
115 {
116 aBuf.append( " "
117 + OUStringToOString( rProperty.first, RTL_TEXTENCODING_UTF8 )
118 + "=\""
119 + OUStringToOString( rProperty.second, RTL_TEXTENCODING_UTF8 )
120 + "\"" );
121 }
122 aBuf.append( ">\n" );
123 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
124 nIndent++;
125#endif
126}
127
128void SaxEmitter::write( const OUString& rText )
129{
130 try
131 {
132 m_xDocHdl->characters( rText );
133 }
134 catch( xml::sax::SAXException& )
135 {
136 }
137#if OSL_DEBUG_LEVEL > 0
138 if( pStream )
139 {
140 OString aStr( OUStringToOString( rText, RTL_TEXTENCODING_UTF8 ) );
141 sal_uInt64 nWritten = 0;
142 pStream->write( aStr.getStr(), aStr.getLength(), nWritten );
143 }
144#endif
145}
146
147void SaxEmitter::endTag( const char* pTag )
148{
149 OUString aTag = OUString::createFromAscii( pTag );
150 try
151 {
152 m_xDocHdl->endElement( aTag );
153 }
154 catch( xml::sax::SAXException& )
155 {
156 }
157#if OSL_DEBUG_LEVEL > 0
158 if( !pStream )
159 return;
160
161 sal_uInt64 nWritten = 0;
162 for( int i = 0; i < nIndent; i++ )
163 pStream->write( " ", 4, nWritten );
164
165 OString aBuf = OString::Concat("</") + pTag + ">\n";
166 pStream->write( aBuf.getStr(), aBuf.getLength(), nWritten );
167 nIndent--;
168#endif
169}
170
171XmlEmitterSharedPtr createSaxEmitter( const uno::Reference< xml::sax::XDocumentHandler >& xDocHdl )
172{
173 return std::make_shared<SaxEmitter>(xDocHdl);
174}
175
176}
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::xml::sax::XDocumentHandler > m_xDocHdl
Definition: emitcontext.hxx:36
virtual void write(const OUString &rString) override
Write PCTEXT as-is to output.
virtual void endTag(const char *pTag) override
Close previously opened tag.
virtual ~SaxEmitter() override
Definition: emitcontext.cxx:73
virtual void beginTag(const char *pTag, const PropertyMap &rProperties) override
Open up a tag with the given properties.
Definition: emitcontext.cxx:92
SaxEmitter(const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocHdl)
Definition: emitcontext.cxx:41
static int nIndent
Definition: emitcontext.cxx:33
static osl::File * pStream
Definition: emitcontext.cxx:32
aStr
aBuf
int i
XmlEmitterSharedPtr createSaxEmitter(const css::uno::Reference< css::xml::sax::XDocumentHandler > &xDocHdl)
std::unordered_map< OUString, OUString > PropertyMap
Definition: pdfihelper.hxx:44
std::shared_ptr< XmlEmitter > XmlEmitterSharedPtr
Definition: xmlemitter.hxx:48
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)