Difference between revisions of "Documentation/DevGuide/OfficeDev/Resolving Path Variables"
OOoWikiBot (talk | contribs) m (FINAL VERSION FOR L10N) |
m (typo) |
||
Line 149: | Line 149: | ||
} | } | ||
catch ( com.sun.star.container.NoSuchElementException e) { | catch ( com.sun.star.container.NoSuchElementException e) { | ||
− | System.out.println( " | + | System.out.println( "NoSuchElementException has been thrown accessing" |
+ predefinedPathVariables[i]); | + predefinedPathVariables[i]); | ||
} | } |
Revision as of 12:40, 27 January 2010
This section explains how to use the OpenOffice.org 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 OpenOffice.org 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). |