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