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