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