1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 #include <framework/eventsconfiguration.hxx>
27 #include <xml/eventsdocumenthandler.hxx>
28 #include <services.h>
29 
30 #ifndef __FRAMEWORK_XML_SAXNAMESPACEFILTER_HXX_
31 #include <xml/saxnamespacefilter.hxx>
32 #endif
33 
34 //_________________________________________________________________________________________________________________
35 //	interface includes
36 //_________________________________________________________________________________________________________________
37 #include <com/sun/star/xml/sax/XParser.hpp>
38 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <com/sun/star/io/XInputStream.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 
42 //_________________________________________________________________________________________________________________
43 //	other includes
44 //_________________________________________________________________________________________________________________
45 
46 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
47 #include <comphelper/processfactory.hxx>
48 #endif
49 #include <unotools/streamwrap.hxx>
50 #include <tools/debug.hxx>
51 
52 //_________________________________________________________________________________________________________________
53 //	namespace
54 //_________________________________________________________________________________________________________________
55 
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::xml::sax;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::io;
60 
61 
62 namespace framework
63 {
64 
65 static Reference< XParser > GetSaxParser(
66 	// #110897#
67 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
68 	)
69 {
70 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
71 	//return Reference< XParser >( xServiceManager->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
72 	return Reference< XParser >( xServiceFactory->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
73 }
74 
75 static Reference< XDocumentHandler > GetSaxWriter(
76 	// #110897#
77 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
78 	)
79 {
80 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
81 	//return Reference< XDocumentHandler >( xServiceManager->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
82 	return Reference< XDocumentHandler >( xServiceFactory->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
83 }
84 
85 // #110897#
86 sal_Bool EventsConfiguration::LoadEventsConfig(
87 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
88 	SvStream& rInStream, EventsConfig& aItems )
89 {
90 	Reference< XParser > xParser( GetSaxParser( xServiceFactory ) );
91 	Reference< XInputStream > xInputStream(
92 								(::cppu::OWeakObject *)new utl::OInputStreamWrapper( rInStream ),
93 								UNO_QUERY );
94 
95 	// connect stream to input stream to the parser
96 	InputSource aInputSource;
97 
98 	aInputSource.aInputStream = xInputStream;
99 
100 	// create namespace filter and set events document handler inside to support xml namespaces
101 	Reference< XDocumentHandler > xDocHandler( new OReadEventsDocumentHandler( aItems ));
102 	Reference< XDocumentHandler > xFilter( new SaxNamespaceFilter( xDocHandler ));
103 
104 	// connect parser and filter
105 	xParser->setDocumentHandler( xFilter );
106 
107 	try
108 	{
109 		xParser->parseStream( aInputSource );
110 		return sal_True;
111 	}
112 	catch ( RuntimeException& )
113 	{
114 		return sal_False;
115 	}
116 	catch( SAXException& )
117 	{
118 		return sal_False;
119 	}
120 	catch( ::com::sun::star::io::IOException& )
121 	{
122 		return sal_False;
123 	}
124 }
125 
126 // #110897#
127 sal_Bool EventsConfiguration::StoreEventsConfig(
128 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
129 	SvStream& rOutStream, const EventsConfig& aItems )
130 {
131 	Reference< XDocumentHandler > xWriter( GetSaxWriter( xServiceFactory ) );
132 
133 	Reference< XOutputStream > xOutputStream(
134 								(::cppu::OWeakObject *)new utl::OOutputStreamWrapper( rOutStream ),
135 								UNO_QUERY );
136 
137 	Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
138 	xDataSource->setOutputStream( xOutputStream );
139 
140 	try
141 	{
142 		OWriteEventsDocumentHandler aWriteEventsDocumentHandler( aItems, xWriter );
143 		aWriteEventsDocumentHandler.WriteEventsDocument();
144 		return sal_True;
145 	}
146 	catch ( RuntimeException& )
147 	{
148 		return sal_False;
149 	}
150 	catch ( SAXException& )
151 	{
152 		return sal_False;
153 	}
154 	catch ( ::com::sun::star::io::IOException& )
155 	{
156 		return sal_False;
157 	}
158 }
159 
160 }
161 
162