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