summaryrefslogtreecommitdiff
path: root/scripting/java/com/sun/star/script/framework/container/ParcelContainer.java
blob: 22a0b0059bfbb576b122914612bd40808f13287b (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
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

package com.sun.star.script.framework.container;

import  com.sun.star.script.framework.log.*;
import  com.sun.star.script.framework.io.*;
import  com.sun.star.script.framework.provider.PathUtils;

import com.sun.star.container.*;
import com.sun.star.uno.Type;
import com.sun.star.lang.*;
import com.sun.star.io.*;
import java.io.*;
import java.util.*;
import com.sun.star.ucb.XSimpleFileAccess;
import com.sun.star.ucb.XSimpleFileAccess2;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;

import com.sun.star.uri.XUriReference;
import com.sun.star.uri.XUriReferenceFactory;
import com.sun.star.uri.XVndSunStarScriptUrl;

/**
 * The <code>ParcelContainer</code> object is used to store the
 * ScripingFramework specific Libraries.
 */

public class ParcelContainer implements XNameAccess
{
    protected String language;
    protected String containerUrl;
    protected Collection<Parcel> parcels = new ArrayList<Parcel>(10);
    static protected XSimpleFileAccess m_xSFA;
    protected XComponentContext m_xCtx;
    private ParcelContainer parent = null;
    private Collection<ParcelContainer> childContainers = new ArrayList<ParcelContainer>(10);
    private boolean isPkgContainer = false;

    /**
     * Tests if this <tt>ParcelContainer</tt> represents an UNO package
     * or sub package within an UNO package
     *
     * @return    <tt>true</tt> if has parent <tt>false</tt> otherwise
     */
    public boolean isUnoPkg() { return isPkgContainer; }
    /**
     * Tests if this <tt>ParcelContainer</tt> has a parent
     * a parent.
     *
     * @return    <tt>true</tt> if has parent <tt>false</tt> otherwise
     */
    public boolean hasParent()
    {
        if ( parent == null )
        {
            return false;
        }
        return true;
    }

    /**
     * Returns this <tt>ParcelContainer</tt>'s  parent
     *
     * @return    <tt>ParcelContainer</tt> if has parent null otherwise
     */
    public ParcelContainer parent()
    {
        return parent;
    }

    /**
     * Returns all child <tt>ParcelContainer</tt>
     * this instance of <tt>ParcelContainer</tt>
     *
     * @return    a new array of ParcelContainers. A zero
     * length array is returned if no child ParcelContainers.
     */
    public ParcelContainer[] getChildContainers()
    {
        if ( childContainers.isEmpty() )
        {
            return new ParcelContainer[0];
        }
        return childContainers.toArray( new ParcelContainer[childContainers.size()] );

    }
    /**
     * Removes a child <tt>ParcelContainer</tt>
     * from this instance.
     * @param   child  <tt>ParcelContainer</tt> to be added.
     *
     * @return    <tt>true</tt> if child successfully removed
     */
    public boolean removeChildContainer( ParcelContainer child )
    {
        return childContainers.remove( child );
    }

    /**
     * Adds a new child <tt>ParcelContainer</tt>
     * to this instance.
     * @param   child  <tt>ParcelContainer</tt> to be added.
     *
     */
    public void addChildContainer( ParcelContainer child )
    {
        childContainers.add( child );
    }

    /**
     * Returns a child <tt>ParcelContainer</tt> whose location
     * matches the <tt>location</tt> argument passed to this method.
     * @param    key the <tt>location</tt> which is to
     *           be matched.
     *
     * @return    child <tt>ParcelContainer</tt> or <null> if none
     * found.
     */

    public ParcelContainer getChildContainer( String key )
    {
        ParcelContainer result = null;
        Iterator<ParcelContainer> iter = childContainers.iterator();
        while ( iter.hasNext() )
        {
            ParcelContainer c = iter.next();
            String location = ScriptMetaData.getLocationPlaceHolder(
                c.containerUrl, c.getName());

            if ( key.equals( location ) )
            {
                result = c;
                break;
            }
        }
        return result;
    }

    /**
     * Returns a child <tt>ParcelContainer</tt> whose member
     * <tt>containerUrl</tt> matches the <tt>containerUrl</tt>
     * argument passed to this method.
     * @param    containerUrl the <tt>containerUrl</tt> which is to
     *           be matched.
     *
     * @return    child <tt>ParcelContainer</tt> or <null> if none
     * found.
     */

    public ParcelContainer getChildContainerForURL( String containerUrl )
    {
        ParcelContainer result = null;
        Iterator<ParcelContainer> iter = childContainers.iterator();
        while ( iter.hasNext() )
        {
            ParcelContainer c = iter.next();
            if ( containerUrl.equals( c.containerUrl ) )
            {
                result = c;
                break;
            }
        }
        return result;
    }

    /**
     * Returns Name of this container. Name of this <tt>ParcelContainer</tt>
     * is determined from the <tt>containerUrl</tt> as the last portion
     * of the URL after the last forward slash.
     * @return    name of <tt>ParcelContainer</tt>
     * found.
     */
    public String getName()
    {
        String name = null;
        // TODO handler package ParcelContainer?
        if (  !containerUrl.startsWith( "vnd.sun.star.tdoc:" ) )
        {
            // return name
            String decodedUrl = java.net.URLDecoder.decode( containerUrl );
            int indexOfSlash = decodedUrl.lastIndexOf( "/" );
            if ( indexOfSlash != -1 )
            {
                name =  decodedUrl.substring( indexOfSlash + 1 );
            }
        }
        else
        {
            name =  "document";
        }
        return name;
    }

    /**
     * Initializes a newly created <code>ParcelContainer</code> object.
     * @param    xCtx UNO component context
     * @param   containerUrl location of this container.
     * @param   language language for which entries are stored
     */
    public ParcelContainer( XComponentContext xCtx, String containerUrl, String language ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException

    {
        this( null, xCtx, containerUrl, language, true );
    }

    /**
     * Initializes a newly created <code>ParcelContainer</code> object.
     * @param    xCtx UNO component context
     * @param   containerUrl location of this container.
     * @param   language language for which entries are stored
     * @param   loadParcels set to <tt>true</tt> if parcels are to be loaded
     *          on construction.
     */
    public ParcelContainer( XComponentContext xCtx, String containerUrl, String language, boolean loadParcels ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
    {
        this( null, xCtx, containerUrl, language, loadParcels );
    }

    /**
     * Initializes a newly created <code>ParcelContainer</code> object.
     * @param   parent parent ParcelContainer
     * @param    xCtx UNO component context
     * @param   containerUrl location of this container.
     * @param   language language for which entries are stored
     * @param   loadParcels set to <tt>true</tt> if parcels are to be loaded
     *          on construction.
     */
    public ParcelContainer( ParcelContainer parent, XComponentContext xCtx, String containerUrl, String language, boolean loadParcels ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException

    {
        LogUtils.DEBUG("Creating ParcelContainer for " + containerUrl + " loadParcels = " + loadParcels + " language = " + language );
        this.m_xCtx = xCtx;
        this.language = language;
        this.parent = parent;
        this.containerUrl = containerUrl;

        initSimpleFileAccess();
        boolean parentIsPkgContainer = false;

        if ( parent != null )
        {
            parentIsPkgContainer = parent.isUnoPkg();
        }

        if ( containerUrl.endsWith("uno_packages") || parentIsPkgContainer  )
        {
            isPkgContainer = true;
        }

        if ( loadParcels )
        {
            loadParcels();
        }
    }


    public String getContainerURL() { return this.containerUrl; }

    private synchronized void initSimpleFileAccess()
    {
        if ( m_xSFA != null )
        {
            return;
        }
        try
        {
            m_xSFA = UnoRuntime.queryInterface(
                XSimpleFileAccess.class,
                m_xCtx.getServiceManager().createInstanceWithContext(
                    "com.sun.star.ucb.SimpleFileAccess", m_xCtx ) );
        }
        catch ( Exception e )
        {
            // TODO should throw
            LogUtils.DEBUG("Error instantiating simplefile access ");
            LogUtils.DEBUG( LogUtils.getTrace( e ) );
        }
    }

    public String getParcelContainerDir()
    {
        // If this container does not represent an uno-package
        // then then it is a document, user or share
        // in each case the convention is to have a Scripts/[language]
        // dir where scripts reside
        if (  !isUnoPkg() )
        {
            return PathUtils.make_url( containerUrl  ,  "Scripts/" + language.toLowerCase() );
        }
        return containerUrl;
    }
    public Object getByName( String aName ) throws com.sun.star.container.NoSuchElementException, WrappedTargetException
    {
        Parcel parcel = null;
        try
        {
            if ( hasElements() )
            {
                Iterator<Parcel> iter = parcels.iterator();
                while ( iter.hasNext() )
                {
                    Parcel parcelToCheck = iter.next();

                    if ( parcelToCheck.getName().equals( aName ) )
                    {
                       parcel = parcelToCheck;
                       break;
                    }
                }
            }
        }
        catch ( Exception e)
        {
            throw new WrappedTargetException( e.toString() );
        }
        if ( parcel == null )
        {
            throw new com.sun.star.container.NoSuchElementException( "Macro Library " + aName + " not found" );
        }
        return parcel;
    }
    public String[] getElementNames()
    {
        if ( hasElements() )
        {
            Parcel[] theParcels = parcels.toArray( new Parcel[parcels.size()] );
            String[] names = new String[ theParcels.length ];
            for ( int count = 0; count < names.length; count++ )
            {
                names[count] = theParcels[ count ].getName();
            }
            return names;
        }

        return new String[0];
    }
    public boolean hasByName( String aName )
    {
        boolean isFound = false;
        try
        {
            if ( getByName( aName ) != null )
            {
                isFound = true;
            }

        }
        catch ( Exception e )
        {
            //TODO - handle trace
        }
        return isFound;
    }
    public Type getElementType()
    {
        return new Type();
    }
    public boolean hasElements()
    {
        if ( parcels == null || parcels.isEmpty() )
        {
            return false;
        }
        return true;
    }

    private void loadParcels() throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
    {
        try
        {
            LogUtils.DEBUG("About to load parcels from " + containerUrl );
            if ( m_xSFA.isFolder( getParcelContainerDir() ) )
            {
                LogUtils.DEBUG( getParcelContainerDir() + " is a folder " );
                String[] children = m_xSFA.getFolderContents( getParcelContainerDir(), true );
                parcels  = new ArrayList<Parcel>(children.length);
                for ( int  i = 0; i < children.length; i++)
                {
                    LogUtils.DEBUG("Processing " + children[ i ] );
                    try
                    {
                        loadParcel( children[ i ] );
                    }
                    catch (java.lang.Exception e)
                    {
                        // print an error message and move on to
                        // the next parcel
                        LogUtils.DEBUG("ParcelContainer.loadParcels caught " + e.getClass().getName() + " exception loading parcel " + children[i] + ": " + e.getMessage() );
                    }
                }
            }
            else
            {
                LogUtils.DEBUG(" ParcelCOntainer.loadParcels " + getParcelContainerDir()  + " is not a folder ");
            }

        }
        catch ( com.sun.star.ucb.CommandAbortedException e )
        {
            LogUtils.DEBUG("ParcelContainer.loadParcels caught exception processing folders for " + getParcelContainerDir()  );
            LogUtils.DEBUG("TRACE: " + LogUtils.getTrace(e) );
            throw new com.sun.star.lang.WrappedTargetException( e.toString() );
        }
        catch ( com.sun.star.uno.Exception e )
        {
            LogUtils.DEBUG("ParcelContainer.loadParcels caught exception processing folders for " + getParcelContainerDir()  );
            LogUtils.DEBUG("TRACE: " + LogUtils.getTrace(e) );
            throw new com.sun.star.lang.WrappedTargetException( e.toString() );
        }
    }

    public  XNameContainer createParcel(String name) throws ElementExistException, com.sun.star.lang.WrappedTargetException
    {
        Parcel p = null;
        if ( hasByName( name ) )
        {
             throw new ElementExistException( "Parcel " + name + " already exists" );
        }
        String pathToParcel =  PathUtils.make_url( getParcelContainerDir() ,  name );

        try
        {
            LogUtils.DEBUG("ParcelContainer.createParcel, creating folder " + pathToParcel );
            m_xSFA.createFolder( pathToParcel );

            LogUtils.DEBUG("ParcelContainer.createParcel, folder " + pathToParcel  + " created ");

            ParcelDescriptor pd = new ParcelDescriptor();
            pd.setLanguage( language );
            String parcelDesc = PathUtils.make_url( pathToParcel, ParcelDescriptor.PARCEL_DESCRIPTOR_NAME );
            XSimpleFileAccess2 xSFA2 = UnoRuntime.queryInterface( XSimpleFileAccess2.class, m_xSFA );
            if ( xSFA2 != null )
            {
                LogUtils.DEBUG("createParcel() Using XSIMPLEFILEACCESS2 " + parcelDesc );
                ByteArrayOutputStream bos = new ByteArrayOutputStream( 1024 );
                pd.write( bos );
                bos.close();
                ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray() );
                XInputStreamImpl xis = new XInputStreamImpl( bis );
                xSFA2.writeFile( parcelDesc, xis );
                xis.closeInput();
                p = loadParcel( pathToParcel );
            }
        }
        catch ( Exception e )
        {

            LogUtils.DEBUG("createParcel() Exception while attempting to create = " + name );
            throw  new com.sun.star.lang.WrappedTargetException( e.toString() );
        }
        return p;
    }

    public Parcel loadParcel( String parcelUrl ) throws com.sun.star.lang.WrappedTargetException, com.sun.star.lang.IllegalArgumentException
    {

        String name = null;

        String parcelDescUrl =  PathUtils.make_url( parcelUrl,  ParcelDescriptor.PARCEL_DESCRIPTOR_NAME );
        Parcel parcel = null;

        XInputStream xis = null;
        InputStream is = null;

        try
        {
            if ( m_xSFA.exists( parcelDescUrl ) )
            {
                LogUtils.DEBUG("ParcelContainer.loadParcel opening " + parcelDescUrl );
                xis = m_xSFA.openFileRead( parcelDescUrl );
                is = new XInputStreamWrapper( xis );

                ParcelDescriptor pd = new ParcelDescriptor(is) ;
                try
                {
                    is.close();
                    is = null;
                }
                catch ( Exception e )
                {
                    LogUtils.DEBUG("ParcelContainer.loadParcel Exception when closing stream for  " + parcelDescUrl + " :" + e );
                }
                LogUtils.DEBUG("ParcelContainer.loadParcel closed " + parcelDescUrl );
                if ( !pd.getLanguage().equals( language ) )
                {
                    LogUtils.DEBUG("ParcelContainer.loadParcel Language of Parcel does not match this container ");
                    return null;
                }
                LogUtils.DEBUG("Processing " + parcelDescUrl + " closed " );

                int indexOfSlash = parcelUrl.lastIndexOf("/");
                name = parcelUrl.substring( indexOfSlash + 1 );

                parcel = new Parcel( m_xSFA, this, pd, name );

                LogUtils.DEBUG(" ParcelContainer.loadParcel created parcel for " + parcelDescUrl + " for language " + language );
                parcels.add( parcel );
            }
            else
            {
                throw new java.io.IOException( parcelDescUrl + " does NOT exist!");
            }
        }
        catch ( com.sun.star.ucb.CommandAbortedException e )
        {

            LogUtils.DEBUG("loadParcel() Exception while accessing filesystem url = " + parcelDescUrl + e );
            throw  new com.sun.star.lang.WrappedTargetException( e.toString() );
        }
        catch ( java.io.IOException e )
        {
            LogUtils.DEBUG("ParcelContainer.loadParcel() caught IOException while accessing " + parcelDescUrl + ": " + e );
            throw  new com.sun.star.lang.WrappedTargetException( e.toString() );
        }
        catch (  com.sun.star.uno.Exception e )
        {

            LogUtils.DEBUG("loadParcel() Exception while accessing filesystem url = " + parcelDescUrl + e );
            throw  new com.sun.star.lang.WrappedTargetException( e.toString() );
        }

        finally
        {
            if ( is != null )
            {
                try
                {
                    is.close(); // is will close xis
                }
                catch ( Exception ignore )
                {
                }
            }
            else if ( xis != null )
            {
                try
                {
                    xis.closeInput();
                }
                catch ( Exception ignore )
                {
                }
            }
        }
        return parcel;
    }
    public void renameParcel(String oldName, String newName) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
    {
        LogUtils.DEBUG(" ** ParcelContainer Renaming parcel " + oldName + " to " + newName );
        LogUtils.DEBUG(" ** ParcelContainer is " + this );
        Parcel p = (Parcel)getByName( oldName );
        if ( p == null )
        {
            throw new com.sun.star.container.NoSuchElementException( "No parcel named " + oldName );
        }
        String oldParcelDirUrl = PathUtils.make_url( getParcelContainerDir(),
            oldName );
        String newParcelDirUrl = PathUtils.make_url( getParcelContainerDir(),
            newName );
        try
        {
            if (!m_xSFA.isFolder( oldParcelDirUrl ) )
            {
                Exception e = new com.sun.star.io.IOException("Invalid Parcel directory: " + oldName );
                throw new com.sun.star.lang.WrappedTargetException( e.toString() );
            }
            LogUtils.DEBUG(" ** ParcelContainer Renaming folder " + oldParcelDirUrl + " to " + newParcelDirUrl );
            m_xSFA.move( oldParcelDirUrl, newParcelDirUrl );
        }
        catch ( com.sun.star.ucb.CommandAbortedException ce )
        {
            LogUtils.DEBUG(" ** ParcelContainer Renaming failed with " + ce  );
            throw new com.sun.star.lang.WrappedTargetException( ce.toString() );
        }
        catch ( com.sun.star.uno.Exception e )
        {
            LogUtils.DEBUG(" ** ParcelContainer Renaming failed with " + e  );
            throw new com.sun.star.lang.WrappedTargetException( e.toString() );
        }

        p.rename( newName );
    }
    // removes but doesn't physically delele parcel from container
    public boolean removeParcel(String name) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
    {
        boolean result = false;
        Parcel p = (Parcel)getByName( name );
        if ( p == null )
        {
            throw new com.sun.star.container.NoSuchElementException("No parcel named " + name );
        }

        result =  parcels.remove( p );
        return result;
    }
    public boolean deleteParcel(String name) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
    {
        LogUtils.DEBUG( "deleteParcel for containerURL " + containerUrl + " name = " + name  + " Langueg = " + language );
        boolean result = false;

        Parcel p = (Parcel)getByName( name );
        if ( p == null )
        {
            throw new com.sun.star.container.NoSuchElementException("No parcel named " + name );
        }

        try
        {
            String pathToParcel =  PathUtils.make_url( getParcelContainerDir(),   name );
            m_xSFA.kill( pathToParcel );
        }
        catch( Exception e )
        {
            LogUtils.DEBUG("Error deleteing parcel " + name );
            throw new com.sun.star.lang.WrappedTargetException( e.toString() );
        }

        result =  parcels.remove( p );
        return result;
    }

    public String getLanguage() { return language; }

    public ScriptMetaData findScript( ParsedScriptUri  parsedUri ) throws  com.sun.star.container.NoSuchElementException,  com.sun.star.lang.WrappedTargetException
    {
        ScriptMetaData scriptData = null;
        Parcel p = null;
        p = (Parcel)getByName( parsedUri.parcel);
        scriptData = (ScriptMetaData)p.getByName( parsedUri.function);
        LogUtils.DEBUG("** found script data for " +  parsedUri.function + " script is " + scriptData );
        return scriptData;

    }
public  ParsedScriptUri parseScriptUri( String scriptURI ) throws com.sun.star.lang.IllegalArgumentException
{

        XMultiComponentFactory xMcFac = null;
        XUriReferenceFactory xFac = null;

        try
        {
            xMcFac = m_xCtx.getServiceManager();
            xFac = UnoRuntime.queryInterface( XUriReferenceFactory.class,
                xMcFac.createInstanceWithContext(
                    "com.sun.star.uri.UriReferenceFactory", m_xCtx ) );
        }
        catch( com.sun.star.uno.Exception e )
        {
            LogUtils.DEBUG("Problems parsing  URL:" + e.toString() );
            throw new  com.sun.star.lang.IllegalArgumentException( "Problems parsing  URL reason: " + e.toString() );
        }
        if ( xFac == null )
        {
            LogUtils.DEBUG("Failed to create UrlReference factory");
            throw new  com.sun.star.lang.IllegalArgumentException( "Failed to create UrlReference factory for url " + scriptURI );
        }

        XUriReference uriRef = xFac.parse( scriptURI );
        XVndSunStarScriptUrl  sfUri = UnoRuntime.queryInterface( XVndSunStarScriptUrl.class, uriRef );

        if ( sfUri == null )
        {
            LogUtils.DEBUG("Failed to parse url");
            throw new  com.sun.star.lang.IllegalArgumentException( "Failed to parse url " + scriptURI );
        }

        ParsedScriptUri parsedUri = new ParsedScriptUri();
        // parse language
        parsedUri.language = sfUri.getParameter("language");
        parsedUri.function= sfUri.getName();
        parsedUri.parcel = "";

        // parse parcel name;
        StringTokenizer tokenizer = new StringTokenizer( parsedUri.function, "." );

        if ( tokenizer.hasMoreElements() )
        {
            parsedUri.parcel = (String)tokenizer.nextElement();
            LogUtils.DEBUG("** parcelName = " + parsedUri.parcel );
        }

        if ( parsedUri.function != null && ( parsedUri.function.length() > 0 ) )
        {
            // strip out parcel name
            parsedUri.function = parsedUri.function.substring( parsedUri.parcel.length() + 1);
        }

        // parse location
        parsedUri.location = sfUri.getParameter("location");

        // TODO basic sanity check on language, location, functioname, parcel
        // should be correct e.g. verified  by MSP and LangProvider by the
        // time its got to here

        LogUtils.DEBUG("** location = " + parsedUri.location +
            "\nfunction = " + parsedUri.function +
            "\nparcel = " + parsedUri.parcel +
            "\nlocation = " + parsedUri.location );
        return parsedUri;
}


}