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.OpenCommandArgument2; 36*cdf0e10cSrcweir import com.sun.star.ucb.OpenMode; 37*cdf0e10cSrcweir import com.sun.star.ucb.XContent; 38*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSink; 39*cdf0e10cSrcweir import com.sun.star.io.XInputStream; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir /** 42*cdf0e10cSrcweir * Accessing (Loading) the Content Data Stream of a UCB Document Content 43*cdf0e10cSrcweir */ 44*cdf0e10cSrcweir public class DataStreamRetriever { 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir /** 47*cdf0e10cSrcweir * Member properties 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir private Helper m_helper; 50*cdf0e10cSrcweir private XContent m_content; 51*cdf0e10cSrcweir private String m_contenturl = ""; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir /** 54*cdf0e10cSrcweir * Constructor. 55*cdf0e10cSrcweir * 56*cdf0e10cSrcweir *@param String[] This construtor requires the arguments: 57*cdf0e10cSrcweir * -url=... (optional) 58*cdf0e10cSrcweir * See Help (method printCmdLineUsage()). 59*cdf0e10cSrcweir * Without the arguments a new connection to a 60*cdf0e10cSrcweir * running office cannot created. 61*cdf0e10cSrcweir *@exception java.lang.Exception 62*cdf0e10cSrcweir */ 63*cdf0e10cSrcweir public DataStreamRetriever( String args[] ) throws java.lang.Exception { 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // Parse arguments 66*cdf0e10cSrcweir parseArguments( args ); 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir // Init 69*cdf0e10cSrcweir m_helper = new Helper( getContentURL() ); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // Create UCB content 72*cdf0e10cSrcweir m_content = m_helper.createUCBContent(); 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir /** 76*cdf0e10cSrcweir * Read the document data stream of a document content using a 77*cdf0e10cSrcweir * XActiveDataSink implementation as data sink.... 78*cdf0e10cSrcweir * 79*cdf0e10cSrcweir *@return XInputStream Returns input stream if stream successfully retrieved, 80*cdf0e10cSrcweir * null otherwise 81*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 82*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 83*cdf0e10cSrcweir */ 84*cdf0e10cSrcweir public XInputStream getDataStream() 85*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir XInputStream data = null; 88*cdf0e10cSrcweir if ( m_content != null ) { 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir // Fill argument structure... 91*cdf0e10cSrcweir OpenCommandArgument2 arg = new OpenCommandArgument2(); 92*cdf0e10cSrcweir arg.Mode = OpenMode.DOCUMENT; 93*cdf0e10cSrcweir arg.Priority = 32768; // Final static for 32768 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir // Create data sink implementation object. 96*cdf0e10cSrcweir XActiveDataSink dataSink = new MyActiveDataSink(); 97*cdf0e10cSrcweir arg.Sink = dataSink; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // Execute command "open". The implementation of the command will 100*cdf0e10cSrcweir // supply an XInputStream implementation to the data sink. 101*cdf0e10cSrcweir m_helper.executeCommand( m_content, "open", arg ); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // Get input stream supplied by the open command implementation. 104*cdf0e10cSrcweir data = dataSink.getInputStream(); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir return data; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** 110*cdf0e10cSrcweir * Get connect URL. 111*cdf0e10cSrcweir * 112*cdf0e10cSrcweir *@return String That contains the connect URL 113*cdf0e10cSrcweir */ 114*cdf0e10cSrcweir public String getContentURL() { 115*cdf0e10cSrcweir return m_contenturl; 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir /** 119*cdf0e10cSrcweir * Parse arguments 120*cdf0e10cSrcweir * 121*cdf0e10cSrcweir *@param String[] Arguments 122*cdf0e10cSrcweir *@exception java.lang.Exception 123*cdf0e10cSrcweir */ 124*cdf0e10cSrcweir public void parseArguments( String[] args ) throws java.lang.Exception { 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir for ( int i = 0; i < args.length; i++ ) { 127*cdf0e10cSrcweir if ( args[i].startsWith( "-url=" )) { 128*cdf0e10cSrcweir m_contenturl = args[i].substring( 5 ); 129*cdf0e10cSrcweir } else if ( args[i].startsWith( "-help" ) || 130*cdf0e10cSrcweir args[i].startsWith( "-?" )) { 131*cdf0e10cSrcweir printCmdLineUsage(); 132*cdf0e10cSrcweir System.exit( 0 ); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir if ( m_contenturl == null || m_contenturl.equals( "" )) { 137*cdf0e10cSrcweir m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" ); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir /** 142*cdf0e10cSrcweir * Print the commands options 143*cdf0e10cSrcweir */ 144*cdf0e10cSrcweir public void printCmdLineUsage() { 145*cdf0e10cSrcweir System.out.println( 146*cdf0e10cSrcweir "Usage : DataStreamRetriever -url=..." ); 147*cdf0e10cSrcweir System.out.println( 148*cdf0e10cSrcweir "Defaults: -url=<currentdir>/data/data.txt" ); 149*cdf0e10cSrcweir System.out.println( 150*cdf0e10cSrcweir "\nExample : -url=file:///temp/my.txt" ); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir /** 154*cdf0e10cSrcweir * Print Stream content. 155*cdf0e10cSrcweir * 156*cdf0e10cSrcweir *@param XInputStream 157*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 158*cdf0e10cSrcweir */ 159*cdf0e10cSrcweir public void printStream( XInputStream data ) 160*cdf0e10cSrcweir throws com.sun.star.uno.Exception { 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 163*cdf0e10cSrcweir // Read data from input stream...65536 164*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir // Data buffer. Will be allocated by input stream implementation! 167*cdf0e10cSrcweir byte[][] buffer = new byte[ 1 ][ 65536 ]; 168*cdf0e10cSrcweir int read = data.readSomeBytes( buffer, 65536 ); 169*cdf0e10cSrcweir System.out.println( "Read bytes : " + read ); 170*cdf0e10cSrcweir System.out.println( "Read data (only first 64K displayed): "); 171*cdf0e10cSrcweir while ( read > 0 ) { 172*cdf0e10cSrcweir byte[] bytes = new byte[ read ]; 173*cdf0e10cSrcweir for( int i = 0; i < read; i++ ) { 174*cdf0e10cSrcweir bytes[ i ] = buffer[ 0 ][ i ]; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir System.out.println( new String(bytes) ); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir // Process data contained in buffer. 179*cdf0e10cSrcweir read = data.readSomeBytes( buffer, 65536 ); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir /** 184*cdf0e10cSrcweir * Create a new connection with the specific args to a running office and 185*cdf0e10cSrcweir * access (Load) the content data stream of a UCB document content. 186*cdf0e10cSrcweir * 187*cdf0e10cSrcweir *@param String[] Arguments 188*cdf0e10cSrcweir */ 189*cdf0e10cSrcweir public static void main ( String args[] ) { 190*cdf0e10cSrcweir System.out.println( "\n" ); 191*cdf0e10cSrcweir System.out.println( 192*cdf0e10cSrcweir "-----------------------------------------------------------------------" ); 193*cdf0e10cSrcweir System.out.println( 194*cdf0e10cSrcweir "DataStreamRetriever - obtains the data stream from a document resource." ); 195*cdf0e10cSrcweir System.out.println( 196*cdf0e10cSrcweir "-----------------------------------------------------------------------" ); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir try { 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir DataStreamRetriever access = new DataStreamRetriever( args ); 201*cdf0e10cSrcweir XInputStream data = access.getDataStream(); 202*cdf0e10cSrcweir String url = access.getContentURL(); 203*cdf0e10cSrcweir if ( data != null ) { 204*cdf0e10cSrcweir String tempPrint = "\nGetting data stream for resource " + url + 205*cdf0e10cSrcweir " succeeded."; 206*cdf0e10cSrcweir int size = tempPrint.length(); 207*cdf0e10cSrcweir System.out.println( tempPrint ); 208*cdf0e10cSrcweir tempPrint = ""; 209*cdf0e10cSrcweir for( int i = 0; i < size; i++ ) { 210*cdf0e10cSrcweir tempPrint += "-"; 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir System.out.println( tempPrint ); 213*cdf0e10cSrcweir access.printStream( data ); 214*cdf0e10cSrcweir } else { 215*cdf0e10cSrcweir System.out.println( 216*cdf0e10cSrcweir "Getting data stream for resource " + url + " failed." ); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } catch ( com.sun.star.io.NotConnectedException e ) { 219*cdf0e10cSrcweir System.out.println( "Error: " + e ); 220*cdf0e10cSrcweir } catch ( com.sun.star.io.BufferSizeExceededException e ) { 221*cdf0e10cSrcweir System.out.println( "Error: " + e ); 222*cdf0e10cSrcweir } catch ( com.sun.star.io.IOException e ) { 223*cdf0e10cSrcweir System.out.println( "Error: " + e ); 224*cdf0e10cSrcweir } catch ( com.sun.star.ucb.CommandAbortedException e ) { 225*cdf0e10cSrcweir System.out.println( "Error: " + e ); 226*cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e ) { 227*cdf0e10cSrcweir System.out.println( "Error: " + e ); 228*cdf0e10cSrcweir } catch ( java.lang.Exception e ) { 229*cdf0e10cSrcweir System.out.println( "Error: " + e ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir System.exit( 0 ); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir } 234