Branch data Line data Source code
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 <cppuhelper/exc_hlp.hxx>
25 : : #include <com/sun/star/io/XInputStream.hpp>
26 : : #include <com/sun/star/io/XOutputStream.hpp>
27 : : #include <boost/bind.hpp>
28 : :
29 : : using namespace com::sun::star;
30 : :
31 : : namespace pdfi
32 : : {
33 : :
34 [ + - ][ + - ]: 20 : class OdfEmitter : public XmlEmitter
[ - + ]
35 : : {
36 : : private:
37 : : uno::Reference<io::XOutputStream> m_xOutput;
38 : : uno::Sequence<sal_Int8> m_aLineFeed;
39 : : uno::Sequence<sal_Int8> m_aBuf;
40 : :
41 : : public:
42 : : explicit OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput );
43 : :
44 : : virtual void beginTag( const char* pTag, const PropertyMap& rProperties );
45 : : virtual void write( const rtl::OUString& rString );
46 : : virtual void endTag( const char* pTag );
47 : : };
48 : :
49 : 10 : OdfEmitter::OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput ) :
50 : : m_xOutput( xOutput ),
51 : : m_aLineFeed(1),
52 [ + - ][ + - ]: 10 : m_aBuf()
53 : : {
54 : : OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
55 [ + - ]: 10 : m_aLineFeed[0] = '\n';
56 : :
57 : 10 : rtl::OUStringBuffer aElement;
58 [ + - ]: 10 : aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
59 [ + - ][ + - ]: 10 : write(aElement.makeStringAndClear());
60 : 10 : }
61 : :
62 : 1025 : void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
63 : : {
64 : : OSL_PRECOND(pTag,"Invalid tag string");
65 : :
66 : 1025 : rtl::OUStringBuffer aElement;
67 [ + - ]: 1025 : aElement.appendAscii("<");
68 [ + - ]: 1025 : aElement.appendAscii(pTag);
69 [ + - ]: 1025 : aElement.appendAscii(" ");
70 : :
71 [ + - ]: 1025 : std::vector<rtl::OUString> aAttributes;
72 [ + - ]: 1025 : PropertyMap::const_iterator aCurr(rProperties.begin());
73 [ + - ]: 1025 : const PropertyMap::const_iterator aEnd(rProperties.end());
74 [ + + ]: 3775 : while( aCurr != aEnd )
75 : : {
76 : 2750 : rtl::OUStringBuffer aAttribute;
77 [ + - ][ + - ]: 2750 : aAttribute.append(aCurr->first);
78 [ + - ]: 2750 : aAttribute.appendAscii("=\"");
79 [ + - ][ + - ]: 2750 : aAttribute.append(aCurr->second);
80 [ + - ]: 2750 : aAttribute.appendAscii("\" ");
81 [ + - ][ + - ]: 2750 : aAttributes.push_back(aAttribute.makeStringAndClear());
82 : 2750 : ++aCurr;
83 : 2750 : }
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 [ + - ]: 1025 : std::sort(aAttributes.begin(), aAttributes.end());
89 : : std::for_each(aAttributes.begin(),
90 : : aAttributes.end(),
91 : : boost::bind( (rtl::OUStringBuffer& (rtl::OUStringBuffer::*)(const rtl::OUString&))
92 : : (&rtl::OUStringBuffer::append),
93 : : boost::ref(aElement),
94 [ + - ][ + - ]: 1025 : _1 ));
[ + - ]
95 [ + - ]: 1025 : aElement.appendAscii(">");
96 : :
97 [ + - ][ + - ]: 1025 : write(aElement.makeStringAndClear());
98 : 1025 : }
99 : :
100 : 2565 : void OdfEmitter::write( const rtl::OUString& rText )
101 : : {
102 [ + - ]: 2565 : const rtl::OString aStr = rtl::OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
103 : 2565 : const sal_Int32 nLen( aStr.getLength() );
104 [ + - ]: 2565 : m_aBuf.realloc( nLen );
105 : 2565 : const sal_Char* pStr = aStr.getStr();
106 [ + - ][ + - ]: 2565 : std::copy(pStr,pStr+nLen,m_aBuf.getArray());
107 : :
108 [ + - ][ + - ]: 2565 : m_xOutput->writeBytes(m_aBuf);
109 [ + - ][ + - ]: 2565 : m_xOutput->writeBytes(m_aLineFeed);
110 : 2565 : }
111 : :
112 : 1025 : void OdfEmitter::endTag( const char* pTag )
113 : : {
114 : 1025 : rtl::OUStringBuffer aElement;
115 [ + - ]: 1025 : aElement.appendAscii("</");
116 [ + - ]: 1025 : aElement.appendAscii(pTag);
117 [ + - ]: 1025 : aElement.appendAscii(">");
118 [ + - ][ + - ]: 1025 : write(aElement.makeStringAndClear());
119 : 1025 : }
120 : :
121 : 10 : XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
122 : : {
123 [ + - ]: 10 : return XmlEmitterSharedPtr(new OdfEmitter(xOut));
124 : : }
125 : :
126 [ + - ][ + - ]: 33 : }
127 : :
128 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|