1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 #include <memory>
31 #include "submission.hxx"
32 #include "serialization_app_xml.hxx"
33 
34 #include <rtl/ustring.hxx>
35 #include <rtl/string.hxx>
36 
37 #include <unotools/processfactory.hxx>
38 #include <com/sun/star/uno/Reference.hxx>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/xml/dom/XDocument.hpp>
41 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
42 #include <com/sun/star/frame/XComponentLoader.hpp>
43 #include <com/sun/star/frame/FrameSearchFlag.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <ucbhelper/content.hxx>
46 
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::ucb;
49 using namespace com::sun::star::frame;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::beans;
52 using namespace com::sun::star::task;
53 using namespace com::sun::star::xml::dom;
54 
55 CSubmission::SubmissionResult CSubmission::replace(const ::rtl::OUString& aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame)
56 {
57     if (!m_aResultStream.is())
58         return CSubmission::UNKNOWN_ERROR;
59 
60     try {
61         Reference< XMultiServiceFactory > xFactory = utl::getProcessServiceFactory();
62         if (aReplace.equalsIgnoreAsciiCaseAscii("all") || aReplace.equalsIgnoreAsciiCaseAscii("document")) {
63             Reference< XComponentLoader > xLoader;
64             if (aFrame.is())
65                 xLoader = Reference< XComponentLoader >(aFrame, UNO_QUERY);
66 
67             if (!xLoader.is())
68                 xLoader = Reference< XComponentLoader >(xFactory->createInstance(
69                     ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop")), UNO_QUERY_THROW);
70 
71             // open the stream from the result...
72             // build media descriptor
73             Sequence< PropertyValue > descriptor(2);
74             descriptor[0] = PropertyValue(::rtl::OUString::createFromAscii(
75                 "InputStream"), -1, makeAny(m_aResultStream), PropertyState_DIRECT_VALUE);
76             descriptor[1] = PropertyValue(::rtl::OUString::createFromAscii(
77                 "ReadOnly"), -1, makeAny(sal_True), PropertyState_DIRECT_VALUE);
78 
79             //::rtl::OUString aURL = ::rtl::OUString::createFromAscii("private:stream");
80             ::rtl::OUString aURL = m_aURLObj.GetMainURL(INetURLObject::NO_DECODE);
81             ::rtl::OUString aTarget = ::rtl::OUString::createFromAscii("_default");
82             xLoader->loadComponentFromURL(aURL, aTarget, FrameSearchFlag::ALL, descriptor);
83 
84             return CSubmission::SUCCESS;
85 
86         } else if (aReplace.equalsIgnoreAsciiCaseAscii("instance")) {
87             if (aDocument.is()) {
88                 // parse the result stream into a new document
89                 Reference< XDocumentBuilder > xBuilder(xFactory->createInstance(
90                     ::rtl::OUString::createFromAscii("com.sun.star.xml.dom.DocumentBuilder")), UNO_QUERY_THROW);
91                 Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream);
92 
93 				if (aNewDocument.is()) {
94 					// and replace the content of the current instance
95 					Reference< XElement > oldRoot = aDocument->getDocumentElement();
96 					Reference< XElement > newRoot = aNewDocument->getDocumentElement();
97 
98 					// aDocument->removeChild(Reference< XNode >(oldRoot, UNO_QUERY_THROW));
99 					Reference< XNode > aImportedNode = aDocument->importNode(Reference< XNode >(newRoot, UNO_QUERY_THROW), sal_True);
100 					Reference< XNode >(aDocument, UNO_QUERY_THROW)->replaceChild(aImportedNode, Reference< XNode >(oldRoot, UNO_QUERY_THROW));
101 					return CSubmission::SUCCESS;
102 				} else {
103 					return CSubmission::UNKNOWN_ERROR;
104 				}
105             } else {
106                 // nothing to replace
107                 return CSubmission::UNKNOWN_ERROR;
108             }
109         } else if (aReplace.equalsIgnoreAsciiCaseAscii("none")) {
110             // do nothing \o/
111             return CSubmission::SUCCESS;
112         }
113     } catch (Exception& e) {
114         ::rtl::OString aMsg("Exception during replace:\n");
115         aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8);
116         OSL_ENSURE(sal_False, aMsg.getStr());
117     }
118     return CSubmission::UNKNOWN_ERROR;
119 }
120 ::std::auto_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv)
121 {
122     // PUT always uses application/xml
123     ::std::auto_ptr< CSerialization > apSerialization(new CSerializationAppXML());
124     apSerialization->setSource(m_aFragment);
125     apSerialization->serialize();
126 
127     // create a commandEnvironment and use the default interaction handler
128     CCommandEnvironmentHelper *pHelper = new CCommandEnvironmentHelper;
129     if( _xHandler.is() )
130         pHelper->m_aInteractionHandler = _xHandler;
131     else
132         pHelper->m_aInteractionHandler = CSS::uno::Reference< XInteractionHandler >(m_aFactory->createInstance(
133         ::rtl::OUString::createFromAscii("com.sun.star.task.InteractionHandler")), UNO_QUERY);
134     OSL_ENSURE(pHelper->m_aInteractionHandler.is(), "failed to create IntreractionHandler");
135 
136     CProgressHandlerHelper *pProgressHelper = new CProgressHandlerHelper;
137     pHelper->m_aProgressHandler = Reference< XProgressHandler >(pProgressHelper);
138 
139     // UCB has ownership of environment...
140     _rOutEnv = pHelper;
141     return apSerialization;
142 }
143 
144 
145 
146