1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "odfserializer.hxx"
21 #include <osl/diagnose.h>
22 #include <rtl/ustrbuf.hxx>
23 #include <cppuhelper/compbase.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
27 using namespace ::com::sun::star
;
32 typedef ::cppu::WeakComponentImplHelper
<
33 css::xml::sax::XDocumentHandler
> ODFSerializerBase
;
35 class ODFSerializer
: private cppu::BaseMutex
,
36 public ODFSerializerBase
39 explicit ODFSerializer(const uno::Reference
<io::XOutputStream
>& xOut
) :
40 ODFSerializerBase(m_aMutex
),
45 m_aLineFeed
[0] = '\n';
47 ODFSerializer(const ODFSerializer
&) = delete;
48 ODFSerializer
& operator=(const ODFSerializer
&) = delete;
50 virtual void SAL_CALL
startDocument( ) override
;
51 virtual void SAL_CALL
endDocument( ) override
;
52 virtual void SAL_CALL
startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
) override
;
53 virtual void SAL_CALL
endElement( const OUString
& aName
) override
;
54 virtual void SAL_CALL
characters( const OUString
& aChars
) override
;
55 virtual void SAL_CALL
ignorableWhitespace( const OUString
& aWhitespaces
) override
;
56 virtual void SAL_CALL
processingInstruction( const OUString
& aTarget
, const OUString
& aData
) override
;
57 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) override
;
60 uno::Reference
<io::XOutputStream
> m_xOutStream
;
61 uno::Sequence
<sal_Int8
> m_aLineFeed
;
62 uno::Sequence
<sal_Int8
> m_aBuf
;
65 void SAL_CALL
ODFSerializer::startDocument( )
67 OSL_PRECOND(m_xOutStream
.is(), "ODFSerializer(): invalid output stream");
69 OUStringBuffer aElement
;
70 aElement
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 characters(aElement
.makeStringAndClear());
74 void SAL_CALL
ODFSerializer::endDocument()
77 void SAL_CALL
ODFSerializer::startElement( const OUString
& aName
,
78 const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
80 OUStringBuffer
aElement("<" + aName
+ " ");
82 const sal_Int16 nLen
=xAttribs
->getLength();
83 for( sal_Int16 i
=0; i
<nLen
; ++i
)
84 aElement
.append(xAttribs
->getNameByIndex(i
) + "=\"" +
85 xAttribs
->getValueByIndex(i
) + "\" ");
87 characters(aElement
.makeStringAndClear() + ">");
90 void SAL_CALL
ODFSerializer::endElement( const OUString
& aName
)
92 characters("</" + aName
+ ">");
95 void SAL_CALL
ODFSerializer::characters( const OUString
& aChars
)
97 const OString aStr
= OUStringToOString(aChars
, 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());
103 m_xOutStream
->writeBytes(m_aBuf
);
104 // TODO(F1): Make pretty printing configurable
105 m_xOutStream
->writeBytes(m_aLineFeed
);
108 void SAL_CALL
ODFSerializer::ignorableWhitespace( const OUString
& aWhitespaces
)
110 // TODO(F1): Make pretty printing configurable
111 characters(aWhitespaces
);
114 void SAL_CALL
ODFSerializer::processingInstruction( const OUString
&,
118 void SAL_CALL
ODFSerializer::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& )
121 uno::Reference
< xml::sax::XDocumentHandler
> createSerializer(const uno::Reference
<io::XOutputStream
>& xOut
)
123 return uno::Reference
<xml::sax::XDocumentHandler
>(new ODFSerializer(xOut
));
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */