tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / DataStreamRetriever.java
blob5644af0c2f0cca5208619deeb1914450c6552316
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.ucb.OpenCommandArgument2;
37 import com.sun.star.ucb.OpenMode;
38 import com.sun.star.ucb.XContent;
39 import com.sun.star.io.XActiveDataSink;
40 import com.sun.star.io.XInputStream;
42 /**
43 * Accessing (Loading) the Content Data Stream of a UCB Document Content
45 public class DataStreamRetriever {
47 /**
48 * Member properties
50 private Helper m_helper;
51 private XContent m_content;
52 private String m_contenturl = "";
54 /**
55 * Constructor.
57 *@param args This constructor requires the arguments:
58 * -url=... (optional)
59 * See Help (method printCmdLineUsage()).
60 * Without the arguments a new connection to a
61 * running office cannot created.
63 public DataStreamRetriever( String args[] ) throws java.lang.Exception {
65 // Parse arguments
66 parseArguments( args );
68 // Init
69 m_helper = new Helper( getContentURL() );
71 // Create UCB content
72 m_content = m_helper.createUCBContent();
75 /**
76 * Read the document data stream of a document content using a
77 * XActiveDataSink implementation as data sink...
79 *@return XInputStream Returns input stream if stream successfully retrieved,
80 * null otherwise
82 public XInputStream getDataStream()
83 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
85 XInputStream data = null;
86 if ( m_content != null ) {
88 // Fill argument structure...
89 OpenCommandArgument2 arg = new OpenCommandArgument2();
90 arg.Mode = OpenMode.DOCUMENT;
91 arg.Priority = 32768; // static final for 32768
93 // Create data sink implementation object.
94 XActiveDataSink dataSink = new MyActiveDataSink();
95 arg.Sink = dataSink;
97 // Execute command "open". The implementation of the command will
98 // supply an XInputStream implementation to the data sink.
99 m_helper.executeCommand( m_content, "open", arg );
101 // Get input stream supplied by the open command implementation.
102 data = dataSink.getInputStream();
104 return data;
108 * Get connect URL.
110 *@return String That contains the connect URL
112 public String getContentURL() {
113 return m_contenturl;
117 * Parse arguments
119 *@param args Arguments
121 private void parseArguments( String[] args ) throws java.lang.Exception {
123 for ( int i = 0; i < args.length; i++ ) {
124 if ( args[i].startsWith( "-url=" )) {
125 m_contenturl = args[i].substring( 5 );
126 } else if ( args[i].startsWith( "-help" ) ||
127 args[i].startsWith( "-?" )) {
128 printCmdLineUsage();
129 System.exit( 0 );
133 if ( m_contenturl == null || m_contenturl.equals( "" )) {
134 m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
139 * Print the commands options
141 private void printCmdLineUsage() {
142 System.out.println(
143 "Usage : DataStreamRetriever -url=..." );
144 System.out.println(
145 "Defaults: -url=<currentdir>/data/data.txt" );
146 System.out.println(
147 "\nExample : -url=file:///temp/my.txt" );
151 * Print Stream content.
153 private void printStream( XInputStream data )
154 throws com.sun.star.uno.Exception {
157 // Read data from input stream...65536
160 // Data buffer. Will be allocated by input stream implementation!
161 byte[][] buffer = new byte[ 1 ][ 65536 ];
162 int read = data.readSomeBytes( buffer, 65536 );
163 System.out.println( "Read bytes : " + read );
164 System.out.println( "Read data (only first 64K displayed): ");
165 while ( read > 0 ) {
166 byte[] bytes = new byte[ read ];
167 for( int i = 0; i < read; i++ ) {
168 bytes[ i ] = buffer[ 0 ][ i ];
170 System.out.println( new String(bytes) );
172 // Process data contained in buffer.
173 read = data.readSomeBytes( buffer, 65536 );
178 * Create a new connection with the specific args to a running office and
179 * access (Load) the content data stream of a UCB document content.
181 public static void main ( String args[] ) {
182 System.out.println( "\n" );
183 System.out.println(
184 "-----------------------------------------------------------------------" );
185 System.out.println(
186 "DataStreamRetriever - obtains the data stream from a document resource." );
187 System.out.println(
188 "-----------------------------------------------------------------------" );
190 try {
192 DataStreamRetriever access = new DataStreamRetriever( args );
193 XInputStream data = access.getDataStream();
194 String url = access.getContentURL();
195 if ( data != null ) {
196 String tempPrint = "\nGetting data stream for resource " + url +
197 " succeeded.";
198 int size = tempPrint.length();
199 System.out.println( tempPrint );
200 tempPrint = "";
201 for( int i = 0; i < size; i++ ) {
202 tempPrint += "-";
204 System.out.println( tempPrint );
205 access.printStream( data );
206 } else {
207 System.out.println(
208 "Getting data stream for resource " + url + " failed." );
210 } catch ( com.sun.star.io.NotConnectedException e ) {
211 System.out.println( "Error: " + e );
212 } catch ( com.sun.star.io.BufferSizeExceededException e ) {
213 System.out.println( "Error: " + e );
214 } catch ( com.sun.star.io.IOException e ) {
215 System.out.println( "Error: " + e );
216 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
217 System.out.println( "Error: " + e );
218 } catch ( com.sun.star.uno.Exception e ) {
219 System.out.println( "Error: " + e );
220 } catch ( java.lang.Exception e ) {
221 System.out.println( "Error: " + e );
223 System.exit( 0 );
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */