Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / PropertiesRetriever.java
blob0b984907ab7745b201dd316d1d6097f2fb07d4b9
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.XRow;
40 import com.sun.star.ucb.XContent;
41 import com.sun.star.uno.UnoRuntime;
44 /**
45 * Obtaining Property Values from a UCB Content
47 public class PropertiesRetriever {
49 /**
50 * Member properties
52 private Helper m_helper;
53 private XContent m_content;
54 private String m_contenturl = "";
55 private ArrayList<String> m_propNames = new ArrayList<String>();
57 /**
58 * Constructor.
60 *@param args This construtor requires the arguments:
61 * -url=... (optional)
62 * -propNames=... (optional)
63 * See Help (method printCmdLineUsage()).
64 * Without the arguments a new connection to a
65 * running office cannot created.
67 public PropertiesRetriever( String args[] ) throws java.lang.Exception {
69 // Parse arguments
70 parseArguments( args );
72 // Init
73 m_helper = new Helper( getContentURL() );
75 // Create UCB content
76 m_content = m_helper.createUCBContent();
79 /**
80 * Get values of the properties.
81 * This method requires the main and the optional arguments to be set in order to work.
82 * See Constructor.
84 *@return Properties values if values successfully retrieved, null otherwise
86 public ArrayList<Object> getPropertyValues()
87 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
88 ArrayList<String> properties = getProperties();
89 return getPropertyValues ( properties );
92 /**
93 * Get values of the properties.
95 *@return Properties values if values successfully retrieved, null otherwise
97 public ArrayList<Object> getPropertyValues( ArrayList<String> properties )
98 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
99 ArrayList<Object> m_propValues = null;
100 if ( m_content != null && properties != null && !properties.isEmpty() ) {
102 int size = properties.size();
104 // Fill info for the properties wanted.
105 Property[] props = new Property[ size ];
106 for ( int index = 0 ; index < size; index++ ) {
108 // Define property sequence.
109 Property prop = new Property();
110 prop.Name = properties.get( index );
111 prop.Handle = -1; // n/a
112 props[ index ] = prop;
115 // Execute command "getPropertyValues".
116 XRow values =
117 UnoRuntime.queryInterface(
118 XRow.class, m_helper.executeCommand( m_content,"getPropertyValues", props ));
120 m_propValues = new ArrayList<Object>();
123 Extract values from row object. Note that the
124 first column is 1, not 0.
125 Title: Obtain value of column 1 as string.*/
126 for ( int index = 1 ; index <= size; index++ ) {
127 Object propertyValue = values.getObject( index, null );
128 if ( !values.wasNull() && !(propertyValue instanceof com.sun.star.uno.Any ))
129 m_propValues.add( propertyValue );
130 else
131 m_propValues.add( "[ Property not found ]" );
134 return m_propValues;
138 * Get connect URL.
140 *@return String That contains the connect URL
142 public String getContentURL() {
143 return m_contenturl;
147 * Get the properties.
149 *@return Vector That contains the properties
151 public ArrayList<String> getProperties() {
152 return m_propNames;
156 * Parse arguments
158 public void parseArguments( String[] args ) throws java.lang.Exception {
160 for ( int i = 0; i < args.length; i++ ) {
161 if ( args[i].startsWith( "-url=" )) {
162 m_contenturl = args[i].substring( 5 );
163 } else if ( args[i].startsWith( "-propNames=" )) {
164 StringTokenizer tok
165 = new StringTokenizer( args[i].substring( 11 ), ";" );
167 while ( tok.hasMoreTokens() )
168 m_propNames.add( tok.nextToken() );
170 } else if ( args[i].startsWith( "-help" ) ||
171 args[i].startsWith( "-?" )) {
172 printCmdLineUsage();
173 System.exit( 0 );
177 if ( m_contenturl == null || m_contenturl.equals( "" )) {
178 m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
181 if ( m_propNames.size() == 0 ) {
182 m_propNames.add( "Title" );
183 m_propNames.add( "IsDocument" );
188 * Print the commands options
190 public void printCmdLineUsage() {
191 System.out.println(
192 "Usage : PropertiesRetriever -url=... -propNames=..." );
193 System.out.println(
194 "Defaults: -url=<currentdir>/data/data.txt -propNames=Title;IsDocument" );
195 System.out.println(
196 "\nExample : -propNames=Title;IsFolder" );
200 * Create a new connection with the specific args to a running office and
201 * get the properties values from a resource.
203 public static void main ( String args[] ) {
204 System.out.println( "\n" );
205 System.out.println(
206 "--------------------------------------------------------------" );
207 System.out.println(
208 "PropertiesRetriever - obtains property values from a resource." );
209 System.out.println(
210 "--------------------------------------------------------------" );
211 try {
212 PropertiesRetriever obtProperty = new PropertiesRetriever( args );
213 ArrayList<String> properties = obtProperty.getProperties();
214 ArrayList<Object> propertiesValues = obtProperty.getPropertyValues( properties );
216 String tempPrint = "\nProperties of resource " + obtProperty.getContentURL();
217 int size = tempPrint.length();
218 System.out.println( tempPrint );
219 tempPrint = "";
220 for( int i = 0; i < size; i++ ) {
221 tempPrint += "-";
223 System.out.println( tempPrint );
225 if ( properties != null && propertiesValues != null ) {
226 size = properties.size();
227 for (int index = 0; index < size ; index++ ) {
228 String property = properties.get( index );
229 Object propValue = propertiesValues.get( index );
230 System.out.println( property + " : " + propValue );
233 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
234 System.out.println( "Error: " + e );
235 } catch ( com.sun.star.uno.Exception e ) {
236 System.out.println( "Error: " + e );
237 } catch ( java.lang.Exception e ) {
238 System.out.println( "Error: " + e );
240 System.exit( 0 );