merge the formfield patch from ooo-build
[ooovba.git] / framework / qa / complex / dispatches / helper / Interceptor.java
blob824d7f71e621540878ba3006d95d51651e9150ea
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Interceptor.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex.dispatches;
32 // __________ Imports __________
34 // structs, const, ...
35 import com.sun.star.beans.PropertyValue;
37 // exceptions
38 import com.sun.star.uno.Exception;
39 import com.sun.star.uno.RuntimeException;
41 // interfaces
42 import com.sun.star.frame.XDispatchProvider;
43 import com.sun.star.frame.XDispatch;
44 import com.sun.star.frame.XDispatchProviderInterceptor;
45 import com.sun.star.frame.XDispatchProviderInterception;
46 import com.sun.star.frame.XInterceptorInfo;
48 // helper
49 import com.sun.star.uno.UnoRuntime;
50 import share.LogWriter;
52 // others
53 //import java.lang.*;
55 // __________ Implementation __________
57 /**
58 * implements a configurable interceptor for dispatch events.
60 public class Interceptor implements com.sun.star.frame.XDispatchProvider,
61 com.sun.star.frame.XDispatch,
62 com.sun.star.frame.XDispatchProviderInterceptor,
63 com.sun.star.frame.XInterceptorInfo
65 // ____________________
67 /** contains the list of interception URL schema's (wildcards are allowed there!)
68 supported by this interceptor. It can be set from outside.
69 If no external URLs are set, the default "*" is used instead.
70 That would have the same effect as if this implementation would not support the
71 interface XInterceptorInfo !
73 private String[] m_lURLs4InterceptionInfo = null;
75 // ____________________
77 /** These URL's will be blocked by this interceptor.
78 Can be set from outside. Every queryDispatch() for these
79 set of URL's will be answered with an empty dispatch object!
80 If no external URLs are set the default "*" is used instead.
81 So every incoming URL will be blocked .-)
83 private String[] m_lURLs4Blocking = null;
85 // ____________________
87 /** Every dispatch interceptor knows it's master and slave interceptor
88 of the dispatch chain. These values must be stupid handled .-)
89 They have to be set and reset in case the right interface methods are called.
90 Nothing more. It's not allowed to dispose() it.
91 The slave can be used inside queryDispatch() to forward requests,
92 which are not handled by this interceptor instance.
94 private com.sun.star.frame.XDispatchProvider m_xSlave = null;
95 private com.sun.star.frame.XDispatchProvider m_xMaster = null;
97 // ____________________
99 /** counts calls of setSlave...().
100 So the outside API test can use this value to know if this interceptor
101 was realy added to the interceptor chain of OOo.
103 private int m_nRegistrationCount = 0;
105 // ____________________
107 /** indicates if this interceptor object is currently part of the interceptor
108 chain of OOo. Only true if a valid slave or master dispatch is set on this
109 instance.
111 private boolean m_bIsRegistered = false;
113 // ____________________
115 /** used for log output.
117 private LogWriter m_aLog;
119 // ____________________
121 /** ctor
122 * It's initialize an object of this class with default values.
124 public Interceptor(LogWriter aLog)
126 m_aLog = aLog;
129 // ____________________
131 /** XInterceptorInfo */
132 public synchronized String[] getInterceptedURLs()
134 return impl_getURLs4InterceptionInfo();
137 // ____________________
139 /** XDispatchProviderInterceptor */
140 public synchronized com.sun.star.frame.XDispatchProvider getSlaveDispatchProvider()
142 m_aLog.println("Interceptor.getSlaveDispatchProvider() called");
143 return m_xSlave;
146 // ____________________
148 /** XDispatchProviderInterceptor */
149 public synchronized com.sun.star.frame.XDispatchProvider getMasterDispatchProvider()
151 m_aLog.println("Interceptor.getMasterDispatchProvider() called");
152 return m_xMaster;
155 // ____________________
157 /** XDispatchProviderInterceptor */
158 public synchronized void setSlaveDispatchProvider(com.sun.star.frame.XDispatchProvider xSlave)
160 m_aLog.println("Interceptor.setSlaveDispatchProvider("+xSlave+") called");
162 if (xSlave != null)
164 ++m_nRegistrationCount;
165 m_bIsRegistered = true;
167 else
168 m_bIsRegistered = false;
170 m_xSlave = xSlave;
173 // ____________________
175 /** XDispatchProviderInterceptor */
176 public synchronized void setMasterDispatchProvider(com.sun.star.frame.XDispatchProvider xMaster)
178 m_aLog.println("Interceptor.setMasterDispatchProvider("+xMaster+") called");
179 m_xMaster = xMaster;
182 // ____________________
184 /** XDispatchProvider
186 public synchronized com.sun.star.frame.XDispatch queryDispatch(com.sun.star.util.URL aURL ,
187 String sTargetFrameName,
188 int nSearchFlags )
190 m_aLog.println("Interceptor.queryDispatch('"+aURL.Complete+"', '"+sTargetFrameName+"', "+nSearchFlags+") called");
192 if (impl_isBlockedURL(aURL.Complete))
194 m_aLog.println("Interceptor.queryDispatch(): URL blocked => returns NULL");
195 return null;
198 if (m_xSlave != null)
200 m_aLog.println("Interceptor.queryDispatch(): ask slave ...");
201 return m_xSlave.queryDispatch(aURL, sTargetFrameName, nSearchFlags);
204 m_aLog.println("Interceptor.queryDispatch(): no idea => returns this");
205 return this;
208 // ____________________
210 /** XDispatchProvider
212 public com.sun.star.frame.XDispatch[] queryDispatches(com.sun.star.frame.DispatchDescriptor[] lRequests)
214 int i = 0;
215 int c = lRequests.length;
217 com.sun.star.frame.XDispatch[] lResults = new com.sun.star.frame.XDispatch[c];
218 for (i=0; i<c; ++i)
220 lResults[i] = queryDispatch(lRequests[i].FeatureURL ,
221 lRequests[i].FrameName ,
222 lRequests[i].SearchFlags);
225 return lResults;
228 // ____________________
230 /** XDispatch
232 public synchronized void dispatch(com.sun.star.util.URL aURL ,
233 com.sun.star.beans.PropertyValue[] lArguments)
235 m_aLog.println("Interceptor.dispatch('"+aURL.Complete+"') called");
238 // ____________________
240 /** XDispatch
242 public synchronized void addStatusListener(com.sun.star.frame.XStatusListener xListener,
243 com.sun.star.util.URL aURL )
245 m_aLog.println("Interceptor.addStatusListener(..., '"+aURL.Complete+"') called");
248 // ____________________
250 /** XDispatch
252 public synchronized void removeStatusListener(com.sun.star.frame.XStatusListener xListener,
253 com.sun.star.util.URL aURL )
255 m_aLog.println("Interceptor.removeStatusListener(..., '"+aURL.Complete+"') called");
258 // ____________________
260 public synchronized int getRegistrationCount()
262 return m_nRegistrationCount;
265 // ____________________
267 public synchronized boolean isRegistered()
269 return m_bIsRegistered;
272 // ____________________
274 /** set a new list of URL's, which should be used on registration time
275 (that's why it's neccessary to call this impl-method before the interceptor
276 is used at the OOo API!) to optimize the interception chain.
278 public synchronized void setURLs4InterceptionInfo(String[] lURLs)
280 m_lURLs4InterceptionInfo = lURLs;
283 // ____________________
285 /** set a new list of URL's, which should be blocked by this interceptor.
286 (that's why it's neccessary to call this impl-method before the interceptor
287 is used at the OOo API!)
289 public synchronized void setURLs4URLs4Blocking(String[] lURLs)
291 m_lURLs4Blocking = lURLs;
294 // ____________________
296 /** must be used internal to access the member m_lURLs4InterceptionInfo
297 - threadsafe
298 - and to make sure it's initialized on demand
300 private synchronized String[] impl_getURLs4InterceptionInfo()
302 if (m_lURLs4InterceptionInfo == null)
304 m_lURLs4InterceptionInfo = new String[1];
305 m_lURLs4InterceptionInfo[0] = "*";
308 return m_lURLs4InterceptionInfo;
311 // ____________________
313 /** must be used internal to access the member m_lURLs4Blocking
314 - threadsafe
315 - and to make sure it's initialized on demand
317 private synchronized String[] impl_getURLs4Blocking()
319 if (m_lURLs4Blocking == null)
321 m_lURLs4Blocking = new String[1];
322 m_lURLs4Blocking[0] = "*";
325 return m_lURLs4Blocking;
328 // ____________________
329 private boolean impl_isBlockedURL(String sURL)
331 String[] lBlockedURLs = impl_getURLs4Blocking();
332 int i = 0;
333 int c = lBlockedURLs.length;
335 for (i=0; i<c; ++i)
337 if (impl_match(sURL, lBlockedURLs[i]))
338 return true;
341 return false;
344 // ____________________
346 private boolean impl_match(String sVal1, String sVal2)
348 // TODO implement wildcard match
349 return (sVal1.equals(sVal2));