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:
InputStreamProvider(ByteSequence const & rBytes)51 	inline InputStreamProvider( ByteSequence const & rBytes )
52 		: _bytes( rBytes )
53 		{}
54 
55 	// XInputStreamProvider
56     virtual Reference< io::XInputStream > SAL_CALL createInputStream()
57 		throw (RuntimeException);
58 };
59 //__________________________________________________________________________________________________
createInputStream()60 Reference< io::XInputStream > InputStreamProvider::createInputStream()
61 	throw (RuntimeException)
62 {
63 	return ::xmlscript::createInputStream( _bytes );
64 }
65 
66 //==================================================================================================
exportDialogModel(Reference<container::XNameContainer> const & xDialogModel,Reference<XComponentContext> const & xContext)67 Reference< io::XInputStreamProvider > SAL_CALL exportDialogModel(
68     Reference< container::XNameContainer > const & xDialogModel,
69     Reference< XComponentContext > const & xContext )
70 	SAL_THROW( (Exception) )
71 {
72 	Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
73 	if (! xSMgr.is())
74 	{
75 		throw RuntimeException(
76 			OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
77 			Reference< XInterface >() );
78 	}
79 
80 	Reference< xml::sax::XExtendedDocumentHandler > xHandler( xSMgr->createInstanceWithContext(
81 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ), xContext ), UNO_QUERY );
82 	OSL_ASSERT( xHandler.is() );
83 	if (! xHandler.is())
84 	{
85 		throw RuntimeException(
86 			OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-writer component!") ),
87 			Reference< XInterface >() );
88 	}
89 
90 	ByteSequence aBytes;
91 
92 	Reference< io::XActiveDataSource > xSource( xHandler, UNO_QUERY );
93 	xSource->setOutputStream( createOutputStream( &aBytes ) );
94 	exportDialogModel( xHandler, xDialogModel );
95 
96 	return new InputStreamProvider( aBytes );
97 }
98 
99 //==================================================================================================
importDialogModel(Reference<io::XInputStream> xInput,Reference<container::XNameContainer> const & xDialogModel,Reference<XComponentContext> const & xContext)100 void SAL_CALL importDialogModel(
101 	Reference< io::XInputStream > xInput,
102 	Reference< container::XNameContainer > const & xDialogModel,
103     Reference< XComponentContext > const & xContext )
104 	SAL_THROW( (Exception) )
105 {
106 	Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager() );
107 	if (! xSMgr.is())
108 	{
109 		throw RuntimeException(
110 			OUString( RTL_CONSTASCII_USTRINGPARAM("no service manager available!") ),
111 			Reference< XInterface >() );
112 	}
113 
114 	Reference< xml::sax::XParser > xParser( xSMgr->createInstanceWithContext(
115 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ), xContext ), UNO_QUERY );
116 	OSL_ASSERT( xParser.is() );
117 	if (! xParser.is())
118 	{
119 		throw RuntimeException(
120 			OUString( RTL_CONSTASCII_USTRINGPARAM("could not create sax-parser component!") ),
121 			Reference< XInterface >() );
122 	}
123 
124 	// error handler, entity resolver omitted for this helper function
125 	xParser->setDocumentHandler( importDialogModel( xDialogModel, xContext ) );
126 
127 	xml::sax::InputSource source;
128 	source.aInputStream = xInput;
129 	source.sSystemId = OUString( RTL_CONSTASCII_USTRINGPARAM("virtual file") );
130 
131 	xParser->parseStream( source );
132 }
133 
134 }
135