Difference between revisions of "Documentation/DevGuide/OfficeDev/Resolving Path Variables"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (typo)
 
Line 9: Line 9:
 
{{Documentation/DevGuideLanguages|Documentation/DevGuide/OfficeDev/{{SUBPAGENAME}}}}  
 
{{Documentation/DevGuideLanguages|Documentation/DevGuide/OfficeDev/{{SUBPAGENAME}}}}  
 
  {{DISPLAYTITLE:Resolving Path Variables}}
 
  {{DISPLAYTITLE:Resolving Path Variables}}
This section explains how to use the {{PRODUCTNAME}} implementation of the path substitution service. The following code snippet creates a path substitution service.
+
This section explains how to use the {{AOo}} implementation of the path substitution service. The following code snippet creates a path substitution service.
<source lang="java">
+
<syntaxhighlight lang="java">
 
   import com.sun.star.lang.XMultiServiceFactory;
 
   import com.sun.star.lang.XMultiServiceFactory;
 
   import com.sun.star.uno.Exception;
 
   import com.sun.star.uno.Exception;
Line 42: Line 42:
 
           return null;
 
           return null;
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
The service is implemented as a one-instance service and supports the interface <idl>com.sun.star.util.XStringSubstitution</idl>. The interface has three methods:
 
The service is implemented as a one-instance service and supports the interface <idl>com.sun.star.util.XStringSubstitution</idl>. The interface has three methods:
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   string substituteVariables( [in] string aText, [in] boolean bSubstRequired )
 
   string substituteVariables( [in] string aText, [in] boolean bSubstRequired )
 
   string reSubstituteVariables( [in] string aText )
 
   string reSubstituteVariables( [in] string aText )
 
   string getSubstituteVariableValue( [in] string variable )
 
   string getSubstituteVariableValue( [in] string variable )
</source>
+
</syntaxhighlight>
 
The method <code>substituteVariables()</code> returns a string where all known variables are replaced by their value. Unknown variables are not replaced. The argument <code>bSubstRequired</code> can be used to indicate that the client needs a full substitution - otherwise the function fails and throws a <idl>com.sun.star.container.NoSuchElementException</idl>. For example: ''$(inst)/share/autotext/$(vlang)'' could be substituted to ''file:///c:/OpenOffice.org1.0.2/share/autotext/english''.
 
The method <code>substituteVariables()</code> returns a string where all known variables are replaced by their value. Unknown variables are not replaced. The argument <code>bSubstRequired</code> can be used to indicate that the client needs a full substitution - otherwise the function fails and throws a <idl>com.sun.star.container.NoSuchElementException</idl>. For example: ''$(inst)/share/autotext/$(vlang)'' could be substituted to ''file:///c:/OpenOffice.org1.0.2/share/autotext/english''.
  
Line 57: Line 57:
 
The method <code>getSubstituteVariableValue()</code> returns the current value of the provided path variable as a predefined or a user-defined value. If an unknown variable name is provided, a <idl>com.sun.star.container.NoSuchElementException</idl> is thrown. The argument variable can be provided with preceding "<code>$(</code>" and succeeding "<code>)</code>" or without them. So both ''$(work)'' and ''work'' can be used.
 
The method <code>getSubstituteVariableValue()</code> returns the current value of the provided path variable as a predefined or a user-defined value. If an unknown variable name is provided, a <idl>com.sun.star.container.NoSuchElementException</idl> is thrown. The argument variable can be provided with preceding "<code>$(</code>" and succeeding "<code>)</code>" or without them. So both ''$(work)'' and ''work'' can be used.
  
This code example shows how to access, substitute, and resubstitute path variables by means of the {{PRODUCTNAME}} API.
+
This code example shows how to access, substitute, and resubstitute path variables by means of the {{AOo}} API.
<source lang="java">
+
<syntaxhighlight lang="java">
 
   import com.sun.star.bridge.XUnoUrlResolver;
 
   import com.sun.star.bridge.XUnoUrlResolver;
 
   import com.sun.star.uno.UnoRuntime;
 
   import com.sun.star.uno.UnoRuntime;
Line 156: Line 156:
 
       }
 
       }
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Office Development]]
 
