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