xref: /AOO41X/main/forms/source/xforms/submission/replace.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include <memory>
31*cdf0e10cSrcweir #include "submission.hxx"
32*cdf0e10cSrcweir #include "serialization_app_xml.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <rtl/ustring.hxx>
35*cdf0e10cSrcweir #include <rtl/string.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <unotools/processfactory.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocument.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
45*cdf0e10cSrcweir #include <ucbhelper/content.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace com::sun::star::uno;
48*cdf0e10cSrcweir using namespace com::sun::star::ucb;
49*cdf0e10cSrcweir using namespace com::sun::star::frame;
50*cdf0e10cSrcweir using namespace com::sun::star::lang;
51*cdf0e10cSrcweir using namespace com::sun::star::beans;
52*cdf0e10cSrcweir using namespace com::sun::star::task;
53*cdf0e10cSrcweir using namespace com::sun::star::xml::dom;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir CSubmission::SubmissionResult CSubmission::replace(const ::rtl::OUString& aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     if (!m_aResultStream.is())
58*cdf0e10cSrcweir         return CSubmission::UNKNOWN_ERROR;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     try {
61*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory = utl::getProcessServiceFactory();
62*cdf0e10cSrcweir         if (aReplace.equalsIgnoreAsciiCaseAscii("all") || aReplace.equalsIgnoreAsciiCaseAscii("document")) {
63*cdf0e10cSrcweir             Reference< XComponentLoader > xLoader;
64*cdf0e10cSrcweir             if (aFrame.is())
65*cdf0e10cSrcweir                 xLoader = Reference< XComponentLoader >(aFrame, UNO_QUERY);
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir             if (!xLoader.is())
68*cdf0e10cSrcweir                 xLoader = Reference< XComponentLoader >(xFactory->createInstance(
69*cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop")), UNO_QUERY_THROW);
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir             // open the stream from the result...
72*cdf0e10cSrcweir             // build media descriptor
73*cdf0e10cSrcweir             Sequence< PropertyValue > descriptor(2);
74*cdf0e10cSrcweir             descriptor[0] = PropertyValue(::rtl::OUString::createFromAscii(
75*cdf0e10cSrcweir                 "InputStream"), -1, makeAny(m_aResultStream), PropertyState_DIRECT_VALUE);
76*cdf0e10cSrcweir             descriptor[1] = PropertyValue(::rtl::OUString::createFromAscii(
77*cdf0e10cSrcweir                 "ReadOnly"), -1, makeAny(sal_True), PropertyState_DIRECT_VALUE);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir             //::rtl::OUString aURL = ::rtl::OUString::createFromAscii("private:stream");
80*cdf0e10cSrcweir             ::rtl::OUString aURL = m_aURLObj.GetMainURL(INetURLObject::NO_DECODE);
81*cdf0e10cSrcweir             ::rtl::OUString aTarget = ::rtl::OUString::createFromAscii("_default");
82*cdf0e10cSrcweir             xLoader->loadComponentFromURL(aURL, aTarget, FrameSearchFlag::ALL, descriptor);
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir             return CSubmission::SUCCESS;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         } else if (aReplace.equalsIgnoreAsciiCaseAscii("instance")) {
87*cdf0e10cSrcweir             if (aDocument.is()) {
88*cdf0e10cSrcweir                 // parse the result stream into a new document
89*cdf0e10cSrcweir                 Reference< XDocumentBuilder > xBuilder(xFactory->createInstance(
90*cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii("com.sun.star.xml.dom.DocumentBuilder")), UNO_QUERY_THROW);
91*cdf0e10cSrcweir                 Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 				if (aNewDocument.is()) {
94*cdf0e10cSrcweir 					// and replace the content of the current instance
95*cdf0e10cSrcweir 					Reference< XElement > oldRoot = aDocument->getDocumentElement();
96*cdf0e10cSrcweir 					Reference< XElement > newRoot = aNewDocument->getDocumentElement();
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 					// aDocument->removeChild(Reference< XNode >(oldRoot, UNO_QUERY_THROW));
99*cdf0e10cSrcweir 					Reference< XNode > aImportedNode = aDocument->importNode(Reference< XNode >(newRoot, UNO_QUERY_THROW), sal_True);
100*cdf0e10cSrcweir 					Reference< XNode >(aDocument, UNO_QUERY_THROW)->replaceChild(aImportedNode, Reference< XNode >(oldRoot, UNO_QUERY_THROW));
101*cdf0e10cSrcweir 					return CSubmission::SUCCESS;
102*cdf0e10cSrcweir 				} else {
103*cdf0e10cSrcweir 					return CSubmission::UNKNOWN_ERROR;
104*cdf0e10cSrcweir 				}
105*cdf0e10cSrcweir             } else {
106*cdf0e10cSrcweir                 // nothing to replace
107*cdf0e10cSrcweir                 return CSubmission::UNKNOWN_ERROR;
108*cdf0e10cSrcweir             }
109*cdf0e10cSrcweir         } else if (aReplace.equalsIgnoreAsciiCaseAscii("none")) {
110*cdf0e10cSrcweir             // do nothing \o/
111*cdf0e10cSrcweir             return CSubmission::SUCCESS;
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir     } catch (Exception& e) {
114*cdf0e10cSrcweir         ::rtl::OString aMsg("Exception during replace:\n");
115*cdf0e10cSrcweir         aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8);
116*cdf0e10cSrcweir         OSL_ENSURE(sal_False, aMsg.getStr());
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir     return CSubmission::UNKNOWN_ERROR;
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir ::std::auto_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     // PUT always uses application/xml
123*cdf0e10cSrcweir     ::std::auto_ptr< CSerialization > apSerialization(new CSerializationAppXML());
124*cdf0e10cSrcweir     apSerialization->setSource(m_aFragment);
125*cdf0e10cSrcweir     apSerialization->serialize();
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     // create a commandEnvironment and use the default interaction handler
128*cdf0e10cSrcweir     CCommandEnvironmentHelper *pHelper = new CCommandEnvironmentHelper;
129*cdf0e10cSrcweir     if( _xHandler.is() )
130*cdf0e10cSrcweir         pHelper->m_aInteractionHandler = _xHandler;
131*cdf0e10cSrcweir     else
132*cdf0e10cSrcweir         pHelper->m_aInteractionHandler = CSS::uno::Reference< XInteractionHandler >(m_aFactory->createInstance(
133*cdf0e10cSrcweir         ::rtl::OUString::createFromAscii("com.sun.star.task.InteractionHandler")), UNO_QUERY);
134*cdf0e10cSrcweir     OSL_ENSURE(pHelper->m_aInteractionHandler.is(), "failed to create IntreractionHandler");
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     CProgressHandlerHelper *pProgressHelper = new CProgressHandlerHelper;
137*cdf0e10cSrcweir     pHelper->m_aProgressHandler = Reference< XProgressHandler >(pProgressHelper);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     // UCB has ownership of environment...
140*cdf0e10cSrcweir     _rOutEnv = pHelper;
141*cdf0e10cSrcweir     return apSerialization;
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 
146