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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 #include <framework/eventsconfiguration.hxx>
31 #include <xml/eventsdocumenthandler.hxx>
32 #include <services.h>
33 
34 #ifndef __FRAMEWORK_XML_SAXNAMESPACEFILTER_HXX_
35 #include <xml/saxnamespacefilter.hxx>
36 #endif
37 
38 //_________________________________________________________________________________________________________________
39 //	interface includes
40 //_________________________________________________________________________________________________________________
41 #include <com/sun/star/xml/sax/XParser.hpp>
42 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #include <com/sun/star/io/XInputStream.hpp>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 
46 //_________________________________________________________________________________________________________________
47 //	other includes
48 //_________________________________________________________________________________________________________________
49 
50 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
51 #include <comphelper/processfactory.hxx>
52 #endif
53 #include <unotools/streamwrap.hxx>
54 #include <tools/debug.hxx>
55 
56 //_________________________________________________________________________________________________________________
57 //	namespace
58 //_________________________________________________________________________________________________________________
59 
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::xml::sax;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::io;
64 
65 
66 namespace framework
67 {
68 
69 static Reference< XParser > GetSaxParser(
70 	// #110897#
71 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
72 	)
73 {
74 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
75 	//return Reference< XParser >( xServiceManager->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
76 	return Reference< XParser >( xServiceFactory->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
77 }
78 
79 static Reference< XDocumentHandler > GetSaxWriter(
80 	// #110897#
81 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
82 	)
83 {
84 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
85 	//return Reference< XDocumentHandler >( xServiceManager->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
86 	return Reference< XDocumentHandler >( xServiceFactory->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
87 }
88 
89 // #110897#
90 sal_Bool EventsConfiguration::LoadEventsConfig(
91 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
92 	SvStream& rInStream, EventsConfig& aItems )
93 {
94 	Reference< XParser > xParser( GetSaxParser( xServiceFactory ) );
95 	Reference< XInputStream > xInputStream(
96 								(::cppu::OWeakObject *)new utl::OInputStreamWrapper( rInStream ),
97 								UNO_QUERY );
98 
99 	// connect stream to input stream to the parser
100 	InputSource aInputSource;
101 
102 	aInputSource.aInputStream = xInputStream;
103 
104 	// create namespace filter and set events document handler inside to support xml namespaces
105 	Reference< XDocumentHandler > xDocHandler( new OReadEventsDocumentHandler( aItems ));
106 	Reference< XDocumentHandler > xFilter( new SaxNamespaceFilter( xDocHandler ));
107 
108 	// connect parser and filter
109 	xParser->setDocumentHandler( xFilter );
110 
111 	try
112 	{
113 		xParser->parseStream( aInputSource );
114 		return sal_True;
115 	}
116 	catch ( RuntimeException& )
117 	{
118 		return sal_False;
119 	}
120 	catch( SAXException& )
121 	{
122 		return sal_False;
123 	}
124 	catch( ::com::sun::star::io::IOException& )
125 	{
126 		return sal_False;
127 	}
128 }
129 
130 // #110897#
131 sal_Bool EventsConfiguration::StoreEventsConfig(
132 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
133 	SvStream& rOutStream, const EventsConfig& aItems )
134 {
135 	Reference< XDocumentHandler > xWriter( GetSaxWriter( xServiceFactory ) );
136 
137 	Reference< XOutputStream > xOutputStream(
138 								(::cppu::OWeakObject *)new utl::OOutputStreamWrapper( rOutStream ),
139 								UNO_QUERY );
140 
141 	Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
142 	xDataSource->setOutputStream( xOutputStream );
143 
144 	try
145 	{
146 		OWriteEventsDocumentHandler aWriteEventsDocumentHandler( aItems, xWriter );
147 		aWriteEventsDocumentHandler.WriteEventsDocument();
148 		return sal_True;
149 	}
150 	catch ( RuntimeException& )
151 	{
152 		return sal_False;
153 	}
154 	catch ( SAXException& )
155 	{
156 		return sal_False;
157 	}
158 	catch ( ::com::sun::star::io::IOException& )
159 	{
160 		return sal_False;
161 	}
162 }
163 
164 }
165 
166