xref: /AOO41X/main/odk/examples/DevelopersGuide/OfficeDev/PathSubstitution/PathSubstitutionTest.java (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.util.XStringSubstitution;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /*
33cdf0e10cSrcweir  * @author  Carsten Driesner
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir public class PathSubstitutionTest extends java.lang.Object {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     /*
38cdf0e10cSrcweir      * List of pre-defined path variables supported by
39cdf0e10cSrcweir      * the path substitution service.
40cdf0e10cSrcweir      */
41cdf0e10cSrcweir     private static String[] predefinedPathVariables = {
42cdf0e10cSrcweir         "$(home)","$(inst)","$(prog)","$(temp)","$(user)",
43cdf0e10cSrcweir         "$(work)","$(path)","$(lang)","$(langid)","$(vlang)"
44cdf0e10cSrcweir     };
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /*
47cdf0e10cSrcweir      * @param args the command line arguments
48cdf0e10cSrcweir      */
main(String[] args)49cdf0e10cSrcweir     public static void main(String[] args) {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir         XComponentContext xRemoteContext = null;
52cdf0e10cSrcweir         XMultiComponentFactory xRemoteServiceManager = null;
53cdf0e10cSrcweir         XStringSubstitution xPathSubstService = null;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         try {
56cdf0e10cSrcweir             // get the remote office context. If necessary a new office
57cdf0e10cSrcweir             // process is started
58cdf0e10cSrcweir             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
59cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
60cdf0e10cSrcweir             xRemoteServiceManager = xRemoteContext.getServiceManager();
61cdf0e10cSrcweir 
62cdf0e10cSrcweir             Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
63cdf0e10cSrcweir                 "com.sun.star.comp.framework.PathSubstitution", xRemoteContext );
64cdf0e10cSrcweir             xPathSubstService = (XStringSubstitution)UnoRuntime.queryInterface(
65cdf0e10cSrcweir                 XStringSubstitution.class, pathSubst);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             /* Work with path variables */
68cdf0e10cSrcweir             workWithPathVariables( xPathSubstService );
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         catch (java.lang.Exception e){
71cdf0e10cSrcweir             e.printStackTrace();
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir         finally {
74cdf0e10cSrcweir             System.exit(0);
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
workWithPathVariables( XStringSubstitution xPathSubstService )78cdf0e10cSrcweir     public static void workWithPathVariables( XStringSubstitution xPathSubstService )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         if ( xPathSubstService != null ) {
81cdf0e10cSrcweir             for ( int i=0; i<predefinedPathVariables.length; i++ ) {
82cdf0e10cSrcweir                 try {
83cdf0e10cSrcweir                         /* Retrieve values for pre-defined path variables */
84cdf0e10cSrcweir                         String aValue = xPathSubstService.getSubstituteVariableValue(
85cdf0e10cSrcweir                                             predefinedPathVariables[i] );
86cdf0e10cSrcweir                         System.out.println( "Variable: "+ predefinedPathVariables[i] +
87cdf0e10cSrcweir                                             " value=" + aValue );
88cdf0e10cSrcweir                 }
89cdf0e10cSrcweir                 catch ( com.sun.star.container.NoSuchElementException e) {
90cdf0e10cSrcweir                     System.err.println( "NoSuchElementException has been thrown accessing "+predefinedPathVariables[i]);
91cdf0e10cSrcweir                 }
92cdf0e10cSrcweir             }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 			// Check the resubstitution function
95cdf0e10cSrcweir 			try {
96cdf0e10cSrcweir 				String aPath = xPathSubstService.getSubstituteVariableValue(
97cdf0e10cSrcweir 											predefinedPathVariables[0] ); // Use $(home) as starting point
98cdf0e10cSrcweir 				aPath += "/test"; // extend the path
99cdf0e10cSrcweir 				System.out.println( "Path="+aPath );
100cdf0e10cSrcweir 				String aResubstPath = xPathSubstService.reSubstituteVariables( aPath );
101cdf0e10cSrcweir 				System.out.println( "Resubstituted path="+aResubstPath );
102cdf0e10cSrcweir 			}
103cdf0e10cSrcweir 			catch ( com.sun.star.container.NoSuchElementException e ) {
104cdf0e10cSrcweir                 System.err.println( "NoSuchElementException has been thrown accessing "+predefinedPathVariables[0]);
105cdf0e10cSrcweir 			}
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir }
109