1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
;
49 * Retrieve the Children of a UCB Folder Content
51 public class ChildrenRetriever
{
56 private Helper m_helper
;
57 private XContent m_content
;
58 private String m_contenturl
= "";
59 private ArrayList
<String
> m_propnames
= new ArrayList
<String
>();
62 * Constructor. Create a new connection with the specific args to a running office
64 *@param args This construtor requires the arguments:
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
{
74 parseArguments( args
);
77 m_helper
= new Helper( getContentURL() );
80 m_content
= m_helper
.createUCBContent();
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.
88 *@return Returns children properties values if values successfully retrieved,
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
);
98 * Open a folder content, get properties values for the properties.
100 *@return Returns children properties values if values successfully retrieved,
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 ) {
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...
143 if ( resultSet
.first() ) {
144 XContentAccess contentAccess
= UnoRuntime
.queryInterface(
145 XContentAccess
.class, resultSet
);
146 XRow row
= UnoRuntime
.queryInterface( XRow
.class, resultSet
);
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
);
159 propsValues
.add( "[ Property not found ]" );
162 result
.add( propsValues
);
163 } while ( resultSet
.next() ); // next child
172 *@return String That contains the connect URL
174 public String
getContentURL() {
179 * Get the properties.
181 *@return String That contains the properties
183 public ArrayList
<String
> getProperties() {
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=" )) {
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( "-?" )) {
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() {
226 "Usage : ChildrenRetriever -url=... -propNames=..." );
228 "Defaults: -url=file:/// -propNames=Title,IsDocument" );
230 "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" );
234 * Print all properties out contained in vector .
236 public void printLine( ArrayList
<Object
> props
) {
238 while ( !props
.isEmpty() ) {
240 int size
= props
.size();
241 for ( int i
= 0; i
< size
; i
++ ) {
243 Object obj
= props
.get( i
);
245 String prop
= obj
.toString();
246 int leng
= prop
.length();
247 if ( leng
< limit
) {
248 for ( int l
= leng
; l
< limit
; l
++) {
252 props
.set( i
, null );
254 String temp1
= prop
.substring( 0, limit
);
255 String temp2
= prop
.substring( limit
);
257 props
.set( i
, temp2
);
260 for ( int l
= 0; l
< limit
; l
++) {
266 System
.out
.println( print
);
267 boolean isEmpty
= true;
268 for ( int i
= 0; i
< size
; i
++ ) {
269 Object obj
= props
.get( i
);
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" );
286 "-----------------------------------------------------------------" );
288 "ChildrenRetriever - obtains the children of a folder resource." );
290 "-----------------------------------------------------------------" );
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
);
302 for( int i
= 0; i
< size
; i
++ ) {
305 System
.out
.println( tempPrint
);
307 if ( result
!= null && !result
.isEmpty() ) {
309 ArrayList
<Object
> cont
= new ArrayList
<Object
>();
311 ArrayList
<String
> props
= access
.getProperties();
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
);