LibreOffice Module filter (master) 1
odfserializer.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#include "odfserializer.hxx"
21#include <osl/diagnose.h>
22#include <rtl/ustrbuf.hxx>
25#include <com/sun/star/uno/Sequence.hxx>
26
27using namespace ::com::sun::star;
28
29namespace svgi
30{
31
32typedef ::cppu::WeakComponentImplHelper<
33 css::xml::sax::XDocumentHandler> ODFSerializerBase;
34
37{
38public:
39 explicit ODFSerializer(const uno::Reference<io::XOutputStream>& xOut) :
41 m_xOutStream(xOut),
42 m_aLineFeed(1),
43 m_aBuf()
44 {
45 m_aLineFeed[0] = '\n';
46 }
47 ODFSerializer(const ODFSerializer&) = delete;
49
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;
58
59private:
60 uno::Reference<io::XOutputStream> m_xOutStream;
61 uno::Sequence<sal_Int8> m_aLineFeed;
62 uno::Sequence<sal_Int8> m_aBuf;
63};
64
66{
67 OSL_PRECOND(m_xOutStream.is(), "ODFSerializer(): invalid output stream");
68
69 OUStringBuffer aElement;
70 aElement.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 characters(aElement.makeStringAndClear());
72}
73
75{}
76
77void SAL_CALL ODFSerializer::startElement( const OUString& aName,
78 const uno::Reference< xml::sax::XAttributeList >& xAttribs )
79{
80 OUStringBuffer aElement("<" + aName + " ");
81
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) + "\" ");
86
87 characters(aElement.makeStringAndClear() + ">");
88}
89
90void SAL_CALL ODFSerializer::endElement( const OUString& aName )
91{
92 characters("</" + aName + ">");
93}
94
95void SAL_CALL ODFSerializer::characters( const OUString& aChars )
96{
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());
102
103 m_xOutStream->writeBytes(m_aBuf);
104 // TODO(F1): Make pretty printing configurable
105 m_xOutStream->writeBytes(m_aLineFeed);
106}
107
108void SAL_CALL ODFSerializer::ignorableWhitespace( const OUString& aWhitespaces )
109{
110 // TODO(F1): Make pretty printing configurable
111 characters(aWhitespaces);
112}
113
114void SAL_CALL ODFSerializer::processingInstruction( const OUString&,
115 const OUString& )
116{}
117
118void SAL_CALL ODFSerializer::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& )
119{}
120
121uno::Reference< xml::sax::XDocumentHandler> createSerializer(const uno::Reference<io::XOutputStream>& xOut )
122{
123 return uno::Reference<xml::sax::XDocumentHandler>(new ODFSerializer(xOut));
124}
125
126}
127
128/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
mutable::osl::Mutex m_aMutex
virtual void SAL_CALL endElement(const OUString &aName) override
uno::Reference< io::XOutputStream > m_xOutStream
uno::Sequence< sal_Int8 > m_aLineFeed
virtual void SAL_CALL setDocumentLocator(const uno::Reference< xml::sax::XLocator > &xLocator) override
virtual void SAL_CALL startElement(const OUString &aName, const uno::Reference< xml::sax::XAttributeList > &xAttribs) override
virtual void SAL_CALL startDocument() override
virtual void SAL_CALL ignorableWhitespace(const OUString &aWhitespaces) override
uno::Sequence< sal_Int8 > m_aBuf
ODFSerializer & operator=(const ODFSerializer &)=delete
virtual void SAL_CALL processingInstruction(const OUString &aTarget, const OUString &aData) override
virtual void SAL_CALL characters(const OUString &aChars) override
virtual void SAL_CALL endDocument() override
ODFSerializer(const uno::Reference< io::XOutputStream > &xOut)
ODFSerializer(const ODFSerializer &)=delete
OUString aName
aStr
Shape IDs per cluster in DGG atom.
int i
Definition: gentoken.py:48
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
::cppu::WeakComponentImplHelper< css::xml::sax::XDocumentHandler > ODFSerializerBase
uno::Reference< xml::sax::XDocumentHandler > createSerializer(const uno::Reference< io::XOutputStream > &xOut)