1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 import java
.io
.FileOutputStream
;
39 import com
.sun
.star
.ucb
.Command
;
40 import com
.sun
.star
.ucb
.UniversalContentBroker
;
41 import com
.sun
.star
.ucb
.XContent
;
42 import com
.sun
.star
.ucb
.XContentProvider
;
43 import com
.sun
.star
.ucb
.XContentIdentifier
;
44 import com
.sun
.star
.ucb
.XContentIdentifierFactory
;
45 import com
.sun
.star
.ucb
.XCommandProcessor
;
47 import com
.sun
.star
.uno
.XInterface
;
48 import com
.sun
.star
.uno
.UnoRuntime
;
49 import com
.sun
.star
.uno
.XComponentContext
;
53 * Helper for creating a new connection with the specific args to a running office.
60 private XInterface m_ucb
= null;
61 private String m_contenturl
= null;
62 private static XComponentContext m_xContext
= null;
65 * Constructor, create a new instance of the ucb. UNO is bootstrapped and
66 * the remote office service manager is used to create the ucb. If necessary
67 * a new office process is started.
69 public Helper(String url
) throws java
.lang
.Exception
{
72 if (null == m_xContext
) {
73 // get the remote office component context
74 m_xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
75 System
.out
.println("Connected to a running office ...");
78 m_ucb
= UnoRuntime
.queryInterface(
79 XInterface
.class, UniversalContentBroker
.create(m_xContext
));
83 * Returns created identifier object for given URL...
85 *@return XContent Created identifier object for given URL
87 public XContent
createUCBContent() throws java
.lang
.Exception
{
88 return createUCBContent( getContentURL() );
92 * Returned created identifier object for given URL.
94 *@param connectURL Connect URL. Example : -url=file:///
95 *@return Created identifier object for given URL
97 private XContent
createUCBContent( String connectURL
) throws java
.lang
.Exception
{
98 XContent content
= null;
99 if ( connectURL
!= null && !connectURL
.equals( "" )) {
101 // Obtain required UCB interfaces...
102 XContentIdentifierFactory idFactory
103 = UnoRuntime
.queryInterface(
104 XContentIdentifierFactory
.class, m_ucb
);
105 XContentProvider provider
106 = UnoRuntime
.queryInterface(
107 XContentProvider
.class, m_ucb
);
109 // Create identifier object for given URL.
110 XContentIdentifier id
= idFactory
.createContentIdentifier( connectURL
);
111 content
= provider
.queryContent( id
);
119 *@return XInterface That contains the ucb instance
121 public XInterface
getUCB() {
128 *@return String That contains the connect URL
130 public String
getContentURL() {
135 * Executes a command.
140 *@return Object The result according to the specification of the command.
141 *@exception com.sun.star.ucb.CommandAbortedException
142 *@exception com.sun.star.uno.Exception
144 Object
executeCommand( XInterface ifc
, String commandName
, Object argument
)
145 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
148 // Obtain command processor interface from given content.
151 XCommandProcessor cmdProcessor
152 = UnoRuntime
.queryInterface(
153 XCommandProcessor
.class, ifc
);
156 // Assemble command to execute.
159 Command command
= new Command();
160 command
.Name
= commandName
;
161 command
.Handle
= -1; // not available
162 command
.Argument
= argument
;
164 // Note: throws CommandAbortedException, Exception
165 return cmdProcessor
.execute( command
, 0, null );
168 public static String
getAbsoluteFileURLFromSystemPath( String systemPath
)
172 File file
= new File( systemPath
);
173 String url
= file
.toURI().toURL().toString();
174 if ( url
.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
175 StringBuffer buf1
= new StringBuffer( "file:///" );
176 buf1
.append( url
.substring( 6 ) );
177 url
= buf1
.toString();
181 catch ( java
.net
.MalformedURLException e
)
188 public static String
prependCurrentDirAsAbsoluteFileURL( String relativeURL
)
190 // get url of current dir.
191 String url
= getAbsoluteFileURLFromSystemPath( "" );
192 StringBuffer buf
= new StringBuffer( url
);
193 if ( !url
.endsWith( File
.separator
) )
194 buf
.append( File
.separator
);
195 buf
.append( relativeURL
);
196 return buf
.toString();
199 public static String
createTargetDataFile( String workDir
)
203 StringBuffer buf
= new StringBuffer();
204 if ( workDir
!= null && workDir
.length() > 0 ) {
205 buf
.append( workDir
);
206 buf
.append( File
.separator
);
208 buf
.append( "resource-" );
209 buf
.append( System
.currentTimeMillis() );
210 File file
= new File( buf
.toString() );
211 String url
= file
.toURI().toURL().toString();
212 if ( url
.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
213 StringBuffer buf1
= new StringBuffer( "file:///" );
214 buf1
.append( url
.substring( 6 ) );
215 url
= buf1
.toString();
220 file
.createNewFile();
221 String content
= "This is the content of a sample data file.";
222 FileOutputStream stream
= new FileOutputStream( file
);
223 stream
.write( content
.getBytes() );
226 catch ( java
.io
.IOException e
)
233 catch ( java
.net
.MalformedURLException e
)
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */