1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ResourceFactoryManager.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "precompiled_sd.hxx"
34 #include "ResourceFactoryManager.hxx"
35 #include <tools/wldcrd.hxx>
36 #include <com/sun/star/lang/IllegalArgumentException.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <comphelper/processfactory.hxx>
40 #include <boost/bind.hpp>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::drawing::framework
;
46 using ::rtl::OUString
;
48 namespace sd
{ namespace framework
{
50 ResourceFactoryManager::ResourceFactoryManager (const Reference
<XControllerManager
>& rxManager
)
53 maFactoryPatternList(),
54 mxControllerManager(rxManager
),
57 // Create the URL transformer.
58 Reference
<lang::XMultiServiceFactory
> xServiceManager (
59 ::comphelper::getProcessServiceFactory());
60 mxURLTransformer
= Reference
<util::XURLTransformer
>(
61 xServiceManager
->createInstance(
62 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.URLTransformer"))),
69 ResourceFactoryManager::~ResourceFactoryManager (void)
71 Reference
<lang::XComponent
> xComponent (mxURLTransformer
, UNO_QUERY
);
73 xComponent
->dispose();
79 void ResourceFactoryManager::AddFactory (
80 const OUString
& rsURL
,
81 const Reference
<XResourceFactory
>& rxFactory
)
82 throw (RuntimeException
)
84 if ( ! rxFactory
.is())
85 throw lang::IllegalArgumentException();
86 if (rsURL
.getLength() == 0)
87 throw lang::IllegalArgumentException();
89 ::osl::MutexGuard
aGuard (maMutex
);
91 if (rsURL
.indexOf('*') >= 0 || rsURL
.indexOf('?') >= 0)
93 // The URL is a URL pattern not an single URL.
94 maFactoryPatternList
.push_back(FactoryPatternList::value_type(rsURL
, rxFactory
));
98 maFactoryMap
[rsURL
] = rxFactory
;
105 void ResourceFactoryManager::RemoveFactoryForURL (
106 const OUString
& rsURL
)
107 throw (RuntimeException
)
109 if (rsURL
.getLength() == 0)
110 throw lang::IllegalArgumentException();
112 ::osl::MutexGuard
aGuard (maMutex
);
114 FactoryMap::iterator
iFactory (maFactoryMap
.find(rsURL
));
115 if (iFactory
!= maFactoryMap
.end())
117 maFactoryMap
.erase(iFactory
);
121 // The URL may be a pattern. Look that up.
122 FactoryPatternList::iterator iPattern
;
123 for (iPattern
=maFactoryPatternList
.begin();
124 iPattern
!=maFactoryPatternList
.end();
127 if (iPattern
->first
== rsURL
)
129 // Found the pattern. Remove it.
130 maFactoryPatternList
.erase(iPattern
);
141 void ResourceFactoryManager::RemoveFactoryForReference(
142 const Reference
<XResourceFactory
>& rxFactory
)
143 throw (RuntimeException
)
145 ::osl::MutexGuard
aGuard (maMutex
);
147 // Collect a list with all keys that map to the given factory.
148 ::std::vector
<OUString
> aKeys
;
149 FactoryMap::const_iterator iFactory
;
150 for (iFactory
=maFactoryMap
.begin(); iFactory
!=maFactoryMap
.end(); ++iFactory
)
151 if (iFactory
->second
== rxFactory
)
152 aKeys
.push_back(iFactory
->first
);
154 // Remove the entries whose keys we just have collected.
155 ::std::vector
<OUString
>::const_iterator iKey
;
156 for (iKey
=aKeys
.begin(); iKey
!=aKeys
.end(); ++iKey
)
157 maFactoryMap
.erase(maFactoryMap
.find(*iKey
));
159 // Remove the pattern entries whose factories are identical to the given
161 FactoryPatternList::iterator
iNewEnd (
163 maFactoryPatternList
.begin(),
164 maFactoryPatternList
.end(),
166 std::equal_to
<Reference
<XResourceFactory
> >(),
167 ::boost::bind(&FactoryPatternList::value_type::second
, _1
),
169 if (iNewEnd
!= maFactoryPatternList
.end())
170 maFactoryPatternList
.erase(iNewEnd
, maFactoryPatternList
.end());
176 Reference
<XResourceFactory
> ResourceFactoryManager::GetFactory (
177 const OUString
& rsCompleteURL
)
178 throw (RuntimeException
)
180 OUString
sURLBase (rsCompleteURL
);
181 if (mxURLTransformer
.is())
184 aURL
.Complete
= rsCompleteURL
;
185 if (mxURLTransformer
->parseStrict(aURL
))
186 sURLBase
= aURL
.Main
;
189 Reference
<XResourceFactory
> xFactory
= FindFactory(sURLBase
);
191 if ( ! xFactory
.is() && mxControllerManager
.is())
193 Reference
<XModuleController
> xModuleController(mxControllerManager
->getModuleController());
194 if (xModuleController
.is())
196 // Ask the module controller to provide a factory of the
197 // requested view type. Note that this can (and should) cause
198 // intermediate calls to AddFactory().
199 xModuleController
->requestResource(sURLBase
);
201 xFactory
= FindFactory(sURLBase
);
211 Reference
<XResourceFactory
> ResourceFactoryManager::FindFactory (const OUString
& rsURLBase
)
212 throw (RuntimeException
)
214 ::osl::MutexGuard
aGuard (maMutex
);
215 FactoryMap::const_iterator
iFactory (maFactoryMap
.find(rsURLBase
));
216 if (iFactory
!= maFactoryMap
.end())
217 return iFactory
->second
;
220 // Check the URL patterns.
221 FactoryPatternList::const_iterator iPattern
;
222 for (iPattern
=maFactoryPatternList
.begin();
223 iPattern
!=maFactoryPatternList
.end();
226 WildCard
aWildCard (iPattern
->first
);
227 if (aWildCard
.Matches(rsURLBase
))
228 return iPattern
->second
;
234 } } // end of namespace sd::framework