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