Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "ResourceFactoryManager.hxx"
31 : : #include <tools/wldcrd.hxx>
32 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 : : #include <com/sun/star/lang/XComponent.hpp>
34 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 : : #include <com/sun/star/util/URLTransformer.hpp>
36 : : #include <comphelper/processfactory.hxx>
37 : : #include <boost/bind.hpp>
38 : : #include <algorithm>
39 : :
40 : : using namespace ::com::sun::star;
41 : : using namespace ::com::sun::star::uno;
42 : : using namespace ::com::sun::star::drawing::framework;
43 : : using ::rtl::OUString;
44 : :
45 : : namespace sd { namespace framework {
46 : :
47 : 130 : ResourceFactoryManager::ResourceFactoryManager (const Reference<XControllerManager>& rxManager)
48 : : : maMutex(),
49 : : maFactoryMap(),
50 : : maFactoryPatternList(),
51 : : mxControllerManager(rxManager),
52 [ + - ][ + - ]: 130 : mxURLTransformer()
53 : : {
54 : : // Create the URL transformer.
55 [ + - ]: 130 : Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
56 [ + - ][ + - ]: 130 : mxURLTransformer = util::URLTransformer::create(xContext);
57 : 130 : }
58 : :
59 : :
60 : :
61 : :
62 [ + - ]: 130 : ResourceFactoryManager::~ResourceFactoryManager (void)
63 : : {
64 [ + - ]: 130 : Reference<lang::XComponent> xComponent (mxURLTransformer, UNO_QUERY);
65 [ - + ]: 130 : if (xComponent.is())
66 [ # # ][ # # ]: 130 : xComponent->dispose();
67 : 130 : }
68 : :
69 : :
70 : :
71 : :
72 : 1926 : void ResourceFactoryManager::AddFactory (
73 : : const OUString& rsURL,
74 : : const Reference<XResourceFactory>& rxFactory)
75 : : throw (RuntimeException)
76 : : {
77 [ - + ]: 1926 : if ( ! rxFactory.is())
78 [ # # ]: 0 : throw lang::IllegalArgumentException();
79 [ - + ]: 1926 : if (rsURL.isEmpty())
80 [ # # ]: 0 : throw lang::IllegalArgumentException();
81 : :
82 [ + - ]: 1926 : ::osl::MutexGuard aGuard (maMutex);
83 : :
84 [ + - ][ - + ]: 1926 : if (rsURL.indexOf('*') >= 0 || rsURL.indexOf('?') >= 0)
[ - + ]
85 : : {
86 : : // The URL is a URL pattern not an single URL.
87 [ # # ][ # # ]: 0 : maFactoryPatternList.push_back(FactoryPatternList::value_type(rsURL, rxFactory));
[ # # ]
88 : : }
89 : : else
90 : : {
91 [ + - ][ + - ]: 1926 : maFactoryMap[rsURL] = rxFactory;
92 [ + - ]: 1926 : }
93 : 1926 : }
94 : :
95 : :
96 : :
97 : :
98 : 0 : void ResourceFactoryManager::RemoveFactoryForURL (
99 : : const OUString& rsURL)
100 : : throw (RuntimeException)
101 : : {
102 [ # # ]: 0 : if (rsURL.isEmpty())
103 [ # # ]: 0 : throw lang::IllegalArgumentException();
104 : :
105 [ # # ]: 0 : ::osl::MutexGuard aGuard (maMutex);
106 : :
107 [ # # ]: 0 : FactoryMap::iterator iFactory (maFactoryMap.find(rsURL));
108 [ # # ][ # # ]: 0 : if (iFactory != maFactoryMap.end())
109 : : {
110 [ # # ]: 0 : maFactoryMap.erase(iFactory);
111 : : }
112 : : else
113 : : {
114 : : // The URL may be a pattern. Look that up.
115 : 0 : FactoryPatternList::iterator iPattern;
116 [ # # ][ # # ]: 0 : for (iPattern=maFactoryPatternList.begin();
117 : 0 : iPattern!=maFactoryPatternList.end();
118 : : ++iPattern)
119 : : {
120 [ # # ]: 0 : if (iPattern->first == rsURL)
121 : : {
122 : : // Found the pattern. Remove it.
123 [ # # ]: 0 : maFactoryPatternList.erase(iPattern);
124 : 0 : break;
125 : : }
126 : : }
127 [ # # ]: 0 : }
128 : 0 : }
129 : :
130 : :
131 : :
132 : :
133 : :
134 : 0 : void ResourceFactoryManager::RemoveFactoryForReference(
135 : : const Reference<XResourceFactory>& rxFactory)
136 : : throw (RuntimeException)
137 : : {
138 [ # # ]: 0 : ::osl::MutexGuard aGuard (maMutex);
139 : :
140 : : // Collect a list with all keys that map to the given factory.
141 [ # # ]: 0 : ::std::vector<OUString> aKeys;
142 : 0 : FactoryMap::const_iterator iFactory;
143 [ # # ][ # # ]: 0 : for (iFactory=maFactoryMap.begin(); iFactory!=maFactoryMap.end(); ++iFactory)
[ # # ]
144 [ # # ][ # # ]: 0 : if (iFactory->second == rxFactory)
[ # # ]
145 [ # # ][ # # ]: 0 : aKeys.push_back(iFactory->first);
146 : :
147 : : // Remove the entries whose keys we just have collected.
148 : 0 : ::std::vector<OUString>::const_iterator iKey;
149 [ # # ][ # # ]: 0 : for (iKey=aKeys.begin(); iKey!=aKeys.end(); ++iKey)
[ # # ]
150 [ # # ][ # # ]: 0 : maFactoryMap.erase(maFactoryMap.find(*iKey));
151 : :
152 : : // Remove the pattern entries whose factories are identical to the given
153 : : // factory.
154 : : FactoryPatternList::iterator iNewEnd (
155 : : std::remove_if(
156 : : maFactoryPatternList.begin(),
157 : : maFactoryPatternList.end(),
158 : : ::boost::bind(
159 : : std::equal_to<Reference<XResourceFactory> >(),
160 : : ::boost::bind(&FactoryPatternList::value_type::second, _1),
161 [ # # ][ # # ]: 0 : rxFactory)));
[ # # ][ # # ]
162 [ # # ][ # # ]: 0 : if (iNewEnd != maFactoryPatternList.end())
163 [ # # ][ # # ]: 0 : maFactoryPatternList.erase(iNewEnd, maFactoryPatternList.end());
164 : 0 : }
165 : :
166 : :
167 : :
168 : :
169 : 932 : Reference<XResourceFactory> ResourceFactoryManager::GetFactory (
170 : : const OUString& rsCompleteURL)
171 : : throw (RuntimeException)
172 : : {
173 : 932 : OUString sURLBase (rsCompleteURL);
174 [ + - ]: 932 : if (mxURLTransformer.is())
175 : : {
176 : 932 : util::URL aURL;
177 : 932 : aURL.Complete = rsCompleteURL;
178 [ + - ][ + - ]: 932 : if (mxURLTransformer->parseStrict(aURL))
[ + - ]
179 : 932 : sURLBase = aURL.Main;
180 : : }
181 : :
182 [ + - ]: 932 : Reference<XResourceFactory> xFactory = FindFactory(sURLBase);
183 : :
184 [ + + ][ + - ]: 932 : if ( ! xFactory.is() && mxControllerManager.is())
[ + + ]
185 : : {
186 [ + - ][ + - ]: 302 : Reference<XModuleController> xModuleController(mxControllerManager->getModuleController());
187 [ + - ]: 302 : if (xModuleController.is())
188 : : {
189 : : // Ask the module controller to provide a factory of the
190 : : // requested view type. Note that this can (and should) cause
191 : : // intermediate calls to AddFactory().
192 [ + - ][ + - ]: 302 : xModuleController->requestResource(sURLBase);
193 : :
194 [ + - ][ + - ]: 302 : xFactory = FindFactory(sURLBase);
195 : 302 : }
196 : : }
197 : :
198 : 932 : return xFactory;
199 : : }
200 : :
201 : :
202 : :
203 : :
204 : 1234 : Reference<XResourceFactory> ResourceFactoryManager::FindFactory (const OUString& rsURLBase)
205 : : throw (RuntimeException)
206 : : {
207 [ + - ]: 1234 : ::osl::MutexGuard aGuard (maMutex);
208 [ + - ]: 1234 : FactoryMap::const_iterator iFactory (maFactoryMap.find(rsURLBase));
209 [ + + ][ + - ]: 1234 : if (iFactory != maFactoryMap.end())
210 [ + - ]: 932 : return iFactory->second;
211 : : else
212 : : {
213 : : // Check the URL patterns.
214 : 302 : FactoryPatternList::const_iterator iPattern;
215 [ + - ][ + - ]: 604 : for (iPattern=maFactoryPatternList.begin();
[ - + ]
216 : 302 : iPattern!=maFactoryPatternList.end();
217 : : ++iPattern)
218 : : {
219 [ # # ]: 0 : WildCard aWildCard (iPattern->first);
220 [ # # ][ # # ]: 0 : if (aWildCard.Matches(rsURLBase))
[ # # ][ # # ]
221 : 0 : return iPattern->second;
222 [ # # ][ # # ]: 0 : }
223 : : }
224 [ + - ][ + - ]: 1234 : return NULL;
225 : : }
226 : :
227 [ + - ][ + - ]: 75 : } } // end of namespace sd::framework
228 : :
229 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|