merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / jobs / joburl.cxx
blob59e6384a0e937175f021ae58b0a9cf0c0eaf9112
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 //________________________________
32 // my own includes
33 #include <jobs/joburl.hxx>
34 #include <threadhelp/readguard.hxx>
35 #include <threadhelp/writeguard.hxx>
36 #include <general.h>
38 //________________________________
39 // interface includes
41 //________________________________
42 // includes of other projects
43 #include <rtl/ustrbuf.hxx>
44 #include <vcl/svapp.hxx>
46 //________________________________
47 // namespace
49 namespace framework{
51 //________________________________
52 // non exported const
54 //________________________________
55 // non exported definitions
57 //________________________________
58 // declarations
60 //________________________________
61 /**
62 @short special ctor
63 @descr It initialize this new instance with a (hopyfully) valid job URL.
64 This URL will be parsed. After that we set our members right,
65 so other interface methods of this class can be used to get
66 all items of this URL. Of course it will be possible to know,
67 if this URL was valid too.
69 @param sURL
70 the job URL for parsing
72 JobURL::JobURL( /*IN*/ const ::rtl::OUString& sURL )
73 : ThreadHelpBase( &Application::GetSolarMutex() )
75 #ifdef ENABLE_COMPONENT_SELF_CHECK
76 JobURL::impldbg_checkIt();
77 #endif
79 m_eRequest = E_UNKNOWN;
81 // syntax: vnd.sun.star.job:{[event=<name>],[alias=<name>],[service=<name>]}
83 // check for "vnd.sun.star.job:"
84 if (sURL.matchIgnoreAsciiCaseAsciiL(JOBURL_PROTOCOL_STR,JOBURL_PROTOCOL_LEN,0))
86 sal_Int32 t = JOBURL_PROTOCOL_LEN;
89 // seperate all token of "{[event=<name>],[alias=<name>],[service=<name>]}"
90 ::rtl::OUString sToken = sURL.getToken(0, JOBURL_PART_SEPERATOR, t);
91 ::rtl::OUString sPartValue ;
92 ::rtl::OUString sPartArguments;
94 // check for "event="
95 if (
96 (JobURL::implst_split(sToken,JOBURL_EVENT_STR,JOBURL_EVENT_LEN,sPartValue,sPartArguments)) &&
97 (sPartValue.getLength()>0 )
100 // set the part value
101 m_sEvent = sPartValue ;
102 m_sEventArgs = sPartArguments;
103 m_eRequest |= E_EVENT ;
105 else
106 // check for "alias="
107 if (
108 (JobURL::implst_split(sToken,JOBURL_ALIAS_STR,JOBURL_ALIAS_LEN,sPartValue,sPartArguments)) &&
109 (sPartValue.getLength()>0 )
112 // set the part value
113 m_sAlias = sPartValue ;
114 m_sAliasArgs = sPartArguments;
115 m_eRequest |= E_ALIAS ;
117 else
118 // check for "service="
119 if (
120 (JobURL::implst_split(sToken,JOBURL_SERVICE_STR,JOBURL_SERVICE_LEN,sPartValue,sPartArguments)) &&
121 (sPartValue.getLength()>0 )
124 // set the part value
125 m_sService = sPartValue ;
126 m_sServiceArgs = sPartArguments;
127 m_eRequest |= E_SERVICE ;
130 while(t!=-1);
134 //________________________________
136 @short knows, if this job URL object hold a valid URL inside
138 @return <TRUE/> if it represent a valid job URL.
140 sal_Bool JobURL::isValid() const
142 /* SAFE { */
143 ReadGuard aReadLock(m_aLock);
144 return (m_eRequest!=E_UNKNOWN);
147 //________________________________
149 @short get the event item of this job URL
150 @descr Because the three possible parts of such URL (event, alias, service)
151 can't be combined, this method can(!) return a valid value - but it's
152 not a must. Thats why the return value must be used too, to detect a missing
153 event value.
155 @param sEvent
156 returns the possible existing event value
157 e.g. "vnd.sun.star.job:event=myEvent" returns "myEvent"
159 @return <TRUE/> if an event part of the job URL exist and the out parameter
160 sEvent was filled.
162 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
164 sal_Bool JobURL::getEvent( /*OUT*/ ::rtl::OUString& sEvent ) const
166 /* SAFE { */
167 ReadGuard aReadLock(m_aLock);
169 sEvent = ::rtl::OUString();
170 sal_Bool bSet = ((m_eRequest & E_EVENT) == E_EVENT);
171 if (bSet)
172 sEvent = m_sEvent;
174 aReadLock.unlock();
175 /* } SAFE */
177 return bSet;
180 //________________________________
182 @short get the alias item of this job URL
183 @descr Because the three possible parts of such URL (event, alias, service)
184 can't be combined, this method can(!) return a valid value - but it's
185 not a must. Thats why the return value must be used too, to detect a missing
186 alias value.
188 @param sAlias
189 returns the possible existing alias value
190 e.g. "vnd.sun.star.job:alias=myAlias" returns "myAlias"
192 @return <TRUE/> if an alias part of the job URL exist and the out parameter
193 sAlias was filled.
195 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
197 sal_Bool JobURL::getAlias( /*OUT*/ ::rtl::OUString& sAlias ) const
199 /* SAFE { */
200 ReadGuard aReadLock(m_aLock);
202 sAlias = ::rtl::OUString();
203 sal_Bool bSet = ((m_eRequest & E_ALIAS) == E_ALIAS);
204 if (bSet)
205 sAlias = m_sAlias;
207 aReadLock.unlock();
208 /* } SAFE */
210 return bSet;
213 //________________________________
215 @short get the service item of this job URL
216 @descr Because the three possible parts of such URL (event, service, service)
217 can't be combined, this method can(!) return a valid value - but it's
218 not a must. Thats why the return value must be used too, to detect a missing
219 service value.
221 @param sAlias
222 returns the possible existing service value
223 e.g. "vnd.sun.star.job:service=com.sun.star.Service" returns "com.sun.star.Service"
225 @return <TRUE/> if an service part of the job URL exist and the out parameter
226 sService was filled.
228 @attention The out parameter will be reseted everytime. Don't use it if method returns <FALSE/>!
230 sal_Bool JobURL::getService( /*OUT*/ ::rtl::OUString& sService ) const
232 /* SAFE { */
233 ReadGuard aReadLock(m_aLock);
235 sService = ::rtl::OUString();
236 sal_Bool bSet = ((m_eRequest & E_SERVICE) == E_SERVICE);
237 if (bSet)
238 sService = m_sService;
240 aReadLock.unlock();
241 /* } SAFE */
243 return bSet;
246 //________________________________
248 @short searches for a special identifier in the given string and split it
249 @descr If the given identifier could be found at the beginning of the given string,
250 this method split it into different parts and return it.
251 Following schema is used: <partidentifier>=<partvalue>[?<partarguments>]
253 @param sPart
254 the string, which should be analyzed
256 @param pPartIdentifier
257 the part identifier value, which must be found at the beginning of the
258 parameter <var>sPart</var>
260 @param nPartLength
261 the length of the ascii value <var>pPartIdentifier</var>
263 @param rPartValue
264 returns the part value if <var>sPart</var> was splitted successfully
266 @param rPartArguments
267 returns the part arguments if <var>sPart</var> was splitted successfully
269 @return <TRUE/> if the identifier could be found and the string was splitted.
270 <FALSE/> otherwhise.
272 sal_Bool JobURL::implst_split( /*IN*/ const ::rtl::OUString& sPart ,
273 /*IN*/ const sal_Char* pPartIdentifier ,
274 /*IN*/ sal_Int32 nPartLength ,
275 /*OUT*/ ::rtl::OUString& rPartValue ,
276 /*OUT*/ ::rtl::OUString& rPartArguments )
278 // first search for the given identifier
279 sal_Bool bPartFound = (sPart.matchIgnoreAsciiCaseAsciiL(pPartIdentifier,nPartLength,0));
281 // If it exist - we can split the part and return TRUE.
282 // Otherwhise we do nothing and return FALSE.
283 if (bPartFound)
285 // But may the part has optional arguments - seperated by a "?".
286 // Do so - we set the return value with the whole part string.
287 // Arguments will be set to an empty string as default.
288 // If we detect the right sign - we split the arguments and overwrite the default.
289 ::rtl::OUString sValueAndArguments = sPart.copy(nPartLength);
290 ::rtl::OUString sValue = sValueAndArguments ;
291 ::rtl::OUString sArguments;
293 sal_Int32 nArgStart = sValueAndArguments.indexOf('?',0);
294 if (nArgStart!=-1)
296 sValue = sValueAndArguments.copy(0,nArgStart);
297 ++nArgStart; // ignore '?'!
298 sArguments = sValueAndArguments.copy(nArgStart);
301 rPartValue = sValue ;
302 rPartArguments = sArguments;
305 return bPartFound;
308 //________________________________
310 @short special debug method
311 @descr It's the entry point method to start a self component check for this class.
312 It's used for internal purposes only and never a part of a legal product.
313 Use it for testing and debug only!
315 #ifdef ENABLE_COMPONENT_SELF_CHECK
317 #define LOGFILE_JOBURL "joburl.log"
319 void JobURL::impldbg_checkIt()
321 // check simple URL's
322 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent" , E_EVENT , "onMyEvent", "" , "" , NULL, NULL, NULL);
323 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=myAlias" , E_ALIAS , "" , "myAlias", "" , NULL, NULL, NULL);
324 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service", E_SERVICE, "" , "" , "css.Service", NULL, NULL, NULL);
325 JobURL::impldbg_checkURL("vnd.sun.star.job:service=;" , E_UNKNOWN, "" , "" , "" , NULL, NULL, NULL);
327 // check combinations
328 // Note: No additional spaces or tabs are allowed after a seperator occured.
329 // Tab and spaces before a seperator will be used as value!
330 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);
331 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service;alias=myAlias" , E_ALIAS | E_SERVICE , "" , "myAlias", "css.Service" , NULL, NULL, NULL);
332 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service ;alias=myAlias" , E_ALIAS | E_SERVICE , "" , "myAlias", "css.Service ", NULL, NULL, NULL);
333 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.Service; alias=myAlias" , E_UNKNOWN , "" , "" , "" , NULL, NULL, NULL);
334 JobURL::impldbg_checkURL("vnd.sun.star.job : event=onMyEvent" , E_UNKNOWN , "" , "" , "" , NULL, NULL, NULL);
335 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent;event=onMyEvent;service=css.Service", E_UNKNOWN , "" , "" , "" , NULL, NULL, NULL);
337 // check upper/lower case
338 // fix parts of the URL are case insensitive (e.g. "vnd.SUN.star.job:eVEnt=")
339 // values are case sensitive (e.g. "myAlias" )
340 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);
341 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);
343 // check stupid URLs
344 JobURL::impldbg_checkURL("vnd.sun.star.jobs:service=css.Service" , E_UNKNOWN, "", "", "", NULL, NULL, NULL);
345 JobURL::impldbg_checkURL("vnd.sun.star.job service=css.Service" , E_UNKNOWN, "", "", "", NULL, NULL, NULL);
346 JobURL::impldbg_checkURL("vnd.sun.star.job:service;css.Service" , E_UNKNOWN, "", "", "", NULL, NULL, NULL);
347 JobURL::impldbg_checkURL("vnd.sun.star.job:service;" , E_UNKNOWN, "", "", "", NULL, NULL, NULL);
348 JobURL::impldbg_checkURL("vnd.sun.star.job:;alias;service;event=" , E_UNKNOWN, "", "", "", NULL, NULL, NULL);
349 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=a;service=s;event=", E_UNKNOWN, "", "", "", NULL, NULL, NULL);
351 // check argument handling
352 JobURL::impldbg_checkURL("vnd.sun.star.job:event=onMyEvent?eventArg1,eventArg2=3,eventArg4," , E_EVENT , "onMyEvent", "" , "" , "eventArg1,eventArg2=3,eventArg4,", NULL , NULL );
353 JobURL::impldbg_checkURL("vnd.sun.star.job:alias=myAlias?aliasArg1,aliasarg2" , E_EVENT , "" , "myAlias", "" , NULL , "aliasArg1,aliasarg2", NULL );
354 JobURL::impldbg_checkURL("vnd.sun.star.job:service=css.myService?serviceArg1" , E_EVENT , "" , "" , "css.myService", NULL , NULL , "serviceArg1" );
355 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" );
358 //________________________________
360 @short helper debug method
361 @descr It uses the given parameter to create a new instance of a JobURL.
362 They results will be compared with the exepected ones.
363 The a log will be written, which contains some detailed informations
364 for this sub test.
366 @param pURL
367 the job URL, which should be checked
369 @param eExpectedPart
370 the expected result
372 @param pExpectedEvent
373 the expected event value
375 @param pExpectedAlias
376 the expected alias value
378 @param pExpectedService
379 the expected service value
381 @param pExpectedEventArgs
382 the expected event arguments
384 @param pExpectedAliasArgs
385 the expected alias arguments
387 @param pExpectedServiceArgs
388 the expected service arguments
390 void JobURL::impldbg_checkURL( /*IN*/ const sal_Char* pURL ,
391 /*IN*/ sal_uInt32 eExpectedPart ,
392 /*IN*/ const sal_Char* pExpectedEvent ,
393 /*IN*/ const sal_Char* pExpectedAlias ,
394 /*IN*/ const sal_Char* pExpectedService ,
395 /*IN*/ const sal_Char* pExpectedEventArgs ,
396 /*IN*/ const sal_Char* pExpectedAliasArgs ,
397 /*IN*/ const sal_Char* pExpectedServiceArgs )
399 ::rtl::OUString sEvent ;
400 ::rtl::OUString sAlias ;
401 ::rtl::OUString sService ;
402 ::rtl::OUString sEventArgs ;
403 ::rtl::OUString sAliasArgs ;
404 ::rtl::OUString sServiceArgs;
405 ::rtl::OUString sURL (::rtl::OUString::createFromAscii(pURL));
406 sal_Bool bOK = sal_True;
408 JobURL aURL(sURL);
410 // check if URL is invalid
411 if (eExpectedPart==E_UNKNOWN)
412 bOK = !aURL.isValid();
414 // check if URL has the expected event part
415 if (
416 (bOK ) &&
417 ((eExpectedPart & E_EVENT) == E_EVENT)
420 bOK = (
421 (aURL.isValid() ) &&
422 (aURL.getEvent(sEvent) ) &&
423 (sEvent.getLength()>0 ) &&
424 (sEvent.compareToAscii(pExpectedEvent)==0)
427 if (bOK && pExpectedEventArgs!=NULL)
429 bOK = (
430 (aURL.getEventArgs(sEventArgs) ) &&
431 (sEventArgs.compareToAscii(pExpectedEventArgs)==0)
436 // check if URL has no event part
437 if (
438 (bOK ) &&
439 ((eExpectedPart & E_EVENT) != E_EVENT)
442 bOK = (
443 (!aURL.getEvent(sEvent) ) &&
444 (sEvent.getLength()==0 ) &&
445 (!aURL.getEventArgs(sEventArgs)) &&
446 (sEventArgs.getLength()==0 )
450 // check if URL has the expected alias part
451 if (
452 (bOK ) &&
453 ((eExpectedPart & E_ALIAS) == E_ALIAS)
456 bOK = (
457 (aURL.isValid() ) &&
458 (aURL.getAlias(sAlias) ) &&
459 (sAlias.getLength()>0 ) &&
460 (sAlias.compareToAscii(pExpectedAlias)==0)
463 if (bOK && pExpectedAliasArgs!=NULL)
465 bOK = (
466 (aURL.getAliasArgs(sAliasArgs) ) &&
467 (sAliasArgs.compareToAscii(pExpectedAliasArgs)==0)
472 // check if URL has the no alias part
473 if (
474 (bOK ) &&
475 ((eExpectedPart & E_ALIAS) != E_ALIAS)
478 bOK = (
479 (!aURL.getAlias(sAlias) ) &&
480 (sAlias.getLength()==0 ) &&
481 (!aURL.getAliasArgs(sAliasArgs)) &&
482 (sAliasArgs.getLength()==0 )
486 // check if URL has the expected service part
487 if (
488 (bOK ) &&
489 ((eExpectedPart & E_SERVICE) == E_SERVICE)
492 bOK = (
493 (aURL.isValid() ) &&
494 (aURL.getService(sService) ) &&
495 (sService.getLength()>0 ) &&
496 (sService.compareToAscii(pExpectedService)==0)
499 if (bOK && pExpectedServiceArgs!=NULL)
501 bOK = (
502 (aURL.getServiceArgs(sServiceArgs) ) &&
503 (sServiceArgs.compareToAscii(pExpectedServiceArgs)==0)
508 // check if URL has the no service part
509 if (
510 (bOK ) &&
511 ((eExpectedPart & E_SERVICE) != E_SERVICE)
514 bOK = (
515 (!aURL.getService(sService) ) &&
516 (sService.getLength()==0 ) &&
517 (!aURL.getServiceArgs(sServiceArgs)) &&
518 (sServiceArgs.getLength()==0 )
522 ::rtl::OUStringBuffer sMsg(256);
524 sMsg.appendAscii("\"" );
525 sMsg.append (sURL );
526 sMsg.appendAscii("\" ");
528 if (bOK)
530 sMsg.appendAscii("... OK\n");
532 else
534 sMsg.appendAscii("... failed\n");
535 sMsg.appendAscii("expected was: ");
536 if (eExpectedPart==E_UNKNOWN)
537 sMsg.appendAscii("E_UNKNOWN");
538 if ((eExpectedPart & E_EVENT) == E_EVENT)
540 sMsg.appendAscii("| E_EVENT e=\"");
541 sMsg.appendAscii(pExpectedEvent );
542 sMsg.appendAscii("\"" );
544 if ((eExpectedPart & E_ALIAS) == E_ALIAS)
546 sMsg.appendAscii("| E_ALIAS a=\"");
547 sMsg.appendAscii(pExpectedAlias );
548 sMsg.appendAscii("\"" );
550 if ((eExpectedPart & E_SERVICE) == E_SERVICE)
552 sMsg.appendAscii("| E_SERVICE s=\"");
553 sMsg.appendAscii(pExpectedService );
554 sMsg.appendAscii("\"" );
556 sMsg.appendAscii("\tbut it was : " );
557 sMsg.append (aURL.impldbg_toString());
558 sMsg.appendAscii("\n" );
561 WRITE_LOGFILE(LOGFILE_JOBURL, U2B(sMsg.makeStringAndClear()))
564 //________________________________
566 @short helper debug method
567 @descr It returns a representation of the internal object state
568 as string notation.
570 @returns The formated string representation.
572 ::rtl::OUString JobURL::impldbg_toString() const
574 /* SAFE { */
575 ReadGuard aReadLock(m_aLock);
577 ::rtl::OUStringBuffer sBuffer(256);
579 if (m_eRequest==E_UNKNOWN)
580 sBuffer.appendAscii("E_UNKNOWN");
581 if ((m_eRequest & E_EVENT) == E_EVENT)
582 sBuffer.appendAscii("| E_EVENT");
583 if ((m_eRequest & E_ALIAS) == E_ALIAS)
584 sBuffer.appendAscii("| E_ALIAS");
585 if ((m_eRequest & E_SERVICE) == E_SERVICE)
586 sBuffer.appendAscii("| E_SERVICE");
587 sBuffer.appendAscii("{ e=\"" );
588 sBuffer.append (m_sEvent );
589 sBuffer.appendAscii("\" - a=\"");
590 sBuffer.append (m_sAlias );
591 sBuffer.appendAscii("\" - s=\"");
592 sBuffer.append (m_sService );
593 sBuffer.appendAscii("\" }" );
595 aReadLock.unlock();
596 /* } SAFE */
598 return sBuffer.makeStringAndClear();
601 //________________________________
603 sal_Bool JobURL::getServiceArgs( /*OUT*/ ::rtl::OUString& sServiceArgs ) const
605 /* SAFE { */
606 ReadGuard aReadLock(m_aLock);
608 sServiceArgs = ::rtl::OUString();
609 sal_Bool bSet = ((m_eRequest & E_SERVICE) == E_SERVICE);
610 if (bSet)
611 sServiceArgs = m_sServiceArgs;
613 aReadLock.unlock();
614 /* } SAFE */
616 return bSet;
619 //________________________________
621 sal_Bool JobURL::getEventArgs( /*OUT*/ ::rtl::OUString& sEventArgs ) const
623 /* SAFE { */
624 ReadGuard aReadLock(m_aLock);
626 sEventArgs = ::rtl::OUString();
627 sal_Bool bSet = ((m_eRequest & E_EVENT) == E_EVENT);
628 if (bSet)
629 sEventArgs = m_sEventArgs;
631 aReadLock.unlock();
632 /* } SAFE */
634 return bSet;
637 //________________________________
639 sal_Bool JobURL::getAliasArgs( /*OUT*/ ::rtl::OUString& sAliasArgs ) const
641 /* SAFE { */
642 ReadGuard aReadLock(m_aLock);
644 sAliasArgs = ::rtl::OUString();
645 sal_Bool bSet = ((m_eRequest & E_ALIAS) == E_ALIAS);
646 if (bSet)
647 sAliasArgs = m_sAliasArgs;
649 aReadLock.unlock();
650 /* } SAFE */
652 return bSet;
655 #endif // ENABLE_COMPONENT_SELF_CHECK
657 } // namespace framework