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 com.sun.star.beans.PropertyValue; 36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37*cdf0e10cSrcweir import com.sun.star.ucb.ContentInfo; 38*cdf0e10cSrcweir import com.sun.star.ucb.InsertCommandArgument; 39*cdf0e10cSrcweir import com.sun.star.ucb.XContent; 40*cdf0e10cSrcweir import com.sun.star.io.XInputStream; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /** 44*cdf0e10cSrcweir * Creating a New Resource 45*cdf0e10cSrcweir */ 46*cdf0e10cSrcweir public class ResourceCreator { 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir /** 49*cdf0e10cSrcweir * Member properties 50*cdf0e10cSrcweir */ 51*cdf0e10cSrcweir private Helper m_helper; 52*cdf0e10cSrcweir private XContent m_content; 53*cdf0e10cSrcweir private String m_contenturl = ""; 54*cdf0e10cSrcweir private String m_name = ""; 55*cdf0e10cSrcweir private String m_srcURL = ""; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir /** 58*cdf0e10cSrcweir * Constructor. 59*cdf0e10cSrcweir * 60*cdf0e10cSrcweir *@param String[] This construtor requires the arguments: 61*cdf0e10cSrcweir * -url=... (optional) 62*cdf0e10cSrcweir * -name=... (optional) 63*cdf0e10cSrcweir * -srcURL=... (optional) 64*cdf0e10cSrcweir * -workdir=... (optional) 65*cdf0e10cSrcweir * See Help (method printCmdLineUsage()). 66*cdf0e10cSrcweir * Without the arguments a new connection to a 67*cdf0e10cSrcweir * running office cannot created. 68*cdf0e10cSrcweir *@exception java.lang.Exception 69*cdf0e10cSrcweir */ 70*cdf0e10cSrcweir public ResourceCreator( String args[] ) throws java.lang.Exception { 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir // Parse arguments 73*cdf0e10cSrcweir parseArguments( args ); 74*cdf0e10cSrcweir String url = getContentURL(); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir // Init 77*cdf0e10cSrcweir m_helper = new Helper( url ); 78*cdf0e10cSrcweir if ( url.startsWith( "file:///" )) { 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir // Create UCB content 81*cdf0e10cSrcweir m_content = m_helper.createUCBContent(); 82*cdf0e10cSrcweir } else { 83*cdf0e10cSrcweir throw new Exception( 84*cdf0e10cSrcweir "Create new resource : parameter 'url' must contain a File URL " + 85*cdf0e10cSrcweir "pointing to the file system folder in which the new resource " + 86*cdf0e10cSrcweir "shall be created. (Example: file:///tmp/)" ); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir /** 91*cdf0e10cSrcweir * Create a new resource. 92*cdf0e10cSrcweir * This method requires the main and the optional arguments to be set in order to work. 93*cdf0e10cSrcweir * See Constructor. 94*cdf0e10cSrcweir * 95*cdf0e10cSrcweir *@return boolean Returns true if resource successfully created, false otherwise 96*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 97*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 98*cdf0e10cSrcweir */ 99*cdf0e10cSrcweir public boolean createNewResource() 100*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, 101*cdf0e10cSrcweir com.sun.star.uno.Exception, 102*cdf0e10cSrcweir java.lang.Exception { 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir String sourceURL = getSourceURL(); 105*cdf0e10cSrcweir String name = getName(); 106*cdf0e10cSrcweir return createNewResource( sourceURL, name ); 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** 110*cdf0e10cSrcweir * Create a new resource. 111*cdf0e10cSrcweir * 112*cdf0e10cSrcweir *@param String Source resource URL 113*cdf0e10cSrcweir *@param String New resource name 114*cdf0e10cSrcweir *@return boolean Returns true if resource successfully created, false otherwise 115*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 116*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 117*cdf0e10cSrcweir */ 118*cdf0e10cSrcweir public boolean createNewResource( String sourceURL, String name ) 119*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, 120*cdf0e10cSrcweir com.sun.star.uno.Exception, 121*cdf0e10cSrcweir java.lang.Exception { 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir XInputStream stream = null; 124*cdf0e10cSrcweir if ( sourceURL == null || sourceURL.equals( "" )) { 125*cdf0e10cSrcweir stream = new MyInputStream(); 126*cdf0e10cSrcweir } else { 127*cdf0e10cSrcweir String[] args = new String[ 1 ]; 128*cdf0e10cSrcweir args[ 0 ] = "-url=" + sourceURL; 129*cdf0e10cSrcweir DataStreamRetriever access = new DataStreamRetriever( args ); 130*cdf0e10cSrcweir stream = access.getDataStream(); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir return createNewResource( stream, name ); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /** 136*cdf0e10cSrcweir * Create a new resource. 137*cdf0e10cSrcweir * 138*cdf0e10cSrcweir *@param XInputStream Source resource stream 139*cdf0e10cSrcweir *@param String New resource name 140*cdf0e10cSrcweir *@return boolean Returns true if resource successfully created, false otherwise 141*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 142*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 143*cdf0e10cSrcweir */ 144*cdf0e10cSrcweir public boolean createNewResource( XInputStream stream, String name ) 145*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir boolean result = false; 148*cdf0e10cSrcweir if ( stream != null && name != null && !name.equals( "" )) { 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir // Note: The data for info may have been obtained from 151*cdf0e10cSrcweir // property CreatableContentsInfo. 152*cdf0e10cSrcweir ContentInfo info = new ContentInfo(); 153*cdf0e10cSrcweir info.Type = "application/vnd.sun.staroffice.fsys-file"; 154*cdf0e10cSrcweir info.Attributes = 0; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir // Create new, empty content (execute command "createNewContent"). 157*cdf0e10cSrcweir XContent newContent = ( XContent )UnoRuntime.queryInterface( 158*cdf0e10cSrcweir XContent.class, 159*cdf0e10cSrcweir m_helper.executeCommand( m_content, "createNewContent", info ) ); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir if ( newContent != null ) { 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 164*cdf0e10cSrcweir // Set mandatory properties... 165*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir // Define property value sequence. 168*cdf0e10cSrcweir PropertyValue[] props = new PropertyValue[ 1 ]; 169*cdf0e10cSrcweir PropertyValue prop = new PropertyValue(); 170*cdf0e10cSrcweir prop.Name = "Title"; 171*cdf0e10cSrcweir prop.Handle = -1; // n/a 172*cdf0e10cSrcweir prop.Value = name; 173*cdf0e10cSrcweir props[ 0 ] = prop; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir // Execute command "setPropertyValues". 176*cdf0e10cSrcweir m_helper.executeCommand( newContent, "setPropertyValues", props ); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 179*cdf0e10cSrcweir // Write the new file to disk... 180*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir // Obtain document data for the new file. 183*cdf0e10cSrcweir XInputStream data = stream; 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir // Fill argument structure... 186*cdf0e10cSrcweir InsertCommandArgument arg = new InsertCommandArgument(); 187*cdf0e10cSrcweir arg.Data = data; 188*cdf0e10cSrcweir arg.ReplaceExisting = false; 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir // Execute command "insert". 191*cdf0e10cSrcweir m_helper.executeCommand( newContent, "insert", arg ); 192*cdf0e10cSrcweir result = true; 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir return result; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir /** 199*cdf0e10cSrcweir * Get new resource name. 200*cdf0e10cSrcweir * 201*cdf0e10cSrcweir *@return String That contains the name 202*cdf0e10cSrcweir */ 203*cdf0e10cSrcweir public String getName() { 204*cdf0e10cSrcweir return m_name; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir /** 208*cdf0e10cSrcweir * Get source URL. 209*cdf0e10cSrcweir * 210*cdf0e10cSrcweir *@return String That contains the source URL 211*cdf0e10cSrcweir */ 212*cdf0e10cSrcweir public String getSourceURL() { 213*cdf0e10cSrcweir return m_srcURL; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir /** 217*cdf0e10cSrcweir * Get connect URL. 218*cdf0e10cSrcweir * 219*cdf0e10cSrcweir *@return String That contains the connect URL 220*cdf0e10cSrcweir */ 221*cdf0e10cSrcweir public String getContentURL() { 222*cdf0e10cSrcweir return m_contenturl; 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir /** 226*cdf0e10cSrcweir * Parse arguments 227*cdf0e10cSrcweir * 228*cdf0e10cSrcweir *@param String[] Arguments 229*cdf0e10cSrcweir *@exception java.lang.Exception 230*cdf0e10cSrcweir */ 231*cdf0e10cSrcweir public void parseArguments( String[] args ) throws java.lang.Exception { 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir String workdir = ""; 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir for ( int i = 0; i < args.length; i++ ) { 236*cdf0e10cSrcweir if ( args[i].startsWith( "-url=" )) { 237*cdf0e10cSrcweir m_contenturl = args[i].substring( 5 ); 238*cdf0e10cSrcweir } else if ( args[i].startsWith( "-name=" )) { 239*cdf0e10cSrcweir m_name = args[i].substring( 6 ); 240*cdf0e10cSrcweir } else if ( args[i].startsWith( "-srcURL=" )) { 241*cdf0e10cSrcweir m_srcURL = args[i].substring( 8 ); 242*cdf0e10cSrcweir } else if ( args[i].startsWith( "-workdir=" )) { 243*cdf0e10cSrcweir workdir = args[i].substring( 9 ); 244*cdf0e10cSrcweir } else if ( args[i].startsWith( "-help" ) || 245*cdf0e10cSrcweir args[i].startsWith( "-?" )) { 246*cdf0e10cSrcweir printCmdLineUsage(); 247*cdf0e10cSrcweir System.exit( 0 ); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir if ( m_contenturl == null || m_contenturl.equals( "" )) { 252*cdf0e10cSrcweir m_contenturl = Helper.getAbsoluteFileURLFromSystemPath( workdir ); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir if ( m_name == null || m_name.equals( "" )) { 256*cdf0e10cSrcweir m_name = "created-resource-" + System.currentTimeMillis(); 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir if ( m_srcURL == null || m_srcURL.equals( "" )) { 260*cdf0e10cSrcweir m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" ); 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir /** 265*cdf0e10cSrcweir * Print the commands options 266*cdf0e10cSrcweir */ 267*cdf0e10cSrcweir public void printCmdLineUsage() { 268*cdf0e10cSrcweir System.out.println( 269*cdf0e10cSrcweir "Usage : ResourceCreator -url=... -name=... -srcURL=... -workdir=..." ); 270*cdf0e10cSrcweir System.out.println( 271*cdf0e10cSrcweir "Defaults: -url=<workdir> -name=created-resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt> -workdir=<currentdir>" ); 272*cdf0e10cSrcweir System.out.println( 273*cdf0e10cSrcweir "\nExample : -url=file:///home/kai/ -name=newfile.txt -srcURL=file:///home/kai/sourcefile.txt" ); 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir /** 277*cdf0e10cSrcweir * Create a new connection with the specific args to a running office and 278*cdf0e10cSrcweir * create a new resource. 279*cdf0e10cSrcweir * 280*cdf0e10cSrcweir *@param String[] Arguments 281*cdf0e10cSrcweir */ 282*cdf0e10cSrcweir public static void main ( String args[] ) { 283*cdf0e10cSrcweir System.out.println( "\n" ); 284*cdf0e10cSrcweir System.out.println( 285*cdf0e10cSrcweir "-----------------------------------------------------------------------" ); 286*cdf0e10cSrcweir System.out.println( 287*cdf0e10cSrcweir "ResourceCreator - creates a new file in an existing file system folder." ); 288*cdf0e10cSrcweir System.out.println( 289*cdf0e10cSrcweir " (Content for the new file can be retrieved from another file)." ); 290*cdf0e10cSrcweir System.out.println( 291*cdf0e10cSrcweir "-----------------------------------------------------------------------" ); 292*cdf0e10cSrcweir try { 293*cdf0e10cSrcweir ResourceCreator create = new ResourceCreator( args ); 294*cdf0e10cSrcweir boolean result = create.createNewResource(); 295*cdf0e10cSrcweir if ( result ) { 296*cdf0e10cSrcweir System.out.println( 297*cdf0e10cSrcweir "Creation of new resource " + create.getName() + " in folder: " + 298*cdf0e10cSrcweir create.getContentURL() + " succeeded." ); 299*cdf0e10cSrcweir } else { 300*cdf0e10cSrcweir System.out.println( 301*cdf0e10cSrcweir "Creation of new resource " + create.getName() + " in folder: " + 302*cdf0e10cSrcweir create.getContentURL() + " failed." ); 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir } catch ( com.sun.star.ucb.CommandAbortedException e ) { 305*cdf0e10cSrcweir System.out.println( "Error: " + e ); 306*cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e ) { 307*cdf0e10cSrcweir System.out.println( "Error: " + e ); 308*cdf0e10cSrcweir } catch ( java.lang.Exception e ) { 309*cdf0e10cSrcweir System.out.println( "Error: " + e ); 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir System.exit( 0 ); 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir } 314