bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / xforms / submission / replace.cxx
blob0abdab0c8dcd2ae8f87225725e1c9b554394130e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <memory>
21 #include "submission.hxx"
22 #include "serialization_app_xml.hxx"
24 #include <osl/diagnose.h>
25 #include <rtl/ustring.hxx>
26 #include <rtl/string.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/xml/dom/XDocument.hpp>
32 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
33 #include <com/sun/star/frame/Desktop.hpp>
34 #include <com/sun/star/frame/XComponentLoader.hpp>
35 #include <com/sun/star/frame/FrameSearchFlag.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/task/InteractionHandler.hpp>
38 #include <ucbhelper/content.hxx>
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::ucb;
42 using namespace com::sun::star::frame;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::task;
46 using namespace com::sun::star::xml::dom;
48 CSubmission::SubmissionResult CSubmission::replace(const OUString& aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
50 if (!m_aResultStream.is())
51 return CSubmission::UNKNOWN_ERROR;
53 try {
54 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
55 if (aReplace.equalsIgnoreAsciiCase("all")
56 || aReplace.equalsIgnoreAsciiCase("document")) {
57 Reference< XComponentLoader > xLoader;
58 if (aFrame.is())
59 xLoader = Reference< XComponentLoader >(aFrame, UNO_QUERY);
61 if (!xLoader.is())
62 xLoader = Reference< XComponentLoader >( Desktop::create(xContext), UNO_QUERY_THROW);
64 // open the stream from the result...
65 // build media descriptor
66 Sequence< PropertyValue > descriptor(2);
67 descriptor[0] = PropertyValue(OUString("InputStream"),
68 -1, makeAny(m_aResultStream), PropertyState_DIRECT_VALUE);
69 descriptor[1] = PropertyValue(OUString("ReadOnly"),
70 -1, makeAny(sal_True), PropertyState_DIRECT_VALUE);
72 OUString aURL = m_aURLObj.GetMainURL(INetURLObject::NO_DECODE);
73 OUString aTarget = "_default";
74 xLoader->loadComponentFromURL(aURL, aTarget, FrameSearchFlag::ALL, descriptor);
76 return CSubmission::SUCCESS;
78 } else if (aReplace.equalsIgnoreAsciiCase("instance")) {
79 if (aDocument.is()) {
80 // parse the result stream into a new document
81 Reference< XDocumentBuilder > xBuilder(DocumentBuilder::create(xContext));
82 Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
84 if (aNewDocument.is()) {
85 // and replace the content of the current instance
86 Reference< XElement > oldRoot = aDocument->getDocumentElement();
87 Reference< XElement > newRoot = aNewDocument->getDocumentElement();
89 Reference< XNode > aImportedNode = aDocument->importNode(newRoot, sal_True);
90 aDocument->replaceChild(aImportedNode, oldRoot);
91 return CSubmission::SUCCESS;
92 } else {
93 return CSubmission::UNKNOWN_ERROR;
95 } else {
96 // nothing to replace
97 return CSubmission::UNKNOWN_ERROR;
99 } else if (aReplace.equalsIgnoreAsciiCase("none")) {
100 // do nothing \o/
101 return CSubmission::SUCCESS;
103 } catch (const Exception& e) {
104 OString aMsg("Exception during replace:\n");
105 aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8);
106 OSL_FAIL(aMsg.getStr());
108 return CSubmission::UNKNOWN_ERROR;
111 ::std::unique_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
113 // PUT always uses application/xml
114 ::std::unique_ptr< CSerialization > apSerialization(new CSerializationAppXML());
115 apSerialization->setSource(m_aFragment);
116 apSerialization->serialize();
118 // create a commandEnvironment and use the default interaction handler
119 CCommandEnvironmentHelper *pHelper = new CCommandEnvironmentHelper;
120 if( _xHandler.is() )
121 pHelper->m_aInteractionHandler = _xHandler;
122 else
123 pHelper->m_aInteractionHandler.set(
124 InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY_THROW);
126 CProgressHandlerHelper *pProgressHelper = new CProgressHandlerHelper;
127 pHelper->m_aProgressHandler = Reference< XProgressHandler >(pProgressHelper);
129 // UCB has ownership of environment...
130 _rOutEnv = pHelper;
131 return apSerialization;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */