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