bump product version to 5.0.4.1
[LibreOffice.git] / sdext / source / pdfimport / odf / odfemitter.cxx
blobf99a50fd9c800324bfd5590592b1b71b1a8b277a
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 <cppuhelper/exc_hlp.hxx>
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/io/XOutputStream.hpp>
28 #include <boost/bind.hpp>
30 using namespace com::sun::star;
32 namespace pdfi
35 class OdfEmitter : public XmlEmitter
37 private:
38 uno::Reference<io::XOutputStream> m_xOutput;
39 uno::Sequence<sal_Int8> m_aLineFeed;
40 uno::Sequence<sal_Int8> m_aBuf;
42 public:
43 explicit OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput );
45 virtual void beginTag( const char* pTag, const PropertyMap& rProperties ) SAL_OVERRIDE;
46 virtual void write( const OUString& rString ) SAL_OVERRIDE;
47 virtual void endTag( const char* pTag ) SAL_OVERRIDE;
50 OdfEmitter::OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput ) :
51 m_xOutput( xOutput ),
52 m_aLineFeed(1),
53 m_aBuf()
55 OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
56 m_aLineFeed[0] = '\n';
58 OUStringBuffer aElement;
59 aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
60 write(aElement.makeStringAndClear());
63 void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
65 OSL_PRECOND(pTag,"Invalid tag string");
67 OUStringBuffer aElement;
68 aElement.appendAscii("<");
69 aElement.appendAscii(pTag);
70 aElement.appendAscii(" ");
72 std::vector<OUString> aAttributes;
73 PropertyMap::const_iterator aCurr(rProperties.begin());
74 const PropertyMap::const_iterator aEnd(rProperties.end());
75 while( aCurr != aEnd )
77 OUStringBuffer aAttribute;
78 aAttribute.append(aCurr->first);
79 aAttribute.appendAscii("=\"");
80 aAttribute.append(aCurr->second);
81 aAttribute.appendAscii("\" ");
82 aAttributes.push_back(aAttribute.makeStringAndClear());
83 ++aCurr;
86 // since the hash map's sorting is undefined (and varies across
87 // platforms, and even between different compile-time settings),
88 // sort the attributes.
89 std::sort(aAttributes.begin(), aAttributes.end());
90 std::for_each(aAttributes.begin(),
91 aAttributes.end(),
92 boost::bind( (OUStringBuffer& (OUStringBuffer::*)(const OUString&))
93 (&OUStringBuffer::append),
94 boost::ref(aElement),
95 _1 ));
96 aElement.appendAscii(">");
98 write(aElement.makeStringAndClear());
101 void OdfEmitter::write( const OUString& rText )
103 const OString aStr = OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
104 const sal_Int32 nLen( aStr.getLength() );
105 m_aBuf.realloc( nLen );
106 const sal_Char* pStr = aStr.getStr();
107 std::copy(pStr,pStr+nLen,m_aBuf.getArray());
109 m_xOutput->writeBytes(m_aBuf);
110 m_xOutput->writeBytes(m_aLineFeed);
113 void OdfEmitter::endTag( const char* pTag )
115 OUStringBuffer aElement;
116 aElement.appendAscii("</");
117 aElement.appendAscii(pTag);
118 aElement.appendAscii(">");
119 write(aElement.makeStringAndClear());
122 XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
124 return XmlEmitterSharedPtr(new OdfEmitter(xOut));
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */