LibreOffice Module forms (master) 1
replace.cxx
Go to the documentation of this file.
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"
23
24#include <rtl/ustring.hxx>
25#include <rtl/ref.hxx>
27#include <o3tl/string_view.hxx>
28
30#include <com/sun/star/uno/Reference.hxx>
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
39using namespace com::sun::star::uno;
40using namespace com::sun::star::ucb;
41using namespace com::sun::star::frame;
42using namespace com::sun::star::lang;
43using namespace com::sun::star::beans;
44using namespace com::sun::star::task;
45using namespace com::sun::star::xml::dom;
46
47CSubmission::SubmissionResult CSubmission::replace(std::u16string_view aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
48{
49 if (!m_aResultStream.is())
51
52 try {
54 if (o3tl::equalsIgnoreAsciiCase(aReplace, u"all")
55 || o3tl::equalsIgnoreAsciiCase(aReplace, u"document")) {
57 if (aFrame.is())
58 xLoader.set(aFrame, UNO_QUERY);
59
60 if (!xLoader.is())
61 xLoader.set( Desktop::create(xContext), UNO_QUERY_THROW);
62
63 // open the stream from the result...
64 // build media descriptor
66 PropertyValue("InputStream",
67 -1, Any(m_aResultStream), PropertyState_DIRECT_VALUE),
68 PropertyValue("ReadOnly",
69 -1, Any(true), PropertyState_DIRECT_VALUE)
70 };
71
73 xLoader->loadComponentFromURL(aURL, "_default", FrameSearchFlag::ALL, descriptor);
74
76
77 } else if (o3tl::equalsIgnoreAsciiCase(aReplace, u"instance")) {
78 if (aDocument.is()) {
79 // parse the result stream into a new document
80 Reference< XDocumentBuilder > xBuilder(DocumentBuilder::create(xContext));
81 Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
82
83 if (aNewDocument.is()) {
84 // and replace the content of the current instance
85 Reference< XElement > oldRoot = aDocument->getDocumentElement();
86 Reference< XElement > newRoot = aNewDocument->getDocumentElement();
87
88 Reference< XNode > aImportedNode = aDocument->importNode(newRoot, true);
89 aDocument->replaceChild(aImportedNode, oldRoot);
91 } else {
93 }
94 } else {
95 // nothing to replace
97 }
98 } else if (o3tl::equalsIgnoreAsciiCase(aReplace, u"none")) {
99 // do nothing \o/
101 }
102 } catch (const Exception&) {
103 TOOLS_WARN_EXCEPTION( "forms.xforms", "Exception during replace");
104 }
106}
107
108::std::unique_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
109{
110 // PUT always uses application/xml
111 ::std::unique_ptr< CSerialization > apSerialization(new CSerializationAppXML());
112 apSerialization->setSource(m_aFragment);
113 apSerialization->serialize();
114
115 // create a commandEnvironment and use the default interaction handler
117 if( _xHandler.is() )
118 pHelper->m_aInteractionHandler = _xHandler;
119 else
120 pHelper->m_aInteractionHandler.set(
121 InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY_THROW);
122
124 pHelper->m_aProgressHandler.set(pProgressHelper);
125
126 // UCB has ownership of environment...
127 _rOutEnv = pHelper;
128 return apSerialization;
129}
130
131/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScriptDocument aDocument
INetURLObject m_aURLObj
::std::unique_ptr< CSerialization > createSerialization(const css::uno::Reference< css::task::XInteractionHandler > &aHandler, css::uno::Reference< css::ucb::XCommandEnvironment > &_rOutEnv)
Definition: replace.cxx:108
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Reference< css::xml::dom::XDocumentFragment > m_aFragment
css::uno::Reference< css::io::XInputStream > m_aResultStream
SubmissionResult replace(std::u16string_view, const css::uno::Reference< css::xml::dom::XDocument > &, const css::uno::Reference< css::frame::XFrame > &)
Definition: replace.cxx:47
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
#define TOOLS_WARN_EXCEPTION(area, stream)
URL aURL
float u
@ Exception
Reference< XComponentContext > getProcessComponentContext()
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)