1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2; 25cdf0e10cSrcweir import com.sun.star.ucb.OpenMode; 26cdf0e10cSrcweir import com.sun.star.ucb.XContent; 27cdf0e10cSrcweir import com.sun.star.ucb.XContentAccess; 28cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet; 29cdf0e10cSrcweir import com.sun.star.beans.Property; 30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 31cdf0e10cSrcweir import com.sun.star.sdbc.XRow; 32cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet; 33cdf0e10cSrcweir 34cdf0e10cSrcweir import java.util.Vector; 35cdf0e10cSrcweir import java.util.Enumeration; 36cdf0e10cSrcweir import java.util.StringTokenizer; 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** 39cdf0e10cSrcweir * Retrieve the Children of a UCB Folder Content 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir public class ChildrenRetriever { 42cdf0e10cSrcweir 43cdf0e10cSrcweir /** 44cdf0e10cSrcweir * Member properties 45cdf0e10cSrcweir */ 46cdf0e10cSrcweir private Helper m_helper; 47cdf0e10cSrcweir private XContent m_content; 48cdf0e10cSrcweir private String m_contenturl = ""; 49cdf0e10cSrcweir private Vector m_propnames = new Vector(); 50cdf0e10cSrcweir 51cdf0e10cSrcweir /** 52cdf0e10cSrcweir * Constructor. Create a new connection with the specific args to a running office 53cdf0e10cSrcweir * 54cdf0e10cSrcweir *@param String[] This construtor requires the arguments: 55cdf0e10cSrcweir * -url=... (optional) 56cdf0e10cSrcweir * -propNames=... (optional) 57cdf0e10cSrcweir * See Help (method printCmdLineUsage()). 58cdf0e10cSrcweir * Without the arguments a new connection to a 59cdf0e10cSrcweir * running office cannot created. 60cdf0e10cSrcweir *@exception java.lang.Exception 61cdf0e10cSrcweir */ ChildrenRetriever( String args[] )62cdf0e10cSrcweir public ChildrenRetriever( String args[] ) throws java.lang.Exception { 63cdf0e10cSrcweir 64cdf0e10cSrcweir // Parse arguments 65cdf0e10cSrcweir parseArguments( args ); 66cdf0e10cSrcweir 67cdf0e10cSrcweir // Init 68cdf0e10cSrcweir m_helper = new Helper( getContentURL() ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir // Create UCB content 71cdf0e10cSrcweir m_content = m_helper.createUCBContent(); 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** 75cdf0e10cSrcweir * Open a folder content, get properties values. 76cdf0e10cSrcweir * This method requires the main and the optional arguments to be set in order to work. 77cdf0e10cSrcweir * See Constructor. 78cdf0e10cSrcweir * 79cdf0e10cSrcweir *@return Vector Returns children properties values if values successfully retrieved, 80cdf0e10cSrcweir * null otherwise 81cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 82cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 83cdf0e10cSrcweir */ getChildren()84cdf0e10cSrcweir public Vector getChildren() 85cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 86cdf0e10cSrcweir Vector properties = getProperties(); 87cdf0e10cSrcweir return getChildren ( properties ); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir /** 91cdf0e10cSrcweir * Open a folder content, get properties values for the properties. 92cdf0e10cSrcweir * 93cdf0e10cSrcweir *@param Vector Properties 94cdf0e10cSrcweir *@return Vector Returns children properties values if values successfully retrieved, 95cdf0e10cSrcweir * null otherwise 96cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 97cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 98cdf0e10cSrcweir */ getChildren( Vector properties )99cdf0e10cSrcweir public Vector getChildren( Vector properties ) 100cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 101cdf0e10cSrcweir 102cdf0e10cSrcweir Vector result = null; 103cdf0e10cSrcweir if ( m_content != null ) { 104cdf0e10cSrcweir int size = 0; 105cdf0e10cSrcweir if ( properties != null && !properties.isEmpty()) { 106cdf0e10cSrcweir size = properties.size(); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir // Fill info for the properties wanted. 109cdf0e10cSrcweir Property[] props = new Property[ size ]; 110cdf0e10cSrcweir for ( int index = 0 ; index < size; index++ ) { 111cdf0e10cSrcweir 112cdf0e10cSrcweir // Define property sequence. 113cdf0e10cSrcweir Property prop = new Property(); 114cdf0e10cSrcweir prop.Name = ( String )properties.get( index ); 115cdf0e10cSrcweir prop.Handle = -1; // n/a 116cdf0e10cSrcweir props[ index ] = prop; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir // Fill argument structure... 120cdf0e10cSrcweir OpenCommandArgument2 arg = new OpenCommandArgument2(); 121cdf0e10cSrcweir arg.Mode = OpenMode.ALL; // FOLDER, DOCUMENTS -> simple filter 122cdf0e10cSrcweir arg.Priority = 32768; // Final static for 32768 123cdf0e10cSrcweir arg.Properties = props; 124cdf0e10cSrcweir 125cdf0e10cSrcweir XDynamicResultSet set; 126cdf0e10cSrcweir 127cdf0e10cSrcweir // Execute command "open". 128cdf0e10cSrcweir set = ( XDynamicResultSet )UnoRuntime.queryInterface( 129cdf0e10cSrcweir XDynamicResultSet.class, m_helper.executeCommand( m_content, "open", arg )); 130cdf0e10cSrcweir XResultSet resultSet = ( XResultSet )set.getStaticResultSet(); 131cdf0e10cSrcweir 132cdf0e10cSrcweir result = new Vector(); 133cdf0e10cSrcweir 134cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 135cdf0e10cSrcweir // Iterate over children, access children and property values... 136cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 137cdf0e10cSrcweir 138cdf0e10cSrcweir // Move to begin. 139cdf0e10cSrcweir if ( resultSet.first() ) { 140cdf0e10cSrcweir XContentAccess contentAccess = ( XContentAccess )UnoRuntime.queryInterface( 141cdf0e10cSrcweir XContentAccess.class, resultSet ); 142cdf0e10cSrcweir XRow row = ( XRow )UnoRuntime.queryInterface( XRow.class, resultSet ); 143cdf0e10cSrcweir 144cdf0e10cSrcweir do { 145cdf0e10cSrcweir Vector propsValues = new Vector(); 146cdf0e10cSrcweir 147cdf0e10cSrcweir // Obtain URL of child. 148cdf0e10cSrcweir String id = contentAccess.queryContentIdentifierString(); 149cdf0e10cSrcweir propsValues.add( id ); 150cdf0e10cSrcweir for ( int i = 1; i <= size ; i++) { 151cdf0e10cSrcweir Object propValue = row.getObject( i, null ); 152cdf0e10cSrcweir if ( !row.wasNull() && !(propValue instanceof com.sun.star.uno.Any )) { 153cdf0e10cSrcweir propsValues.add( propValue ); 154cdf0e10cSrcweir } else { 155cdf0e10cSrcweir propsValues.add( "[ Property not found ]" ); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir } 158cdf0e10cSrcweir result.add( propsValues ); 159cdf0e10cSrcweir } while ( resultSet.next() ); // next child 160cdf0e10cSrcweir } 161cdf0e10cSrcweir } 162cdf0e10cSrcweir return result; 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir /** 166cdf0e10cSrcweir * Get connect URL. 167cdf0e10cSrcweir * 168cdf0e10cSrcweir *@return String That contains the connect URL 169cdf0e10cSrcweir */ getContentURL()170cdf0e10cSrcweir public String getContentURL() { 171cdf0e10cSrcweir return m_contenturl; 172cdf0e10cSrcweir } 173cdf0e10cSrcweir 174cdf0e10cSrcweir /** 175cdf0e10cSrcweir * Get the properties. 176cdf0e10cSrcweir * 177cdf0e10cSrcweir *@return String That contains the properties 178cdf0e10cSrcweir */ getProperties()179cdf0e10cSrcweir public Vector getProperties() { 180cdf0e10cSrcweir return m_propnames; 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir /** 184cdf0e10cSrcweir * Parse arguments 185cdf0e10cSrcweir * 186cdf0e10cSrcweir *@param String[] Arguments 187cdf0e10cSrcweir *@exception java.lang.Exception 188cdf0e10cSrcweir */ parseArguments( String[] args )189cdf0e10cSrcweir public void parseArguments( String[] args ) throws java.lang.Exception { 190cdf0e10cSrcweir 191cdf0e10cSrcweir for ( int i = 0; i < args.length; i++ ) { 192cdf0e10cSrcweir if ( args[i].startsWith( "-url=" )) { 193cdf0e10cSrcweir m_contenturl = args[i].substring( 5 ); 194cdf0e10cSrcweir } else if ( args[i].startsWith( "-propNames=" )) { 195cdf0e10cSrcweir StringTokenizer tok 196cdf0e10cSrcweir = new StringTokenizer( args[i].substring( 11 ), ";" ); 197cdf0e10cSrcweir 198cdf0e10cSrcweir while ( tok.hasMoreTokens() ) 199cdf0e10cSrcweir m_propnames.add( tok.nextToken() ); 200cdf0e10cSrcweir 201cdf0e10cSrcweir } else if ( args[i].startsWith( "-help" ) || 202cdf0e10cSrcweir args[i].startsWith( "-?" )) { 203cdf0e10cSrcweir printCmdLineUsage(); 204cdf0e10cSrcweir System.exit( 0 ); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir } 207cdf0e10cSrcweir 208cdf0e10cSrcweir if ( m_contenturl == null || m_contenturl.equals( "" )) { 209cdf0e10cSrcweir m_contenturl = "file:///"; 210cdf0e10cSrcweir } 211cdf0e10cSrcweir 212cdf0e10cSrcweir if ( m_propnames.size() == 0 ) { 213cdf0e10cSrcweir m_propnames.add( "Title" ); 214cdf0e10cSrcweir m_propnames.add( "IsDocument" ); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir } 217cdf0e10cSrcweir 218cdf0e10cSrcweir /** 219cdf0e10cSrcweir * Print the commands options 220cdf0e10cSrcweir */ printCmdLineUsage()221cdf0e10cSrcweir public void printCmdLineUsage() { 222cdf0e10cSrcweir System.out.println( 223cdf0e10cSrcweir "Usage : ChildrenRetriever -url=... -propNames=..." ); 224cdf0e10cSrcweir System.out.println( 225cdf0e10cSrcweir "Defaults: -url=file:/// -propNames=Title,IsDocument" ); 226cdf0e10cSrcweir System.out.println( 227cdf0e10cSrcweir "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" ); 228cdf0e10cSrcweir } 229cdf0e10cSrcweir 230cdf0e10cSrcweir /** 231cdf0e10cSrcweir * Print all properties out contained in vector . 232cdf0e10cSrcweir * 233cdf0e10cSrcweir *@param Vector 234cdf0e10cSrcweir */ printLine( Vector props )235cdf0e10cSrcweir public void printLine( Vector props ) { 236cdf0e10cSrcweir int limit; 237cdf0e10cSrcweir while ( !props.isEmpty() ) { 238cdf0e10cSrcweir String print = ""; 239cdf0e10cSrcweir int size = props.size(); 240cdf0e10cSrcweir for ( int i = 0; i < size; i++ ) { 241cdf0e10cSrcweir limit = 15; 242cdf0e10cSrcweir Object obj = props.get( i ); 243cdf0e10cSrcweir if ( obj != null) { 244cdf0e10cSrcweir String prop = obj.toString(); 245cdf0e10cSrcweir int leng = prop.length(); 246cdf0e10cSrcweir if ( leng < limit ) { 247cdf0e10cSrcweir for ( int l = leng; l < limit; l++) { 248cdf0e10cSrcweir prop += " "; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir print+= prop + " "; 251cdf0e10cSrcweir props.set( i, null ); 252cdf0e10cSrcweir } else { 253cdf0e10cSrcweir String temp1 = prop.substring( 0, limit ); 254cdf0e10cSrcweir String temp2 = prop.substring( limit ); 255cdf0e10cSrcweir print+= temp1 + " "; 256cdf0e10cSrcweir props.set( i, temp2 ); 257cdf0e10cSrcweir } 258cdf0e10cSrcweir } else { 259cdf0e10cSrcweir for ( int l = 0; l < limit; l++) { 260cdf0e10cSrcweir print += " "; 261cdf0e10cSrcweir } 262cdf0e10cSrcweir print+= " "; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir } 265cdf0e10cSrcweir System.out.println( print ); 266cdf0e10cSrcweir boolean isEmpty = true; 267cdf0e10cSrcweir for ( int i = 0; i < size; i++ ) { 268cdf0e10cSrcweir Object obj = props.get( i ); 269cdf0e10cSrcweir if( obj != null ) 270cdf0e10cSrcweir isEmpty = false; 271cdf0e10cSrcweir } 272cdf0e10cSrcweir if( isEmpty ) 273cdf0e10cSrcweir props.clear(); 274cdf0e10cSrcweir } 275cdf0e10cSrcweir } 276cdf0e10cSrcweir 277cdf0e10cSrcweir /** 278cdf0e10cSrcweir * Create a new connection with the specific args to a running office and 279cdf0e10cSrcweir * access the children from a folder. 280cdf0e10cSrcweir * 281cdf0e10cSrcweir *@param String[] Arguments 282cdf0e10cSrcweir */ main( String args[] )283cdf0e10cSrcweir public static void main ( String args[] ) { 284cdf0e10cSrcweir 285cdf0e10cSrcweir System.out.println( "\n" ); 286cdf0e10cSrcweir System.out.println( 287cdf0e10cSrcweir "-----------------------------------------------------------------" ); 288cdf0e10cSrcweir System.out.println( 289cdf0e10cSrcweir "ChildrenRetriever - obtains the children of a folder resource." ); 290cdf0e10cSrcweir System.out.println( 291cdf0e10cSrcweir "-----------------------------------------------------------------" ); 292cdf0e10cSrcweir 293cdf0e10cSrcweir try { 294cdf0e10cSrcweir ChildrenRetriever access = new ChildrenRetriever( args ); 295cdf0e10cSrcweir 296cdf0e10cSrcweir // Get the properties Title and IsFolder for the children. 297cdf0e10cSrcweir Vector result = access.getChildren(); 298cdf0e10cSrcweir 299cdf0e10cSrcweir String tempPrint = "\nChildren of resource " + access.getContentURL(); 300cdf0e10cSrcweir int size = tempPrint.length(); 301cdf0e10cSrcweir System.out.println( tempPrint ); 302cdf0e10cSrcweir tempPrint = ""; 303cdf0e10cSrcweir for( int i = 0; i < size; i++ ) { 304cdf0e10cSrcweir tempPrint += "-"; 305cdf0e10cSrcweir } 306cdf0e10cSrcweir System.out.println( tempPrint ); 307cdf0e10cSrcweir 308cdf0e10cSrcweir if ( result != null && !result.isEmpty() ) { 309cdf0e10cSrcweir 310cdf0e10cSrcweir Vector cont = new Vector(); 311cdf0e10cSrcweir cont.add("URL:"); 312cdf0e10cSrcweir Vector props = access.getProperties(); 313cdf0e10cSrcweir size = props.size(); 314cdf0e10cSrcweir for ( int i = 0; i < size; i++ ) { 315cdf0e10cSrcweir Object obj = props.get( i ); 316cdf0e10cSrcweir String prop = obj.toString(); 317cdf0e10cSrcweir cont.add( prop + ":" ); 318cdf0e10cSrcweir } 319cdf0e10cSrcweir access.printLine(cont); 320cdf0e10cSrcweir System.out.println( "\n" ); 321cdf0e10cSrcweir for ( Enumeration e = result.elements(); e.hasMoreElements(); ) { 322cdf0e10cSrcweir Vector propsV = ( Vector )e.nextElement(); 323cdf0e10cSrcweir access.printLine( propsV ); 324cdf0e10cSrcweir } 325cdf0e10cSrcweir } 326cdf0e10cSrcweir } catch ( com.sun.star.ucb.ResultSetException e ) { 327cdf0e10cSrcweir System.out.println( "Error: " + e ); 328cdf0e10cSrcweir } catch ( com.sun.star.ucb.CommandAbortedException e ) { 329cdf0e10cSrcweir System.out.println( "Error: " + e ); 330cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e ) { 331cdf0e10cSrcweir System.out.println( "Error: " + e ); 332cdf0e10cSrcweir } catch ( java.lang.Exception e ) { 333cdf0e10cSrcweir System.out.println( "Error: " + e ); 334cdf0e10cSrcweir } 335cdf0e10cSrcweir System.exit( 0 ); 336cdf0e10cSrcweir } 337cdf0e10cSrcweir } 338