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/toolboxconfiguration.hxx>
27 #include <xml/toolboxdocumenthandler.hxx>
28 #include <xml/saxnamespacefilter.hxx>
29 #include <services.h>
30 
31 //_________________________________________________________________________________________________________________
32 //	interface includes
33 //_________________________________________________________________________________________________________________
34 #include <com/sun/star/xml/sax/XParser.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 
39 //_________________________________________________________________________________________________________________
40 //	other includes
41 //_________________________________________________________________________________________________________________
42 
43 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
44 #include <comphelper/processfactory.hxx>
45 #endif
46 #include <unotools/streamwrap.hxx>
47 #include <tools/debug.hxx>
48 
49 //_________________________________________________________________________________________________________________
50 //	namespace
51 //_________________________________________________________________________________________________________________
52 
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::xml::sax;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::container;
58 
59 
60 namespace framework
61 {
62 
GetSaxParser(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xServiceFactory)63 static Reference< XParser > GetSaxParser(
64 	// #110897#
65 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
66 	)
67 {
68 	return Reference< XParser >( xServiceFactory->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
69 }
70 
GetSaxWriter(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xServiceFactory)71 static Reference< XDocumentHandler > GetSaxWriter(
72 	// #110897#
73 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
74 	)
75 {
76 	return Reference< XDocumentHandler >( xServiceFactory->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
77 }
78 
79 // #110897#
LoadToolBox(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xServiceFactory,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & rInputStream,const::com::sun::star::uno::Reference<::com::sun::star::container::XIndexContainer> & rToolbarConfiguration)80 sal_Bool ToolBoxConfiguration::LoadToolBox(
81 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
82 	const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rInputStream,
83 	const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexContainer >& rToolbarConfiguration )
84 {
85 	Reference< XParser > xParser( GetSaxParser( xServiceFactory ) );
86 
87 	// connect stream to input stream to the parser
88 	InputSource aInputSource;
89 
90 	aInputSource.aInputStream = rInputStream;
91 
92 	// create namespace filter and set menudocument handler inside to support xml namespaces
93 	Reference< XDocumentHandler > xDocHandler( new OReadToolBoxDocumentHandler( rToolbarConfiguration ));
94 	Reference< XDocumentHandler > xFilter( new SaxNamespaceFilter( xDocHandler ));
95 
96 	// connect parser and filter
97 	xParser->setDocumentHandler( xFilter );
98 
99 	try
100 	{
101 		xParser->parseStream( aInputSource );
102 		return sal_True;
103 	}
104 	catch ( RuntimeException& )
105 	{
106 		return sal_False;
107 	}
108 	catch( SAXException& )
109 	{
110 		return sal_False;
111 	}
112 	catch( ::com::sun::star::io::IOException& )
113 	{
114 		return sal_False;
115 	}
116 }
117 
118 
119 // #110897#
StoreToolBox(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xServiceFactory,const::com::sun::star::uno::Reference<::com::sun::star::io::XOutputStream> & rOutputStream,const::com::sun::star::uno::Reference<::com::sun::star::container::XIndexAccess> & rToolbarConfiguration)120 sal_Bool ToolBoxConfiguration::StoreToolBox(
121 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
122 	const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rOutputStream,
123 	const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& rToolbarConfiguration )
124 {
125 	Reference< XDocumentHandler > xWriter( GetSaxWriter( xServiceFactory ) );
126 
127 	Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
128 	xDataSource->setOutputStream( rOutputStream );
129 
130 	try
131 	{
132 		OWriteToolBoxDocumentHandler aWriteToolBoxDocumentHandler( rToolbarConfiguration, xWriter );
133 		aWriteToolBoxDocumentHandler.WriteToolBoxDocument();
134 		return sal_True;
135 	}
136 	catch ( RuntimeException& )
137 	{
138 		return sal_False;
139 	}
140 	catch ( SAXException& )
141 	{
142 		return sal_False;
143 	}
144 	catch ( ::com::sun::star::io::IOException& )
145 	{
146 		return sal_False;
147 	}
148 }
149 
150 }
151