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