1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /************************************************************************* 3 * 4 * The Contents of this file are made available subject to the terms of 5 * the BSD license. 6 * 7 * Copyright 2000, 2010 Oracle and/or its affiliates. 8 * All rights reserved. 9 * 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. 21 * 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. 33 * 34 *************************************************************************/ 35 36 import java.util.ArrayList; 37 import java.util.StringTokenizer; 38 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; 48 49 /** 50 * Retrieve the Children of a UCB Folder Content 51 */ 52 public class ChildrenRetriever { 53 54 /** 55 * Member properties 56 */ 57 private Helper m_helper; 58 private XContent m_content; 59 private String m_contenturl = ""; 60 private ArrayList<String> m_propnames = new ArrayList<String>(); 61 62 /** 63 * Constructor. Create a new connection with the specific args to a running office 64 * 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. 71 */ ChildrenRetriever( String args[] )72 public ChildrenRetriever( String args[] ) throws java.lang.Exception { 73 74 // Parse arguments 75 parseArguments( args ); 76 77 // Init 78 m_helper = new Helper( getContentURL() ); 79 80 // Create UCB content 81 m_content = m_helper.createUCBContent(); 82 } 83 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. 88 * 89 *@return Returns children properties values if values successfully retrieved, 90 * null otherwise 91 */ getChildren()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 ); 96 } 97 98 /** 99 * Open a folder content, get properties values for the properties. 100 * 101 *@return Returns children properties values if values successfully retrieved, 102 * null otherwise 103 */ getChildren( ArrayList<String> properties )104 public ArrayList<ArrayList<Object>> getChildren( ArrayList<String> properties ) 105 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 106 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(); 112 } 113 // Fill info for the properties wanted. 114 Property[] props = new Property[ size ]; 115 for ( int index = 0 ; index < size; index++ ) { 116 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; 122 } 123 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; 129 130 XDynamicResultSet set; 131 132 // Execute command "open". 133 set = UnoRuntime.queryInterface( 134 XDynamicResultSet.class, m_helper.executeCommand( m_content, "open", arg )); 135 XResultSet resultSet = set.getStaticResultSet(); 136 137 result = new ArrayList<ArrayList<Object>>(); 138 139 140 // Iterate over children, access children and property values... 141 142 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 ); 148 149 do { 150 ArrayList<Object> propsValues = new ArrayList<Object>(); 151 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 ]" ); 161 } 162 } 163 result.add( propsValues ); 164 } while ( resultSet.next() ); // next child 165 } 166 } 167 return result; 168 } 169 170 /** 171 * Get connect URL. 172 * 173 *@return String That contains the connect URL 174 */ getContentURL()175 public String getContentURL() { 176 return m_contenturl; 177 } 178 179 /** 180 * Get the properties. 181 * 182 *@return String That contains the properties 183 */ getProperties()184 public ArrayList<String> getProperties() { 185 return m_propnames; 186 } 187 188 /** 189 * Parse arguments 190 * 191 *@param args Arguments 192 */ parseArguments( String[] args )193 public void parseArguments( String[] args ) throws java.lang.Exception { 194 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 ), ";" ); 201 202 while ( tok.hasMoreTokens() ) 203 m_propnames.add( tok.nextToken() ); 204 205 } else if ( args[i].startsWith( "-help" ) || 206 args[i].startsWith( "-?" )) { 207 printCmdLineUsage(); 208 System.exit( 0 ); 209 } 210 } 211 212 if ( m_contenturl == null || m_contenturl.equals( "" )) { 213 m_contenturl = "file:///"; 214 } 215 216 if ( m_propnames.size() == 0 ) { 217 m_propnames.add( "Title" ); 218 m_propnames.add( "IsDocument" ); 219 } 220 } 221 222 /** 223 * Print the commands options 224 */ printCmdLineUsage()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" ); 232 } 233 234 /** 235 * Print all properties out contained in vector . 236 */ printLine( ArrayList<Object> props )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 += " "; 251 } 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 ); 259 } 260 } else { 261 for ( int l = 0; l < limit; l++) { 262 print += " "; 263 } 264 print+= " "; 265 } 266 } 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; 273 } 274 if( isEmpty ) 275 props.clear(); 276 } 277 } 278 279 /** 280 * Create a new connection with the specific args to a running office and 281 * access the children from a folder. 282 */ main( String args[] )283 public static void main ( String args[] ) { 284 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 "-----------------------------------------------------------------" ); 292 293 try { 294 ChildrenRetriever access = new ChildrenRetriever( args ); 295 296 // Get the properties Title and IsFolder for the children. 297 ArrayList<ArrayList<Object>> result = access.getChildren(); 298 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 += "-"; 305 } 306 System.out.println( tempPrint ); 307 308 if ( result != null && !result.isEmpty() ) { 309 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 + ":" ); 318 } 319 access.printLine(cont); 320 System.out.println( "\n" ); 321 for ( ArrayList<Object> propsV : result ) { 322 access.printLine( propsV ); 323 } 324 } 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 ); 333 } 334 System.exit( 0 ); 335 } 336 } 337 338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 339