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
.XRow
;
41 import com
.sun
.star
.ucb
.XContent
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
46 * Obtaining Property Values from a UCB Content
48 public class PropertiesRetriever
{
53 private Helper m_helper
;
54 private XContent m_content
;
55 private String m_contenturl
= "";
56 private ArrayList
<String
> m_propNames
= new ArrayList
<String
>();
61 *@param args This constructor requires the arguments:
63 * -propNames=... (optional)
64 * See Help (method printCmdLineUsage()).
65 * Without the arguments a new connection to a
66 * running office cannot created.
68 public PropertiesRetriever( String args
[] ) throws java
.lang
.Exception
{
71 parseArguments( args
);
74 m_helper
= new Helper( getContentURL() );
77 m_content
= m_helper
.createUCBContent();
81 * Get values of the properties.
82 * This method requires the main and the optional arguments to be set in order to work.
85 *@return Properties values if values successfully retrieved, null otherwise
87 public ArrayList
<Object
> getPropertyValues()
88 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
89 ArrayList
<String
> properties
= getProperties();
90 return getPropertyValues ( properties
);
94 * Get values of the properties.
96 *@return Properties values if values successfully retrieved, null otherwise
98 public ArrayList
<Object
> getPropertyValues( ArrayList
<String
> properties
)
99 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
100 ArrayList
<Object
> m_propValues
= null;
101 if ( m_content
!= null && properties
!= null && !properties
.isEmpty() ) {
103 int size
= properties
.size();
105 // Fill info for the properties wanted.
106 Property
[] props
= new Property
[ size
];
107 for ( int index
= 0 ; index
< size
; index
++ ) {
109 // Define property sequence.
110 Property prop
= new Property();
111 prop
.Name
= properties
.get( index
);
112 prop
.Handle
= -1; // n/a
113 props
[ index
] = prop
;
116 // Execute command "getPropertyValues".
118 UnoRuntime
.queryInterface(
119 XRow
.class, m_helper
.executeCommand( m_content
,"getPropertyValues", props
));
121 m_propValues
= new ArrayList
<Object
>();
124 Extract values from row object. Note that the
125 first column is 1, not 0.
126 Title: Obtain value of column 1 as string.*/
127 for ( int index
= 1 ; index
<= size
; index
++ ) {
128 Object propertyValue
= values
.getObject( index
, null );
129 if ( !values
.wasNull() && !(propertyValue
instanceof com
.sun
.star
.uno
.Any
))
130 m_propValues
.add( propertyValue
);
132 m_propValues
.add( "[ Property not found ]" );
141 *@return String That contains the connect URL
143 public String
getContentURL() {
148 * Get the properties.
150 *@return Vector That contains the properties
152 public ArrayList
<String
> getProperties() {
159 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
161 for ( int i
= 0; i
< args
.length
; i
++ ) {
162 if ( args
[i
].startsWith( "-url=" )) {
163 m_contenturl
= args
[i
].substring( 5 );
164 } else if ( args
[i
].startsWith( "-propNames=" )) {
166 = new StringTokenizer( args
[i
].substring( 11 ), ";" );
168 while ( tok
.hasMoreTokens() )
169 m_propNames
.add( tok
.nextToken() );
171 } else if ( args
[i
].startsWith( "-help" ) ||
172 args
[i
].startsWith( "-?" )) {
178 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
179 m_contenturl
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
182 if ( m_propNames
.size() == 0 ) {
183 m_propNames
.add( "Title" );
184 m_propNames
.add( "IsDocument" );
189 * Print the commands options
191 public void printCmdLineUsage() {
193 "Usage : PropertiesRetriever -url=... -propNames=..." );
195 "Defaults: -url=<currentdir>/data/data.txt -propNames=Title;IsDocument" );
197 "\nExample : -propNames=Title;IsFolder" );
201 * Create a new connection with the specific args to a running office and
202 * get the properties values from a resource.
204 public static void main ( String args
[] ) {
205 System
.out
.println( "\n" );
207 "--------------------------------------------------------------" );
209 "PropertiesRetriever - obtains property values from a resource." );
211 "--------------------------------------------------------------" );
213 PropertiesRetriever obtProperty
= new PropertiesRetriever( args
);
214 ArrayList
<String
> properties
= obtProperty
.getProperties();
215 ArrayList
<Object
> propertiesValues
= obtProperty
.getPropertyValues( properties
);
217 String tempPrint
= "\nProperties of resource " + obtProperty
.getContentURL();
218 int size
= tempPrint
.length();
219 System
.out
.println( tempPrint
);
221 for( int i
= 0; i
< size
; i
++ ) {
224 System
.out
.println( tempPrint
);
226 if ( properties
!= null && propertiesValues
!= null ) {
227 size
= properties
.size();
228 for (int index
= 0; index
< size
; index
++ ) {
229 String property
= properties
.get( index
);
230 Object propValue
= propertiesValues
.get( index
);
231 System
.out
.println( property
+ " : " + propValue
);
234 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
235 System
.out
.println( "Error: " + e
);
236 } catch ( com
.sun
.star
.uno
.Exception e
) {
237 System
.out
.println( "Error: " + e
);
238 } catch ( java
.lang
.Exception e
) {
239 System
.out
.println( "Error: " + e
);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */