Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / DataStreamComposer.java
blobce6edae31998bcdd15825b2767aeda28b1a425bf
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.InsertCommandArgument;
37 import com.sun.star.ucb.XContent;
38 import com.sun.star.io.XInputStream;
40 /**
41 * Setting (Storing) the Content Data Stream of a UCB Document Content.
43 public class DataStreamComposer {
45 /**
46 * Member properties
48 private Helper m_helper;
49 private XContent m_content;
50 private String m_contenturl = "";
51 private String m_srcURL = "";
54 /**
55 * Constructor.
57 *@param args This constructor requires the arguments:
58 * -url=... (optional)
59 * -srcURL=... (optional)
60 * -workdir=... (optional)
61 * See Help (method printCmdLineUsage()).
62 * Without the arguments a new connection to a
63 * running office cannot created.
65 public DataStreamComposer( String args[] ) throws java.lang.Exception {
67 // Parse arguments
68 parseArguments( args );
70 // Init
71 m_helper = new Helper( getContentURL() );
73 // Create UCB content
74 m_content = m_helper.createUCBContent();
77 /**
78 * Write the document data stream of a document content.
79 * This method requires the main and the optional arguments to be set in order to work.
80 * See Constructor.
82 *@return boolean Result
84 public boolean setDataStream()
85 throws com.sun.star.ucb.CommandAbortedException,
86 com.sun.star.uno.Exception,
87 java.lang.Exception {
89 String sourceURL = getSourceURL();
90 return ( setDataStream( sourceURL ));
93 /**
94 * Write the document data stream of a document content.
96 *@param sourceURL Source URL
97 *@return true if data stream successfully seted, false otherwise
99 public boolean setDataStream( String sourceURL )
100 throws com.sun.star.ucb.CommandAbortedException,
101 com.sun.star.uno.Exception,
102 java.lang.Exception {
104 XInputStream stream;
105 if ( sourceURL == null || sourceURL.equals("") ) {
106 stream = new MyInputStream();
107 } else {
108 String[] args = new String[ 1 ];
109 args[ 0 ] = "-url=" + sourceURL;
110 DataStreamRetriever access = new DataStreamRetriever( args );
111 stream = access.getDataStream();
113 return ( setDataStream( stream ));
117 * Write the document data stream of a document content...
119 *@return boolean Returns true if data stream successfully seted, false otherwise
121 public boolean setDataStream( XInputStream stream )
122 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
124 boolean result = false;
125 XInputStream data = stream;
126 if ( data != null && m_content != null ) {
128 // Fill argument structure...
129 InsertCommandArgument arg = new InsertCommandArgument();
130 arg.Data = data;
131 arg.ReplaceExisting = true;
133 // Execute command "insert".
134 m_helper.executeCommand( m_content, "insert", arg );
135 result = true;
137 return result;
141 * Get source URL.
143 *@return String That contains the source URL
145 public String getSourceURL() {
146 return m_srcURL;
150 * Get connect URL.
152 *@return String That contains the connect URL
154 public String getContentURL() {
155 return m_contenturl;
159 * Parse arguments
161 public void parseArguments( String[] args ) throws java.lang.Exception {
163 String workdir = "";
165 for ( int i = 0; i < args.length; i++ ) {
166 if ( args[i].startsWith( "-url=" )) {
167 m_contenturl = args[i].substring( 5 );
168 } else if ( args[i].startsWith( "-srcURL=" )) {
169 m_srcURL = args[i].substring( 9 );
170 } else if ( args[i].startsWith( "-workdir=" )) {
171 workdir = args[i].substring( 9 );
172 } else if ( args[i].startsWith( "-help" ) ||
173 args[i].startsWith( "-?" )) {
174 printCmdLineUsage();
175 System.exit( 0 );
179 if ( m_contenturl == null || m_contenturl.equals( "" )) {
180 m_contenturl = Helper.createTargetDataFile( workdir );
183 if ( m_srcURL == null || m_srcURL.equals( "" )) {
184 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
189 * Print the commands options
191 public void printCmdLineUsage() {
192 System.out.println(
193 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
194 System.out.println(
195 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
196 System.out.println(
197 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
202 * Create a new connection with the specific args to a running office and
203 * set the Content Data Stream of a UCB Document Content.
205 public static void main ( String args[] ) {
206 System.out.println( "\n" );
207 System.out.println(
208 "-----------------------------------------------------------------" );
209 System.out.println(
210 "DataStreamComposer - sets the data stream of a document resource." );
211 System.out.println(
212 " The data stream is obtained from another (the source) document " );
213 System.out.println(
214 " resource before." );
215 System.out.println(
216 "-----------------------------------------------------------------" );
217 try {
219 DataStreamComposer dataStream = new DataStreamComposer( args );
220 String sourceURL = dataStream.getSourceURL();
221 boolean result = dataStream.setDataStream( sourceURL );
222 if ( result ) {
223 System.out.println(
224 "\nSetting data stream succeeded.\n Source URL: " +
225 dataStream.getSourceURL() +
226 "\n Target URL: " +
227 dataStream.getContentURL() );
228 } else {
229 System.out.println(
230 "\nSetting data stream failed. \n Source URL: " +
231 dataStream.getSourceURL() +
232 "\n Target URL: " +
233 dataStream.getContentURL() );
235 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
236 System.out.println( "Error: " + e );
237 } catch ( com.sun.star.uno.Exception e ) {
238 System.out.println( "Error: " + e );
239 } catch ( java.lang.Exception e ) {
240 System.out.println( "Error: " + e );
242 System.exit( 0 );
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */