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