Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / ChildrenRetriever.java
blob9764718a175126ff91cbf6ef59b93f23093bf911
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 java.util.ArrayList;
36 import java.util.StringTokenizer;
38 import com.sun.star.beans.Property;
39 import com.sun.star.sdbc.XResultSet;
40 import com.sun.star.sdbc.XRow;
41 import com.sun.star.ucb.OpenCommandArgument2;
42 import com.sun.star.ucb.OpenMode;
43 import com.sun.star.ucb.XContent;
44 import com.sun.star.ucb.XContentAccess;
45 import com.sun.star.ucb.XDynamicResultSet;
46 import com.sun.star.uno.UnoRuntime;
48 /**
49 * Retrieve the Children of a UCB Folder Content
51 public class ChildrenRetriever {
53 /**
54 * Member properties
56 private Helper m_helper;
57 private XContent m_content;
58 private String m_contenturl = "";
59 private ArrayList<String> m_propnames = new ArrayList<String>();
61 /**
62 * Constructor. Create a new connection with the specific args to a running office
64 *@param args This construtor requires the arguments:
65 * -url=... (optional)
66 * -propNames=... (optional)
67 * See Help (method printCmdLineUsage()).
68 * Without the arguments a new connection to a
69 * running office cannot created.
71 public ChildrenRetriever( 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 * Open a folder content, get properties values.
85 * This method requires the main and the optional arguments to be set in order to work.
86 * See Constructor.
88 *@return Returns children properties values if values successfully retrieved,
89 * null otherwise
91 public ArrayList<ArrayList<Object>> getChildren()
92 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
93 ArrayList<String> properties = getProperties();
94 return getChildren ( properties );
97 /**
98 * Open a folder content, get properties values for the properties.
100 *@return Returns children properties values if values successfully retrieved,
101 * null otherwise
103 public ArrayList<ArrayList<Object>> getChildren( ArrayList<String> properties )
104 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
106 ArrayList<ArrayList<Object>> result = null;
107 if ( m_content != null ) {
108 int size = 0;
109 if ( properties != null && !properties.isEmpty()) {
110 size = properties.size();
112 // Fill info for the properties wanted.
113 Property[] props = new Property[ size ];
114 for ( int index = 0 ; index < size; index++ ) {
116 // Define property sequence.
117 Property prop = new Property();
118 prop.Name = properties.get( index );
119 prop.Handle = -1; // n/a
120 props[ index ] = prop;
123 // Fill argument structure...
124 OpenCommandArgument2 arg = new OpenCommandArgument2();
125 arg.Mode = OpenMode.ALL; // FOLDER, DOCUMENTS -> simple filter
126 arg.Priority = 32768; // Final static for 32768
127 arg.Properties = props;
129 XDynamicResultSet set;
131 // Execute command "open".
132 set = UnoRuntime.queryInterface(
133 XDynamicResultSet.class, m_helper.executeCommand( m_content, "open", arg ));
134 XResultSet resultSet = set.getStaticResultSet();
136 result = new ArrayList<ArrayList<Object>>();
139 // Iterate over children, access children and property values...
142 // Move to begin.
143 if ( resultSet.first() ) {
144 XContentAccess contentAccess = UnoRuntime.queryInterface(
145 XContentAccess.class, resultSet );
146 XRow row = UnoRuntime.queryInterface( XRow.class, resultSet );
148 do {
149 ArrayList<Object> propsValues = new ArrayList<Object>();
151 // Obtain URL of child.
152 String id = contentAccess.queryContentIdentifierString();
153 propsValues.add( id );
154 for ( int i = 1; i <= size ; i++) {
155 Object propValue = row.getObject( i, null );
156 if ( !row.wasNull() && !(propValue instanceof com.sun.star.uno.Any )) {
157 propsValues.add( propValue );
158 } else {
159 propsValues.add( "[ Property not found ]" );
162 result.add( propsValues );
163 } while ( resultSet.next() ); // next child
166 return result;
170 * Get connect URL.
172 *@return String That contains the connect URL
174 public String getContentURL() {
175 return m_contenturl;
179 * Get the properties.
181 *@return String That contains the properties
183 public ArrayList<String> getProperties() {
184 return m_propnames;
188 * Parse arguments
190 *@param args Arguments
192 public void parseArguments( String[] args ) throws java.lang.Exception {
194 for ( int i = 0; i < args.length; i++ ) {
195 if ( args[i].startsWith( "-url=" )) {
196 m_contenturl = args[i].substring( 5 );
197 } else if ( args[i].startsWith( "-propNames=" )) {
198 StringTokenizer tok
199 = new StringTokenizer( args[i].substring( 11 ), ";" );
201 while ( tok.hasMoreTokens() )
202 m_propnames.add( tok.nextToken() );
204 } else if ( args[i].startsWith( "-help" ) ||
205 args[i].startsWith( "-?" )) {
206 printCmdLineUsage();
207 System.exit( 0 );
211 if ( m_contenturl == null || m_contenturl.equals( "" )) {
212 m_contenturl = "file:///";
215 if ( m_propnames.size() == 0 ) {
216 m_propnames.add( "Title" );
217 m_propnames.add( "IsDocument" );
222 * Print the commands options
224 public void printCmdLineUsage() {
225 System.out.println(
226 "Usage : ChildrenRetriever -url=... -propNames=..." );
227 System.out.println(
228 "Defaults: -url=file:/// -propNames=Title,IsDocument" );
229 System.out.println(
230 "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" );
234 * Print all properties out contained in vector .
236 public void printLine( ArrayList<Object> props ) {
237 int limit;
238 while ( !props.isEmpty() ) {
239 String print = "";
240 int size = props.size();
241 for ( int i = 0; i < size; i++ ) {
242 limit = 15;
243 Object obj = props.get( i );
244 if ( obj != null) {
245 String prop = obj.toString();
246 int leng = prop.length();
247 if ( leng < limit ) {
248 for ( int l = leng; l < limit; l++) {
249 prop += " ";
251 print+= prop + " ";
252 props.set( i, null );
253 } else {
254 String temp1 = prop.substring( 0, limit );
255 String temp2 = prop.substring( limit );
256 print+= temp1 + " ";
257 props.set( i, temp2 );
259 } else {
260 for ( int l = 0; l < limit; l++) {
261 print += " ";
263 print+= " ";
266 System.out.println( print );
267 boolean isEmpty = true;
268 for ( int i = 0; i < size; i++ ) {
269 Object obj = props.get( i );
270 if( obj != null )
271 isEmpty = false;
273 if( isEmpty )
274 props.clear();
279 * Create a new connection with the specific args to a running office and
280 * access the children from a folder.
282 public static void main ( String args[] ) {
284 System.out.println( "\n" );
285 System.out.println(
286 "-----------------------------------------------------------------" );
287 System.out.println(
288 "ChildrenRetriever - obtains the children of a folder resource." );
289 System.out.println(
290 "-----------------------------------------------------------------" );
292 try {
293 ChildrenRetriever access = new ChildrenRetriever( args );
295 // Get the properties Title and IsFolder for the children.
296 ArrayList<ArrayList<Object>> result = access.getChildren();
298 String tempPrint = "\nChildren of resource " + access.getContentURL();
299 int size = tempPrint.length();
300 System.out.println( tempPrint );
301 tempPrint = "";
302 for( int i = 0; i < size; i++ ) {
303 tempPrint += "-";
305 System.out.println( tempPrint );
307 if ( result != null && !result.isEmpty() ) {
309 ArrayList<Object> cont = new ArrayList<Object>();
310 cont.add("URL:");
311 ArrayList<String> props = access.getProperties();
312 size = props.size();
313 for ( int i = 0; i < size; i++ ) {
314 Object obj = props.get( i );
315 String prop = obj.toString();
316 cont.add( prop + ":" );
318 access.printLine(cont);
319 System.out.println( "\n" );
320 for ( ArrayList<Object> propsV : result ) {
321 access.printLine( propsV );
324 } catch ( com.sun.star.ucb.ResultSetException e ) {
325 System.out.println( "Error: " + e );
326 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
327 System.out.println( "Error: " + e );
328 } catch ( com.sun.star.uno.Exception e ) {
329 System.out.println( "Error: " + e );
330 } catch ( java.lang.Exception e ) {
331 System.out.println( "Error: " + e );
333 System.exit( 0 );