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