summaryrefslogtreecommitdiff
path: root/jvmfwk/source/fwkutil.cxx
blob: 5fe8c7290579f00d4d54f89da7c1ed67c627f14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*************************************************************************
 *
 *  $RCSfile: fwkutil.cxx,v $
 *
 *  $Revision: 1.13 $
 *
 *  last change: $Author: jl $ $Date: 2004-05-18 08:37:33 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef WNT
#include <windows.h>
#endif

#include "libxmlutil.hxx"
#include "osl/mutex.hxx"
#include "osl/module.hxx"
#include "rtl/ustring.hxx"
#include "rtl/ustrbuf.hxx"
#include "rtl/bootstrap.hxx"
#include "osl/file.hxx"
#include "osl/thread.hxx"
#include "osl/process.h"
#include "rtl/instance.hxx"
#include "rtl/uri.hxx"
#include "rtl/process.h"
#include "osl/getglobalmutex.hxx"
#include "libxml/xpathInternals.h"

#include "framework.hxx"
#include "elements.hxx"
#include "fwkutil.hxx"


#define JAVASETTINGS "javasettings"
#define VENDORSETTINGS "javavendors.xml"
/** The vector contains on return file urls to the plugins.
 */
namespace jfw
{

bool  g_bJavaSet = false;

struct Init
{
    osl::Mutex * operator()()
        {
            static osl::Mutex aInstance;
            return &aInstance;
        }
};
osl::Mutex * getFwkMutex()
{
    return rtl_Instance< osl::Mutex, Init, ::osl::MutexGuard,
        ::osl::GetGlobalMutex >::create(
            Init(), ::osl::GetGlobalMutex());
}

const rtl::Bootstrap & getBootstrapHandle()
{
    static rtl::Bootstrap *pBootstrap = 0;
    if( !pBootstrap )
    {
        rtl::OUString exe;
        osl_getExecutableFile( &(exe.pData) );

        sal_Int32 nIndex = exe.lastIndexOf( '/' );
        rtl::OUString ret;
        if( exe.getLength() && nIndex != -1 )
        {
            rtl::OUStringBuffer buf( exe.getLength() + 10 );
            buf.append( exe.getStr() , nIndex +1 ).appendAscii( SAL_CONFIGFILE("uno") );
            ret = buf.makeStringAndClear();
        }
#if OSL_DEBUG_LEVEL > 1
        rtl::OString o = rtl::OUStringToOString( ret , RTL_TEXTENCODING_ASCII_US );
        printf( "JavaVM: Used ininame %s\n" , o.getStr() );
#endif
        static rtl::Bootstrap  bootstrap( ret );
        pBootstrap = &bootstrap;
    }
    return *pBootstrap;
}
rtl::OUString retrieveClassPath( ::rtl::OUString const & macro )
{
    ::rtl::OUString classpath( macro );
    getBootstrapHandle().expandMacrosFrom( classpath );
    ::rtl::OUStringBuffer buf;
    sal_Int32 index = 0;
    char szClassPathSep[] = {SAL_PATHSEPARATOR,0};
    do
    {
        ::rtl::OUString token( classpath.getToken( 0, ' ', index ).trim() );
        if (token.getLength())
        {
            if (token.matchIgnoreAsciiCaseAsciiL(
                    RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.expand:") ))
            {
                token = ::rtl::Uri::decode(
                    token.copy( sizeof ("vnd.sun.star.expand:") -1 ),
                    rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
                getBootstrapHandle().expandMacrosFrom( token );
            }

            ::rtl::OUString systemPathElement;
            oslFileError rc = osl_getSystemPathFromFileURL(
                token.pData, &systemPathElement.pData );
            OSL_ASSERT( rc == osl_File_E_None );
            if (rc == osl_File_E_None && systemPathElement.getLength() > 0)
            {
                if (buf.getLength() > 0)
                    buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
                                         szClassPathSep) );
                buf.append( systemPathElement );
            }
        }
    }
    while (index >= 0);
    return buf.makeStringAndClear();
}

//ToDo we should use a SAL function to determine which platform and OS we are working on.
rtl::OUString getPlatform()
{
    char * szArchitecture =
#if defined SPARC
    "sparc";
#elif defined INTEL
    "x86";
#elif defined POWERPC
    "ppc";
#elif defined MIPS
    "mips";
#elif defined S390
    "s390";
#else
#error unknown plattform
#endif

    char * szOS =

#if defined WNT
        "wnt";
#elif defined  SOLARIS
    "solaris";
#elif defined LINUX
    "linux";
#elif defined MACOSX
    "macosx";
#elif defined FREEBSD
    "freebsd";
#elif defined NETBSD
    "netbsd";
#elif defined AIX
    "aix";
#else
#error unknown operating system
#endif
    rtl::OUStringBuffer buf(256);
    buf.appendAscii("_");
    rtl::OUString sOS = rtl::OStringToOUString(
        rtl::OString(szOS), RTL_TEXTENCODING_UTF8);
    buf.append(sOS);
    buf.appendAscii("_");
    rtl::OUString sArch = rtl::OStringToOUString(
        rtl::OString(szArchitecture), RTL_TEXTENCODING_UTF8);
    buf.append(sArch);
    return buf.makeStringAndClear();
}

javaFrameworkError getPluginLibrary(
    const rtl::OUString& sVendor, rtl::OUString & sLibUrl)
{
    javaFrameworkError errcode = JFW_E_NONE;

    rtl::OString sVendorsPath = getVendorSettingsPath();
    CXmlDocPtr docVendor;
    CXPathContextPtr contextVendor;
    docVendor = xmlParseFile(sVendorsPath.getStr());
    if (docVendor == NULL)
        return JFW_E_CONFIG_READWRITE;

    contextVendor = xmlXPathNewContext(docVendor);
    if (xmlXPathRegisterNs(contextVendor, (xmlChar*) "jf",
        (xmlChar*) NS_JAVA_FRAMEWORK) == -1)
        return JFW_E_CONFIG_READWRITE;

    rtl::OUStringBuffer usBuffer(256);
    usBuffer.appendAscii("/jf:javaSelection/jf:plugins/jf:library[@vendor=\"");
    usBuffer.append(sVendor);
    usBuffer.appendAscii("\"]/text()");
    rtl::OUString ouExpr = usBuffer.makeStringAndClear();
    rtl::OString sExpression =
        rtl::OUStringToOString(ouExpr, osl_getThreadTextEncoding());
    CXPathObjectPtr pathObjVendor;
    pathObjVendor = xmlXPathEvalExpression(
        (xmlChar*) sExpression.getStr(), contextVendor);
    if (xmlXPathNodeSetIsEmpty(pathObjVendor->nodesetval))
        return JFW_E_FORMAT_STORE;
    CXmlCharPtr xmlCharPlugin;
    xmlCharPlugin =
        xmlNodeListGetString(
            docVendor,pathObjVendor->nodesetval->nodeTab[0], 1);

    //make an absolute file url from the relativ plugin URL
    rtl::OUString sLibPath = getBaseInstallation()  +
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/")) + xmlCharPlugin;

    sLibUrl = sLibPath;
    return errcode;
}


javaFrameworkError getVendorPluginURLs(
    const xmlDocPtr doc,
    const xmlXPathContextPtr  context,
    std::vector<PluginLibrary> * vecPlugins)
{
    OSL_ASSERT(vecPlugins && doc && context);

    //get the nodeset for the library elements
    jfw::CXPathObjectPtr result;
    result = xmlXPathEvalExpression(
        (xmlChar*)"/jf:javaSelection/jf:plugins/jf:library", context);
    if (xmlXPathNodeSetIsEmpty(result->nodesetval))
    {
        return JFW_E_ERROR;
    }
    vecPlugins->clear();

    //get the values of the library elements + vendor attribute
    xmlNode* cur = result->nodesetval->nodeTab[0];
    while (cur != NULL)
    {
        //between library elements are also text elements
        if (cur->type == XML_ELEMENT_NODE)
        {
            jfw::CXmlCharPtr sAttrVendor(xmlGetProp(cur, (xmlChar*) "vendor"));
            jfw::CXmlCharPtr sTextLibrary(
                xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
            PluginLibrary plugin;
            plugin.sVendor = rtl::OString((sal_Char*)(xmlChar*) sAttrVendor);
            //create the file URL to the library

            rtl::OUString sBase = getBaseInstallation();
            plugin.sPath = sBase +
                rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/")) + sTextLibrary;

            vecPlugins->push_back(plugin);
        }
        cur = cur->next;
    }
    return JFW_E_NONE;
}

/** Get the file URL to the javasettings.xml
 */
rtl::OUString getUserSettingsURL()
{
    //get the system path to the javasettings.xml file
    rtl::OUString sUserDir;
    rtl::Bootstrap::get(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UserInstallation")),
        sUserDir,
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("${$SYSBINDIR/"
             SAL_CONFIGFILE("bootstrap") ":UserInstallation}")));

    if (sUserDir.getLength() == 0)
        return rtl::OUString();
    rtl::OUStringBuffer sSettingsBuffer(512);
    sSettingsBuffer.append(sUserDir);
    sSettingsBuffer.appendAscii("/user/config/");
    sSettingsBuffer.appendAscii(JAVASETTINGS);
    sSettingsBuffer.append(getPlatform());
    sSettingsBuffer.appendAscii(".xml");
    return sSettingsBuffer.makeStringAndClear();
}

rtl::OString getUserSettingsPath()
{
    rtl::OUString sURL = getUserSettingsURL();
    rtl::OUString sSystemPathSettings;
    if (osl_getSystemPathFromFileURL(sURL.pData,
        & sSystemPathSettings.pData) != osl_File_E_None)
        return rtl::OString();

    rtl::OString osSystemPathSettings =
        rtl::OUStringToOString(sSystemPathSettings,osl_getThreadTextEncoding());
    return osSystemPathSettings;
}
rtl::OUString getSharedSettingsURL()
{
    rtl::OUString sBase = getBaseInstallation();
    if (sBase.getLength() == 0)
        return sBase;
    rtl::OUStringBuffer sBufSettings(256);
    sBufSettings.append(sBase);
    sBufSettings.appendAscii("/share/config/");
    sBufSettings.appendAscii(JAVASETTINGS);
    sBufSettings.append(getPlatform());
    sBufSettings.appendAscii(".xml");
    return sBufSettings.makeStringAndClear();
}

rtl::OUString getSharedSettingsURLNoPlatformSuffix()
{
    rtl::OUString sBase = getBaseInstallation();
    if (sBase.getLength() == 0)
        return sBase;
    rtl::OUStringBuffer sBufSettings(256);
    sBufSettings.append(sBase);
    sBufSettings.appendAscii("/share/config/");
    sBufSettings.appendAscii(JAVASETTINGS);
    sBufSettings.appendAscii(".xml");
    return sBufSettings.makeStringAndClear();
}

rtl::OString getSharedSettingsPath()
{
    rtl::OUString sURL = getSharedSettingsURL();
    rtl::OUString sSystemPathSettings;
    if (osl_getSystemPathFromFileURL(sURL.pData,
        & sSystemPathSettings.pData) != osl_File_E_None)
        return rtl::OString();

    rtl::OString osSystemPathSettings =
        rtl::OUStringToOString(sSystemPathSettings,osl_getThreadTextEncoding());
    return osSystemPathSettings;
}
rtl::OString getSharedSettingsPathNoPlatformSuffix()
{
    rtl::OUString sURL = getSharedSettingsURLNoPlatformSuffix();
    rtl::OUString sSystemPathSettings;
    if (osl_getSystemPathFromFileURL(sURL.pData,
        & sSystemPathSettings.pData) != osl_File_E_None)
        return rtl::OString();

    rtl::OString osSystemPathSettings =
        rtl::OUStringToOString(sSystemPathSettings,osl_getThreadTextEncoding());
    return osSystemPathSettings;
}

rtl::OUString getBaseInstallation()
{
    rtl::OUString sBaseDir;
    rtl::Bootstrap::get(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BaseInstallation")),
        sBaseDir,
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
            "${$SYSBINDIR/"SAL_CONFIGFILE("bootstrap") ":BaseInstallation}")));
    return sBaseDir;

}
rtl::OUString getVendorSettingsURL()
{
    //get the system path to the javavendors.xml file
    //First try in an office installation
    rtl::OUString sBaseDir = getBaseInstallation();
    if (sBaseDir.getLength() != 0)
    {
        //We are run within office installation
        rtl::OUStringBuffer sSettings(256);
        sSettings.append(sBaseDir);
        sSettings.appendAscii("/share/config/");
        sSettings.appendAscii(VENDORSETTINGS);
        return sSettings.makeStringAndClear();
    }

    //try next to the executable
    rtl_uString* sExe = NULL;
    if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
    {
        OSL_ASSERT(0);
        return rtl::OUString();
    }

    rtl::OUString ouExe(sExe, SAL_NO_ACQUIRE);
    rtl::OUString sVendor = getDirFromFile(ouExe);
    rtl::OUStringBuffer sBufVendor(256);
    sBufVendor.append(sVendor);
    sBufVendor.appendAscii("/");
    sBufVendor.appendAscii(VENDORSETTINGS);
    sVendor = sBufVendor.makeStringAndClear();

    //check if the file exists
    osl::DirectoryItem vendorItem;
    osl::File::RC fileError = osl::DirectoryItem::get(sVendor, vendorItem);
    if (fileError == osl::FileBase::E_None)
        return sVendor;

    //try next to the jvmfwk.dll
    rtl::OUString sLib;
    if (osl_getModuleURLFromAddress((void *) & getVendorSettingsURL,
                                    & sLib.pData) == sal_True)
    {
        sLib = getDirFromFile(sLib);
        rtl::OUStringBuffer sBufVendor(256);
        sBufVendor.append(sLib);
        sBufVendor.appendAscii("/");
        sBufVendor.appendAscii(VENDORSETTINGS);
        rtl::OUString sVendor = sBufVendor.makeStringAndClear();
        //check if the file exists
        osl::DirectoryItem vendorItem;
        osl::File::RC fileError = osl::DirectoryItem::get(sVendor, vendorItem);
        if (fileError == osl::FileBase::E_None)
            return sVendor;
    }

    OSL_ASSERT(0);
    return rtl::OUString();
}

