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