xref: /trunk/main/odk/examples/DevelopersGuide/UCB/Helper.java (revision 5226f0b8dea4ef117ddd2bc4948a8601d0ac403b)
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 java.io.File;
25 import java.io.FileOutputStream;
26 
27 import com.sun.star.lang.XMultiComponentFactory;
28 
29 import com.sun.star.ucb.Command;
30 import com.sun.star.ucb.XContent;
31 import com.sun.star.ucb.XContentProvider;
32 import com.sun.star.ucb.XContentIdentifier;
33 import com.sun.star.ucb.XContentIdentifierFactory;
34 import com.sun.star.ucb.XCommandProcessor;
35 
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XComponentContext;
39 
40 
41 /**
42  * Helper for creating a new connection with the specific args to a running office.
43  */
44 public class Helper {
45 
46     /**
47      * Member properties
48      */
49     private XInterface  m_ucb        = null;
50     private String      m_contenturl = null;
51     private static XComponentContext   m_xContext   = null;
52 
53     /**
54      *  Constructor, create a new instance of the ucb. UNO is bootstrapped and
55      *  the remote office service manager is used to create the ucb. If necessary
56      *  a new office process is started.
57      *
58      *  @exception  java.lang.Exception
59      */
Helper(String url)60     public Helper(String url) throws java.lang.Exception {
61         m_contenturl    = url;
62 
63         if (null == m_xContext ) {
64             // get the remote office component context
65             m_xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
66             System.out.println("Connected to a running office ...");
67         }
68 
69         XMultiComponentFactory xMCF = m_xContext.getServiceManager();
70 
71         m_ucb = UnoRuntime.queryInterface(XInterface.class,
72             xMCF.createInstanceWithContext(
73                         "com.sun.star.ucb.UniversalContentBroker", m_xContext));
74     }
75 
76     /**
77      * Returns created identifier object for given URL..
78      *
79      *@return     XContent       Created identifier object for given URL
80      *@exception  java.lang.Exception
81      */
createUCBContent()82     public XContent createUCBContent() throws java.lang.Exception {
83         return createUCBContent( getContentURL() );
84     }
85 
86     /**
87      * Returned created identifier object for given URL.
88      *
89      *@param      String         Connect URL. Example : -url=file:///
90      *@return     XContent       Created identifier object for given URL
91      *@exception  java.lang.Exception
92      */
createUCBContent( String connectURL )93     public XContent createUCBContent( String connectURL ) throws java.lang.Exception {
94         XContent content = null;
95         if ( connectURL != null && !connectURL.equals( "" )) {
96 
97             // Obtain required UCB interfaces...
98             XContentIdentifierFactory idFactory
99                 = UnoRuntime.queryInterface(
100                     XContentIdentifierFactory.class, m_ucb );
101             XContentProvider provider
102                 = UnoRuntime.queryInterface(
103                     XContentProvider.class, m_ucb );
104 
105             // Create identifier object for given URL.
106             XContentIdentifier id = idFactory.createContentIdentifier( connectURL );
107             content = provider.queryContent( id );
108         }
109         return content;
110     }
111 
112     /**
113      *  Get ucb instance.
114      *
115      *@return   XInterface  That contains the ucb  instance
116      */
getUCB()117     public XInterface getUCB() {
118         return m_ucb;
119     }
120 
121     /**
122      *  Get connect URL.
123      *
124      *@return   String  That contains the connect URL
125      */
getContentURL()126     public String getContentURL() {
127         return m_contenturl;
128     }
129 
130     /**
131      *  Executes a command.
132      *
133      *param       XInterface
134      *param       String
135      *param       Object
136      *@return     Object     The result according to the specification of the command.
137      *@exception  com.sun.star.ucb.CommandAbortedException
138      *@exception  com.sun.star.uno.Exception
139      */
executeCommand( XInterface ifc, String commandName, Object argument )140     Object executeCommand( XInterface ifc, String commandName, Object argument )
141         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception  {
142 
143         /////////////////////////////////////////////////////////////////////
144         // Obtain command processor interface from given content.
145         /////////////////////////////////////////////////////////////////////
146 
147         XCommandProcessor cmdProcessor
148             = UnoRuntime.queryInterface(
149                 XCommandProcessor.class, ifc );
150 
151         /////////////////////////////////////////////////////////////////////
152         // Assemble command to execute.
153         /////////////////////////////////////////////////////////////////////
154 
155         Command command = new Command();
156         command.Name     = commandName;
157         command.Handle   = -1; // not available
158         command.Argument = argument;
159 
160         // Note: throws CommandAbortedException, Exception
161         return cmdProcessor.execute( command, 0, null );
162     }
163 
getAbsoluteFileURLFromSystemPath( String systemPath )164     public static String getAbsoluteFileURLFromSystemPath( String systemPath )
165     {
166         try
167         {
168             File file = new File( systemPath );
169             String url = file.toURL().toString();
170             if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
171                 StringBuilder buf1 = new StringBuilder( "file:///" );
172                 buf1.append( url.substring( 6 ) );
173                 url = buf1.toString();
174             }
175             return url;
176         }
177         catch ( java.net.MalformedURLException e )
178         {
179             e.printStackTrace();
180         }
181         return new String();
182     }
183 
prependCurrentDirAsAbsoluteFileURL( String relativeURL )184     public static String prependCurrentDirAsAbsoluteFileURL( String relativeURL )
185     {
186         // get url of current dir.
187         String url = getAbsoluteFileURLFromSystemPath( "" );
188         StringBuilder buf = new StringBuilder( url );
189         if ( !url.endsWith( File.separator ) )
190             buf.append( File.separator );
191         buf.append( relativeURL );
192         return buf.toString();
193     }
194 
createTargetDataFile( String workDir )195     public static String createTargetDataFile( String workDir )
196     {
197         try
198         {
199             StringBuilder buf = new StringBuilder();
200             if ( workDir != null && workDir.length() > 0 ) {
201                 buf.append( workDir );
202                 buf.append( File.separator );
203             }
204             buf.append( "resource-" );
205             buf.append( System.currentTimeMillis() );
206             File file = new File( buf.toString() );
207             String url = file.toURL().toString();
208             if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
209                 StringBuilder buf1 = new StringBuilder( "file:///" );
210                 buf1.append( url.substring( 6 ) );
211                 url = buf1.toString();
212             }
213 
214             try
215             {
216                 file.createNewFile();
217                 String content = new String(
218                     "This is the content of a sample data file." );
219                 FileOutputStream stream = new FileOutputStream( file );
220                 stream.write( content.getBytes() );
221                 stream.close();
222             }
223             catch ( java.io.IOException e )
224             {
225                 e.printStackTrace();
226             }
227 
228             return url;
229         }
230         catch ( java.net.MalformedURLException e )
231         {
232             e.printStackTrace();
233         }
234 
235         return new String();
236     }
237 }
238