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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
;
50 * Retrieve the Children of a UCB Folder Content
52 public class ChildrenRetriever
{
57 private Helper m_helper
;
58 private XContent m_content
;
59 private String m_contenturl
= "";
60 private ArrayList
<String
> m_propnames
= new ArrayList
<String
>();
63 * Constructor. Create a new connection with the specific args to a running office
65 *@param args This constructor requires the arguments:
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
{
75 parseArguments( args
);
78 m_helper
= new Helper( getContentURL() );
81 m_content
= m_helper
.createUCBContent();
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.
89 *@return Returns children properties values if values successfully retrieved,
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
);
99 * Open a folder content, get properties values for the properties.
101 *@return Returns children properties values if values successfully retrieved,
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 ) {
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...
144 if ( resultSet
.first() ) {
145 XContentAccess contentAccess
= UnoRuntime
.queryInterface(
146 XContentAccess
.class, resultSet
);
147 XRow row
= UnoRuntime
.queryInterface( XRow
.class, resultSet
);
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
);
160 propsValues
.add( "[ Property not found ]" );
163 result
.add( propsValues
);
164 } while ( resultSet
.next() ); // next child
173 *@return String That contains the connect URL
175 public String
getContentURL() {
180 * Get the properties.
182 *@return String That contains the properties
184 public ArrayList
<String
> getProperties() {
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=" )) {
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( "-?" )) {
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() {
227 "Usage : ChildrenRetriever -url=... -propNames=..." );
229 "Defaults: -url=file:/// -propNames=Title,IsDocument" );
231 "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" );
235 * Print all properties out contained in vector .
237 public void printLine( ArrayList
<Object
> props
) {
239 while ( !props
.isEmpty() ) {
241 int size
= props
.size();
242 for ( int i
= 0; i
< size
; i
++ ) {
244 Object obj
= props
.get( i
);
246 String prop
= obj
.toString();
247 int leng
= prop
.length();
248 if ( leng
< limit
) {
249 for ( int l
= leng
; l
< limit
; l
++) {
253 props
.set( i
, null );
255 String temp1
= prop
.substring( 0, limit
);
256 String temp2
= prop
.substring( limit
);
258 props
.set( i
, temp2
);
261 for ( int l
= 0; l
< limit
; l
++) {
267 System
.out
.println( print
);
268 boolean isEmpty
= true;
269 for ( int i
= 0; i
< size
; i
++ ) {
270 Object obj
= props
.get( i
);
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" );
287 "-----------------------------------------------------------------" );
289 "ChildrenRetriever - obtains the children of a folder resource." );
291 "-----------------------------------------------------------------" );
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
);
303 for( int i
= 0; i
< size
; i
++ ) {
306 System
.out
.println( tempPrint
);
308 if ( result
!= null && !result
.isEmpty() ) {
310 ArrayList
<Object
> cont
= new ArrayList
<Object
>();
312 ArrayList
<String
> props
= access
.getProperties();
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
);
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */