1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.lang.XMultiComponentFactory;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.beans.PropertyValue;
40 
41 import com.sun.star.util.XStringSubstitution;
42 
43 /*
44  * @author  Carsten Driesner
45  */
46 public class PathSubstitutionTest extends java.lang.Object {
47 
48     /*
49      * List of pre-defined path variables supported by
50      * the path substitution service.
51      */
52     private static String[] predefinedPathVariables = {
53         "$(home)","$(inst)","$(prog)","$(temp)","$(user)",
54         "$(work)","$(path)","$(lang)","$(langid)","$(vlang)"
55     };
56 
57     /*
58      * @param args the command line arguments
59      */
60     public static void main(String[] args) {
61 
62         XComponentContext xRemoteContext = null;
63         XMultiComponentFactory xRemoteServiceManager = null;
64         XStringSubstitution xPathSubstService = null;
65 
66         try {
67             // get the remote office context. If necessary a new office
68             // process is started
69             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
70             System.out.println("Connected to a running office ...");
71             xRemoteServiceManager = xRemoteContext.getServiceManager();
72 
73             Object pathSubst = xRemoteServiceManager.createInstanceWithContext(
74                 "com.sun.star.comp.framework.PathSubstitution", xRemoteContext );
75             xPathSubstService = (XStringSubstitution)UnoRuntime.queryInterface(
76                 XStringSubstitution.class, pathSubst);
77 
78             /* Work with path variables */
79             workWithPathVariables( xPathSubstService );
80         }
81         catch (java.lang.Exception e){
82             e.printStackTrace();
83         }
84         finally {
85             System.exit(0);
86         }
87     }
88 
89     public static void workWithPathVariables( XStringSubstitution xPathSubstService )
90     {
91         if ( xPathSubstService != null ) {
92             for ( int i=0; i<predefinedPathVariables.length; i++ ) {
93                 try {
94                         /* Retrieve values for pre-defined path variables */
95                         String aValue = xPathSubstService.getSubstituteVariableValue(
96                                             predefinedPathVariables[i] );
97                         System.out.println( "Variable: "+ predefinedPathVariables[i] +
98                                             " value=" + aValue );
99                 }
100                 catch ( com.sun.star.container.NoSuchElementException e) {
101                     System.err.println( "NoSuchElementException has been thrown accessing "+predefinedPathVariables[i]);
102                 }
103             }
104 
105 			// Check the resubstitution function
106 			try {
107 				String aPath = xPathSubstService.getSubstituteVariableValue(
108 											predefinedPathVariables[0] ); // Use $(home) as starting point
109 				aPath += "/test"; // extend the path
110 				System.out.println( "Path="+aPath );
111 				String aResubstPath = xPathSubstService.reSubstituteVariables( aPath );
112 				System.out.println( "Resubstituted path="+aResubstPath );
113 			}
114 			catch ( com.sun.star.container.NoSuchElementException e ) {
115                 System.err.println( "NoSuchElementException has been thrown accessing "+predefinedPathVariables[0]);
116 			}
117         }
118     }
119 }
120