merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / UCB / DataStreamComposer.java
blob26a77587fb95c038269ffd3f4b1f842927e49624
1 /*************************************************************************
3 * $RCSfile: DataStreamComposer.java,v $
5 * $Revision: 1.6 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:57:25 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.ucb.InsertCommandArgument;
42 import com.sun.star.ucb.XContent;
43 import com.sun.star.io.XInputStream;
45 /**
46 * Setting (Storing) the Content Data Stream of a UCB Document Content.
48 public class DataStreamComposer {
50 /**
51 * Member properties
53 private Helper m_helper;
54 private XContent m_content;
55 private String m_contenturl = "";
56 private String m_srcURL = "";
59 /**
60 * Constructor.
62 *@param String[] This construtor requires the arguments:
63 * -url=... (optional)
64 * -srcURL=... (optional)
65 * -workdir=... (optional)
66 * See Help (method printCmdLineUsage()).
67 * Without the arguments a new connection to a
68 * running office cannot created.
69 *@exception java.lang.Exception
71 public DataStreamComposer( String args[] ) throws java.lang.Exception {
73 // Parse arguments
74 parseArguments( args );
76 // Init
77 m_helper = new Helper( getContentURL() );
79 // Create UCB content
80 m_content = m_helper.createUCBContent();
83 /**
84 * Write the document data stream of a document content.
85 * This method requires the main and the optional arguments to be set in order to work.
86 * See Constructor.
88 *@return boolean Result
89 *@exception com.sun.star.ucb.CommandAbortedException
90 *@exception com.sun.star.uno.Exception
91 *@exception java.lang.Exception
93 public boolean setDataStream()
94 throws com.sun.star.ucb.CommandAbortedException,
95 com.sun.star.uno.Exception,
96 java.lang.Exception {
98 String sourceURL = getSourceURL();
99 return ( setDataStream( sourceURL ));
103 * Write the document data stream of a document content.
105 *@param String Source URL
106 *@return boolean Returns true if data stream successfully seted, false otherwise
107 *@exception com.sun.star.ucb.CommandAbortedException
108 *@exception com.sun.star.uno.Exception
109 *@exception java.lang.Exception
111 public boolean setDataStream( String sourceURL )
112 throws com.sun.star.ucb.CommandAbortedException,
113 com.sun.star.uno.Exception,
114 java.lang.Exception {
116 XInputStream stream;
117 if ( sourceURL == null || sourceURL.equals("") ) {
118 stream = new MyInputStream();
119 } else {
120 String[] args = new String[ 1 ];
121 args[ 0 ] = "-url=" + sourceURL;
122 DataStreamRetriever access = new DataStreamRetriever( args );
123 stream = access.getDataStream();
125 return ( setDataStream( stream ));
129 * Write the document data stream of a document content...
131 *@param XInputStream Stream
132 *@return boolean Returns true if data stream successfully seted, false otherwise
133 *@exception com.sun.star.ucb.CommandAbortedException
134 *@exception com.sun.star.uno.Exception
136 public boolean setDataStream( XInputStream stream )
137 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
139 boolean result = false;
140 XInputStream data = stream;
141 if ( data != null && m_content != null ) {
143 // Fill argument structure...
144 InsertCommandArgument arg = new InsertCommandArgument();
145 arg.Data = data;
146 arg.ReplaceExisting = true;
148 // Execute command "insert".
149 m_helper.executeCommand( m_content, "insert", arg );
150 result = true;
152 return result;
156 * Get source URL.
158 *@return String That contains the source URL
160 public String getSourceURL() {
161 return m_srcURL;
165 * Get connect URL.
167 *@return String That contains the connect URL
169 public String getContentURL() {
170 return m_contenturl;
174 * Parse arguments
176 *@param String[] Arguments
177 *@exception java.lang.Exception
179 public void parseArguments( String[] args ) throws java.lang.Exception {
181 String workdir = "";
183 for ( int i = 0; i < args.length; i++ ) {
184 if ( args[i].startsWith( "-url=" )) {
185 m_contenturl = args[i].substring( 5 );
186 } else if ( args[i].startsWith( "-srcURL=" )) {
187 m_srcURL = args[i].substring( 9 );
188 } else if ( args[i].startsWith( "-workdir=" )) {
189 workdir = args[i].substring( 9 );
190 } else if ( args[i].startsWith( "-help" ) ||
191 args[i].startsWith( "-?" )) {
192 printCmdLineUsage();
193 System.exit( 0 );
197 if ( m_contenturl == null || m_contenturl.equals( "" )) {
198 m_contenturl = Helper.createTargetDataFile( workdir );
201 if ( m_srcURL == null || m_srcURL.equals( "" )) {
202 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
207 * Print the commands options
209 public void printCmdLineUsage() {
210 System.out.println(
211 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
212 System.out.println(
213 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
214 System.out.println(
215 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
220 * Create a new connection with the specific args to a running office and
221 * set the Content Data Stream of a UCB Document Content.
223 *@param String[] Arguments
225 public static void main ( String args[] ) {
226 System.out.println( "\n" );
227 System.out.println(
228 "-----------------------------------------------------------------" );
229 System.out.println(
230 "DataStreamComposer - sets the data stream of a document resource." );
231 System.out.println(
232 " The data stream is obtained from another (the source) document " );
233 System.out.println(
234 " resource before." );
235 System.out.println(
236 "-----------------------------------------------------------------" );
237 try {
239 DataStreamComposer dataStream = new DataStreamComposer( args );
240 String sourceURL = dataStream.getSourceURL();
241 boolean result = dataStream.setDataStream( sourceURL );
242 if ( result ) {
243 System.out.println(
244 "\nSetting data stream succeeded.\n Source URL: " +
245 dataStream.getSourceURL() +
246 "\n Target URL: " +
247 dataStream.getContentURL() );
248 } else {
249 System.out.println(
250 "\nSetting data stream failed. \n Source URL: " +
251 dataStream.getSourceURL() +
252 "\n Target URL: " +
253 dataStream.getContentURL() );
255 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
256 System.out.println( "Error: " + e );
257 } catch ( com.sun.star.uno.Exception e ) {
258 System.out.println( "Error: " + e );
259 } catch ( java.lang.Exception e ) {
260 System.out.println( "Error: " + e );
262 System.exit( 0 );