1*8d192041SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*8d192041SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*8d192041SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*8d192041SAndrew Rist  * distributed with this work for additional information
6*8d192041SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*8d192041SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*8d192041SAndrew Rist  * "License"); you may not use this file except in compliance
9*8d192041SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8d192041SAndrew Rist  *
11*8d192041SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8d192041SAndrew Rist  *
13*8d192041SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*8d192041SAndrew Rist  * software distributed under the License is distributed on an
15*8d192041SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8d192041SAndrew Rist  * KIND, either express or implied.  See the License for the
17*8d192041SAndrew Rist  * specific language governing permissions and limitations
18*8d192041SAndrew Rist  * under the License.
19*8d192041SAndrew Rist  *
20*8d192041SAndrew Rist  *************************************************************/
21*8d192041SAndrew Rist 
22*8d192041SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SAX_FASTSERIALIZER_HXX
25cdf0e10cSrcweir #define SAX_FASTSERIALIZER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastSerializer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
31cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <stack>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "sax/dllapi.h"
36cdf0e10cSrcweir #include "sax/fshelper.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define SERIALIZER_IMPLEMENTATION_NAME	"com.sun.star.comp.extensions.xml.sax.FastSerializer"
39cdf0e10cSrcweir #define SERIALIZER_SERVICE_NAME		"com.sun.star.xml.sax.FastSerializer"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace sax_fastparser {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class SAX_DLLPUBLIC FastSaxSerializer : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastSerializer, ::com::sun::star::lang::XServiceInfo >
44cdf0e10cSrcweir {
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir     explicit            FastSaxSerializer(  );
47cdf0e10cSrcweir     virtual             ~FastSaxSerializer();
48cdf0e10cSrcweir 
getOutputStream()49cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream() {return mxOutputStream;}
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	// The implementation details
52cdf0e10cSrcweir     static ::com::sun::star::uno::Sequence< ::rtl::OUString > 	getSupportedServiceNames_Static(void);
53cdf0e10cSrcweir 	static ::rtl::OUString 				                        getImplementationName_Static();
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	// XFastSerializer
56cdf0e10cSrcweir 	virtual void SAL_CALL startDocument(  ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
57cdf0e10cSrcweir     virtual void SAL_CALL endDocument(  ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
58cdf0e10cSrcweir     virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
59cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
60cdf0e10cSrcweir     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 )
61cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
62cdf0e10cSrcweir     virtual void SAL_CALL endFastElement( ::sal_Int32 Element )
63cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
64cdf0e10cSrcweir     virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name )
65cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
66cdf0e10cSrcweir     virtual void SAL_CALL singleFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
67cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
68cdf0e10cSrcweir     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 )
69cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
70cdf0e10cSrcweir     virtual void SAL_CALL characters( const ::rtl::OUString& aChars )
71cdf0e10cSrcweir 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
72cdf0e10cSrcweir     virtual void SAL_CALL setOutputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutputStream )
73cdf0e10cSrcweir 		throw (::com::sun::star::uno::RuntimeException);
74cdf0e10cSrcweir     virtual void SAL_CALL setFastTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xFastTokenHandler )
75cdf0e10cSrcweir 		throw (::com::sun::star::uno::RuntimeException);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	// XServiceInfo
78cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw ( ::com::sun::star::uno::RuntimeException );
79cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw ( ::com::sun::star::uno::RuntimeException );
80cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw ( ::com::sun::star::uno::RuntimeException );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	// C++ helpers
83cdf0e10cSrcweir     virtual void SAL_CALL writeId( ::sal_Int32 Element );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	static ::rtl::OUString escapeXml( const ::rtl::OUString& s );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir public:
88cdf0e10cSrcweir     /** From now on, don't write directly to the stream, but to top of a stack.
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         This is to be able to change the order of the data being written.
91cdf0e10cSrcweir         If you need to write eg.
92cdf0e10cSrcweir           p, r, rPr, [something], /rPr, t, [text], /r, /p,
93cdf0e10cSrcweir         but get it in order
94cdf0e10cSrcweir           p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
95cdf0e10cSrcweir         simply do
96cdf0e10cSrcweir           p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
97cdf0e10cSrcweir           mergeTopMarks( true ), mergeTopMarks(), /r, /p
98cdf0e10cSrcweir         and you are done.
99cdf0e10cSrcweir      */
100cdf0e10cSrcweir     void mark();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /** Merge 2 topmost marks.
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         There are 3 possibilities - prepend the top before the second top-most
105cdf0e10cSrcweir         mark, append it, or append it later; prepending brings the possibility
106cdf0e10cSrcweir         to switch parts of the output, appending later allows to write some
107cdf0e10cSrcweir         output in advance.
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         Writes the result to the output stream if the mark stack becomes empty
110cdf0e10cSrcweir         by the operation.
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         When the MERGE_MARKS_POSTPONE is specified, the merge happens just
113cdf0e10cSrcweir         before the next merge.
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         @see mark()
116cdf0e10cSrcweir      */
117cdf0e10cSrcweir     void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir private:
120cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > mxOutputStream;
121cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
124cdf0e10cSrcweir     class ForMerge
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         Int8Sequence maData;
127cdf0e10cSrcweir         Int8Sequence maPostponed;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     public:
ForMerge()130cdf0e10cSrcweir         ForMerge() : maData(), maPostponed() {}
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         Int8Sequence& getData();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         void prepend( const Int8Sequence &rWhat );
135cdf0e10cSrcweir         void append( const Int8Sequence &rWhat );
136cdf0e10cSrcweir         void postpone( const Int8Sequence &rWhat );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     private:
139cdf0e10cSrcweir         static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
140cdf0e10cSrcweir     };
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     ::std::stack< ForMerge > maMarkStack;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	void writeFastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs );
145cdf0e10cSrcweir     void write( const ::rtl::OUString& s );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir protected:
148cdf0e10cSrcweir     /** Forward the call to the output stream, or write to the stack.
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         The latter in the case that we are inside a mark().
151cdf0e10cSrcweir      */
152cdf0e10cSrcweir     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);
153cdf0e10cSrcweir };
154cdf0e10cSrcweir 
155cdf0e10cSrcweir } // namespace sax_fastparser
156cdf0e10cSrcweir 
157cdf0e10cSrcweir #endif
158