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/compbase1.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <boost/noncopyable.hpp>
28 using namespace ::com::sun::star
;
33 typedef ::cppu::WeakComponentImplHelper1
<
34 com::sun::star::xml::sax::XDocumentHandler
> ODFSerializerBase
;
36 class ODFSerializer
: private cppu::BaseMutex
,
37 public ODFSerializerBase
,
41 explicit ODFSerializer(const uno::Reference
<io::XOutputStream
>& xOut
) :
42 ODFSerializerBase(m_aMutex
),
47 m_aLineFeed
[0] = '\n';
50 virtual void SAL_CALL
startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
);
51 virtual void SAL_CALL
endDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
);
52 virtual void SAL_CALL
startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
) throw (xml::sax::SAXException
, uno::RuntimeException
);
53 virtual void SAL_CALL
endElement( const OUString
& aName
) throw (xml::sax::SAXException
, uno::RuntimeException
);
54 virtual void SAL_CALL
characters( const OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
);
55 virtual void SAL_CALL
ignorableWhitespace( const OUString
& aWhitespaces
) throw (xml::sax::SAXException
, uno::RuntimeException
);
56 virtual void SAL_CALL
processingInstruction( const OUString
& aTarget
, const OUString
& aData
) throw (xml::sax::SAXException
, uno::RuntimeException
);
57 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) throw (xml::sax::SAXException
, uno::RuntimeException
);
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( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
67 OSL_PRECOND(m_xOutStream
.is(), "ODFSerializer(): invalid output stream");
69 OUStringBuffer aElement
;
70 aElement
.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 characters(aElement
.makeStringAndClear());
74 void SAL_CALL
ODFSerializer::endDocument() throw (xml::sax::SAXException
, uno::RuntimeException
)
77 void SAL_CALL
ODFSerializer::startElement( const OUString
& aName
,
78 const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
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
) throw (xml::sax::SAXException
, uno::RuntimeException
)
92 characters("</" + aName
+ ">");
95 void SAL_CALL
ODFSerializer::characters( const OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
)
97 const OString aStr
= OUStringToOString(aChars
,
98 RTL_TEXTENCODING_UTF8
);
99 const sal_Int32
nLen( aStr
.getLength() );
100 m_aBuf
.realloc( nLen
);
101 const sal_Char
* pStr
= aStr
.getStr();
102 std::copy(pStr
,pStr
+nLen
,m_aBuf
.getArray());
104 m_xOutStream
->writeBytes(m_aBuf
);
105 // TODO(F1): Make pretty printing configurable
106 m_xOutStream
->writeBytes(m_aLineFeed
);
109 void SAL_CALL
ODFSerializer::ignorableWhitespace( const OUString
& aWhitespaces
) throw (xml::sax::SAXException
, uno::RuntimeException
)
111 // TODO(F1): Make pretty printing configurable
112 characters(aWhitespaces
);
115 void SAL_CALL
ODFSerializer::processingInstruction( const OUString
&,
116 const OUString
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
119 void SAL_CALL
ODFSerializer::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
122 uno::Reference
< xml::sax::XDocumentHandler
> createSerializer(const uno::Reference
<io::XOutputStream
>& xOut
)
124 return uno::Reference
<xml::sax::XDocumentHandler
>(new ODFSerializer(xOut
));
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */