Line data Source code
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 :
10 : #include <com/sun/star/beans/PropertyValue.hpp>
11 : #include <com/sun/star/container/NoSuchElementException.hpp>
12 : #include <com/sun/star/container/XNameAccess.hpp>
13 : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
14 : #include <com/sun/star/document/XFilter.hpp>
15 : #include <com/sun/star/document/XImporter.hpp>
16 : #include <com/sun/star/document/XTypeDetection.hpp>
17 : #include <com/sun/star/frame/theDesktop.hpp>
18 : #include <com/sun/star/frame/XController.hpp>
19 : #include <com/sun/star/frame/XFrame.hpp>
20 : #include <com/sun/star/frame/XModel.hpp>
21 : #include <com/sun/star/io/XInputStream.hpp>
22 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 : #include <com/sun/star/lang/XComponent.hpp>
24 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
25 : #include <com/sun/star/util/XCloseable.hpp>
26 :
27 : #include "WpftImportTestBase.hxx"
28 :
29 : namespace beans = com::sun::star::beans;
30 : namespace container = com::sun::star::container;
31 : namespace document = com::sun::star::document;
32 : namespace frame = com::sun::star::frame;
33 : namespace io = com::sun::star::io;
34 : namespace lang = com::sun::star::lang;
35 : namespace ucb = com::sun::star::ucb;
36 : namespace uno = com::sun::star::uno;
37 : namespace util = com::sun::star::util;
38 :
39 : namespace writerperfect
40 : {
41 : namespace test
42 : {
43 :
44 3 : WpftImportTestBase::WpftImportTestBase(const rtl::OUString &rFactoryURL)
45 : : ::test::FiltersTest()
46 : , ::test::BootstrapFixture()
47 : , m_aFactoryURL(rFactoryURL)
48 : , m_xDesktop()
49 : , m_xFileAccess()
50 : , m_xFilter()
51 3 : , m_xTypeMap()
52 : {
53 3 : }
54 :
55 3 : void WpftImportTestBase::setUp()
56 : {
57 3 : ::test::BootstrapFixture::setUp();
58 :
59 3 : m_xDesktop = frame::theDesktop::get(m_xContext);
60 3 : m_xFileAccess = ucb::SimpleFileAccess::create(m_xContext);
61 :
62 : const uno::Reference<document::XTypeDetection> xTypeDetection(
63 3 : m_xFactory->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
64 3 : uno::UNO_QUERY_THROW);
65 3 : m_xTypeMap.set(xTypeDetection, uno::UNO_QUERY_THROW);
66 3 : }
67 :
68 3 : void WpftImportTestBase::tearDown()
69 : {
70 3 : m_xDesktop->terminate();
71 :
72 3 : ::test::BootstrapFixture::tearDown();
73 3 : }
74 :
75 43 : bool WpftImportTestBase::load(const OUString &, const OUString &rURL, const OUString &,
76 : unsigned int, unsigned int, unsigned int)
77 : {
78 : // create an empty frame
79 : const uno::Reference<lang::XComponent> xDoc(
80 86 : m_xDesktop->loadComponentFromURL(m_aFactoryURL, "_blank", 0, uno::Sequence<beans::PropertyValue>()),
81 86 : uno::UNO_QUERY_THROW);
82 :
83 : // Find the model and frame. We need them later.
84 86 : uno::Reference<frame::XFrame> xFrame(xDoc, uno::UNO_QUERY);
85 86 : uno::Reference<frame::XModel> xModel(xDoc, uno::UNO_QUERY);
86 86 : uno::Reference<frame::XController> xController(xDoc, uno::UNO_QUERY);
87 :
88 43 : if (xFrame.is())
89 : {
90 0 : xController = xFrame->getController();
91 0 : xModel = xController->getModel();
92 : }
93 43 : else if (xModel.is())
94 : {
95 43 : xController = xModel->getCurrentController();
96 43 : xFrame = xController->getFrame();
97 : }
98 0 : else if (xController.is())
99 : {
100 0 : xFrame = xController->getFrame();
101 0 : xModel = xController->getModel();
102 : }
103 :
104 43 : if (!xFrame.is() || !xModel.is())
105 0 : throw uno::RuntimeException();
106 :
107 43 : bool result = false;
108 :
109 : // try to import the document (and load it into the prepared frame)
110 : try
111 : {
112 43 : const uno::Reference<document::XImporter> xImporter(m_xFilter, uno::UNO_QUERY_THROW);
113 :
114 43 : xImporter->setTargetDocument(xDoc);
115 :
116 86 : uno::Sequence<beans::PropertyValue> aDescriptor(2);
117 43 : aDescriptor[0].Name = "URL";
118 43 : aDescriptor[0].Value <<= rURL;
119 :
120 86 : const uno::Reference<io::XInputStream> xInputStream(m_xFileAccess->openFileRead(rURL), uno::UNO_QUERY_THROW);
121 43 : aDescriptor[1].Name = "InputStream";
122 43 : aDescriptor[1].Value <<= xInputStream;
123 :
124 86 : const uno::Reference<document::XExtendedFilterDetection> xDetector(m_xFilter, uno::UNO_QUERY_THROW);
125 :
126 86 : const rtl::OUString aTypeName(xDetector->detect(aDescriptor));
127 43 : if (aTypeName.isEmpty())
128 1 : throw lang::IllegalArgumentException();
129 :
130 42 : impl_detectFilterName(aDescriptor, aTypeName);
131 :
132 42 : xModel->lockControllers();
133 42 : result = m_xFilter->filter(aDescriptor);
134 85 : xModel->unlockControllers();
135 : }
136 1 : catch (const uno::Exception &)
137 : {
138 : // ignore
139 : }
140 :
141 : // close the opened document
142 : try
143 : {
144 43 : uno::Reference<util::XCloseable> xCloseable(xFrame, uno::UNO_QUERY);
145 43 : if (xCloseable.is())
146 43 : xCloseable->close(true);
147 : else
148 0 : xDoc->dispose();
149 : }
150 0 : catch (const uno::Exception &)
151 : {
152 : // ignore
153 : }
154 :
155 86 : return result;
156 : }
157 :
158 12 : void WpftImportTestBase::doTest(const rtl::OUString &rFilter, const rtl::OUString &rPath)
159 : {
160 12 : m_xFilter.set(m_xFactory->createInstanceWithContext(rFilter, m_xContext), uno::UNO_QUERY_THROW);
161 12 : testDir(OUString(), getURLFromSrc(rPath), OUString());
162 12 : }
163 :
164 42 : void WpftImportTestBase::impl_detectFilterName(uno::Sequence<beans::PropertyValue> &rDescriptor, const rtl::OUString &rTypeName)
165 : {
166 42 : const sal_Int32 nDescriptorLen = rDescriptor.getLength();
167 :
168 168 : for (sal_Int32 n = 0; nDescriptorLen != n; ++n)
169 : {
170 126 : if ("FilterName" == rDescriptor[n].Name)
171 0 : return;
172 : }
173 :
174 42 : uno::Sequence<beans::PropertyValue> aTypes;
175 42 : if (m_xTypeMap->getByName(rTypeName) >>= aTypes)
176 : {
177 756 : for (sal_Int32 n = 0; aTypes.getLength() != n; ++n)
178 : {
179 378 : rtl::OUString aFilterName;
180 378 : if (("PreferredFilter" == aTypes[n].Name) && (aTypes[n].Value >>= aFilterName))
181 : {
182 42 : rDescriptor.realloc(nDescriptorLen + 1);
183 42 : rDescriptor[nDescriptorLen].Name = "FilterName";
184 42 : rDescriptor[nDescriptorLen].Value <<= aFilterName;
185 42 : return;
186 : }
187 336 : }
188 : }
189 :
190 42 : throw container::NoSuchElementException();
191 : }
192 :
193 : }
194 9 : }
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|