1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package complex.dispatches;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir // __________ Imports __________
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // structs, const, ...
32*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir // exceptions
35*cdf0e10cSrcweir import com.sun.star.frame.DispatchDescriptor;
36*cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
37*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
38*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProviderInterceptor;
39*cdf0e10cSrcweir import com.sun.star.frame.XInterceptorInfo;
40*cdf0e10cSrcweir import com.sun.star.frame.XStatusListener;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // interfaces
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // helper
46*cdf0e10cSrcweir import com.sun.star.util.URL;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // others
49*cdf0e10cSrcweir //import java.lang.*;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir // __________ Implementation __________
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir /**
54*cdf0e10cSrcweir  * implements a configurable interceptor for dispatch events.
55*cdf0e10cSrcweir  */
56*cdf0e10cSrcweir public class Interceptor implements XDispatchProvider,
57*cdf0e10cSrcweir                                     XDispatch,
58*cdf0e10cSrcweir                                     XDispatchProviderInterceptor,
59*cdf0e10cSrcweir                                     XInterceptorInfo
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir     // ____________________
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /** contains the list of interception URL schema's (wildcards are allowed there!)
64*cdf0e10cSrcweir         supported by this interceptor. It can be set from outside.
65*cdf0e10cSrcweir         If no external URLs are set, the default "*" is used instead.
66*cdf0e10cSrcweir         That would have the same effect as if this implementation would not support the
67*cdf0e10cSrcweir         interface XInterceptorInfo !
68*cdf0e10cSrcweir      */
69*cdf0e10cSrcweir     private String[] m_lURLs4InterceptionInfo = null;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     // ____________________
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     /** These URL's will be blocked by this interceptor.
74*cdf0e10cSrcweir         Can be set from outside. Every queryDispatch() for these
75*cdf0e10cSrcweir         set of URL's will be answered with an empty dispatch object!
76*cdf0e10cSrcweir         If no external URLs are set the default "*" is used instead.
77*cdf0e10cSrcweir         So every incoming URL will be blocked .-)
78*cdf0e10cSrcweir      */
79*cdf0e10cSrcweir     private String[] m_lURLs4Blocking = null;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     // ____________________
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     /** Every dispatch interceptor knows it's master and slave interceptor
84*cdf0e10cSrcweir         of the dispatch chain. These values must be stupid handled .-)
85*cdf0e10cSrcweir         They have to be set and reset in case the right interface methods are called.
86*cdf0e10cSrcweir         Nothing more. It's not allowed to dispose() it.
87*cdf0e10cSrcweir         The slave can be used inside queryDispatch() to forward requests,
88*cdf0e10cSrcweir         which are not handled by this interceptor instance.
89*cdf0e10cSrcweir      */
90*cdf0e10cSrcweir     private XDispatchProvider m_xSlave  = null;
91*cdf0e10cSrcweir     private XDispatchProvider m_xMaster = null;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     // ____________________
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     /** counts calls of setSlave...().
96*cdf0e10cSrcweir         So the outside API test can use this value to know if this interceptor
97*cdf0e10cSrcweir         was realy added to the interceptor chain of OOo.
98*cdf0e10cSrcweir      */
99*cdf0e10cSrcweir     private int m_nRegistrationCount = 0;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     // ____________________
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     /** indicates if this interceptor object is currently part of the interceptor
104*cdf0e10cSrcweir         chain of OOo. Only true if a valid slave or master dispatch is set on this
105*cdf0e10cSrcweir         instance.
106*cdf0e10cSrcweir      */
107*cdf0e10cSrcweir     private boolean m_bIsRegistered = false;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     // ____________________
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     /** ctor
113*cdf0e10cSrcweir      *  It's initialize an object of this class with default values.
114*cdf0e10cSrcweir      */
115*cdf0e10cSrcweir     public Interceptor()
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // ____________________
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /** XInterceptorInfo */
122*cdf0e10cSrcweir     public synchronized String[] getInterceptedURLs()
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         return impl_getURLs4InterceptionInfo();
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     // ____________________
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     /** XDispatchProviderInterceptor */
130*cdf0e10cSrcweir     public synchronized XDispatchProvider getSlaveDispatchProvider()
131*cdf0e10cSrcweir     {
132*cdf0e10cSrcweir         System.out.println("Interceptor.getSlaveDispatchProvider() called");
133*cdf0e10cSrcweir         return m_xSlave;
134*cdf0e10cSrcweir     }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     // ____________________
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     /** XDispatchProviderInterceptor */
139*cdf0e10cSrcweir     public synchronized XDispatchProvider getMasterDispatchProvider()
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         System.out.println("Interceptor.getMasterDispatchProvider() called");
142*cdf0e10cSrcweir         return m_xMaster;
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     // ____________________
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     /** XDispatchProviderInterceptor */
148*cdf0e10cSrcweir     public synchronized void setSlaveDispatchProvider(XDispatchProvider xSlave)
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         System.out.println("Interceptor.setSlaveDispatchProvider("+xSlave+") called");
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         if (xSlave != null)
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             ++m_nRegistrationCount;
155*cdf0e10cSrcweir             m_bIsRegistered = true;
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         else
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             m_bIsRegistered = false;
160*cdf0e10cSrcweir         }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         m_xSlave = xSlave;
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     // ____________________
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     /** XDispatchProviderInterceptor */
168*cdf0e10cSrcweir     public synchronized void setMasterDispatchProvider(XDispatchProvider xMaster)
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         System.out.println("Interceptor.setMasterDispatchProvider("+xMaster+") called");
171*cdf0e10cSrcweir         m_xMaster = xMaster;
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     // ____________________
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /** XDispatchProvider
177*cdf0e10cSrcweir      */
178*cdf0e10cSrcweir     public synchronized XDispatch queryDispatch(URL aURL            ,
179*cdf0e10cSrcweir                                                                    String                sTargetFrameName,
180*cdf0e10cSrcweir                                                                    int                   nSearchFlags    )
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         System.out.println("Interceptor.queryDispatch('"+aURL.Complete+"', '"+sTargetFrameName+"', "+nSearchFlags+") called");
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         if (impl_isBlockedURL(aURL.Complete))
185*cdf0e10cSrcweir         {
186*cdf0e10cSrcweir             System.out.println("Interceptor.queryDispatch(): URL blocked => returns NULL");
187*cdf0e10cSrcweir             return null;
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         if (m_xSlave != null)
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             System.out.println("Interceptor.queryDispatch(): ask slave ...");
193*cdf0e10cSrcweir             return m_xSlave.queryDispatch(aURL, sTargetFrameName, nSearchFlags);
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         System.out.println("Interceptor.queryDispatch(): no idea => returns this");
197*cdf0e10cSrcweir         return this;
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     // ____________________
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     /** XDispatchProvider
203*cdf0e10cSrcweir      */
204*cdf0e10cSrcweir     public XDispatch[] queryDispatches(DispatchDescriptor[] lRequests)
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         int i = 0;
207*cdf0e10cSrcweir         int c = lRequests.length;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         XDispatch[] lResults = new XDispatch[c];
210*cdf0e10cSrcweir         for (i=0; i<c; ++i)
211*cdf0e10cSrcweir         {
212*cdf0e10cSrcweir             lResults[i] = queryDispatch(lRequests[i].FeatureURL ,
213*cdf0e10cSrcweir                                         lRequests[i].FrameName  ,
214*cdf0e10cSrcweir                                         lRequests[i].SearchFlags);
215*cdf0e10cSrcweir         }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         return lResults;
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     // ____________________
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     /** XDispatch
223*cdf0e10cSrcweir      */
224*cdf0e10cSrcweir     public synchronized void dispatch(URL              aURL      ,
225*cdf0e10cSrcweir                                       PropertyValue[] lArguments)
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         System.out.println("Interceptor.dispatch('"+aURL.Complete+"') called");
228*cdf0e10cSrcweir     }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     // ____________________
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir     /** XDispatch
233*cdf0e10cSrcweir      */
234*cdf0e10cSrcweir     public synchronized void addStatusListener(XStatusListener xListener,
235*cdf0e10cSrcweir                                                com.sun.star.util.URL              aURL     )
236*cdf0e10cSrcweir     {
237*cdf0e10cSrcweir         System.out.println("Interceptor.addStatusListener(..., '"+aURL.Complete+"') called");
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     // ____________________
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     /** XDispatch
243*cdf0e10cSrcweir      */
244*cdf0e10cSrcweir     public synchronized void removeStatusListener(XStatusListener xListener,
245*cdf0e10cSrcweir                                                   com.sun.star.util.URL              aURL     )
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         System.out.println("Interceptor.removeStatusListener(..., '"+aURL.Complete+"') called");
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     // ____________________
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     public synchronized int getRegistrationCount()
253*cdf0e10cSrcweir     {
254*cdf0e10cSrcweir         return m_nRegistrationCount;
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     // ____________________
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir     public synchronized boolean isRegistered()
260*cdf0e10cSrcweir     {
261*cdf0e10cSrcweir         return m_bIsRegistered;
262*cdf0e10cSrcweir     }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     // ____________________
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     /** set a new list of URL's, which should be used on registration time
267*cdf0e10cSrcweir         (that's why it's neccessary to call this impl-method before the interceptor
268*cdf0e10cSrcweir         is used at the OOo API!) to optimize the interception chain.
269*cdf0e10cSrcweir      */
270*cdf0e10cSrcweir     public synchronized void setURLs4InterceptionInfo(String[] lURLs)
271*cdf0e10cSrcweir     {
272*cdf0e10cSrcweir         m_lURLs4InterceptionInfo = lURLs;
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     // ____________________
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     /** set a new list of URL's, which should be blocked by this interceptor.
278*cdf0e10cSrcweir         (that's why it's neccessary to call this impl-method before the interceptor
279*cdf0e10cSrcweir         is used at the OOo API!)
280*cdf0e10cSrcweir      */
281*cdf0e10cSrcweir     public synchronized void setURLs4URLs4Blocking(String[] lURLs)
282*cdf0e10cSrcweir     {
283*cdf0e10cSrcweir         m_lURLs4Blocking = lURLs;
284*cdf0e10cSrcweir     }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     // ____________________
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     /** must be used internal to access the member m_lURLs4InterceptionInfo
289*cdf0e10cSrcweir         - threadsafe
290*cdf0e10cSrcweir         - and to make sure it's initialized on demand
291*cdf0e10cSrcweir      */
292*cdf0e10cSrcweir     private synchronized String[] impl_getURLs4InterceptionInfo()
293*cdf0e10cSrcweir     {
294*cdf0e10cSrcweir         if (m_lURLs4InterceptionInfo == null)
295*cdf0e10cSrcweir         {
296*cdf0e10cSrcweir             m_lURLs4InterceptionInfo    = new String[1];
297*cdf0e10cSrcweir             m_lURLs4InterceptionInfo[0] = "*";
298*cdf0e10cSrcweir         }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir         return m_lURLs4InterceptionInfo;
301*cdf0e10cSrcweir     }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir     // ____________________
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir     /** must be used internal to access the member m_lURLs4Blocking
306*cdf0e10cSrcweir         - threadsafe
307*cdf0e10cSrcweir         - and to make sure it's initialized on demand
308*cdf0e10cSrcweir      */
309*cdf0e10cSrcweir     private synchronized String[] impl_getURLs4Blocking()
310*cdf0e10cSrcweir     {
311*cdf0e10cSrcweir         if (m_lURLs4Blocking == null)
312*cdf0e10cSrcweir         {
313*cdf0e10cSrcweir             m_lURLs4Blocking    = new String[1];
314*cdf0e10cSrcweir             m_lURLs4Blocking[0] = "*";
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         return m_lURLs4Blocking;
318*cdf0e10cSrcweir     }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     // ____________________
321*cdf0e10cSrcweir     private boolean impl_isBlockedURL(String sURL)
322*cdf0e10cSrcweir     {
323*cdf0e10cSrcweir         String[] lBlockedURLs = impl_getURLs4Blocking();
324*cdf0e10cSrcweir         int      i            = 0;
325*cdf0e10cSrcweir         int      c            = lBlockedURLs.length;
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir         for (i=0; i<c; ++i)
328*cdf0e10cSrcweir         {
329*cdf0e10cSrcweir             if (impl_match(sURL, lBlockedURLs[i]))
330*cdf0e10cSrcweir             {
331*cdf0e10cSrcweir                 return true;
332*cdf0e10cSrcweir             }
333*cdf0e10cSrcweir         }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir         return false;
336*cdf0e10cSrcweir     }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir     // ____________________
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir     private boolean impl_match(String sVal1, String sVal2)
341*cdf0e10cSrcweir     {
342*cdf0e10cSrcweir         // TODO implement wildcard match
343*cdf0e10cSrcweir         return (sVal1.equals(sVal2));
344*cdf0e10cSrcweir     }
345*cdf0e10cSrcweir }
346