1*e1d5bd03SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e1d5bd03SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1d5bd03SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1d5bd03SAndrew Rist  * distributed with this work for additional information
6*e1d5bd03SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1d5bd03SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1d5bd03SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1d5bd03SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e1d5bd03SAndrew Rist  *
11*e1d5bd03SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e1d5bd03SAndrew Rist  *
13*e1d5bd03SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1d5bd03SAndrew Rist  * software distributed under the License is distributed on an
15*e1d5bd03SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1d5bd03SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1d5bd03SAndrew Rist  * specific language governing permissions and limitations
18*e1d5bd03SAndrew Rist  * under the License.
19*e1d5bd03SAndrew Rist  *
20*e1d5bd03SAndrew Rist  *************************************************************/
21*e1d5bd03SAndrew Rist 
22*e1d5bd03SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlscript.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
29cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
32cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
33cdf0e10cSrcweir #include <xmlscript/xml_helper.hxx>
34cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::rtl;
38cdf0e10cSrcweir using namespace ::com::sun::star;
39cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace xmlscript
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //==================================================================================================
45cdf0e10cSrcweir class InputStreamProvider
46cdf0e10cSrcweir 	: public ::cppu::WeakImplHelper1< io::XInputStreamProvider >
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	ByteSequence _bytes;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir public:
InputStreamProvider(ByteSequence const & rBytes)51cdf0e10cSrcweir 	inline InputStreamProvider( ByteSequence const & rBytes )
52cdf0e10cSrcweir 		: _bytes( rBytes )
53cdf0e10cSrcweir 		{}
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	// XInputStreamProvider
56cdf0e10cSrcweir     virtual Reference< io::XInputStream > SAL_CALL createInputStream()
57cdf0e10cSrcweir 		throw (RuntimeException);
58cdf0e10cSrcweir };
59cdf0e10cSrcweir //__________________________________________________________________________________________________
createInputStream()60cdf0e10cSrcweir Reference< io::XInputStream > InputStreamProvider::createInputStream()
61cdf0e10cSrcweir 	throw (RuntimeException)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	return ::xmlscript::createInputStream( _bytes );
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //==================================================================================================
exportDialogModel(Reference<container::XNameContainer> const & xDialogModel,Reference<XComponentContext> const & xContext)67cdf0e10cSrcweir Reference< io::XInputStreamProvider > SAL_CALL exportDialogModel(
68cdf0e10cSrcweir     Reference< container::XNameContainer > const & xDialogModel,
69cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
70cdf0e10cSrcweir 	SAL_THROW( (Exception) )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
73cdf0e10cSrcweir 	if (! xSMgr.is())
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		throw RuntimeException(
76cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
77cdf0e10cSrcweir 			Reference< XInterface >() );
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	Reference< xml::sax::XExtendedDocumentHandler > xHandler( xSMgr->createInstanceWithContext(
81cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ), xContext ), UNO_QUERY );
82cdf0e10cSrcweir 	OSL_ASSERT( xHandler.is() );
83cdf0e10cSrcweir 	if (! xHandler.is())
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 		throw RuntimeException(
86cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-writer component!") ),
87cdf0e10cSrcweir 			Reference< XInterface >() );
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	ByteSequence aBytes;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	Reference< io::XActiveDataSource > xSource( xHandler, UNO_QUERY );
93cdf0e10cSrcweir 	xSource->setOutputStream( createOutputStream( &aBytes ) );
94cdf0e10cSrcweir 	exportDialogModel( xHandler, xDialogModel );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	return new InputStreamProvider( aBytes );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //==================================================================================================
importDialogModel(Reference<io::XInputStream> xInput,Reference<container::XNameContainer> const & xDialogModel,Reference<XComponentContext> const & xContext)100cdf0e10cSrcweir void SAL_CALL importDialogModel(
101cdf0e10cSrcweir 	Reference< io::XInputStream > xInput,
102cdf0e10cSrcweir 	Reference< container::XNameContainer > const & xDialogModel,
103cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
104cdf0e10cSrcweir 	SAL_THROW( (Exception) )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
107cdf0e10cSrcweir 	if (! xSMgr.is())
108cdf0e10cSrcweir 	{
109cdf0e10cSrcweir 		throw RuntimeException(
110cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
111cdf0e10cSrcweir 			Reference< XInterface >() );
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	Reference< xml::sax::XParser > xParser( xSMgr->createInstanceWithContext(
115cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ), xContext ), UNO_QUERY );
116cdf0e10cSrcweir 	OSL_ASSERT( xParser.is() );
117cdf0e10cSrcweir 	if (! xParser.is())
118cdf0e10cSrcweir 	{
119cdf0e10cSrcweir 		throw RuntimeException(
120cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-parser component!") ),
121cdf0e10cSrcweir 			Reference< XInterface >() );
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	// error handler, entity resolver omitted for this helper function
125cdf0e10cSrcweir 	xParser->setDocumentHandler( importDialogModel( xDialogModel, xContext ) );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	xml::sax::InputSource source;
128cdf0e10cSrcweir 	source.aInputStream = xInput;
129cdf0e10cSrcweir 	source.sSystemId = OUString( RTL_CONSTASCII_USTRINGPARAM("virtual file") );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	xParser->parseStream( source );
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir }
135