1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SAX_FASTSERIALIZER_HXX
29 #define SAX_FASTSERIALIZER_HXX
30 
31 #include <com/sun/star/xml/sax/XFastSerializer.hpp>
32 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/io/XOutputStream.hpp>
35 #include <cppuhelper/implbase2.hxx>
36 
37 #include <stack>
38 
39 #include "sax/dllapi.h"
40 #include "sax/fshelper.hxx"
41 
42 #define SERIALIZER_IMPLEMENTATION_NAME	"com.sun.star.comp.extensions.xml.sax.FastSerializer"
43 #define SERIALIZER_SERVICE_NAME		"com.sun.star.xml.sax.FastSerializer"
44 
45 namespace sax_fastparser {
46 
47 class SAX_DLLPUBLIC FastSaxSerializer : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastSerializer, ::com::sun::star::lang::XServiceInfo >
48 {
49 public:
50     explicit            FastSaxSerializer(  );
51     virtual             ~FastSaxSerializer();
52 
53     ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getOutputStream() {return mxOutputStream;}
54 
55 	// The implementation details
56     static ::com::sun::star::uno::Sequence< ::rtl::OUString > 	getSupportedServiceNames_Static(void);
57 	static ::rtl::OUString 				                        getImplementationName_Static();
58 
59 	// XFastSerializer
60 	virtual void SAL_CALL startDocument(  ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
61     virtual void SAL_CALL endDocument(  ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
62     virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
63 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
64     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 )
65 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
66     virtual void SAL_CALL endFastElement( ::sal_Int32 Element )
67 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
68     virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name )
69 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
70     virtual void SAL_CALL singleFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs )
71 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
72     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 )
73 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
74     virtual void SAL_CALL characters( const ::rtl::OUString& aChars )
75 		throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
76     virtual void SAL_CALL setOutputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutputStream )
77 		throw (::com::sun::star::uno::RuntimeException);
78     virtual void SAL_CALL setFastTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xFastTokenHandler )
79 		throw (::com::sun::star::uno::RuntimeException);
80 
81 	// XServiceInfo
82     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw ( ::com::sun::star::uno::RuntimeException );
83     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw ( ::com::sun::star::uno::RuntimeException );
84     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw ( ::com::sun::star::uno::RuntimeException );
85 
86 	// C++ helpers
87     virtual void SAL_CALL writeId( ::sal_Int32 Element );
88 
89 	static ::rtl::OUString escapeXml( const ::rtl::OUString& s );
90 
91 public:
92     /** From now on, don't write directly to the stream, but to top of a stack.
93 
94         This is to be able to change the order of the data being written.
95         If you need to write eg.
96           p, r, rPr, [something], /rPr, t, [text], /r, /p,
97         but get it in order
98           p, r, t, [text], /t, rPr, [something], /rPr, /r, /p,
99         simply do
100           p, r, mark(), t, [text], /t, mark(), rPr, [something], /rPr,
101           mergeTopMarks( true ), mergeTopMarks(), /r, /p
102         and you are done.
103      */
104     void mark();
105 
106     /** Merge 2 topmost marks.
107 
108         There are 3 possibilities - prepend the top before the second top-most
109         mark, append it, or append it later; prepending brings the possibility
110         to switch parts of the output, appending later allows to write some
111         output in advance.
112 
113         Writes the result to the output stream if the mark stack becomes empty
114         by the operation.
115 
116         When the MERGE_MARKS_POSTPONE is specified, the merge happens just
117         before the next merge.
118 
119         @see mark()
120      */
121     void mergeTopMarks( sax_fastparser::MergeMarksEnum eMergeType = sax_fastparser::MERGE_MARKS_APPEND );
122 
123 private:
124 	::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > mxOutputStream;
125 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxFastTokenHandler;
126 
127     typedef ::com::sun::star::uno::Sequence< ::sal_Int8 > Int8Sequence;
128     class ForMerge
129     {
130         Int8Sequence maData;
131         Int8Sequence maPostponed;
132 
133     public:
134         ForMerge() : maData(), maPostponed() {}
135 
136         Int8Sequence& getData();
137 
138         void prepend( const Int8Sequence &rWhat );
139         void append( const Int8Sequence &rWhat );
140         void postpone( const Int8Sequence &rWhat );
141 
142     private:
143         static void merge( Int8Sequence &rTop, const Int8Sequence &rMerge, bool bAppend );
144     };
145 
146     ::std::stack< ForMerge > maMarkStack;
147 
148 	void writeFastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs );
149     void write( const ::rtl::OUString& s );
150 
151 protected:
152     /** Forward the call to the output stream, or write to the stack.
153 
154         The latter in the case that we are inside a mark().
155      */
156     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);
157 };
158 
159 } // namespace sax_fastparser
160 
161 #endif
162