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