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