1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import java.util.Vector; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir import java.io.File; 38*cdf0e10cSrcweir import java.io.FileOutputStream; 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir import com.sun.star.ucb.Command; 43*cdf0e10cSrcweir import com.sun.star.ucb.XContent; 44*cdf0e10cSrcweir import com.sun.star.ucb.XContentProvider; 45*cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifier; 46*cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifierFactory; 47*cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 50*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 51*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir /** 55*cdf0e10cSrcweir * Helper for creating a new connection with the specific args to a running office. 56*cdf0e10cSrcweir */ 57*cdf0e10cSrcweir public class Helper { 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir /** 60*cdf0e10cSrcweir * Member properties 61*cdf0e10cSrcweir */ 62*cdf0e10cSrcweir private XInterface m_ucb = null; 63*cdf0e10cSrcweir private String m_contenturl = null; 64*cdf0e10cSrcweir private static XComponentContext m_xContext = null; 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir /** 67*cdf0e10cSrcweir * Constructor, create a new instance of the ucb. UNO is bootstrapped and 68*cdf0e10cSrcweir * the remote office service manger is used to create the ucb. If necessary 69*cdf0e10cSrcweir * a new office process is started. 70*cdf0e10cSrcweir * 71*cdf0e10cSrcweir * @exception java.lang.Exception 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir public Helper(String url) throws java.lang.Exception { 74*cdf0e10cSrcweir m_contenturl = url; 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir if (null == m_xContext ) { 77*cdf0e10cSrcweir // get the remote office component context 78*cdf0e10cSrcweir m_xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 79*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir XMultiComponentFactory xMCF = m_xContext.getServiceManager(); 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir m_ucb = (XInterface)UnoRuntime.queryInterface(XInterface.class, 85*cdf0e10cSrcweir xMCF.createInstanceWithContext( 86*cdf0e10cSrcweir "com.sun.star.ucb.UniversalContentBroker", m_xContext)); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir /** 90*cdf0e10cSrcweir * Returns created identifier object for given URL.. 91*cdf0e10cSrcweir * 92*cdf0e10cSrcweir *@return XContent Created identifier object for given URL 93*cdf0e10cSrcweir *@exception java.lang.Exception 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir public XContent createUCBContent() throws java.lang.Exception { 96*cdf0e10cSrcweir return createUCBContent( getContentURL() ); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir /** 100*cdf0e10cSrcweir * Returned created identifier object for given URL. 101*cdf0e10cSrcweir * 102*cdf0e10cSrcweir *@param String Connect URL. Example : -url=file:/// 103*cdf0e10cSrcweir *@return XContent Created identifier object for given URL 104*cdf0e10cSrcweir *@exception java.lang.Exception 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir public XContent createUCBContent( String connectURL ) throws java.lang.Exception { 107*cdf0e10cSrcweir XContent content = null; 108*cdf0e10cSrcweir if ( connectURL != null && !connectURL.equals( "" )) { 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // Obtain required UCB interfaces... 111*cdf0e10cSrcweir XContentIdentifierFactory idFactory 112*cdf0e10cSrcweir = ( XContentIdentifierFactory )UnoRuntime.queryInterface( 113*cdf0e10cSrcweir XContentIdentifierFactory.class, m_ucb ); 114*cdf0e10cSrcweir XContentProvider provider 115*cdf0e10cSrcweir = ( XContentProvider )UnoRuntime.queryInterface( 116*cdf0e10cSrcweir XContentProvider.class, m_ucb ); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir // Create identifier object for given URL. 119*cdf0e10cSrcweir XContentIdentifier id = idFactory.createContentIdentifier( connectURL ); 120*cdf0e10cSrcweir content = provider.queryContent( id ); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir return content; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir /** 126*cdf0e10cSrcweir * Get ucb instance. 127*cdf0e10cSrcweir * 128*cdf0e10cSrcweir *@return XInterface That contains the ucb instance 129*cdf0e10cSrcweir */ 130*cdf0e10cSrcweir public XInterface getUCB() { 131*cdf0e10cSrcweir return m_ucb; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir /** 135*cdf0e10cSrcweir * Get connect URL. 136*cdf0e10cSrcweir * 137*cdf0e10cSrcweir *@return String That contains the connect URL 138*cdf0e10cSrcweir */ 139*cdf0e10cSrcweir public String getContentURL() { 140*cdf0e10cSrcweir return m_contenturl; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir /** 144*cdf0e10cSrcweir * Executes a command. 145*cdf0e10cSrcweir * 146*cdf0e10cSrcweir *param XInterface 147*cdf0e10cSrcweir *param String 148*cdf0e10cSrcweir *param Object 149*cdf0e10cSrcweir *@return Object The result according to the specification of the command. 150*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 151*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 152*cdf0e10cSrcweir */ 153*cdf0e10cSrcweir Object executeCommand( XInterface ifc, String commandName, Object argument ) 154*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 157*cdf0e10cSrcweir // Obtain command processor interface from given content. 158*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir XCommandProcessor cmdProcessor 161*cdf0e10cSrcweir = (XCommandProcessor)UnoRuntime.queryInterface( 162*cdf0e10cSrcweir XCommandProcessor.class, ifc ); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 165*cdf0e10cSrcweir // Assemble command to execute. 166*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir Command command = new Command(); 169*cdf0e10cSrcweir command.Name = commandName; 170*cdf0e10cSrcweir command.Handle = -1; // not available 171*cdf0e10cSrcweir command.Argument = argument; 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir // Note: throws CommandAbortedException, Exception 174*cdf0e10cSrcweir return cmdProcessor.execute( command, 0, null ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir public static String getAbsoluteFileURLFromSystemPath( String systemPath ) 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir try 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir File file = new File( systemPath ); 182*cdf0e10cSrcweir String url = file.toURL().toString(); 183*cdf0e10cSrcweir if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx 184*cdf0e10cSrcweir StringBuffer buf1 = new StringBuffer( "file:///" ); 185*cdf0e10cSrcweir buf1.append( url.substring( 6 ) ); 186*cdf0e10cSrcweir url = buf1.toString(); 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir return url; 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir catch ( java.net.MalformedURLException e ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir e.printStackTrace(); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir return new String(); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir public static String prependCurrentDirAsAbsoluteFileURL( String relativeURL ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir // get url of current dir. 200*cdf0e10cSrcweir String url = getAbsoluteFileURLFromSystemPath( "" ); 201*cdf0e10cSrcweir StringBuffer buf = new StringBuffer( url ); 202*cdf0e10cSrcweir if ( !url.endsWith( File.separator ) ) 203*cdf0e10cSrcweir buf.append( File.separator ); 204*cdf0e10cSrcweir buf.append( relativeURL ); 205*cdf0e10cSrcweir return buf.toString(); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir public static String createTargetDataFile( String workDir ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir try 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir StringBuffer buf = new StringBuffer(); 213*cdf0e10cSrcweir if ( workDir != null && workDir.length() > 0 ) { 214*cdf0e10cSrcweir buf.append( workDir ); 215*cdf0e10cSrcweir buf.append( File.separator ); 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir buf.append( "resource-" ); 218*cdf0e10cSrcweir buf.append( System.currentTimeMillis() ); 219*cdf0e10cSrcweir File file = new File( buf.toString() ); 220*cdf0e10cSrcweir String url = file.toURL().toString(); 221*cdf0e10cSrcweir if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx 222*cdf0e10cSrcweir StringBuffer buf1 = new StringBuffer( "file:///" ); 223*cdf0e10cSrcweir buf1.append( url.substring( 6 ) ); 224*cdf0e10cSrcweir url = buf1.toString(); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir try 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir file.createNewFile(); 230*cdf0e10cSrcweir String content = new String( 231*cdf0e10cSrcweir "This is the content of a sample data file." ); 232*cdf0e10cSrcweir FileOutputStream stream = new FileOutputStream( file ); 233*cdf0e10cSrcweir stream.write( content.getBytes() ); 234*cdf0e10cSrcweir stream.close(); 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir catch ( java.io.IOException e ) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir e.printStackTrace(); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir return url; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir catch ( java.net.MalformedURLException e ) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir e.printStackTrace(); 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir return new String(); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir } 251