xref: /trunk/main/xmlscript/source/xmldlg_imexp/xmldlg_addfunc.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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_xmlscript.hxx"
26 
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/xml/sax/XParser.hpp>
30 
31 #include <comphelper/processfactory.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <xmlscript/xml_helper.hxx>
34 #include <xmlscript/xmldlg_imexp.hxx>
35 
36 
37 using namespace ::rtl;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 
41 namespace xmlscript
42 {
43 
44 //==================================================================================================
45 class InputStreamProvider
46     : public ::cppu::WeakImplHelper1< io::XInputStreamProvider >
47 {
48     ByteSequence _bytes;
49 
50 public:
51     inline InputStreamProvider( ByteSequence const & rBytes )
52         : _bytes( rBytes )
53         {}
54 
55     // XInputStreamProvider
56     virtual Reference< io::XInputStream > SAL_CALL createInputStream();
57 };
58 //__________________________________________________________________________________________________
59 Reference< io::XInputStream > InputStreamProvider::createInputStream()
60 {
61     return ::xmlscript::createInputStream( _bytes );
62 }
63 
64 //==================================================================================================
65 Reference< io::XInputStreamProvider > SAL_CALL exportDialogModel(
66     Reference< container::XNameContainer > const & xDialogModel,
67     Reference< XComponentContext > const & xContext )
68 {
69     Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
70     if (! xSMgr.is())
71     {
72         throw RuntimeException(
73             OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
74             Reference< XInterface >() );
75     }
76 
77     Reference< xml::sax::XExtendedDocumentHandler > xHandler( xSMgr->createInstanceWithContext(
78         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ), xContext ), UNO_QUERY );
79     OSL_ASSERT( xHandler.is() );
80     if (! xHandler.is())
81     {
82         throw RuntimeException(
83             OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-writer component!") ),
84             Reference< XInterface >() );
85     }
86 
87     ByteSequence aBytes;
88 
89     Reference< io::XActiveDataSource > xSource( xHandler, UNO_QUERY );
90     xSource->setOutputStream( createOutputStream( &aBytes ) );
91     exportDialogModel( xHandler, xDialogModel );
92 
93     return new InputStreamProvider( aBytes );
94 }
95 
96 //==================================================================================================
97 void SAL_CALL importDialogModel(
98     Reference< io::XInputStream > xInput,
99     Reference< container::XNameContainer > const & xDialogModel,
100     Reference< XComponentContext > const & xContext )
101 {
102     Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
103     if (! xSMgr.is())
104     {
105         throw RuntimeException(
106             OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
107             Reference< XInterface >() );
108     }
109 
110     Reference< xml::sax::XParser > xParser( xSMgr->createInstanceWithContext(
111         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ), xContext ), UNO_QUERY );
112     OSL_ASSERT( xParser.is() );
113     if (! xParser.is())
114     {
115         throw RuntimeException(
116             OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-parser component!") ),
117             Reference< XInterface >() );
118     }
119 
120     // error handler, entity resolver omitted for this helper function
121     xParser->setDocumentHandler( importDialogModel( xDialogModel, xContext ) );
122 
123     xml::sax::InputSource source;
124     source.aInputStream = xInput;
125     source.sSystemId = OUString( RTL_CONSTASCII_USTRINGPARAM("virtual file") );
126 
127     xParser->parseStream( source );
128 }
129 
130 }
131