1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <memory>
21 #include "submission.hxx"
22 #include "serialization_app_xml.hxx"
23 
24 #include <rtl/ustring.hxx>
25 #include <rtl/ref.hxx>
26 #include <tools/diagnose_ex.h>
27 
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/xml/dom/XDocument.hpp>
31 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
32 #include <com/sun/star/frame/Desktop.hpp>
33 #include <com/sun/star/frame/XComponentLoader.hpp>
34 #include <com/sun/star/frame/FrameSearchFlag.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/task/InteractionHandler.hpp>
37 
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::ucb;
40 using namespace com::sun::star::frame;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::beans;
43 using namespace com::sun::star::task;
44 using namespace com::sun::star::xml::dom;
45 
replace(const OUString & aReplace,const Reference<XDocument> & aDocument,const Reference<XFrame> & aFrame)46 CSubmission::SubmissionResult CSubmission::replace(const OUString& aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
47 {
48     if (!m_aResultStream.is())
49         return CSubmission::UNKNOWN_ERROR;
50 
51     try {
52         Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
53         if (aReplace.equalsIgnoreAsciiCase("all")
54          || aReplace.equalsIgnoreAsciiCase("document")) {
55             Reference< XComponentLoader > xLoader;
56             if (aFrame.is())
57                 xLoader.set(aFrame, UNO_QUERY);
58 
59             if (!xLoader.is())
60                 xLoader.set( Desktop::create(xContext), UNO_QUERY_THROW);
61 
62             // open the stream from the result...
63             // build media descriptor
64             Sequence< PropertyValue > descriptor(2);
65             descriptor[0] = PropertyValue("InputStream",
66                 -1, makeAny(m_aResultStream), PropertyState_DIRECT_VALUE);
67             descriptor[1] = PropertyValue("ReadOnly",
68                 -1, makeAny(true), PropertyState_DIRECT_VALUE);
69 
70             OUString aURL = m_aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE);
71             xLoader->loadComponentFromURL(aURL, "_default", FrameSearchFlag::ALL, descriptor);
72 
73             return CSubmission::SUCCESS;
74 
75         } else if (aReplace.equalsIgnoreAsciiCase("instance")) {
76             if (aDocument.is()) {
77                 // parse the result stream into a new document
78                 Reference< XDocumentBuilder > xBuilder(DocumentBuilder::create(xContext));
79                 Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
80 
81                 if (aNewDocument.is()) {
82                     // and replace the content of the current instance
83                     Reference< XElement > oldRoot = aDocument->getDocumentElement();
84                     Reference< XElement > newRoot = aNewDocument->getDocumentElement();
85 
86                     Reference< XNode > aImportedNode = aDocument->importNode(newRoot, true);
87                     aDocument->replaceChild(aImportedNode, oldRoot);
88                     return CSubmission::SUCCESS;
89                 } else {
90                     return CSubmission::UNKNOWN_ERROR;
91                 }
92             } else {
93                 // nothing to replace
94                 return CSubmission::UNKNOWN_ERROR;
95             }
96         } else if (aReplace.equalsIgnoreAsciiCase("none")) {
97             // do nothing \o/
98             return CSubmission::SUCCESS;
99         }
100     } catch (const Exception&) {
101         TOOLS_WARN_EXCEPTION( "forms.xforms", "Exception during replace");
102     }
103     return CSubmission::UNKNOWN_ERROR;
104 }
105 
createSerialization(const Reference<XInteractionHandler> & _xHandler,Reference<XCommandEnvironment> & _rOutEnv)106 ::std::unique_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
107 {
108     // PUT always uses application/xml
109     ::std::unique_ptr< CSerialization > apSerialization(new CSerializationAppXML());
110     apSerialization->setSource(m_aFragment);
111     apSerialization->serialize();
112 
113     // create a commandEnvironment and use the default interaction handler
114     rtl::Reference<CCommandEnvironmentHelper> pHelper = new CCommandEnvironmentHelper;
115     if( _xHandler.is() )
116         pHelper->m_aInteractionHandler = _xHandler;
117     else
118         pHelper->m_aInteractionHandler.set(
119             InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY_THROW);
120 
121     rtl::Reference<CProgressHandlerHelper> pProgressHelper = new CProgressHandlerHelper;
122     pHelper->m_aProgressHandler.set(pProgressHelper);
123 
124     // UCB has ownership of environment...
125     _rOutEnv = pHelper;
126     return apSerialization;
127 }
128 
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
130