fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / DataStreamComposer.java
blobdad05c1153e4378c5b5a11e241716d048a155f4c
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 import com.sun.star.ucb.InsertCommandArgument;
36 import com.sun.star.ucb.XContent;
37 import com.sun.star.io.XInputStream;
39 /**
40 * Setting (Storing) the Content Data Stream of a UCB Document Content.
42 public class DataStreamComposer {
44 /**
45 * Member properties
47 private Helper m_helper;
48 private XContent m_content;
49 private String m_contenturl = "";
50 private String m_srcURL = "";
53 /**
54 * Constructor.
56 *@param args This construtor requires the arguments:
57 * -url=... (optional)
58 * -srcURL=... (optional)
59 * -workdir=... (optional)
60 * See Help (method printCmdLineUsage()).
61 * Without the arguments a new connection to a
62 * running office cannot created.
64 public DataStreamComposer( String args[] ) throws java.lang.Exception {
66 // Parse arguments
67 parseArguments( args );
69 // Init
70 m_helper = new Helper( getContentURL() );
72 // Create UCB content
73 m_content = m_helper.createUCBContent();
76 /**
77 * Write the document data stream of a document content.
78 * This method requires the main and the optional arguments to be set in order to work.
79 * See Constructor.
81 *@return boolean Result
83 public boolean setDataStream()
84 throws com.sun.star.ucb.CommandAbortedException,
85 com.sun.star.uno.Exception,
86 java.lang.Exception {
88 String sourceURL = getSourceURL();
89 return ( setDataStream( sourceURL ));
92 /**
93 * Write the document data stream of a document content.
95 *@param sourceURL Source URL
96 *@return true if data stream successfully seted, false otherwise
98 public boolean setDataStream( String sourceURL )
99 throws com.sun.star.ucb.CommandAbortedException,
100 com.sun.star.uno.Exception,
101 java.lang.Exception {
103 XInputStream stream;
104 if ( sourceURL == null || sourceURL.equals("") ) {
105 stream = new MyInputStream();
106 } else {
107 String[] args = new String[ 1 ];
108 args[ 0 ] = "-url=" + sourceURL;
109 DataStreamRetriever access = new DataStreamRetriever( args );
110 stream = access.getDataStream();
112 return ( setDataStream( stream ));
116 * Write the document data stream of a document content...
118 *@return boolean Returns true if data stream successfully seted, false otherwise
120 public boolean setDataStream( XInputStream stream )
121 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
123 boolean result = false;
124 XInputStream data = stream;
125 if ( data != null && m_content != null ) {
127 // Fill argument structure...
128 InsertCommandArgument arg = new InsertCommandArgument();
129 arg.Data = data;
130 arg.ReplaceExisting = true;
132 // Execute command "insert".
133 m_helper.executeCommand( m_content, "insert", arg );
134 result = true;
136 return result;
140 * Get source URL.
142 *@return String That contains the source URL
144 public String getSourceURL() {
145 return m_srcURL;
149 * Get connect URL.
151 *@return String That contains the connect URL
153 public String getContentURL() {
154 return m_contenturl;
158 * Parse arguments
160 public void parseArguments( String[] args ) throws java.lang.Exception {
162 String workdir = "";
164 for ( int i = 0; i < args.length; i++ ) {
165 if ( args[i].startsWith( "-url=" )) {
166 m_contenturl = args[i].substring( 5 );
167 } else if ( args[i].startsWith( "-srcURL=" )) {
168 m_srcURL = args[i].substring( 9 );
169 } else if ( args[i].startsWith( "-workdir=" )) {
170 workdir = args[i].substring( 9 );
171 } else if ( args[i].startsWith( "-help" ) ||
172 args[i].startsWith( "-?" )) {
173 printCmdLineUsage();
174 System.exit( 0 );
178 if ( m_contenturl == null || m_contenturl.equals( "" )) {
179 m_contenturl = Helper.createTargetDataFile( workdir );
182 if ( m_srcURL == null || m_srcURL.equals( "" )) {
183 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
188 * Print the commands options
190 public void printCmdLineUsage() {
191 System.out.println(
192 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
193 System.out.println(
194 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
195 System.out.println(
196 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
201 * Create a new connection with the specific args to a running office and
202 * set the Content Data Stream of a UCB Document Content.
204 public static void main ( String args[] ) {
205 System.out.println( "\n" );
206 System.out.println(
207 "-----------------------------------------------------------------" );
208 System.out.println(
209 "DataStreamComposer - sets the data stream of a document resource." );
210 System.out.println(
211 " The data stream is obtained from another (the source) document " );
212 System.out.println(
213 " resource before." );
214 System.out.println(
215 "-----------------------------------------------------------------" );
216 try {
218 DataStreamComposer dataStream = new DataStreamComposer( args );
219 String sourceURL = dataStream.getSourceURL();
220 boolean result = dataStream.setDataStream( sourceURL );
221 if ( result ) {
222 System.out.println(
223 "\nSetting data stream succeeded.\n Source URL: " +
224 dataStream.getSourceURL() +
225 "\n Target URL: " +
226 dataStream.getContentURL() );
227 } else {
228 System.out.println(
229 "\nSetting data stream failed. \n Source URL: " +
230 dataStream.getSourceURL() +
231 "\n Target URL: " +
232 dataStream.getContentURL() );
234 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
235 System.out.println( "Error: " + e );
236 } catch ( com.sun.star.uno.Exception e ) {
237 System.out.println( "Error: " + e );
238 } catch ( java.lang.Exception e ) {
239 System.out.println( "Error: " + e );
241 System.exit( 0 );