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  m_aBuf()
57 {
58  OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
59  m_aLineFeed[0] = '\n';
60 
61  OUString aElement = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
62  write(aElement);
63 }
64 
65 void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
66 {
67  OSL_PRECOND(pTag,"Invalid tag string");
68 
69  OUStringBuffer aElement;
70  aElement.append("<");
71  aElement.appendAscii(pTag);
72  aElement.append(" ");
73 
74  std::vector<OUString> aAttributes;
75  for( const auto& rCurr : rProperties )
76  {
77  OUString aAttribute =
78  rCurr.first +
79  "=\"" +
80  rCurr.second +
81  "\" ";
82  aAttributes.push_back(aAttribute);
83  }
84 
85  // since the hash map's sorting is undefined (and varies across
86  // platforms, and even between different compile-time settings),
87  // sort the attributes.
88  std::sort(aAttributes.begin(), aAttributes.end());
89  std::copy(aAttributes.begin(), aAttributes.end(),
91  aElement.append(">");
92 
93  write(aElement.makeStringAndClear());
94 }
95 
96 void OdfEmitter::write( const OUString& rText )
97 {
98  const OString aStr = OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
99  const sal_Int32 nLen( aStr.getLength() );
100  m_aBuf.realloc( nLen );
101  const char* pStr = aStr.getStr();
102  std::copy(pStr,pStr+nLen,m_aBuf.getArray());
103 
104  m_xOutput->writeBytes(m_aBuf);
105  m_xOutput->writeBytes(m_aLineFeed);
106 }
107 
108 void OdfEmitter::endTag( const char* pTag )
109 {
110  OUStringBuffer aElement;
111  aElement.append("</");
112  aElement.appendAscii(pTag);
113  aElement.append(">");
114  write(aElement.makeStringAndClear());
115 }
116 
117 XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
118 {
119  return std::make_shared<OdfEmitter>(xOut);
120 }
121 
122 }
123 
124 /* 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
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:117
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
uno::Sequence< sal_Int8 > m_aLineFeed
Definition: odfemitter.cxx:40
aStr