1 /*************************************************************************
2 *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26 ************************************************************************/
27 package com.sun.star.script.framework.provider;
28 
29 import com.sun.star.container.XNameContainer;
30 
31 import com.sun.star.uno.Exception;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.lang.XInitialization;
35 import com.sun.star.lang.XTypeProvider;
36 import com.sun.star.lang.XServiceInfo;
37 import com.sun.star.frame.XModel;
38 
39 import com.sun.star.util.XMacroExpander;
40 
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 
45 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.beans.XVetoableChangeListener;
47 import com.sun.star.beans.XPropertyChangeListener;
48 import com.sun.star.beans.XPropertySetInfo;
49 import com.sun.star.beans.Property;
50 
51 import com.sun.star.beans.XIntrospectionAccess;
52 import com.sun.star.script.XInvocation;
53 
54 import com.sun.star.script.provider.XScriptContext;
55 import com.sun.star.script.provider.XScriptProvider;
56 import com.sun.star.script.provider.XScript;
57 import com.sun.star.script.provider.ScriptFrameworkErrorException;
58 import com.sun.star.script.provider.ScriptFrameworkErrorType;
59 
60 import com.sun.star.script.browse.XBrowseNode;
61 import com.sun.star.script.browse.BrowseNodeTypes;
62 
63 import com.sun.star.script.framework.log.LogUtils;
64 
65 import com.sun.star.script.framework.container.ScriptMetaData;
66 import com.sun.star.script.framework.container.XMLParserFactory;
67 import com.sun.star.script.framework.container.ParcelContainer;
68 import com.sun.star.script.framework.container.ParsedScriptUri;
69 import com.sun.star.script.framework.container.UnoPkgContainer;
70 
71 import com.sun.star.ucb.Command;
72 import com.sun.star.ucb.XContentProvider;
73 import com.sun.star.ucb.XContent;
74 import com.sun.star.ucb.XCommandProcessor;
75 import com.sun.star.ucb.XContentIdentifier;
76 import com.sun.star.ucb.XContentIdentifierFactory;
77 
78 import com.sun.star.sdbc.XRow;
79 
80 import com.sun.star.script.framework.browse.ProviderBrowseNode;
81 import com.sun.star.script.framework.browse.DialogFactory;
82 
83 import com.sun.star.deployment.XPackage;
84 
85 
86 import com.sun.star.document.XScriptInvocationContext;
87 import com.sun.star.frame.XTransientDocumentsDocumentContentFactory;
88 import com.sun.star.uno.TypeClass;
89 
90 public abstract class ScriptProvider
91     implements XScriptProvider, XBrowseNode, XPropertySet, XInvocation,
92                XInitialization, XTypeProvider, XServiceInfo, XNameContainer
93 {
94     private final String[] __serviceNames = {
95         "com.sun.star.script.provider.ScriptProviderFor",
96         "com.sun.star.script.provider.LanguageScriptProvider"
97     };
98 
99     public final static String CLASSPATH = "classpath";
100 
101     protected String language;
102 
103     protected XComponentContext m_xContext;
104     protected XMultiComponentFactory m_xMultiComponentFactory;
105     protected XModel m_xModel;
106     protected XScriptInvocationContext m_xInvocContext;
107     protected ParcelContainer m_container;
108 
109     // proxies to helper objects which implement interfaces
110     private XPropertySet m_xPropertySetProxy;
111     private XInvocation m_xInvocationProxy;
112    // TODO should this be implemented in this class
113     private XBrowseNode m_xBrowseNodeProxy;
114     private XScriptContext m_xScriptContext;
115 
116     public ScriptProvider( XComponentContext ctx, String language )
117     {
118         this.language = language;
119         __serviceNames[0] += language;
120 
121         LogUtils.DEBUG( "ScriptProvider: constructor - start. " + language );
122 
123         m_xContext = ctx;
124 
125         // Initialize DialogFactory class in case dialogs are required
126         DialogFactory.createDialogFactory(m_xContext);
127 
128         try
129         {
130             m_xMultiComponentFactory = m_xContext.getServiceManager();
131 
132             if ( m_xMultiComponentFactory == null )
133             {
134                 throw new Exception( "Error could not obtain a " +
135                     "multicomponent factory - rethrowing Exception." );
136             }
137 
138             Object serviceObj = m_xContext.getValueByName(
139                 "/singletons/com.sun.star.util.theMacroExpander");
140 
141             XMacroExpander me = (XMacroExpander) AnyConverter.toObject(
142                 new Type(XMacroExpander.class), serviceObj);
143 
144             XMLParserFactory.setOfficeDTDURL(me.expandMacros(
145                 "${$BRAND_BASE_DIR/program/bootstraprc::BaseInstallation}/share/dtd/officedocument/1_0/"));
146 
147         }
148         catch ( Exception e )
149         {
150             LogUtils.DEBUG( LogUtils.getTrace( e ) );
151             throw new com.sun.star.uno.RuntimeException(
152                 "Error constructing  ScriptProvider: "
153                 + e.getMessage() );
154         }
155 
156         LogUtils.DEBUG( "ScriptProvider: constructor - finished." );
157     }
158 
159     synchronized public XScriptContext getScriptingContext()
160     {
161         if ( m_xScriptContext == null )
162         {
163             m_xScriptContext = ScriptContext.createContext( m_xModel, m_xInvocContext, m_xContext, m_xMultiComponentFactory );
164         }
165         return m_xScriptContext;
166     }
167     public void initialize( Object[] aArguments )
168         throws com.sun.star.uno.Exception
169     {
170         LogUtils.DEBUG( "entering XInit for language " + language);
171         boolean isPkgProvider = false;
172         if( aArguments.length == 1 )
173         {
174             String contextUrl = null;
175             if ( AnyConverter.getType(aArguments[0]).getTypeClass().equals(TypeClass.INTERFACE) )
176             {
177                 // try whether it denotes a XScriptInvocationContext
178                 m_xInvocContext = (XScriptInvocationContext)UnoRuntime.queryInterface(
179                         XScriptInvocationContext.class, aArguments[0]);
180                 if ( m_xInvocContext != null )
181                 {
182                     // if so, obtain the document - by definition, this must be
183                     // the ScriptContainer
184                     m_xModel = (XModel)UnoRuntime.queryInterface( XModel.class,
185                             m_xInvocContext.getScriptContainer() );
186                 }
187                 else
188                 {
189                     // otherwise, check whether it's an XModel
190                     m_xModel = (XModel)UnoRuntime.queryInterface( XModel.class,
191                             m_xInvocContext.getScriptContainer() );
192                 }
193                 if ( m_xModel == null )
194                 {
195                     throw new com.sun.star.uno.Exception(
196                         "ScriptProvider argument must be either a string, a valid XScriptInvocationContext, " +
197                         "or an XModel", this);
198                 }
199 
200                 contextUrl = getDocUrlFromModel( m_xModel );
201                 m_container = new ParcelContainer( m_xContext, contextUrl, language  );
202             }
203             else if (AnyConverter.isString(aArguments[0]) == true)
204             {
205                 String originalContextURL = AnyConverter.toString(aArguments[0]);
206                 LogUtils.DEBUG("creating Application, path: " + originalContextURL );
207                 contextUrl = originalContextURL;
208                 // TODO no support for packages in documents yet
209                 if ( originalContextURL.startsWith( "vnd.sun.star.tdoc" ) )
210                 {
211                     m_container = new ParcelContainer( m_xContext, contextUrl, language  );
212                     m_xModel = getModelFromDocUrl( originalContextURL );
213                 }
214                 else
215                 {
216                     String extensionDb = null;
217                     String extensionRepository = null;
218                     if ( originalContextURL.startsWith( "bundled" ) )
219                     {
220                         contextUrl = "vnd.sun.star.expand:$BUNDLED_EXTENSIONS";
221                         extensionDb = "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
222                         extensionRepository = "bundled";
223                     }
224 
225                     if ( originalContextURL.startsWith( "share" ) )
226                     {
227                         contextUrl = "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + PathUtils.BOOTSTRAP_NAME + "::BaseInstallation}/share";
228                         extensionDb = "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
229                         extensionRepository = "shared";
230                     }
231                     else if ( originalContextURL.startsWith( "user" ) )
232                     {
233                         contextUrl = "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
234                         extensionDb = "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
235                         extensionRepository = "user";
236                     }
237 
238                     if ( originalContextURL.endsWith( "uno_packages") )
239                     {
240                         isPkgProvider = true;
241                     }
242                     if ( originalContextURL.endsWith( "uno_packages") &&  !originalContextURL.equals( contextUrl  )
243                             && !extensionRepository.equals("bundled"))
244                     {
245                         contextUrl = PathUtils.make_url( contextUrl, "uno_packages"  );
246                     }
247                     if ( isPkgProvider )
248                     {
249                         m_container = new UnoPkgContainer( m_xContext, contextUrl, extensionDb, extensionRepository, language  );
250                     }
251                     else
252                     {
253                         m_container = new ParcelContainer( m_xContext, contextUrl, language  );
254                     }
255                 }
256             }
257             else
258             {
259                 throw new com.sun.star.uno.RuntimeException(
260                     "ScriptProvider created with invalid argument");
261             }
262 
263             LogUtils.DEBUG("Modified Application path is: " + contextUrl );
264             LogUtils.DEBUG("isPkgProvider is: " + isPkgProvider );
265 
266             // TODO should all be done in this class instead of
267             // deleagation????
268             m_xBrowseNodeProxy = new ProviderBrowseNode( this,
269                 m_container, m_xContext );
270 
271             m_xInvocationProxy = (XInvocation)UnoRuntime.queryInterface(XInvocation.class, m_xBrowseNodeProxy);
272             m_xPropertySetProxy = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, m_xBrowseNodeProxy);
273         }
274         else
275         {
276             // this is ok, for example when executing a script from the
277             // command line
278             LogUtils.DEBUG( "ScriptProviderFor" + language +
279                 " initialized without a context");
280         }
281         LogUtils.DEBUG( "leaving XInit" );
282     }
283 
284     /**
285      *  Gets the types attribute of the ScriptProvider object
286      *
287      * @return    The types value
288      */
289     public com.sun.star.uno.Type[] getTypes()
290     {
291         Type[] retValue = new Type[ 8 ];
292         retValue[ 0 ] = new Type( XScriptProvider.class );
293         retValue[ 1 ] = new Type( XBrowseNode.class );
294         retValue[ 2 ] = new Type( XInitialization.class );
295         retValue[ 3 ] = new Type( XTypeProvider.class );
296         retValue[ 4 ] = new Type( XServiceInfo.class );
297         retValue[ 5 ] = new Type( XPropertySet.class );
298         retValue[ 6 ] = new Type( XInvocation.class );
299         retValue[ 7 ] = new Type( com.sun.star.container.XNameContainer.class );
300         return retValue;
301     }
302 
303     /**
304      *  Gets the implementationId attribute of the ScriptProvider object
305      *
306      * @return    The implementationId value
307      */
308     public byte[] getImplementationId()
309     {
310         return this.getClass().getName().getBytes();
311     }
312 
313     /**
314      *  Gets the implementationName attribute of the ScriptProvider object
315      *
316      * @return    The implementationName value
317      */
318     public String getImplementationName()
319     {
320         return getClass().getName();
321     }
322 
323     /**
324      *  Description of the Method
325      *
326      * @param  serviceName  Description of the Parameter
327      * @return              Description of the Return Value
328      */
329     public boolean supportsService( String serviceName )
330     {
331         for ( int index = __serviceNames.length; index-- > 0; )
332         {
333             if ( serviceName.equals( __serviceNames[ index ]  ) )
334             {
335                 return true;
336             }
337         }
338         return false;
339     }
340 
341     /**
342      *  Gets the supportedServiceNames attribute of the ScriptProvider object
343      *
344      * @return    The supportedServiceNames value
345      */
346     public String[] getSupportedServiceNames()
347     {
348         return __serviceNames;
349     }
350 
351 
352 
353     public abstract XScript getScript( /*IN*/String scriptURI )
354         throws com.sun.star.uno.RuntimeException,
355                ScriptFrameworkErrorException;
356 
357     // TODO need to encapsulate this better,
358     // Some factory concept for creating/accessing Editor
359     // How this is passed down or how it is accessable by BrowseNode
360     // implementations needs thinking about
361     // This method is used to determine whether the ScriptProvider
362     // has a ScriptEditor
363     public abstract boolean hasScriptEditor();
364     // TODO see above
365     // This method is used to get the ScriptEditor for this ScriptProvider
366     public abstract ScriptEditor getScriptEditor();
367 
368     public ScriptMetaData  getScriptData( /*IN*/String scriptURI ) throws ScriptFrameworkErrorException
369 
370     {
371         ParsedScriptUri details = null;
372         try
373         {
374             details = m_container.parseScriptUri( scriptURI );
375             ScriptMetaData scriptData = m_container.findScript( details );
376             if ( scriptData == null )
377             {
378                 throw new ScriptFrameworkErrorException( details.function + " does not exist",
379                     null, details.function, language, ScriptFrameworkErrorType.NO_SUCH_SCRIPT );
380             }
381             return scriptData;
382         }
383         catch (  com.sun.star.lang.IllegalArgumentException ila )
384         {
385             // TODO specify the correct error Type
386             throw new ScriptFrameworkErrorException( ila.getMessage(),
387                 null, scriptURI, language, ScriptFrameworkErrorType.UNKNOWN );
388         }
389         catch ( com.sun.star.container.NoSuchElementException nse )
390         {
391             throw new ScriptFrameworkErrorException( nse.getMessage(),
392                 null, details.function, language, ScriptFrameworkErrorType.NO_SUCH_SCRIPT );
393         }
394         catch ( com.sun.star.lang.WrappedTargetException wta )
395         {
396             // TODO specify the correct error Type
397             Exception wrapped = (Exception)wta.TargetException;
398             String message = wta.getMessage();
399             if ( wrapped != null )
400             {
401                 message = wrapped.getMessage();
402             }
403             throw new ScriptFrameworkErrorException( message,
404                 null, details.function, language, ScriptFrameworkErrorType.UNKNOWN );
405         }
406 
407     }
408 
409 
410     // Implementation of XBrowseNode interface
411     public String getName()
412     {
413         return language;
414     }
415 
416     public XBrowseNode[] getChildNodes()
417     {
418         if ( m_xBrowseNodeProxy  == null )
419         {
420             LogUtils.DEBUG("No Nodes available ");
421             return new XBrowseNode[0];
422         }
423         return m_xBrowseNodeProxy .getChildNodes();
424     }
425 
426     public boolean hasChildNodes()
427     {
428         if (  m_xBrowseNodeProxy == null )
429         {
430             LogUtils.DEBUG("No Nodes available ");
431             return false;
432         }
433         return  m_xBrowseNodeProxy.hasChildNodes();
434     }
435 
436     public short getType()
437     {
438         return BrowseNodeTypes.CONTAINER;
439     }
440 
441     public String toString()
442     {
443         return getName();
444     }
445 
446     // implementation of XInvocation interface
447     public XIntrospectionAccess getIntrospection() {
448         return m_xInvocationProxy.getIntrospection();
449     }
450 
451     public Object invoke(String aFunctionName, Object[] aParams,
452                          short[][] aOutParamIndex, Object[][] aOutParam)
453         throws com.sun.star.lang.IllegalArgumentException,
454                com.sun.star.script.CannotConvertException,
455                com.sun.star.reflection.InvocationTargetException
456     {
457         return m_xInvocationProxy.invoke(
458             aFunctionName, aParams, aOutParamIndex, aOutParam);
459     }
460 
461     public void setValue(String aPropertyName, Object aValue)
462         throws com.sun.star.beans.UnknownPropertyException,
463                com.sun.star.script.CannotConvertException,
464                com.sun.star.reflection.InvocationTargetException
465     {
466         m_xInvocationProxy.setValue(aPropertyName, aValue);
467     }
468 
469     public Object getValue(String aPropertyName)
470         throws com.sun.star.beans.UnknownPropertyException
471     {
472         return m_xInvocationProxy.getValue(aPropertyName);
473     }
474 
475     public boolean hasMethod(String aName) {
476         return m_xInvocationProxy.hasMethod(aName);
477     }
478 
479     public boolean hasProperty(String aName) {
480         return m_xInvocationProxy.hasProperty(aName);
481     }
482 
483     public XPropertySetInfo getPropertySetInfo()
484     {
485         return m_xPropertySetProxy.getPropertySetInfo();
486     }
487 
488     public void setPropertyValue(String aPropertyName, Object aValue)
489         throws com.sun.star.beans.UnknownPropertyException,
490                com.sun.star.beans.PropertyVetoException,
491                com.sun.star.lang.IllegalArgumentException,
492                com.sun.star.lang.WrappedTargetException
493     {
494         m_xPropertySetProxy.setPropertyValue(aPropertyName, aValue);
495     }
496 
497     public Object getPropertyValue(String PropertyName)
498         throws com.sun.star.beans.UnknownPropertyException,
499                com.sun.star.lang.WrappedTargetException
500     {
501         return m_xPropertySetProxy.getPropertyValue(PropertyName);
502     }
503 
504     public void addPropertyChangeListener(
505         String aPropertyName, XPropertyChangeListener xListener)
506         throws com.sun.star.beans.UnknownPropertyException,
507                com.sun.star.lang.WrappedTargetException
508     {
509         m_xPropertySetProxy.addPropertyChangeListener(aPropertyName, xListener);
510     }
511 
512     public void removePropertyChangeListener(
513         String aPropertyName, XPropertyChangeListener aListener)
514         throws com.sun.star.beans.UnknownPropertyException,
515                com.sun.star.lang.WrappedTargetException
516     {
517         m_xPropertySetProxy.removePropertyChangeListener(
518             aPropertyName, aListener);
519     }
520 
521     public void addVetoableChangeListener(
522         String PropertyName, XVetoableChangeListener aListener)
523         throws com.sun.star.beans.UnknownPropertyException,
524                com.sun.star.lang.WrappedTargetException
525     {
526         m_xPropertySetProxy.addVetoableChangeListener(PropertyName, aListener);
527     }
528 
529     public void removeVetoableChangeListener(
530         String PropertyName, XVetoableChangeListener aListener)
531         throws com.sun.star.beans.UnknownPropertyException,
532                com.sun.star.lang.WrappedTargetException
533     {
534         m_xPropertySetProxy.removeVetoableChangeListener(
535             PropertyName, aListener);
536     }
537     public java.lang.Object getByName( String aName ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
538     {
539         // TODO needs implementing?
540         if ( true )
541         {
542             throw new com.sun.star.uno.RuntimeException(
543                 "getByName not implemented" );
544         }
545         return new Object();
546     }
547 
548     public String[] getElementNames()
549     {
550         // TODO needs implementing?
551         String[] result = new String[0];
552         if ( true )
553         {
554             throw new com.sun.star.uno.RuntimeException(
555                 "getElementNames not implemented" );
556 
557         }
558         return result;
559     }
560 
561 
562     // Performs the getRegStatus functionality for the PkgMgr
563     public boolean hasByName( String aName )
564     {
565         boolean result = false;
566         if ( ((UnoPkgContainer)m_container).hasRegisteredUnoPkgContainer( aName ) )
567         {
568             result = true;
569         }
570         return result;
571     }
572 
573     public com.sun.star.uno.Type getElementType()
574     {
575         // TODO at the moment this returns void indicating
576         // type is unknown should indicate XPackage ? do we implement XPackage
577         return new Type();
578     }
579 
580     public boolean hasElements()
581     {
582         // TODO needs implementing?
583         boolean result = false;
584         if ( true )
585         {
586             throw new com.sun.star.uno.RuntimeException(
587                 "hasElements not implemented" );
588 
589         }
590         return result;
591     }
592     public void replaceByName( String aName, java.lang.Object aElement ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
593     {
594         // TODO needs implementing
595         if ( true )
596         {
597             throw new com.sun.star.uno.RuntimeException(
598                 "replaceByName not implemented" );
599 
600         }
601     }
602 
603     public void insertByName( String aName, java.lang.Object aElement ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException
604     {
605         LogUtils.DEBUG("Provider for " + language + " received register for package " + aName );
606         XPackage newPackage = ( XPackage ) UnoRuntime.queryInterface( XPackage.class, aElement );
607         if ( aName.length() == 0 )
608         {
609             throw new  com.sun.star.lang.IllegalArgumentException( "Empty name" );
610         }
611         if ( newPackage == null )
612         {
613             throw new com.sun.star.lang.IllegalArgumentException( "No package supplied" );
614         }
615 
616         ((UnoPkgContainer)m_container).processUnoPackage( newPackage, language  );
617     }
618 
619     // de-register for library only !!
620     public void removeByName( String Name ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException
621     {
622         LogUtils.DEBUG("In ScriptProvider.removeByName() for " + Name + " this provider = " + language );
623         ParcelContainer c = ((UnoPkgContainer)m_container).getRegisteredUnoPkgContainer( Name );
624         if ( c != null )
625         {
626             String libName;
627             if (Name.endsWith("/"))
628             {
629                 String tmp = Name.substring( 0, Name.lastIndexOf( "/" ) );
630                 libName = tmp.substring( tmp.lastIndexOf( "/" ) + 1 );
631             }
632             else
633             {
634                 libName = Name.substring( Name.lastIndexOf( "/" ) + 1 );
635             }
636             LogUtils.DEBUG("Deregistering library " + libName );
637             if ( c.removeParcel( libName ) )
638             {
639                 ((UnoPkgContainer)m_container).deRegisterPackageContainer( Name );
640             }
641             else
642             {
643                 throw new com.sun.star.container.NoSuchElementException( libName + " cannot be removed from container." );
644             }
645         }
646         else
647         {
648             throw new com.sun.star.container.NoSuchElementException( Name + " doesn't exist for " + language );
649         }
650         // TODO see if we want to remove the ParcelContainer is no Parcels/Libraries left
651     }
652 
653     private String getDocUrlFromModel( XModel document )
654     {
655         XTransientDocumentsDocumentContentFactory factory = null;
656         try
657         {
658             factory = (XTransientDocumentsDocumentContentFactory)UnoRuntime.queryInterface(
659                     XTransientDocumentsDocumentContentFactory.class,
660                         m_xMultiComponentFactory.createInstanceWithContext(
661                             "com.sun.star.frame.TransientDocumentsDocumentContentFactory",
662                             m_xContext
663                         )
664                     );
665         }
666         catch (Exception ex)
667         {
668         }
669 
670         if ( factory == null )
671             throw new com.sun.star.uno.RuntimeException( "ScriptProvider: unable to create a TDOC context factory.", this );
672 
673         try
674         {
675             XContent content = factory.createDocumentContent( document );
676             return content.getIdentifier().getContentIdentifier();
677         }
678         catch( Exception ex )
679         {
680         }
681 
682         LogUtils.DEBUG("unable to determine the model's TDOC URL");
683         return "";
684     }
685 
686     private  XModel getModelFromDocUrl( String docUrl )
687     {
688         LogUtils.DEBUG("getModelFromDocUrl - searching for match for ->" + docUrl + "<-" );
689         XModel xModel = null;
690         try
691         {
692             Object[] args = new String[] {"Local", "Office" };
693 
694             Object ucb = m_xMultiComponentFactory.createInstanceWithArgumentsAndContext( "com.sun.star.ucb.UniversalContentBroker", args, m_xContext );
695 
696 
697             XContentIdentifierFactory xFac  =  ( XContentIdentifierFactory )
698                 UnoRuntime.queryInterface( XContentIdentifierFactory.class,
699                     ucb );
700 
701 
702             XContentIdentifier xCntId = xFac.createContentIdentifier( docUrl );
703 
704 
705             XContentProvider xCntAccess = ( XContentProvider )
706                 UnoRuntime.queryInterface( XContentProvider.class,
707                     ucb );
708 
709 
710             XContent xCnt = xCntAccess.queryContent( xCntId );
711 
712 
713             XCommandProcessor xCmd = ( XCommandProcessor )
714                 UnoRuntime.queryInterface( XCommandProcessor.class, xCnt );
715 
716 
717             Property[] pArgs = new Property[ ] { new Property() };
718             pArgs[ 0 ].Name = "DocumentModel";
719             pArgs[ 0 ].Handle = -1;
720 
721             Command command = new Command();
722 
723             command.Handle = -1;
724             command.Name = "getPropertyValues";
725             command.Argument = pArgs;
726 
727             com.sun.star.ucb.XCommandEnvironment env = null ;
728             Object result =  xCmd.execute( command, 0, env ) ;
729 
730             XRow values = ( XRow ) UnoRuntime.queryInterface( XRow.class,
731                 result );
732 
733             xModel = ( XModel )  UnoRuntime.queryInterface( XModel.class,
734                 values.getObject( 1, null ) );
735         }
736         catch ( Exception ignore )
737         {
738             LogUtils.DEBUG("Failed to get model exception " + ignore );
739 
740         }
741         return xModel;
742     }
743 
744 
745 }
746