124acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
324acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
424acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file
524acc546SAndrew Rist * distributed with this work for additional information
624acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file
724acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the
824acc546SAndrew Rist * "License"); you may not use this file except in compliance
924acc546SAndrew Rist * with the License. You may obtain a copy of the License at
1024acc546SAndrew Rist *
1124acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
1224acc546SAndrew Rist *
1324acc546SAndrew Rist * Unless required by applicable law or agreed to in writing,
1424acc546SAndrew Rist * software distributed under the License is distributed on an
1524acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1624acc546SAndrew Rist * KIND, either express or implied. See the License for the
1724acc546SAndrew Rist * specific language governing permissions and limitations
1824acc546SAndrew Rist * under the License.
1924acc546SAndrew Rist *
2024acc546SAndrew Rist *************************************************************/
2124acc546SAndrew Rist
2224acc546SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir #include <memory>
27cdf0e10cSrcweir #include "submission.hxx"
28cdf0e10cSrcweir #include "serialization_app_xml.hxx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <rtl/string.hxx>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include <unotools/processfactory.hxx>
34cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
35cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocument.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
38cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
39cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
40cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
41cdf0e10cSrcweir #include <ucbhelper/content.hxx>
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace com::sun::star::uno;
44cdf0e10cSrcweir using namespace com::sun::star::ucb;
45cdf0e10cSrcweir using namespace com::sun::star::frame;
46cdf0e10cSrcweir using namespace com::sun::star::lang;
47cdf0e10cSrcweir using namespace com::sun::star::beans;
48cdf0e10cSrcweir using namespace com::sun::star::task;
49cdf0e10cSrcweir using namespace com::sun::star::xml::dom;
50cdf0e10cSrcweir
replace(const::rtl::OUString & aReplace,const Reference<XDocument> & aDocument,const Reference<XFrame> & aFrame)51cdf0e10cSrcweir CSubmission::SubmissionResult CSubmission::replace(const ::rtl::OUString& aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir if (!m_aResultStream.is())
54cdf0e10cSrcweir return CSubmission::UNKNOWN_ERROR;
55cdf0e10cSrcweir
56cdf0e10cSrcweir try {
57cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory = utl::getProcessServiceFactory();
58cdf0e10cSrcweir if (aReplace.equalsIgnoreAsciiCaseAscii("all") || aReplace.equalsIgnoreAsciiCaseAscii("document")) {
59cdf0e10cSrcweir Reference< XComponentLoader > xLoader;
60cdf0e10cSrcweir if (aFrame.is())
61cdf0e10cSrcweir xLoader = Reference< XComponentLoader >(aFrame, UNO_QUERY);
62cdf0e10cSrcweir
63cdf0e10cSrcweir if (!xLoader.is())
64cdf0e10cSrcweir xLoader = Reference< XComponentLoader >(xFactory->createInstance(
65cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop")), UNO_QUERY_THROW);
66cdf0e10cSrcweir
67cdf0e10cSrcweir // open the stream from the result...
68cdf0e10cSrcweir // build media descriptor
69cdf0e10cSrcweir Sequence< PropertyValue > descriptor(2);
70cdf0e10cSrcweir descriptor[0] = PropertyValue(::rtl::OUString::createFromAscii(
71cdf0e10cSrcweir "InputStream"), -1, makeAny(m_aResultStream), PropertyState_DIRECT_VALUE);
72cdf0e10cSrcweir descriptor[1] = PropertyValue(::rtl::OUString::createFromAscii(
73cdf0e10cSrcweir "ReadOnly"), -1, makeAny(sal_True), PropertyState_DIRECT_VALUE);
74cdf0e10cSrcweir
75cdf0e10cSrcweir //::rtl::OUString aURL = ::rtl::OUString::createFromAscii("private:stream");
76cdf0e10cSrcweir ::rtl::OUString aURL = m_aURLObj.GetMainURL(INetURLObject::NO_DECODE);
77cdf0e10cSrcweir ::rtl::OUString aTarget = ::rtl::OUString::createFromAscii("_default");
78cdf0e10cSrcweir xLoader->loadComponentFromURL(aURL, aTarget, FrameSearchFlag::ALL, descriptor);
79cdf0e10cSrcweir
80cdf0e10cSrcweir return CSubmission::SUCCESS;
81cdf0e10cSrcweir
82cdf0e10cSrcweir } else if (aReplace.equalsIgnoreAsciiCaseAscii("instance")) {
83cdf0e10cSrcweir if (aDocument.is()) {
84cdf0e10cSrcweir // parse the result stream into a new document
85cdf0e10cSrcweir Reference< XDocumentBuilder > xBuilder(xFactory->createInstance(
86cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.xml.dom.DocumentBuilder")), UNO_QUERY_THROW);
87cdf0e10cSrcweir Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
88cdf0e10cSrcweir
89cdf0e10cSrcweir if (aNewDocument.is()) {
90cdf0e10cSrcweir // and replace the content of the current instance
91cdf0e10cSrcweir Reference< XElement > oldRoot = aDocument->getDocumentElement();
92cdf0e10cSrcweir Reference< XElement > newRoot = aNewDocument->getDocumentElement();
93cdf0e10cSrcweir
94cdf0e10cSrcweir // aDocument->removeChild(Reference< XNode >(oldRoot, UNO_QUERY_THROW));
95cdf0e10cSrcweir Reference< XNode > aImportedNode = aDocument->importNode(Reference< XNode >(newRoot, UNO_QUERY_THROW), sal_True);
96cdf0e10cSrcweir Reference< XNode >(aDocument, UNO_QUERY_THROW)->replaceChild(aImportedNode, Reference< XNode >(oldRoot, UNO_QUERY_THROW));
97cdf0e10cSrcweir return CSubmission::SUCCESS;
98cdf0e10cSrcweir } else {
99cdf0e10cSrcweir return CSubmission::UNKNOWN_ERROR;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir } else {
102cdf0e10cSrcweir // nothing to replace
103cdf0e10cSrcweir return CSubmission::UNKNOWN_ERROR;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir } else if (aReplace.equalsIgnoreAsciiCaseAscii("none")) {
106cdf0e10cSrcweir // do nothing \o/
107cdf0e10cSrcweir return CSubmission::SUCCESS;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir } catch (Exception& e) {
110cdf0e10cSrcweir ::rtl::OString aMsg("Exception during replace:\n");
111cdf0e10cSrcweir aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8);
112cdf0e10cSrcweir OSL_ENSURE(sal_False, aMsg.getStr());
113cdf0e10cSrcweir }
114cdf0e10cSrcweir return CSubmission::UNKNOWN_ERROR;
115cdf0e10cSrcweir }
createSerialization(const Reference<XInteractionHandler> & _xHandler,Reference<XCommandEnvironment> & _rOutEnv)116cdf0e10cSrcweir ::std::auto_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir // PUT always uses application/xml
119cdf0e10cSrcweir ::std::auto_ptr< CSerialization > apSerialization(new CSerializationAppXML());
120cdf0e10cSrcweir apSerialization->setSource(m_aFragment);
121cdf0e10cSrcweir apSerialization->serialize();
122cdf0e10cSrcweir
123cdf0e10cSrcweir // create a commandEnvironment and use the default interaction handler
124cdf0e10cSrcweir CCommandEnvironmentHelper *pHelper = new CCommandEnvironmentHelper;
125cdf0e10cSrcweir if( _xHandler.is() )
126cdf0e10cSrcweir pHelper->m_aInteractionHandler = _xHandler;
127cdf0e10cSrcweir else
128cdf0e10cSrcweir pHelper->m_aInteractionHandler = CSS::uno::Reference< XInteractionHandler >(m_aFactory->createInstance(
129cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.task.InteractionHandler")), UNO_QUERY);
130*8735c047SJohn Bampton OSL_ENSURE(pHelper->m_aInteractionHandler.is(), "failed to create InteractionHandler");
131cdf0e10cSrcweir
132cdf0e10cSrcweir CProgressHandlerHelper *pProgressHelper = new CProgressHandlerHelper;
133cdf0e10cSrcweir pHelper->m_aProgressHandler = Reference< XProgressHandler >(pProgressHelper);
134cdf0e10cSrcweir
135cdf0e10cSrcweir // UCB has ownership of environment...
136cdf0e10cSrcweir _rOutEnv = pHelper;
137cdf0e10cSrcweir return apSerialization;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir
141cdf0e10cSrcweir
142