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/XFastTokenHandler.hpp>
24 : #include <com/sun/star/io/XOutputStream.hpp>
25 : #include <rtl/byteseq.hxx>
26 :
27 : #include <stack>
28 : #include <map>
29 :
30 : #include <boost/shared_ptr.hpp>
31 :
32 : #include "sax/fshelper.hxx"
33 :
34 : namespace sax_fastparser {
35 :
36 : /// Receives notification of sax document events to write into an XOutputStream.
37 : class FastSaxSerializer
38 : {
39 : typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
40 : typedef ::com::sun::star::uno::Sequence< ::sal_Int32 > Int32Sequence;
41 :
42 : public:
43 : explicit FastSaxSerializer();
44 : ~FastSaxSerializer();
45 :
46 78 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream() {return mxOutputStream;}
47 :
48 : /** called by the parser when parsing of an XML stream is started.
49 : */
50 : void SAL_CALL startDocument( ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
51 :
52 : /** called by the parser after the last XML element of a stream is processed.
53 : */
54 : void SAL_CALL endDocument( ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
55 :
56 : /** receives notification of the beginning of an element.
57 :
58 : @param Element
59 : contains the integer token from the <type>XFastTokenHandler</type>
60 : registered at the <type>XFastParser</type>.<br>
61 :
62 : If the element has a namespace that was registered with the
63 : <type>XFastParser</type>, <param>Element</param> contains the integer
64 : token of the elements local name from the <type>XFastTokenHandler</type>
65 : and the integer token of the namespace combined with an arithmetic
66 : <b>or</b> operation.
67 :
68 : @param Attribs
69 : Contains a <type>XFastAttrbitueList</type> to access the attributes
70 : from the element.
71 :
72 : */
73 : void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
74 : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
75 :
76 : /** receives notification of the end of an known element.
77 : @see startFastElement
78 : */
79 : void SAL_CALL endFastElement( ::sal_Int32 Element )
80 : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
81 :
82 : /** receives notification of the beginning of a single element.
83 :
84 : @param Element
85 : contains the integer token from the <type>XFastTokenHandler</type>
86 : registered at the <type>XFastParser</type>.<br>
87 :
88 : If the element has a namespace that was registered with the
89 : <type>XFastParser</type>, <param>Element</param> contains the integer
90 : token of the elements local name from the <type>XFastTokenHandler</type>
91 : and the integer token of the namespace combined with an arithmetic
92 : <b>or</b> operation.
93 :
94 : @param Attribs
95 : Contains a <type>XFastAttrbitueList</type> to access the attributes
96 : from the element.
97 :
98 : */
99 : void SAL_CALL singleFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
100 : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
101 :
102 : /// receives notification of character data.
103 : void SAL_CALL characters( const ::rtl::OUString& aChars )
104 : throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
105 :
106 : void SAL_CALL setOutputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutputStream )
107 : throw (::com::sun::star::uno::RuntimeException);
108 :
109 : void SAL_CALL setFastTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xFastTokenHandler )
110 : throw (::com::sun::star::uno::RuntimeException);
111 :
112 : // C++ helpers
113 : void SAL_CALL writeId( ::sal_Int32 Element );
114 :
115 : static ::rtl::OUString escapeXml( const ::rtl::OUString& s );
116 :
117 : public:
118 : /** From now on, don't write directly to the stream, but to top of a stack.
119 :
120 : This is to be able to change the order of the data being written.
121 : If you need to write eg.
122 : p, r, rPr, [something], /rPr, t, [text], /r, /p,
123 : but get it in order
124 : p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
125 : simply do
126 : p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
127 : mergeTopMarks( true ), mergeTopMarks(), /r, /p
128 : and you are done.
129 : */
130 : void mark( Int32Sequence aOrder = Int32Sequence() );
131 :
132 : /** Merge 2 topmost marks.
133 :
134 : There are 3 possibilities - prepend the top before the second top-most
135 : mark, append it, or append it later; prepending brings the possibility
136 : to switch parts of the output, appending later allows to write some
137 : output in advance.
138 :
139 : Writes the result to the output stream if the mark stack becomes empty
140 : by the operation.
141 :
142 : When the MERGE_MARKS_POSTPONE is specified, the merge happens just
143 : before the next merge.
144 :
145 : @see mark()
146 : */
147 : void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
148 :
149 : private:
150 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > mxOutputStream;
151 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
152 :
153 : class ForMerge
154 : {
155 : Int8Sequence maData;
156 : Int8Sequence maPostponed;
157 :
158 : public:
159 1191 : ForMerge() : maData(), maPostponed() {}
160 1845 : virtual ~ForMerge() {}
161 :
162 1320 : virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
163 : virtual Int8Sequence& getData();
164 : #if DEBUG
165 : virtual void print();
166 : #endif
167 :
168 : virtual void prepend( const Int8Sequence &rWhat );
169 : virtual void append( const Int8Sequence &rWhat );
170 : void postpone( const Int8Sequence &rWhat );
171 :
172 : protected:
173 : void resetData( );
174 : static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
175 : };
176 :
177 1074 : class ForSort : public ForMerge
178 : {
179 : std::map< ::sal_Int32, Int8Sequence > maData;
180 : sal_Int32 mnCurrentElement;
181 :
182 : Int32Sequence maOrder;
183 :
184 : public:
185 537 : ForSort( Int32Sequence aOrder ) :
186 : ForMerge(),
187 : maData(),
188 : mnCurrentElement( 0 ),
189 537 : maOrder( aOrder ) {}
190 :
191 : void setCurrentElement( ::sal_Int32 nToken );
192 :
193 : virtual Int8Sequence& getData();
194 :
195 : #if DEBUG
196 : virtual void print();
197 : #endif
198 :
199 : virtual void prepend( const Int8Sequence &rWhat );
200 : virtual void append( const Int8Sequence &rWhat );
201 : private:
202 : void sort();
203 : };
204 :
205 : ::std::stack< boost::shared_ptr< ForMerge > > maMarkStack;
206 :
207 : void writeFastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs );
208 : void write( const ::rtl::OUString& s );
209 :
210 : protected:
211 : rtl::ByteSequence maClosingBracket;
212 : rtl::ByteSequence maSlashAndClosingBracket;
213 : rtl::ByteSequence maColon;
214 : rtl::ByteSequence maOpeningBracket;
215 : rtl::ByteSequence maOpeningBracketAndSlash;
216 : rtl::ByteSequence maQuote;
217 : rtl::ByteSequence maEqualSignAndQuote;
218 : rtl::ByteSequence maSpace;
219 :
220 : /** Forward the call to the output stream, or write to the stack.
221 :
222 : The latter in the case that we are inside a mark().
223 : */
224 : 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);
225 : };
226 :
227 : } // namespace sax_fastparser
228 :
229 : #endif
230 :
231 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|