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