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 0 : ::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 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 : OString SAL_CALL getId( ::sal_Int32 Element );
115 :
116 : static OUString escapeXml( const OUString& s );
117 :
118 : public:
119 : /** From now on, don't write directly to the stream, but to top of a stack.
120 :
121 : This is to be able to change the order of the data being written.
122 : If you need to write eg.
123 : p, r, rPr, [something], /rPr, t, [text], /r, /p,
124 : but get it in order
125 : p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
126 : simply do
127 : p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
128 : mergeTopMarks( true ), mergeTopMarks(), /r, /p
129 : and you are done.
130 : */
131 : void mark( Int32Sequence aOrder = Int32Sequence() );
132 :
133 : /** Merge 2 topmost marks.
134 :
135 : There are 3 possibilities - prepend the top before the second top-most
136 : mark, append it, or append it later; prepending brings the possibility
137 : to switch parts of the output, appending later allows to write some
138 : output in advance.
139 :
140 : Writes the result to the output stream if the mark stack becomes empty
141 : by the operation.
142 :
143 : When the MERGE_MARKS_POSTPONE is specified, the merge happens just
144 : before the next merge.
145 :
146 : @see mark()
147 : */
148 : void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
149 :
150 : private:
151 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > mxOutputStream;
152 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
153 :
154 : class ForMerge
155 : {
156 : Int8Sequence maData;
157 : Int8Sequence maPostponed;
158 :
159 : public:
160 0 : ForMerge() : maData(), maPostponed() {}
161 0 : virtual ~ForMerge() {}
162 :
163 0 : virtual void setCurrentElement( ::sal_Int32 /*nToken*/ ) {}
164 : virtual Int8Sequence& getData();
165 : #if OSL_DEBUG_LEVEL > 0
166 : virtual void print();
167 : #endif
168 :
169 : virtual void prepend( const Int8Sequence &rWhat );
170 : virtual void append( const Int8Sequence &rWhat );
171 : void postpone( const Int8Sequence &rWhat );
172 :
173 : protected:
174 : void resetData( );
175 : static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
176 : };
177 :
178 0 : class ForSort : public ForMerge
179 : {
180 : std::map< ::sal_Int32, Int8Sequence > maData;
181 : sal_Int32 mnCurrentElement;
182 :
183 : Int32Sequence maOrder;
184 :
185 : public:
186 0 : ForSort( Int32Sequence aOrder ) :
187 : ForMerge(),
188 : maData(),
189 : mnCurrentElement( 0 ),
190 0 : maOrder( aOrder ) {}
191 :
192 : void setCurrentElement( ::sal_Int32 nToken ) SAL_OVERRIDE;
193 :
194 : virtual Int8Sequence& getData() SAL_OVERRIDE;
195 :
196 : #if OSL_DEBUG_LEVEL > 0
197 : virtual void print() SAL_OVERRIDE;
198 : #endif
199 :
200 : virtual void prepend( const Int8Sequence &rWhat ) SAL_OVERRIDE;
201 : virtual void append( const Int8Sequence &rWhat ) SAL_OVERRIDE;
202 : private:
203 : void sort();
204 : };
205 :
206 : ::std::stack< boost::shared_ptr< ForMerge > > maMarkStack;
207 : ::std::stack< boost::shared_ptr< ForMerge > > maSavedMarkStack;
208 :
209 : #ifdef DBG_UTIL
210 : ::std::stack<sal_Int32> m_DebugStartedElements;
211 : #endif
212 :
213 : void writeFastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs );
214 : void write( const OUString& s );
215 :
216 : protected:
217 : rtl::ByteSequence maClosingBracket;
218 : rtl::ByteSequence maSlashAndClosingBracket;
219 : rtl::ByteSequence maColon;
220 : rtl::ByteSequence maOpeningBracket;
221 : rtl::ByteSequence maOpeningBracketAndSlash;
222 : rtl::ByteSequence maQuote;
223 : rtl::ByteSequence maEqualSignAndQuote;
224 : rtl::ByteSequence maSpace;
225 :
226 : /** Forward the call to the output stream, or write to the stack.
227 :
228 : The latter in the case that we are inside a mark().
229 : */
230 : 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);
231 : };
232 :
233 : } // namespace sax_fastparser
234 :
235 : #endif
236 :
237 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|