rtl::OString getVendorSettingsPath()
{
    rtl::OUString sURL = getVendorSettingsURL();
    rtl::OUString sSystemPathSettings;
    if (osl_getSystemPathFromFileURL(sURL.pData,
        & sSystemPathSettings.pData) != osl_File_E_None)
        return rtl::OString();

    rtl::OString osSystemPathSettings =
        rtl::OUStringToOString(sSystemPathSettings,osl_getThreadTextEncoding());
    return osSystemPathSettings;
}

bool isAccessibilitySupportDesired()
{
    bool retVal = false;
#ifdef WNT
    HKEY    hKey = 0;
    if (RegOpenKeyEx(HKEY_CURRENT_USER,
                     "Software\\OpenOffice.org\\Accessibility\\AtToolSupport",
                     0, KEY_READ, &hKey) == ERROR_SUCCESS)
    {
        DWORD   dwType = 0;
        DWORD   dwLen = 16;
        unsigned char arData[16];
        if( RegQueryValueEx(hKey, "SupportAssistiveTechnology", NULL, &dwType, arData,
                            & dwLen)== ERROR_SUCCESS)
        {
            if (dwType == REG_SZ)
            {
                if (strcmp((char*) arData, "true") == 0
                    || strcmp((char*) arData, "1") == 0)
                    retVal = true;
                else if (strcmp((char*) arData, "false") == 0
                         || strcmp((char*) arData, "0") == 0)
                    retVal = false;
#if OSL_DEBUG_LEVER > 1
                else
                    OSL_ASSERT(0);
#endif
            }
            else if (dwType == REG_DWORD)
            {
                if (arData[0] == 1)
                    retVal = true;
                else if (arData[0] == 0)
                    retVal = false;
#if OSL_DEBUG_LEVER > 1
                else
                    OSL_ASSERT(0);
#endif
            }
        }
    }
    RegCloseKey(hKey);

#elif UNX
    char buf[16];
    // use 2 shells to suppress the eventual "gcontool-2 not found" message
    // of the shell trying to execute the command
    FILE* fp = popen( "/bin/sh 2>/dev/null -c \"gconftool-2 -g /desktop/gnome/interface/accessibility\"", "r" );
    if( fp )
    {
        if( fgets( buf, sizeof(buf), fp ) )
        {
            int nCompare = strncasecmp( buf, "true", 4 );
            retVal = (nCompare == 0 ? true : false);
        }
        pclose( fp );
    }
#endif
    return retVal;
}


