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