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