[[Category:Documentation/Developer's Guide/Office Development]]

Latest revision as of 13:25, 3 January 2021



This section explains how to use the Apache OpenOffice implementation of the path substitution service. The following code snippet creates a path substitution service.

  import com.sun.star.lang.XMultiServiceFactory;
  import com.sun.star.uno.Exception;
  import com.sun.star.uno.XInterface;
  import com.sun.star.util.XStringSubstitution
 
  XStringSubstitution createPathSubstitution() {
 
      /////////////////////////////////////////////////////////////////////
      // Obtain Process Service Manager.
      /////////////////////////////////////////////////////////////////////
 
      XMultiServiceFactory xServiceFactory = ...
 
      /////////////////////////////////////////////////////////////////////
      // Create Path Substitution. This needs to be done only once per process.
      /////////////////////////////////////////////////////////////////////
 
      XInterface xPathSubst;
      try {
          xPathSubst = xServiceFactory.createInstance(
              "com.sun.star.util.PathSubstitution" );
      }
      catch (com.sun.star.uno.Exception e) {
      }
 
      if (xPathSubst != null)
          return (XStringSubstitution)UnoRuntime.queryInterface( 
              XStringSubstitution.class, xPathSubst );
      else
          return null;
  }

The service is implemented as a one-instance service and supports the interface com.sun.star.util.XStringSubstitution. The interface has three methods:

  string substituteVariables( [in] string aText, [in] boolean bSubstRequired )
  string reSubstituteVariables( [in] string aText )
  string getSubstituteVariableValue( [in] string variable )

The method substituteVariables() returns a string where all known variables are replaced by their value. Unknown variables are not replaced. The argument bSubstRequired can be used to indicate that the client needs a full substitution - otherwise the function fails and throws a com.sun.star.container.NoSuchElementException. For example: $(inst)/share/autotext/$(vlang) could be substituted to file:///c:/OpenOffice.org1.0.2/share/autotext/english.

The method reSubstituteVariables() returns a string where parts of the provided path aText are replaced by variables that represent this part of the path. If a matching variable is not found, the path is not modified.

The predefined variable $(path) is not used for substitution. Instead, it is a placeholder for the path environment variable does not have a static value during runtime. The path variables $(lang), $(langid) and $(vlang), which represent a directory or a filename in a path, only match inside or at the end of a provided path. For example: english is not replaced by $(vlang), whereas file:///c:/english is replaced by file:///c:/$(vlang).

The method getSubstituteVariableValue() returns the current value of the provided path variable as a predefined or a user-defined value. If an unknown variable name is provided, a com.sun.star.container.NoSuchElementException is thrown. The argument variable can be provided with preceding "$(" and succeeding ")" or without them. So both $(work) and work can be used.

This code example shows how to access, substitute, and resubstitute path variables by means of the Apache OpenOffice API.

  import com.sun.star.bridge.XUnoUrlResolver;
  import com.sun.star.uno.UnoRuntime;
  import com.sun.star.uno.XComponentContext;
  import com.sun.star.lang.XMultiComponentFactory;
  import com.sun.star.beans.XPropertySet;
  import com.sun.star.beans.PropertyValue;
 
  import com.sun.star.util.XStringSubstitution;
  import com.sun.star.frame.TerminationVetoException;
  import com.sun.star.frame.XTerminateListener;
 
  /*
   * Provides example code how to access and use the
   * path substitution sercvice.
   */
  public class PathSubstitutionTest extends java.lang.Object {
 
      /*
       * List of predefined path variables supported by
       * the path substitution service.
       */
      private static String[] predefinedPathVariables = {
          "$(home)","$(inst)","$(prog)","$(temp)","$(user)",
          "$(work)","$(path)","$(lang)","$(langid)","$(vlang)"
      };
 
      /*
       * @param args the command line arguments
       */
      public static void main(String[] args) {
 
          XComponentContext xRemoteContext = null;
          XMultiComponentFactory xRemoteServiceManager = null;
          XStringSubstitution xPathSubstService = null;
 
          try {
              // connect
              XComponentContext xLocalContext =
                  com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
              XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
              Object urlResolver = xLocalServiceManager.createInstanceWithContext(
                  "com.sun.star.bridge.UnoUrlResolver", xLocalContext );
              XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface( 
                  XUnoUrlResolver.class, urlResolver );
              Object initialObject = xUnoUrlResolver.resolve( 
                  "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" );
              XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, initialObject);
              Object context = xPropertySet.getPropertyValue("DefaultContext"); 
              xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(
                  XComponentContext.class, context);
              xRemoteServiceManager = xRemoteContext.getServiceManager();
 
              Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
                  "com.sun.star.comp.framework.PathSubstitution", xRemoteContext );
              xPathSubstService = (XStringSubstitution)UnoRuntime.queryInterface(
                  XStringSubstitution.class, pathSubst);
 
              /* Work with path variables */
              workWithPathVariables( xPathSubstService );
          }
          catch (java.lang.Exception e){
              e.printStackTrace();
          }
          finally {
              System.exit(0);
          }
      }
 
      /*
       * Retrieve, resubstitute path variables
       * @param xPathSubstService the path substitution service
       */
      public static void workWithPathVariables( XStringSubstitution xPathSubstService )
      {
          if ( xPathSubstService != null ) {
              for ( int i=0; i<predefinedPathVariables.length; i++ ) {
                  try {
                          /* Retrieve values for predefined path variables */ 
                          String aPath = xPathSubstService.getSubstituteVariableValue( 
                                              predefinedPathVariables[i] );
                          System.out.println( "Variable="+ predefinedPathVariables[i] + 
                                              " Path=" + aPath );
 
                          /* Check resubstitute */
                          String aValue = xPathSubstService.reSubstituteVariables( aPath );
                          System.out.println( "Path=" + aPath +
                                              " Variable=" + aValue );
                  }
                  catch ( com.sun.star.container.NoSuchElementException e) {
                      System.out.println( "NoSuchElementException has been thrown accessing"
                                          + predefinedPathVariables[i]);
                  }
              }
          }
      }
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages