cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / ChildrenRetriever.java
blob102119dfcb51a6ca731a9b42f666a65411693265
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 java.util.ArrayList;
37 import java.util.StringTokenizer;
39 import com.sun.star.beans.Property;
40 import com.sun.star.sdbc.XResultSet;
41 import com.sun.star.sdbc.XRow;
42 import com.sun.star.ucb.OpenCommandArgument2;
43 import com.sun.star.ucb.OpenMode;
44 import com.sun.star.ucb.XContent;
45 import com.sun.star.ucb.XContentAccess;
46 import com.sun.star.ucb.XDynamicResultSet;
47 import com.sun.star.uno.UnoRuntime;
49 /**
50 * Retrieve the Children of a UCB Folder Content
52 public class ChildrenRetriever {
54 /**
55 * Member properties
57 private Helper m_helper;
58 private XContent m_content;
59 private String m_contenturl = "";
60 private ArrayList<String> m_propnames = new ArrayList<String>();
62 /**
63 * Constructor. Create a new connection with the specific args to a running office
65 *@param args This constructor requires the arguments:
66 * -url=... (optional)
67 * -propNames=... (optional)
68 * See Help (method printCmdLineUsage()).
69 * Without the arguments a new connection to a
70 * running office cannot created.
72 public ChildrenRetriever( String args[] ) throws java.lang.Exception {
74 // Parse arguments
75 parseArguments( args );
77 // Init
78 m_helper = new Helper( getContentURL() );
80 // Create UCB content
81 m_content = m_helper.createUCBContent();
84 /**
85 * Open a folder content, get properties values.
86 * This method requires the main and the optional arguments to be set in order to work.
87 * See Constructor.
89 *@return Returns children properties values if values successfully retrieved,
90 * null otherwise
92 public ArrayList<ArrayList<Object>> getChildren()
93 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
94 ArrayList<String> properties = getProperties();
95 return getChildren ( properties );
98 /**
99 * Open a folder content, get properties values for the properties.
101 *@return Returns children properties values if values successfully retrieved,
102 * null otherwise
104 public ArrayList<ArrayList<Object>> getChildren( ArrayList<String> properties )
105 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
107 ArrayList<ArrayList<Object>> result = null;
108 if ( m_content != null ) {
109 int size = 0;
110 if ( properties != null && !properties.isEmpty()) {
111 size = properties.size();
113 // Fill info for the properties wanted.
114 Property[] props = new Property[ size ];
115 for ( int index = 0 ; index < size; index++ ) {
117 // Define property sequence.
118 Property prop = new Property();
119 prop.Name = properties.get( index );
120 prop.Handle = -1; // n/a
121 props[ index ] = prop;
124 // Fill argument structure...
125 OpenCommandArgument2 arg = new OpenCommandArgument2();
126 arg.Mode = OpenMode.ALL; // FOLDER, DOCUMENTS -> simple filter
127 arg.Priority = 32768; // static final for 32768
128 arg.Properties = props;
130 XDynamicResultSet set;
132 // Execute command "open".
133 set = UnoRuntime.queryInterface(
134 XDynamicResultSet.class, m_helper.executeCommand( m_content, "open", arg ));
135 XResultSet resultSet = set.getStaticResultSet();
137 result = new ArrayList<ArrayList<Object>>();
140 // Iterate over children, access children and property values...
143 // Move to begin.
144 if ( resultSet.first() ) {
145 XContentAccess contentAccess = UnoRuntime.queryInterface(
146 XContentAccess.class, resultSet );
147 XRow row = UnoRuntime.queryInterface( XRow.class, resultSet );
149 do {
150 ArrayList<Object> propsValues = new ArrayList<Object>();
152 // Obtain URL of child.
153 String id = contentAccess.queryContentIdentifierString();
154 propsValues.add( id );
155 for ( int i = 1; i <= size ; i++) {
156 Object propValue = row.getObject( i, null );
157 if ( !row.wasNull() && !(propValue instanceof com.sun.star.uno.Any )) {
158 propsValues.add( propValue );
159 } else {
160 propsValues.add( "[ Property not found ]" );
163 result.add( propsValues );
164 } while ( resultSet.next() ); // next child
167 return result;
171 * Get connect URL.
173 *@return String That contains the connect URL
175 public String getContentURL() {
176 return m_contenturl;
180 * Get the properties.
182 *@return String That contains the properties
184 public ArrayList<String> getProperties() {
185 return m_propnames;
189 * Parse arguments
191 *@param args Arguments
193 public void parseArguments( String[] args ) throws java.lang.Exception {
195 for ( int i = 0; i < args.length; i++ ) {
196 if ( args[i].startsWith( "-url=" )) {
197 m_contenturl = args[i].substring( 5 );
198 } else if ( args[i].startsWith( "-propNames=" )) {
199 StringTokenizer tok
200 = new StringTokenizer( args[i].substring( 11 ), ";" );
202 while ( tok.hasMoreTokens() )
203 m_propnames.add( tok.nextToken() );
205 } else if ( args[i].startsWith( "-help" ) ||
206 args[i].startsWith( "-?" )) {
207 printCmdLineUsage();
208 System.exit( 0 );
212 if ( m_contenturl == null || m_contenturl.equals( "" )) {
213 m_contenturl = "file:///";
216 if ( m_propnames.size() == 0 ) {
217 m_propnames.add( "Title" );
218 m_propnames.add( "IsDocument" );
223 * Print the commands options
225 public void printCmdLineUsage() {
226 System.out.println(
227 "Usage : ChildrenRetriever -url=... -propNames=..." );
228 System.out.println(
229 "Defaults: -url=file:/// -propNames=Title,IsDocument" );
230 System.out.println(
231 "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" );
235 * Print all properties out contained in vector .
237 public void printLine( ArrayList<Object> props ) {
238 int limit;
239 while ( !props.isEmpty() ) {
240 String print = "";
241 int size = props.size();
242 for ( int i = 0; i < size; i++ ) {
243 limit = 15;
244 Object obj = props.get( i );
245 if ( obj != null) {
246 String prop = obj.toString();
247 int leng = prop.length();
248 if ( leng < limit ) {
249 for ( int l = leng; l < limit; l++) {
250 prop += " ";
252 print+= prop + " ";
253 props.set( i, null );
254 } else {
255 String temp1 = prop.substring( 0, limit );
256 String temp2 = prop.substring( limit );
257 print+= temp1 + " ";
258 props.set( i, temp2 );
260 } else {
261 for ( int l = 0; l < limit; l++) {
262 print += " ";
264 print+= " ";
267 System.out.println( print );
268 boolean isEmpty = true;
269 for ( int i = 0; i < size; i++ ) {
270 Object obj = props.get( i );
271 if( obj != null )
272 isEmpty = false;
274 if( isEmpty )
275 props.clear();
280 * Create a new connection with the specific args to a running office and
281 * access the children from a folder.
283 public static void main ( String args[] ) {
285 System.out.println( "\n" );
286 System.out.println(
287 "-----------------------------------------------------------------" );
288 System.out.println(
289 "ChildrenRetriever - obtains the children of a folder resource." );
290 System.out.println(
291 "-----------------------------------------------------------------" );
293 try {
294 ChildrenRetriever access = new ChildrenRetriever( args );
296 // Get the properties Title and IsFolder for the children.
297 ArrayList<ArrayList<Object>> result = access.getChildren();
299 String tempPrint = "\nChildren of resource " + access.getContentURL();
300 int size = tempPrint.length();
301 System.out.println( tempPrint );
302 tempPrint = "";
303 for( int i = 0; i < size; i++ ) {
304 tempPrint += "-";
306 System.out.println( tempPrint );
308 if ( result != null && !result.isEmpty() ) {
310 ArrayList<Object> cont = new ArrayList<Object>();
311 cont.add("URL:");
312 ArrayList<String> props = access.getProperties();
313 size = props.size();
314 for ( int i = 0; i < size; i++ ) {
315 Object obj = props.get( i );
316 String prop = obj.toString();
317 cont.add( prop + ":" );
319 access.printLine(cont);
320 System.out.println( "\n" );
321 for ( ArrayList<Object> propsV : result ) {
322 access.printLine( propsV );
325 } catch ( com.sun.star.ucb.ResultSetException e ) {
326 System.out.println( "Error: " + e );
327 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
328 System.out.println( "Error: " + e );
329 } catch ( com.sun.star.uno.Exception e ) {
330 System.out.println( "Error: " + e );
331 } catch ( java.lang.Exception e ) {
332 System.out.println( "Error: " + e );
334 System.exit( 0 );
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */