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.ucb.InsertCommandArgument; 36 import com.sun.star.ucb.XContent; 37 import com.sun.star.io.XInputStream; 38 39 /** 40 * Setting (Storing) the Content Data Stream of a UCB Document Content. 41 */ 42 public class DataStreamComposer { 43 44 /** 45 * Member properties 46 */ 47 private Helper m_helper; 48 private XContent m_content; 49 private String m_contenturl = ""; 50 private String m_srcURL = ""; 51 52 53 /** 54 * Constructor. 55 * 56 *@param String[] This construtor requires the arguments: 57 * -url=... (optional) 58 * -srcURL=... (optional) 59 * -workdir=... (optional) 60 * See Help (method printCmdLineUsage()). 61 * Without the arguments a new connection to a 62 * running office cannot created. 63 *@exception java.lang.Exception 64 */ 65 public DataStreamComposer( String args[] ) throws java.lang.Exception { 66 67 // Parse arguments 68 parseArguments( args ); 69 70 // Init 71 m_helper = new Helper( getContentURL() ); 72 73 // Create UCB content 74 m_content = m_helper.createUCBContent(); 75 } 76 77 /** 78 * Write the document data stream of a document content. 79 * This method requires the main and the optional arguments to be set in order to work. 80 * See Constructor. 81 * 82 *@return boolean Result 83 *@exception com.sun.star.ucb.CommandAbortedException 84 *@exception com.sun.star.uno.Exception 85 *@exception java.lang.Exception 86 */ 87 public boolean setDataStream() 88 throws com.sun.star.ucb.CommandAbortedException, 89 com.sun.star.uno.Exception, 90 java.lang.Exception { 91 92 String sourceURL = getSourceURL(); 93 return ( setDataStream( sourceURL )); 94 } 95 96 /** 97 * Write the document data stream of a document content. 98 * 99 *@param String Source URL 100 *@return boolean Returns true if data stream successfully seted, false otherwise 101 *@exception com.sun.star.ucb.CommandAbortedException 102 *@exception com.sun.star.uno.Exception 103 *@exception java.lang.Exception 104 */ 105 public boolean setDataStream( String sourceURL ) 106 throws com.sun.star.ucb.CommandAbortedException, 107 com.sun.star.uno.Exception, 108 java.lang.Exception { 109 110 XInputStream stream; 111 if ( sourceURL == null || sourceURL.equals("") ) { 112 stream = new MyInputStream(); 113 } else { 114 String[] args = new String[ 1 ]; 115 args[ 0 ] = "-url=" + sourceURL; 116 DataStreamRetriever access = new DataStreamRetriever( args ); 117 stream = access.getDataStream(); 118 } 119 return ( setDataStream( stream )); 120 } 121 122 /** 123 * Write the document data stream of a document content... 124 * 125 *@param XInputStream Stream 126 *@return boolean Returns true if data stream successfully seted, false otherwise 127 *@exception com.sun.star.ucb.CommandAbortedException 128 *@exception com.sun.star.uno.Exception 129 */ 130 public boolean setDataStream( XInputStream stream ) 131 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 132 133 boolean result = false; 134 XInputStream data = stream; 135 if ( data != null && m_content != null ) { 136 137 // Fill argument structure... 138 InsertCommandArgument arg = new InsertCommandArgument(); 139 arg.Data = data; 140 arg.ReplaceExisting = true; 141 142 // Execute command "insert". 143 m_helper.executeCommand( m_content, "insert", arg ); 144 result = true; 145 } 146 return result; 147 } 148 149 /** 150 * Get source URL. 151 * 152 *@return String That contains the source URL 153 */ 154 public String getSourceURL() { 155 return m_srcURL; 156 } 157 158 /** 159 * Get connect URL. 160 * 161 *@return String That contains the connect URL 162 */ 163 public String getContentURL() { 164 return m_contenturl; 165 } 166 167 /** 168 * Parse arguments 169 * 170 *@param String[] Arguments 171 *@exception java.lang.Exception 172 */ 173 public void parseArguments( String[] args ) throws java.lang.Exception { 174 175 String workdir = ""; 176 177 for ( int i = 0; i < args.length; i++ ) { 178 if ( args[i].startsWith( "-url=" )) { 179 m_contenturl = args[i].substring( 5 ); 180 } else if ( args[i].startsWith( "-srcURL=" )) { 181 m_srcURL = args[i].substring( 9 ); 182 } else if ( args[i].startsWith( "-workdir=" )) { 183 workdir = args[i].substring( 9 ); 184 } else if ( args[i].startsWith( "-help" ) || 185 args[i].startsWith( "-?" )) { 186 printCmdLineUsage(); 187 System.exit( 0 ); 188 } 189 } 190 191 if ( m_contenturl == null || m_contenturl.equals( "" )) { 192 m_contenturl = Helper.createTargetDataFile( workdir ); 193 } 194 195 if ( m_srcURL == null || m_srcURL.equals( "" )) { 196 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" ); 197 } 198 } 199 200 /** 201 * Print the commands options 202 */ 203 public void printCmdLineUsage() { 204 System.out.println( 205 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." ); 206 System.out.println( 207 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" ); 208 System.out.println( 209 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " ); 210 } 211 212 213 /** 214 * Create a new connection with the specific args to a running office and 215 * set the Content Data Stream of a UCB Document Content. 216 * 217 *@param String[] Arguments 218 */ 219 public static void main ( String args[] ) { 220 System.out.println( "\n" ); 221 System.out.println( 222 "-----------------------------------------------------------------" ); 223 System.out.println( 224 "DataStreamComposer - sets the data stream of a document resource." ); 225 System.out.println( 226 " The data stream is obtained from another (the source) document " ); 227 System.out.println( 228 " resource before." ); 229 System.out.println( 230 "-----------------------------------------------------------------" ); 231 try { 232 233 DataStreamComposer dataStream = new DataStreamComposer( args ); 234 String sourceURL = dataStream.getSourceURL(); 235 boolean result = dataStream.setDataStream( sourceURL ); 236 if ( result ) { 237 System.out.println( 238 "\nSetting data stream succeeded.\n Source URL: " + 239 dataStream.getSourceURL() + 240 "\n Target URL: " + 241 dataStream.getContentURL() ); 242 } else { 243 System.out.println( 244 "\nSetting data stream failed. \n Source URL: " + 245 dataStream.getSourceURL() + 246 "\n Target URL: " + 247 dataStream.getContentURL() ); 248 } 249 } catch ( com.sun.star.ucb.CommandAbortedException e ) { 250 System.out.println( "Error: " + e ); 251 } catch ( com.sun.star.uno.Exception e ) { 252 System.out.println( "Error: " + e ); 253 } catch ( java.lang.Exception e ) { 254 System.out.println( "Error: " + e ); 255 } 256 System.exit( 0 ); 257 } 258 } 259