javaFrameworkError getVersionInformation(
    const xmlDocPtr doc,
    const xmlXPathContextPtr context,
    const rtl::OString & sVendor,
    VersionInfo *pVersionInfo)
{
    OSL_ASSERT(doc && context && sVendor.getLength() && pVersionInfo);

    //Get minVersion
    rtl::OString sExpresion= rtl::OString(
        "/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
        sVendor + rtl::OString("\"]/jf:minVersion");

    jfw::CXPathObjectPtr xPathObjectMin;
    xPathObjectMin =
        xmlXPathEvalExpression((xmlChar*) sExpresion.getStr(), context);
    if (xmlXPathNodeSetIsEmpty(xPathObjectMin->nodesetval))
    {
        pVersionInfo->sMinVersion = rtl::OUString();
    }
    else
    {
        jfw::CXmlCharPtr sVersion;
        sVersion = xmlNodeListGetString(
            doc, xPathObjectMin->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
        rtl::OString osVersion((sal_Char*)(xmlChar*) sVersion);
        pVersionInfo->sMinVersion = rtl::OStringToOUString(
            osVersion, RTL_TEXTENCODING_UTF8);
    }

    //Get maxVersion
    sExpresion = rtl::OString("/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
        sVendor + rtl::OString("\"]/jf:maxVersion");
    jfw::CXPathObjectPtr xPathObjectMax;
    xPathObjectMax = xmlXPathEvalExpression(
        (xmlChar*) sExpresion.getStr(), context);
    if (xmlXPathNodeSetIsEmpty(xPathObjectMax->nodesetval))
    {
        pVersionInfo->sMaxVersion = rtl::OUString();
    }
    else
    {
        jfw::CXmlCharPtr sVersion;
        sVersion = xmlNodeListGetString(
            doc,xPathObjectMax->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
        rtl::OString osVersion((sal_Char*) (xmlChar*) sVersion);
        pVersionInfo->sMaxVersion = rtl::OStringToOUString(
            osVersion, RTL_TEXTENCODING_UTF8);
    }

    //Get excludeVersions
    sExpresion = rtl::OString("/jf:javaSelection/jf:vendorInfos/jf:vendor[@name=\"") +
        sVendor + rtl::OString("\"]/jf:excludeVersions/jf:version");
    jfw::CXPathObjectPtr xPathObjectVersions;
    xPathObjectVersions =
        xmlXPathEvalExpression((xmlChar*) sExpresion.getStr(), context);
    if (xPathObjectVersions->nodesetval)
    {
        xmlNode* cur = xPathObjectVersions->nodesetval->nodeTab[0];
        while (cur != NULL)
        {
            if (cur->type == XML_ELEMENT_NODE )
            {
                if (xmlStrcmp(cur->name, (xmlChar*) "version") == 0)
                {
                    jfw::CXmlCharPtr sVersion;
                    sVersion = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                    rtl::OString osVersion((sal_Char*)(xmlChar*) sVersion);
                    rtl::OUString usVersion = rtl::OStringToOUString(
                        osVersion, RTL_TEXTENCODING_UTF8);
                    pVersionInfo->addExcludeVersion(usVersion);
                }
            }
            cur = cur->next;
        }
    }
    return JFW_E_NONE;
}

rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
{
    static char EncodingTable[] =
        {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    sal_Int32 lenRaw = rawData.getLength();
    char* pBuf = new char[lenRaw * 2];
    const sal_Int8* arRaw = rawData.getConstArray();

    char* pCurBuf = pBuf;
    for (int i = 0; i < lenRaw; i++)
    {
        char curChar = arRaw[i];
        curChar >>= 4;

        *pCurBuf = EncodingTable[curChar];
        pCurBuf++;

        curChar = arRaw[i];
        curChar &= 0x0F;

        *pCurBuf = EncodingTable[curChar];
        pCurBuf++;
    }

    rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2);
    delete [] pBuf;
    return ret;
}

rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
{
    static char decodingTable[] =
        {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    sal_Int32 lenData = data.getLength();
    sal_Int32 lenBuf = lenData / 2; //always divisable by two
    char* pBuf = new char[lenBuf];
    const sal_Int8* arData = data.getConstArray();

    char* pCurBuf = pBuf;
    const sal_Int8* pData = arData;
    for (int i = 0; i < lenBuf; i++)
    {
        sal_Int8 curChar = *pData;
        //find the index of the first 4bits
        char nibble;
        for (int ii = 0; ii < 16; ii++)
        {
            if (curChar == decodingTable[ii])
            {
                nibble = ii;
                break;
            }
        }
        nibble <<= 4;
        pData++;
        curChar = *pData;
        //find the index for the next 4bits
        for (int j = 0; j < 16; j++)
        {
            if (curChar == decodingTable[j])
            {
                nibble |= j;
                break;
            }
        }
        *pCurBuf = nibble;
        pData++;
        pCurBuf++;
    }
    rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf );
    delete [] pBuf;
    return ret;
}



javaFrameworkError buildClassPathFromDirectory(const rtl::OUString & relPath,
                             rtl::OUString & sClassPath)
{
    rtl::OUStringBuffer sBufRel(512);
    sBufRel.append(getBaseInstallation());
    sBufRel.appendAscii("/");
    sBufRel.append(relPath);
    rtl::OUString sClassesDir = sBufRel.makeStringAndClear();

    osl::Directory dir(sClassesDir);
    osl::FileBase::RC fileErrorCode;
    if ((fileErrorCode = dir.open()) != osl::FileBase::E_None)
    {
        return JFW_E_ERROR;
    }

    osl::DirectoryItem dirItem;
    rtl::OUStringBuffer sBuffer(2048);
    char szSep[] = {SAL_PATHSEPARATOR,0};

    //insert the path to the directory, so that .class files can be found
    rtl::OUString sDirPath;
    if ((fileErrorCode = osl::FileBase::getSystemPathFromFileURL(
             sClassesDir, sDirPath))
        != osl::FileBase::E_None)
    {
        return JFW_E_ERROR;
    }
    sBuffer.append(sDirPath);

    rtl::OUString sJarExtension(RTL_CONSTASCII_USTRINGPARAM(".jar"));
    sal_Int32 nJarExtensionLength = sJarExtension.getLength();
    for(;;)
    {
        fileErrorCode = dir.getNextItem(dirItem);
        if (fileErrorCode == osl::FileBase::E_None)
        {
            osl::FileStatus stat(FileStatusMask_All);
            if ((fileErrorCode = dirItem.getFileStatus(stat)) !=
                osl::FileBase::E_None)
            {
                return JFW_E_ERROR;
            }
            // check if the item is a file.
            switch (stat.getFileType())
            {
            case osl::FileStatus::Regular:
                break;
            case osl::FileStatus::Link:
            {
                rtl::OUString sLinkURL = stat.getLinkTargetURL();
                osl::DirectoryItem itemLink;
                if (osl::DirectoryItem::get(sLinkURL, itemLink)
                    != osl::FileBase::E_None)
                {
                   return JFW_E_ERROR;
                }
                osl::FileStatus statLink(FileStatusMask_All);
                if (statLink.getFileType() != osl::FileStatus::Regular)
                    continue;
                break;
            }
            default:
                continue;
            }

            //check if the file is a .jar, class files are ignored
            rtl::OUString sFileName = stat.getFileName();
            sal_Int32 len = sFileName.getLength();
            sal_Int32 nIndex = sFileName.lastIndexOf(sJarExtension);
            if ((nIndex == -1)
                || (nIndex + nJarExtensionLength != len))
                continue;

            rtl::OUString sFileURL = stat.getFileURL();
            rtl::OUString sFilePath;
            if ((fileErrorCode = osl::FileBase::getSystemPathFromFileURL(
                     sFileURL, sFilePath))
                != osl::FileBase::E_None)
            {
                return JFW_E_ERROR;
            }
            sBuffer.appendAscii(szSep);
            sBuffer.append(sFilePath);
        }
        else if (fileErrorCode == osl::FileBase::E_NOENT)
        {
            break;
        }
        else
        {
            return JFW_E_ERROR;
        }
    }
    sClassPath = sBuffer.makeStringAndClear();
    return JFW_E_NONE;
}

void setJavaSelected()
{
    g_bJavaSet = true;
}

bool wasJavaSelectedInSameProcess()
{
    //g_setJavaProcId not set means no Java selected
    if (g_bJavaSet == true)
        return true;
    return false;
}

rtl::OUString getDirFromFile(const rtl::OUString& usFilePath)
{
    sal_Int32 index= usFilePath.lastIndexOf('/');
    return rtl::OUString(usFilePath.getStr(), index);
}

//todo !!!
rtl::OUString getJavaSettingsDirectory()
{
    //test if we are running within an office
    return rtl::OUString();
}

JFW_MODE getMode()
{
    //Determine if we run in an office process:
    rtl_uString* sExe = NULL;
    if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
        return JFW_MODE_INDETERMINED;
    rtl::OUString ouExe(sExe, SAL_NO_ACQUIRE);

    //create the name of the executable
    rtl::OUStringBuffer buff(128);
    buff.appendAscii("soffice");
    buff.appendAscii(SAL_PRGEXTENSION);
    rtl::OUString sOffice = buff.makeStringAndClear();

    sal_Int32 index = 0;
    if ((index = ouExe.lastIndexOf(sOffice)) != 1
        && index + sOffice.getLength() == ouExe.getLength())
        return JFW_MODE_OFFICE;


    //FWK_MODE_ENV_SIMPLE ?
    rtl::OUString sOO_USE_JRE(RTL_CONSTASCII_USTRINGPARAM(ENVIRONMENT_VAR_JRE_PATH));

    rtl_uString * psOO_USE_JRE = 0;
    if (osl_getEnvironment(sOO_USE_JRE.pData, & psOO_USE_JRE) != osl_Process_E_None)
        return JFW_MODE_INDETERMINED;

    rtl::OUString sOO_USE_JRE_VALUE(psOO_USE_JRE, SAL_NO_ACQUIRE);
    if (sOO_USE_JRE_VALUE.getLength() > 0)
        return JFW_MODE_ENV_SIMPLE;

    return JFW_MODE_INDETERMINED;


}

javaFrameworkError makeClassPathOption(
    JFW_MODE mode, CNodeJava & javaSettings, rtl::OString & sOption)
{
    //Compose the class path
    rtl::OUStringBuffer sBufCP(4096);
    char szSep[] = {SAL_PATHSEPARATOR,0};
    if (mode == JFW_MODE_OFFICE)
    {
        //build the class path from the classes directory
        rtl::OUString sClassPath;
        javaFrameworkError errcode = jfw::buildClassPathFromDirectory(
            javaSettings.m_sClassesDirectory, sClassPath);
        if (errcode != JFW_E_NONE)
            return JFW_E_ERROR;
        sBufCP.append(sClassPath);
        // append all user selected jars to the classpath
        if (javaSettings.getUserClassPath().getLength() != 0)
        {
            sBufCP.appendAscii(szSep);
            sBufCP.append(javaSettings.getUserClassPath());
        }
    }
    else if (mode == JFW_MODE_ENV_SIMPLE)
    {
        //We use the CLASSPATH environment variable
        rtl::OUString sCP(
            RTL_CONSTASCII_USTRINGPARAM("CLASSPATH"));

        rtl_uString * psCPVal = 0;
        if (osl_getEnvironment(sCP.pData, & psCPVal) != osl_Process_E_None)
            return JFW_E_ERROR;

        rtl::OUString sCPVal(psCPVal, SAL_NO_ACQUIRE);
        sBufCP.append(sCPVal);
    }
    else
        OSL_ASSERT(0);

    //add the path of the UNO components
    rtl::OUString sComponents =
        jfw::retrieveClassPath(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "${$PKG_SharedUnoFile:UNO_JAVA_CLASSPATH}")));
    sBufCP.append(sComponents);
    sComponents = jfw::retrieveClassPath(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
            "${$PKG_UserUnoFile:UNO_JAVA_CLASSPATH}")));

    sBufCP.append(sComponents);
    rtl::OString sOptionClassPath("-Djava.class.path=");
    sOptionClassPath += rtl::OUStringToOString(
        sBufCP.makeStringAndClear(), osl_getThreadTextEncoding());

    sOption = sOptionClassPath;
    return JFW_E_NONE;
}


}