1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <jobs/joburl.hxx>
21 #include <threadhelp/readguard.hxx>
22 #include <threadhelp/writeguard.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <vcl/svapp.hxx>
32 @descr It initialize this new instance with a (hopyfully) valid job URL.
33 This URL will be parsed. After that we set our members right,
34 so other interface methods of this class can be used to get
35 all items of this URL. Of course it will be possible to know,
36 if this URL was valid too.
39 the job URL for parsing
41 JobURL::JobURL( /*IN*/ const OUString
& sURL
)
42 : ThreadHelpBase( &Application::GetSolarMutex() )
44 #ifdef ENABLE_COMPONENT_SELF_CHECK
45 JobURL::impldbg_checkIt();
48 m_eRequest
= E_UNKNOWN
;
50 // syntax: vnd.sun.star.job:{[event=<name>],[alias=<name>],[service=<name>]}
52 // check for "vnd.sun.star.job:"
53 if (sURL
.matchIgnoreAsciiCaseAsciiL(JOBURL_PROTOCOL_STR
,JOBURL_PROTOCOL_LEN
,0))
55 sal_Int32 t
= JOBURL_PROTOCOL_LEN
;
58 // separate all token of "{[event=<name>],[alias=<name>],[service=<name>]}"
59 OUString sToken
= sURL
.getToken(0, JOBURL_PART_SEPARATOR
, t
);
61 OUString sPartArguments
;
65 (JobURL::implst_split(sToken
,JOBURL_EVENT_STR
,JOBURL_EVENT_LEN
,sPartValue
,sPartArguments
)) &&
66 (!sPartValue
.isEmpty())
70 m_sEvent
= sPartValue
;
71 m_sEventArgs
= sPartArguments
;
72 m_eRequest
|= E_EVENT
;
77 (JobURL::implst_split(sToken
,JOBURL_ALIAS_STR
,JOBURL_ALIAS_LEN
,sPartValue
,sPartArguments
)) &&
78 (!sPartValue
.isEmpty())
82 m_sAlias
= sPartValue
;
83 m_sAliasArgs
= sPartArguments
;
84 m_eRequest
|= E_ALIAS
;
87 // check for "service="
89 (JobURL::implst_split(sToken
,JOBURL_SERVICE_STR
,JOBURL_SERVICE_LEN
,sPartValue
,sPartArguments
)) &&
90 (!sPartValue
.isEmpty())
94 m_sService
= sPartValue
;
95 m_sServiceArgs
= sPartArguments
;
96 m_eRequest
|= E_SERVICE
;
103 //________________________________
105 @short knows, if this job URL object hold a valid URL inside
107 @return <TRUE/> if it represent a valid job URL.
109 sal_Bool
JobURL::isValid() const
112 ReadGuard
aReadLock(m_aLock
);
113 return (m_eRequest
!=E_UNKNOWN
);
116 //________________________________
118 @short get the event item of this job URL
119 @descr Because the three possible parts of such URL (event, alias, service)
120 can't be combined, this method can(!) return a valid value - but it's
121 not a must. Thats why the return value must be used too, to detect a missing
125 returns the possible existing event value
126 e.g. "vnd.sun.star.job:event=myEvent" returns "myEvent"
128 @return <TRUE/> if an event part of the job URL exist and the out parameter
131 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
133 sal_Bool
JobURL::getEvent( /*OUT*/ OUString
& sEvent
) const
136 ReadGuard
aReadLock(m_aLock
);
139 sal_Bool bSet
= ((m_eRequest
& E_EVENT
) == E_EVENT
);
149 //________________________________
151 @short get the alias item of this job URL
152 @descr Because the three possible parts of such URL (event, alias, service)
153 can't be combined, this method can(!) return a valid value - but it's
154 not a must. Thats why the return value must be used too, to detect a missing
158 returns the possible existing alias value
159 e.g. "vnd.sun.star.job:alias=myAlias" returns "myAlias"
161 @return <TRUE/> if an alias part of the job URL exist and the out parameter
164 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
166 sal_Bool
JobURL::getAlias( /*OUT*/ OUString
& sAlias
) const
169 ReadGuard
aReadLock(m_aLock
);
172 sal_Bool bSet
= ((m_eRequest
& E_ALIAS
) == E_ALIAS
);
182 //________________________________
184 @short get the service item of this job URL
185 @descr Because the three possible parts of such URL (event, service, service)
186 can't be combined, this method can(!) return a valid value - but it's
187 not a must. Thats why the return value must be used too, to detect a missing
191 returns the possible existing service value
192 e.g. "vnd.sun.star.job:service=com.sun.star.Service" returns "com.sun.star.Service"
194 @return <TRUE/> if an service part of the job URL exist and the out parameter
197 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
199 sal_Bool
JobURL::getService( /*OUT*/ OUString
& sService
) const
202 ReadGuard
aReadLock(m_aLock
);
204 sService
= OUString();
205 sal_Bool bSet
= ((m_eRequest
& E_SERVICE
) == E_SERVICE
);
207 sService
= m_sService
;
215 //________________________________
217 @short searches for a special identifier in the given string and split it
218 @descr If the given identifier could be found at the beginning of the given string,
219 this method split it into different parts and return it.
220 Following schema is used: <partidentifier>=<partvalue>[?<partarguments>]
223 the string, which should be analyzed
225 @param pPartIdentifier
226 the part identifier value, which must be found at the beginning of the
227 parameter <var>sPart</var>
230 the length of the ascii value <var>pPartIdentifier</var>
233 returns the part value if <var>sPart</var> was splitted successfully
235 @param rPartArguments
236 returns the part arguments if <var>sPart</var> was splitted successfully
238 @return <TRUE/> if the identifier could be found and the string was splitted.
241 sal_Bool
JobURL::implst_split( /*IN*/ const OUString
& sPart
,
242 /*IN*/ const sal_Char
* pPartIdentifier
,
243 /*IN*/ sal_Int32 nPartLength
,
244 /*OUT*/ OUString
& rPartValue
,
245 /*OUT*/ OUString
& rPartArguments
)
247 // first search for the given identifier
248 sal_Bool bPartFound
= (sPart
.matchIgnoreAsciiCaseAsciiL(pPartIdentifier
,nPartLength
,0));
250 // If it exist - we can split the part and return sal_True.
251 // Otherwhise we do nothing and return sal_False.
254 // But may the part has optional arguments - separated by a "?".
255 // Do so - we set the return value with the whole part string.
256 // Arguments will be set to an empty string as default.
257 // If we detect the right sign - we split the arguments and overwrite the default.
258 OUString sValueAndArguments
= sPart
.copy(nPartLength
);
259 OUString sValue
= sValueAndArguments
;
262 sal_Int32 nArgStart
= sValueAndArguments
.indexOf('?',0);
265 sValue
= sValueAndArguments
.copy(0,nArgStart
);
266 ++nArgStart
; // ignore '?'!
267 sArguments
= sValueAndArguments
.copy(nArgStart
);
270 rPartValue
= sValue
;
271 rPartArguments
= sArguments
;
277 //________________________________
279 @short special debug method
280 @descr It's the entry point method to start a self component check for this class.
281 It's used for internal purposes only and never a part of a legal product.
282 Use it for testing and debug only!
284 #ifdef ENABLE_COMPONENT_SELF_CHECK
286 #define LOGFILE_JOBURL "joburl.log"
288 void JobURL::impldbg_checkIt()
290 // check simple URL's
291 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent" , E_EVENT
, "onMyEvent", "" , "" , NULL
, NULL
, NULL
);
292 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=myAlias" , E_ALIAS
, "" , "myAlias", "" , NULL
, NULL
, NULL
);
293 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service", E_SERVICE
, "" , "" , "css.Service", NULL
, NULL
, NULL
);
294 JobURL::impldbg_checkURL("vnd.sun.star.job:service=;" , E_UNKNOWN
, "" , "" , "" , NULL
, NULL
, NULL
);
296 // check combinations
297 // Note: No additional spaces or tabs are allowed after a separator occurred.
298 // Tab and spaces before a separator will be used as value!
299 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent;alias=myAlias;service=css.Service" , E_EVENT
| E_ALIAS
| E_SERVICE
, "onMyEvent", "myAlias", "css.Service" , NULL
, NULL
, NULL
);
300 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service;alias=myAlias" , E_ALIAS
| E_SERVICE
, "" , "myAlias", "css.Service" , NULL
, NULL
, NULL
);
301 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service ;alias=myAlias" , E_ALIAS
| E_SERVICE
, "" , "myAlias", "css.Service ", NULL
, NULL
, NULL
);
302 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service; alias=myAlias" , E_UNKNOWN
, "" , "" , "" , NULL
, NULL
, NULL
);
303 JobURL::impldbg_checkURL("vnd.sun.star.job : event=onMyEvent" , E_UNKNOWN
, "" , "" , "" , NULL
, NULL
, NULL
);
304 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent;event=onMyEvent;service=css.Service", E_UNKNOWN
, "" , "" , "" , NULL
, NULL
, NULL
);
306 // check upper/lower case
307 // fix parts of the URL are case insensitive (e.g. "vnd.SUN.star.job:eVEnt=")
308 // values are case sensitive (e.g. "myAlias" )
309 JobURL::impldbg_checkURL("vnd.SUN.star.job:eVEnt=onMyEvent;aliAs=myAlias;serVice=css.Service", E_EVENT
| E_ALIAS
| E_SERVICE
, "onMyEvent", "myAlias", "css.Service" , NULL
, NULL
, NULL
);
310 JobURL::impldbg_checkURL("vnd.SUN.star.job:eVEnt=onMyEVENT;aliAs=myALIAS;serVice=css.SERVICE", E_EVENT
| E_ALIAS
| E_SERVICE
, "onMyEVENT", "myALIAS", "css.SERVICE" , NULL
, NULL
, NULL
);
313 JobURL::impldbg_checkURL("vnd.sun.star.jobs:service=css.Service" , E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
314 JobURL::impldbg_checkURL("vnd.sun.star.job service=css.Service" , E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
315 JobURL::impldbg_checkURL("vnd.sun.star.job:service;css.Service" , E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
316 JobURL::impldbg_checkURL("vnd.sun.star.job:service;" , E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
317 JobURL::impldbg_checkURL("vnd.sun.star.job:;alias;service;event=" , E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
318 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=a;service=s;event=", E_UNKNOWN
, "", "", "", NULL
, NULL
, NULL
);
320 // check argument handling
321 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent?eventArg1,eventArg2=3,eventArg4," , E_EVENT
, "onMyEvent", "" , "" , "eventArg1,eventArg2=3,eventArg4,", NULL
, NULL
);
322 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=myAlias?aliasArg1,aliasarg2" , E_EVENT
, "" , "myAlias", "" , NULL
, "aliasArg1,aliasarg2", NULL
);
323 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.myService?serviceArg1" , E_EVENT
, "" , "" , "css.myService", NULL
, NULL
, "serviceArg1" );
324 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.myService?serviceArg1;alias=myAlias?aliasArg=564", E_EVENT
| E_ALIAS
, "" , "myAlias", "css.myService", NULL
, "aliasArg=564" , "serviceArg1" );
327 //________________________________
329 @short helper debug method
330 @descr It uses the given parameter to create a new instance of a JobURL.
331 They results will be compared with the exepected ones.
332 The a log will be written, which contains some detailed information
336 the job URL, which should be checked
341 @param pExpectedEvent
342 the expected event value
344 @param pExpectedAlias
345 the expected alias value
347 @param pExpectedService
348 the expected service value
350 @param pExpectedEventArgs
351 the expected event arguments
353 @param pExpectedAliasArgs
354 the expected alias arguments
356 @param pExpectedServiceArgs
357 the expected service arguments
359 void JobURL::impldbg_checkURL( /*IN*/ const sal_Char
* pURL
,
360 /*IN*/ sal_uInt32 eExpectedPart
,
361 /*IN*/ const sal_Char
* pExpectedEvent
,
362 /*IN*/ const sal_Char
* pExpectedAlias
,
363 /*IN*/ const sal_Char
* pExpectedService
,
364 /*IN*/ const sal_Char
* pExpectedEventArgs
,
365 /*IN*/ const sal_Char
* pExpectedAliasArgs
,
366 /*IN*/ const sal_Char
* pExpectedServiceArgs
)
371 OUString sEventArgs
;
372 OUString sAliasArgs
;
373 OUString sServiceArgs
;
374 OUString
sURL (OUString::createFromAscii(pURL
));
375 sal_Bool bOK
= sal_True
;
379 // check if URL is invalid
380 if (eExpectedPart
==E_UNKNOWN
)
381 bOK
= !aURL
.isValid();
383 // check if URL has the expected event part
386 ((eExpectedPart
& E_EVENT
) == E_EVENT
)
391 (aURL
.getEvent(sEvent
) ) &&
392 (!sEvent
.isEmpty() ) &&
393 (sEvent
.compareToAscii(pExpectedEvent
)==0)
396 if (bOK
&& pExpectedEventArgs
!=NULL
)
399 (aURL
.getEventArgs(sEventArgs
) ) &&
400 (sEventArgs
.compareToAscii(pExpectedEventArgs
)==0)
405 // check if URL has no event part
408 ((eExpectedPart
& E_EVENT
) != E_EVENT
)
412 (!aURL
.getEvent(sEvent
) ) &&
413 (sEvent
.isEmpty() ) &&
414 (!aURL
.getEventArgs(sEventArgs
)) &&
415 (sEventArgs
.isEmpty() )
419 // check if URL has the expected alias part
422 ((eExpectedPart
& E_ALIAS
) == E_ALIAS
)
427 (aURL
.getAlias(sAlias
) ) &&
428 (!sAlias
.isEmpty() ) &&
429 (sAlias
.compareToAscii(pExpectedAlias
)==0)
432 if (bOK
&& pExpectedAliasArgs
!=NULL
)
435 (aURL
.getAliasArgs(sAliasArgs
) ) &&
436 (sAliasArgs
.compareToAscii(pExpectedAliasArgs
)==0)
441 // check if URL has the no alias part
444 ((eExpectedPart
& E_ALIAS
) != E_ALIAS
)
448 (!aURL
.getAlias(sAlias
) ) &&
449 (sAlias
.isEmpty() ) &&
450 (!aURL
.getAliasArgs(sAliasArgs
)) &&
451 (sAliasArgs
.isEmpty() )
455 // check if URL has the expected service part
458 ((eExpectedPart
& E_SERVICE
) == E_SERVICE
)
463 (aURL
.getService(sService
) ) &&
464 (!sService
.isEmpty() ) &&
465 (sService
.compareToAscii(pExpectedService
)==0)
468 if (bOK
&& pExpectedServiceArgs
!=NULL
)
471 (aURL
.getServiceArgs(sServiceArgs
) ) &&
472 (sServiceArgs
.compareToAscii(pExpectedServiceArgs
)==0)
477 // check if URL has the no service part
480 ((eExpectedPart
& E_SERVICE
) != E_SERVICE
)
484 (!aURL
.getService(sService
) ) &&
485 (sService
.isEmpty() ) &&
486 (!aURL
.getServiceArgs(sServiceArgs
)) &&
487 (sServiceArgs
.isEmpty() )
491 OUStringBuffer
sMsg(256);
493 sMsg
.appendAscii("\"" );
495 sMsg
.appendAscii("\" ");
499 sMsg
.appendAscii("... OK\n");
503 sMsg
.appendAscii("... failed\n");
504 sMsg
.appendAscii("expected was: ");
505 if (eExpectedPart
==E_UNKNOWN
)
506 sMsg
.appendAscii("E_UNKNOWN");
507 if ((eExpectedPart
& E_EVENT
) == E_EVENT
)
509 sMsg
.appendAscii("| E_EVENT e=\"");
510 sMsg
.appendAscii(pExpectedEvent
);
511 sMsg
.appendAscii("\"" );
513 if ((eExpectedPart
& E_ALIAS
) == E_ALIAS
)
515 sMsg
.appendAscii("| E_ALIAS a=\"");
516 sMsg
.appendAscii(pExpectedAlias
);
517 sMsg
.appendAscii("\"" );
519 if ((eExpectedPart
& E_SERVICE
) == E_SERVICE
)
521 sMsg
.appendAscii("| E_SERVICE s=\"");
522 sMsg
.appendAscii(pExpectedService
);
523 sMsg
.appendAscii("\"" );
525 sMsg
.appendAscii("\tbut it was : " );
526 sMsg
.append (aURL
.impldbg_toString());
527 sMsg
.appendAscii("\n" );
530 WRITE_LOGFILE(LOGFILE_JOBURL
, U2B(sMsg
.makeStringAndClear()))
533 //________________________________
535 @short helper debug method
536 @descr It returns a representation of the internal object state
539 @returns The formated string representation.
541 OUString
JobURL::impldbg_toString() const
544 ReadGuard
aReadLock(m_aLock
);
546 OUStringBuffer
sBuffer(256);
548 if (m_eRequest
==E_UNKNOWN
)
549 sBuffer
.appendAscii("E_UNKNOWN");
550 if ((m_eRequest
& E_EVENT
) == E_EVENT
)
551 sBuffer
.appendAscii("| E_EVENT");
552 if ((m_eRequest
& E_ALIAS
) == E_ALIAS
)
553 sBuffer
.appendAscii("| E_ALIAS");
554 if ((m_eRequest
& E_SERVICE
) == E_SERVICE
)
555 sBuffer
.appendAscii("| E_SERVICE");
556 sBuffer
.appendAscii("{ e=\"" );
557 sBuffer
.append (m_sEvent
);
558 sBuffer
.appendAscii("\" - a=\"");
559 sBuffer
.append (m_sAlias
);
560 sBuffer
.appendAscii("\" - s=\"");
561 sBuffer
.append (m_sService
);
562 sBuffer
.appendAscii("\" }" );
567 return sBuffer
.makeStringAndClear();
570 //________________________________
572 sal_Bool
JobURL::getServiceArgs( /*OUT*/ OUString
& sServiceArgs
) const
575 ReadGuard
aReadLock(m_aLock
);
577 sServiceArgs
= OUString();
578 sal_Bool bSet
= ((m_eRequest
& E_SERVICE
) == E_SERVICE
);
580 sServiceArgs
= m_sServiceArgs
;
588 //________________________________
590 sal_Bool
JobURL::getEventArgs( /*OUT*/ OUString
& sEventArgs
) const
593 ReadGuard
aReadLock(m_aLock
);
595 sEventArgs
= OUString();
596 sal_Bool bSet
= ((m_eRequest
& E_EVENT
) == E_EVENT
);
598 sEventArgs
= m_sEventArgs
;
606 //________________________________
608 sal_Bool
JobURL::getAliasArgs( /*OUT*/ OUString
& sAliasArgs
) const
611 ReadGuard
aReadLock(m_aLock
);
613 sAliasArgs
= OUString();
614 sal_Bool bSet
= ((m_eRequest
& E_ALIAS
) == E_ALIAS
);
616 sAliasArgs
= m_sAliasArgs
;
624 #endif // ENABLE_COMPONENT_SELF_CHECK
626 } // namespace framework
628 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */