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 : #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>
27 :
28 : using namespace ::com::sun::star;
29 :
30 : namespace svgi
31 : {
32 :
33 : typedef ::cppu::WeakComponentImplHelper1<
34 : com::sun::star::xml::sax::XDocumentHandler> ODFSerializerBase;
35 :
36 0 : class ODFSerializer : private cppu::BaseMutex,
37 : public ODFSerializerBase,
38 : boost::noncopyable
39 : {
40 : public:
41 0 : explicit ODFSerializer(const uno::Reference<io::XOutputStream>& xOut) :
42 : ODFSerializerBase(m_aMutex),
43 : m_xOutStream(xOut),
44 : m_aLineFeed(1),
45 0 : m_aBuf()
46 : {
47 0 : m_aLineFeed[0] = '\n';
48 0 : }
49 :
50 : virtual void SAL_CALL startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
51 : virtual void SAL_CALL endDocument( ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
52 : virtual void SAL_CALL startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
53 : virtual void SAL_CALL endElement( const OUString& aName ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
54 : virtual void SAL_CALL characters( const OUString& aChars ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 : virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
56 : virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
57 : virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 :
59 : private:
60 : uno::Reference<io::XOutputStream> m_xOutStream;
61 : uno::Sequence<sal_Int8> m_aLineFeed;
62 : uno::Sequence<sal_Int8> m_aBuf;
63 : };
64 :
65 0 : void SAL_CALL ODFSerializer::startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
66 : {
67 : OSL_PRECOND(m_xOutStream.is(), "ODFSerializer(): invalid output stream");
68 :
69 0 : OUStringBuffer aElement;
70 0 : aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 0 : characters(aElement.makeStringAndClear());
72 0 : }
73 :
74 0 : void SAL_CALL ODFSerializer::endDocument() throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
75 0 : {}
76 :
77 0 : void SAL_CALL ODFSerializer::startElement( const OUString& aName,
78 : const uno::Reference< xml::sax::XAttributeList >& xAttribs ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
79 : {
80 0 : OUStringBuffer aElement("<" + aName + " ");
81 :
82 0 : const sal_Int16 nLen=xAttribs->getLength();
83 0 : for( sal_Int16 i=0; i<nLen; ++i )
84 0 : aElement.append(xAttribs->getNameByIndex(i) + "=\"" +
85 0 : xAttribs->getValueByIndex(i) + "\" ");
86 :
87 0 : characters(aElement.makeStringAndClear() + ">");
88 0 : }
89 :
90 0 : void SAL_CALL ODFSerializer::endElement( const OUString& aName ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
91 : {
92 0 : characters("</" + aName + ">");
93 0 : }
94 :
95 0 : void SAL_CALL ODFSerializer::characters( const OUString& aChars ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
96 : {
97 : const OString aStr = OUStringToOString(aChars,
98 0 : RTL_TEXTENCODING_UTF8);
99 0 : const sal_Int32 nLen( aStr.getLength() );
100 0 : m_aBuf.realloc( nLen );
101 0 : const sal_Char* pStr = aStr.getStr();
102 0 : std::copy(pStr,pStr+nLen,m_aBuf.getArray());
103 :
104 0 : m_xOutStream->writeBytes(m_aBuf);
105 : // TODO(F1): Make pretty printing configurable
106 0 : m_xOutStream->writeBytes(m_aLineFeed);
107 0 : }
108 :
109 0 : void SAL_CALL ODFSerializer::ignorableWhitespace( const OUString& aWhitespaces ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
110 : {
111 : // TODO(F1): Make pretty printing configurable
112 0 : characters(aWhitespaces);
113 0 : }
114 :
115 0 : void SAL_CALL ODFSerializer::processingInstruction( const OUString&,
116 : const OUString& ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
117 0 : {}
118 :
119 0 : void SAL_CALL ODFSerializer::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& ) throw (xml::sax::SAXException, uno::RuntimeException, std::exception)
120 0 : {}
121 :
122 0 : uno::Reference< xml::sax::XDocumentHandler> createSerializer(const uno::Reference<io::XOutputStream>& xOut )
123 : {
124 0 : return uno::Reference<xml::sax::XDocumentHandler>(new ODFSerializer(xOut));
125 : }
126 :
127 : }
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|