Branch data 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 : : #ifndef SAX_FASTSERIALIZER_HXX
21 : : #define SAX_FASTSERIALIZER_HXX
22 : :
23 : : #include <com/sun/star/xml/sax/XFastSerializer.hpp>
24 : : #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
25 : : #include <com/sun/star/lang/XServiceInfo.hpp>
26 : : #include <com/sun/star/io/XOutputStream.hpp>
27 : : #include <cppuhelper/implbase2.hxx>
28 : : #include <rtl/byteseq.hxx>
29 : :
30 : : #include <stack>
31 : : #include <map>
32 : :
33 : : #include <boost/shared_ptr.hpp>
34 : :
35 : : #include "sax/fshelper.hxx"
36 : :
37 : : #define SERIALIZER_IMPLEMENTATION_NAME "com.sun.star.comp.extensions.xml.sax.FastSerializer"
38 : : #define SERIALIZER_SERVICE_NAME "com.sun.star.xml.sax.FastSerializer"
39 : :
40 : : namespace sax_fastparser {
41 : :
42 : : class SAX_DLLPUBLIC FastSaxSerializer : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastSerializer, ::com::sun::star::lang::XServiceInfo >
43 : : {
44 : : typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
45 : : typedef ::com::sun::star::uno::Sequence< ::sal_Int32 > Int32Sequence;
46 : :
47 : : public:
48 : : explicit FastSaxSerializer( );
49 : : virtual ~FastSaxSerializer();
50 : :
51 : 174 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream() {return mxOutputStream;}
52 : :
53 : : // The implementation details
54 : : static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(void);
55 : :
56 : : // XFastSerializer
57 : : virtual void SAL_CALL startDocument( ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
58 : : virtual void SAL_CALL endDocument( ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
59 : : virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
60 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
61 : : virtual void SAL_CALL startUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
62 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
63 : : virtual void SAL_CALL endFastElement( ::sal_Int32 Element )
64 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
65 : : virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name )
66 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
67 : : virtual void SAL_CALL singleFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
68 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
69 : : virtual void SAL_CALL singleUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
70 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
71 : : virtual void SAL_CALL characters( const ::rtl::OUString& aChars )
72 : : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
73 : : virtual void SAL_CALL setOutputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutputStream )
74 : : throw (::com::sun::star::uno::RuntimeException);
75 : : virtual void SAL_CALL setFastTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xFastTokenHandler )
76 : : throw (::com::sun::star::uno::RuntimeException);
77 : :
78 : : // XServiceInfo
79 : : virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw ( ::com::sun::star::uno::RuntimeException );
80 : : virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw ( ::com::sun::star::uno::RuntimeException );
81 : : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw ( ::com::sun::star::uno::RuntimeException );
82 : :
83 : : // C++ helpers
84 : : virtual void SAL_CALL writeId( ::sal_Int32 Element );
85 : :
86 : : static ::rtl::OUString escapeXml( const ::rtl::OUString& s );
87 : :
88 : : public:
89 : : /** From now on, don't write directly to the stream, but to top of a stack.
90 : :
91 : : This is to be able to change the order of the data being written.
92 : : If you need to write eg.
93 : : p, r, rPr, [something], /rPr, t, [text], /r, /p,
94 : : but get it in order
95 : : p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
96 : : simply do
97 : : p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
98 : : mergeTopMarks( true ), mergeTopMarks(), /r, /p
99 : : and you are done.
100 : : */
101 : : void mark( Int32Sequence aOrder = Int32Sequence() );
102 : :
103 : : /** Merge 2 topmost marks.
104 : :
105 : : There are 3 possibilities - prepend the top before the second top-most
106 : : mark, append it, or append it later; prepending brings the possibility
107 : : to switch parts of the output, appending later allows to write some
108 : : output in advance.
109 : :
110 : : Writes the result to the output stream if the mark stack becomes empty
111 : : by the operation.
112 : :
113 : : When the MERGE_MARKS_POSTPONE is specified, the merge happens just
114 : : before the next merge.
115 : :
116 : : @see mark()
117 : : */
118 : : void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
119 : :
120 : : private:
121 : : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > mxOutputStream;
122 : : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
123 : :
124 : : class ForMerge
125 : : {
126 : : Int8Sequence maData;
127 : : Int8Sequence maPostponed;
128 : :
129 : : public:
130 [ + - ]: 2061 : ForMerge() : maData(), maPostponed() {}
131 [ + - ][ - + ]: 3054 : virtual ~ForMerge() {}
132 : :
133 : 3258 : virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
134 : : virtual Int8Sequence& getData();
135 : : #if DEBUG
136 : : virtual void print();
137 : : #endif
138 : :
139 : : virtual void prepend( const Int8Sequence &rWhat );
140 : : virtual void append( const Int8Sequence &rWhat );
141 : : void postpone( const Int8Sequence &rWhat );
142 : :
143 : : protected:
144 : : void resetData( );
145 : : static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
146 : : };
147 : :
148 [ + - ][ - + ]: 2136 : class ForSort : public ForMerge
149 : : {
150 : : std::map< ::sal_Int32, Int8Sequence > maData;
151 : : sal_Int32 mnCurrentElement;
152 : :
153 : : Int32Sequence maOrder;
154 : :
155 : : public:
156 : 1068 : ForSort( Int32Sequence aOrder ) :
157 : : ForMerge(),
158 : : maData(),
159 : : mnCurrentElement( 0 ),
160 [ + - ][ + - ]: 1068 : maOrder( aOrder ) {}
161 : :
162 : : void setCurrentElement( ::sal_Int32 nToken );
163 : :
164 : : virtual Int8Sequence& getData();
165 : :
166 : : #if DEBUG
167 : : virtual void print();
168 : : #endif
169 : :
170 : : virtual void prepend( const Int8Sequence &rWhat );
171 : : virtual void append( const Int8Sequence &rWhat );
172 : : private:
173 : : void sort();
174 : : };
175 : :
176 : : ::std::stack< boost::shared_ptr< ForMerge > > maMarkStack;
177 : :
178 : : void writeFastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs );
179 : : void write( const ::rtl::OUString& s );
180 : :
181 : : protected:
182 : : rtl::ByteSequence maClosingBracket;
183 : : rtl::ByteSequence maSlashAndClosingBracket;
184 : : rtl::ByteSequence maColon;
185 : : rtl::ByteSequence maOpeningBracket;
186 : : rtl::ByteSequence maOpeningBracketAndSlash;
187 : : rtl::ByteSequence maQuote;
188 : : rtl::ByteSequence maEqualSignAndQuote;
189 : : rtl::ByteSequence maSpace;
190 : :
191 : : /** Forward the call to the output stream, or write to the stack.
192 : :
193 : : The latter in the case that we are inside a mark().
194 : : */
195 : : void writeBytes( const ::com::sun::star::uno::Sequence< ::sal_Int8 >& aData ) throw (::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
196 : : };
197 : :
198 : : } // namespace sax_fastparser
199 : :
200 : : #endif
201 : :
202 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|