LibreOffice Module sdext (master) 1
odfemitter.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 <odfemitter.hxx>
22
23#include <rtl/ustrbuf.hxx>
24#include <osl/diagnose.h>
25#include <com/sun/star/io/XOutputStream.hpp>
26
28#include <utility>
29
30using namespace com::sun::star;
31
32namespace pdfi
33{
34
35namespace {
36
37class OdfEmitter : public XmlEmitter
38{
39private:
40 uno::Reference<io::XOutputStream> m_xOutput;
41 uno::Sequence<sal_Int8> m_aLineFeed;
42 uno::Sequence<sal_Int8> m_aBuf;
43
44public:
45 explicit OdfEmitter( uno::Reference<io::XOutputStream> xOutput );
46
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;
50};
51
52}
53
54OdfEmitter::OdfEmitter( uno::Reference<io::XOutputStream> xOutput ) :
55 m_xOutput(std::move( xOutput )),
56 m_aLineFeed{ '\n' }
57{
58 OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
59
60 write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
61}
62
63void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
64{
65 OSL_PRECOND(pTag,"Invalid tag string");
66
67 OUStringBuffer aElement("<");
68 aElement.appendAscii(pTag);
69 aElement.append(" ");
70
71 std::vector<OUString> aAttributes;
72 for( const auto& rCurr : rProperties )
73 {
74 OUString aAttribute =
75 rCurr.first +
76 "=\"" +
77 rCurr.second +
78 "\" ";
79 aAttributes.push_back(aAttribute);
80 }
81
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(),
88 aElement.append(">");
89
90 write(aElement.makeStringAndClear());
91}
92
93void OdfEmitter::write( const OUString& rText )
94{
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());
100
101 m_xOutput->writeBytes(m_aBuf);
102 m_xOutput->writeBytes(m_aLineFeed);
103}
104
105void OdfEmitter::endTag( const char* pTag )
106{
107 OUStringBuffer aElement("</");
108 aElement.appendAscii(pTag);
109 aElement.append(">");
110 write(aElement.makeStringAndClear());
111}
112
113XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
114{
115 return std::make_shared<OdfEmitter>(xOut);
116}
117
118}
119
120/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
aStr
std::shared_ptr< XmlEmitter > XmlEmitterSharedPtr
Definition: xmlemitter.hxx:48
XmlEmitterSharedPtr createOdfEmitter(const uno::Reference< io::XOutputStream > &xOut)
Definition: odfemitter.cxx:113
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::map< sal_Int32, STLPropertyMapEntry > PropertyMap
uno::Sequence< sal_Int8 > m_aBuf
Definition: odfemitter.cxx:42
uno::Reference< io::XOutputStream > m_xOutput
Definition: odfemitter.cxx:40
uno::Sequence< sal_Int8 > m_aLineFeed
Definition: odfemitter.cxx:41