Bump version to 4.1-6
[LibreOffice.git] / framework / qa / complex / dispatches / Interceptor.java
blob3ef3a1f25c3db47533403dd018afbaa6721af944
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package complex.dispatches;
20 // __________ Imports __________
22 // structs, const, ...
23 import com.sun.star.beans.PropertyValue;
25 // exceptions
26 import com.sun.star.frame.DispatchDescriptor;
27 import com.sun.star.frame.XDispatch;
28 import com.sun.star.frame.XDispatchProvider;
29 import com.sun.star.frame.XDispatchProviderInterceptor;
30 import com.sun.star.frame.XInterceptorInfo;
31 import com.sun.star.frame.XStatusListener;
33 // interfaces
36 // helper
37 import com.sun.star.util.URL;
39 // __________ Implementation __________
41 /**
42 * implements a configurable interceptor for dispatch events.
44 public class Interceptor implements XDispatchProvider,
45 XDispatch,
46 XDispatchProviderInterceptor,
47 XInterceptorInfo
49 // ____________________
51 /** contains the list of interception URL schema's (wildcards are allowed there!)
52 supported by this interceptor. It can be set from outside.
53 If no external URLs are set, the default "*" is used instead.
54 That would have the same effect as if this implementation would not support the
55 interface XInterceptorInfo !
57 private String[] m_lURLs4InterceptionInfo = null;
59 // ____________________
61 /** These URL's will be blocked by this interceptor.
62 Can be set from outside. Every queryDispatch() for these
63 set of URL's will be answered with an empty dispatch object!
64 If no external URLs are set the default "*" is used instead.
65 So every incoming URL will be blocked .-)
67 private String[] m_lURLs4Blocking = null;
69 // ____________________
71 /** Every dispatch interceptor knows it's master and slave interceptor
72 of the dispatch chain. These values must be stupid handled .-)
73 They have to be set and reset in case the right interface methods are called.
74 Nothing more. It's not allowed to dispose() it.
75 The slave can be used inside queryDispatch() to forward requests,
76 which are not handled by this interceptor instance.
78 private XDispatchProvider m_xSlave = null;
79 private XDispatchProvider m_xMaster = null;
81 // ____________________
83 /** counts calls of setSlave...().
84 So the outside API test can use this value to know if this interceptor
85 was realy added to the interceptor chain of OOo.
87 private int m_nRegistrationCount = 0;
89 // ____________________
91 /** indicates if this interceptor object is currently part of the interceptor
92 chain of OOo. Only true if a valid slave or master dispatch is set on this
93 instance.
95 private boolean m_bIsRegistered = false;
98 // ____________________
100 /** ctor
101 * It's initialize an object of this class with default values.
103 public Interceptor()
107 // ____________________
109 /** XInterceptorInfo */
110 public synchronized String[] getInterceptedURLs()
112 return impl_getURLs4InterceptionInfo();
115 // ____________________
117 /** XDispatchProviderInterceptor */
118 public synchronized XDispatchProvider getSlaveDispatchProvider()
120 System.out.println("Interceptor.getSlaveDispatchProvider() called");
121 return m_xSlave;
124 // ____________________
126 /** XDispatchProviderInterceptor */
127 public synchronized XDispatchProvider getMasterDispatchProvider()
129 System.out.println("Interceptor.getMasterDispatchProvider() called");
130 return m_xMaster;
133 // ____________________
135 /** XDispatchProviderInterceptor */
136 public synchronized void setSlaveDispatchProvider(XDispatchProvider xSlave)
138 System.out.println("Interceptor.setSlaveDispatchProvider("+xSlave+") called");
140 if (xSlave != null)
142 ++m_nRegistrationCount;
143 m_bIsRegistered = true;
145 else
147 m_bIsRegistered = false;
150 m_xSlave = xSlave;
153 // ____________________
155 /** XDispatchProviderInterceptor */
156 public synchronized void setMasterDispatchProvider(XDispatchProvider xMaster)
158 System.out.println("Interceptor.setMasterDispatchProvider("+xMaster+") called");
159 m_xMaster = xMaster;
162 // ____________________
164 /** XDispatchProvider
166 public synchronized XDispatch queryDispatch(URL aURL ,
167 String sTargetFrameName,
168 int nSearchFlags )
170 System.out.println("Interceptor.queryDispatch('"+aURL.Complete+"', '"+sTargetFrameName+"', "+nSearchFlags+") called");
172 if (impl_isBlockedURL(aURL.Complete))
174 System.out.println("Interceptor.queryDispatch(): URL blocked => returns NULL");
175 return null;
178 if (m_xSlave != null)
180 System.out.println("Interceptor.queryDispatch(): ask slave ...");
181 return m_xSlave.queryDispatch(aURL, sTargetFrameName, nSearchFlags);
184 System.out.println("Interceptor.queryDispatch(): no idea => returns this");
185 return this;
188 // ____________________
190 /** XDispatchProvider
192 public XDispatch[] queryDispatches(DispatchDescriptor[] lRequests)
194 int i = 0;
195 int c = lRequests.length;
197 XDispatch[] lResults = new XDispatch[c];
198 for (i=0; i<c; ++i)
200 lResults[i] = queryDispatch(lRequests[i].FeatureURL ,
201 lRequests[i].FrameName ,
202 lRequests[i].SearchFlags);
205 return lResults;
208 // ____________________
210 /** XDispatch
212 public synchronized void dispatch(URL aURL ,
213 PropertyValue[] lArguments)
215 System.out.println("Interceptor.dispatch('"+aURL.Complete+"') called");
218 // ____________________
220 /** XDispatch
222 public synchronized void addStatusListener(XStatusListener xListener,
223 com.sun.star.util.URL aURL )
225 System.out.println("Interceptor.addStatusListener(..., '"+aURL.Complete+"') called");
228 // ____________________
230 /** XDispatch
232 public synchronized void removeStatusListener(XStatusListener xListener,
233 com.sun.star.util.URL aURL )
235 System.out.println("Interceptor.removeStatusListener(..., '"+aURL.Complete+"') called");
238 // ____________________
240 public synchronized int getRegistrationCount()
242 return m_nRegistrationCount;
245 // ____________________
247 public synchronized boolean isRegistered()
249 return m_bIsRegistered;
252 // ____________________
254 /** set a new list of URL's, which should be used on registration time
255 (that's why it's neccessary to call this impl-method before the interceptor
256 is used at the OOo API!) to optimize the interception chain.
258 public synchronized void setURLs4InterceptionInfo(String[] lURLs)
260 m_lURLs4InterceptionInfo = lURLs;
263 // ____________________
265 /** set a new list of URL's, which should be blocked by this interceptor.
266 (that's why it's neccessary to call this impl-method before the interceptor
267 is used at the OOo API!)
269 public synchronized void setURLs4URLs4Blocking(String[] lURLs)
271 m_lURLs4Blocking = lURLs;
274 // ____________________
276 /** must be used internal to access the member m_lURLs4InterceptionInfo
277 - threadsafe
278 - and to make sure it's initialized on demand
280 private synchronized String[] impl_getURLs4InterceptionInfo()
282 if (m_lURLs4InterceptionInfo == null)
284 m_lURLs4InterceptionInfo = new String[1];
285 m_lURLs4InterceptionInfo[0] = "*";
288 return m_lURLs4InterceptionInfo;
291 // ____________________
293 /** must be used internal to access the member m_lURLs4Blocking
294 - threadsafe
295 - and to make sure it's initialized on demand
297 private synchronized String[] impl_getURLs4Blocking()
299 if (m_lURLs4Blocking == null)
301 m_lURLs4Blocking = new String[1];
302 m_lURLs4Blocking[0] = "*";
305 return m_lURLs4Blocking;
308 // ____________________
309 private boolean impl_isBlockedURL(String sURL)
311 String[] lBlockedURLs = impl_getURLs4Blocking();
312 int i = 0;
313 int c = lBlockedURLs.length;
315 for (i=0; i<c; ++i)
317 if (impl_match(sURL, lBlockedURLs[i]))
319 return true;
323 return false;
326 // ____________________
328 private boolean impl_match(String sVal1, String sVal2)
330 // TODO implement wildcard match
331 return (sVal1.equals(sVal2));