cid#1607171 Data race condition
[LibreOffice.git] / sdext / source / pdfimport / odf / odfemitter.cxx
blobc4ee6df310b2417a084c0cccbf89ffdae0fbfcc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <odfemitter.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <osl/diagnose.h>
25 #include <com/sun/star/io/XOutputStream.hpp>
27 #include <comphelper/stl_types.hxx>
28 #include <utility>
30 using namespace com::sun::star;
32 namespace pdfi
35 namespace {
37 class OdfEmitter : public XmlEmitter
39 private:
40 uno::Reference<io::XOutputStream> m_xOutput;
41 uno::Sequence<sal_Int8> m_aLineFeed;
42 uno::Sequence<sal_Int8> m_aBuf;
44 public:
45 explicit OdfEmitter( uno::Reference<io::XOutputStream> xOutput );
47 virtual void beginTag( const char* pTag, const PropertyMap& rProperties ) override;
48 virtual void write( const OUString& rString ) override;
49 virtual void endTag( const char* pTag ) override;
54 OdfEmitter::OdfEmitter( uno::Reference<io::XOutputStream> xOutput ) :
55 m_xOutput(std::move( xOutput )),
56 m_aLineFeed{ '\n' }
58 OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
60 write(u"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"_ustr);
63 void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
65 OSL_PRECOND(pTag,"Invalid tag string");
67 OUStringBuffer aElement("<");
68 aElement.appendAscii(pTag);
69 aElement.append(" ");
71 std::vector<OUString> aAttributes;
72 for( const auto& rCurr : rProperties )
74 OUString aAttribute =
75 rCurr.first +
76 "=\"" +
77 rCurr.second +
78 "\" ";
79 aAttributes.push_back(aAttribute);
82 // since the hash map's sorting is undefined (and varies across
83 // platforms, and even between different compile-time settings),
84 // sort the attributes.
85 std::sort(aAttributes.begin(), aAttributes.end());
86 std::copy(aAttributes.begin(), aAttributes.end(),
87 comphelper::OUStringBufferAppender(aElement));
88 aElement.append(">");
90 write(aElement.makeStringAndClear());
93 void OdfEmitter::write( const OUString& rText )
95 const OString aStr = OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
96 const sal_Int32 nLen( aStr.getLength() );
97 m_aBuf.realloc( nLen );
98 const char* pStr = aStr.getStr();
99 std::copy(pStr,pStr+nLen,m_aBuf.getArray());
101 m_xOutput->writeBytes(m_aBuf);
102 m_xOutput->writeBytes(m_aLineFeed);
105 void OdfEmitter::endTag( const char* pTag )
107 OUStringBuffer aElement("</");
108 aElement.appendAscii(pTag);
109 aElement.append(">");
110 write(aElement.makeStringAndClear());
113 XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
115 return std::make_shared<OdfEmitter>(xOut);
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */