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 
27 #include <comphelper/stl_types.hxx>
28 
29 using namespace com::sun::star;
30 
31 namespace pdfi
32 {
33 
34 namespace {
35 
36 class OdfEmitter : public XmlEmitter
37 {
38 private:
39  uno::Reference<io::XOutputStream> m_xOutput;
40  uno::Sequence<sal_Int8> m_aLineFeed;
41  uno::Sequence<sal_Int8> m_aBuf;
42 
43 public:
44  explicit OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput );
45 
46  virtual void beginTag( const char* pTag, const PropertyMap& rProperties ) override;
47  virtual void write( const OUString& rString ) override;
48  virtual void endTag( const char* pTag ) override;
49 };
50 
51 }
52 
53 OdfEmitter::OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput ) :
54  m_xOutput( xOutput ),
55  m_aLineFeed(1)
56 {
57  OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
58  m_aLineFeed[0] = '\n';
59 
60  write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
61 }
62 
63 void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
64 {
65  OSL_PRECOND(pTag,"Invalid tag string");
66 
67  OUStringBuffer aElement;
68  aElement.append("<");
69  aElement.appendAscii(pTag);
70  aElement.append(" ");
71 
72  std::vector<OUString> aAttributes;
73  for( const auto& rCurr : rProperties )
74  {
75  OUString aAttribute =
76  rCurr.first +
77  "=\"" +
78  rCurr.second +
79  "\" ";
80  aAttributes.push_back(aAttribute);
81  }
82 
83  // since the hash map's sorting is undefined (and varies across
84  // platforms, and even between different compile-time settings),
85  // sort the attributes.
86  std::sort(aAttributes.begin(), aAttributes.end());
87  std::copy(aAttributes.begin(), aAttributes.end(),
89  aElement.append(">");
90 
91  write(aElement.makeStringAndClear());
92 }
93 
94 void OdfEmitter::write( const OUString& rText )
95 {
96  const OString aStr = OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
97  const sal_Int32 nLen( aStr.getLength() );
98  m_aBuf.realloc( nLen );
99  const char* pStr = aStr.getStr();
100  std::copy(pStr,pStr+nLen,m_aBuf.getArray());
101 
102  m_xOutput->writeBytes(m_aBuf);
103  m_xOutput->writeBytes(m_aLineFeed);
104 }
105 
106 void OdfEmitter::endTag( const char* pTag )
107 {
108  OUStringBuffer aElement;
109  aElement.append("</");
110  aElement.appendAscii(pTag);
111  aElement.append(">");
112  write(aElement.makeStringAndClear());
113 }
114 
115 XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
116 {
117  return std::make_shared<OdfEmitter>(xOut);
118 }
119 
120 }
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< io::XOutputStream > m_xOutput
Definition: odfemitter.cxx:39
uno::Sequence< sal_Int8 > m_aBuf
Definition: odfemitter.cxx:41
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::map< sal_Int32, STLPropertyMapEntry > PropertyMap
std::shared_ptr< XmlEmitter > XmlEmitterSharedPtr
Definition: xmlemitter.hxx:48
XmlEmitterSharedPtr createOdfEmitter(const uno::Reference< io::XOutputStream > &xOut)
Definition: odfemitter.cxx:115
uno::Sequence< sal_Int8 > m_aLineFeed
Definition: odfemitter.cxx:40
aStr