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 java.util.Vector;
25 import java.util.StringTokenizer;
26 
27 import com.sun.star.beans.Property;
28 import com.sun.star.ucb.XContent;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.sdbc.XRow;
31 
32 
33 /**
34  * Obtaining Property Values from a UCB Content
35  */
36 public class PropertiesRetriever {
37 
38     /**
39      * Member properties
40      */
41     private  Helper   m_helper;
42     private  XContent m_content;
43     private  String   m_contenturl    = "";
44     private  Vector   m_propNames     = new Vector();
45 
46     /**
47      * Constructor.
48      *
49      *@param      String[]   This construtor requires the arguments:
50      *                          -url=...       (optional)
51      *                          -propNames=... (optional)
52      *                       See Help (method printCmdLineUsage()).
53      *                       Without the arguments a new connection to a
54      *                       running office cannot created.
55      *@exception  java.lang.Exception
56      */
PropertiesRetriever( String args[] )57     public PropertiesRetriever( String args[] ) throws java.lang.Exception {
58 
59         // Parse arguments
60         parseArguments( args );
61 
62         // Init
63         m_helper       = new Helper( getContentURL() );
64 
65         // Create UCB content
66         m_content      = m_helper.createUCBContent();
67     }
68 
69     /**
70      * Get values of the properties.
71      * This method requires the main and the optional arguments to be set in order to work.
72      * See Constructor.
73      *
74      *@param  Vector   Properties
75      *@return Vector   Returns Properties values if values successfully retrieved, null otherwise
76      *@exception  com.sun.star.ucb.CommandAbortedException
77      *@exception  com.sun.star.uno.Exception
78      */
getPropertyValues()79     public Vector getPropertyValues()
80         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
81         Vector properties = getProperties();
82         return getPropertyValues ( properties );
83     }
84 
85     /**
86      *  Get values of the properties.
87      *
88      *@param  Vector   Properties
89      *@return Vector   Returns Properties values if values successfully retrieved, null otherwise
90      *@exception  com.sun.star.ucb.CommandAbortedException
91      *@exception  com.sun.star.uno.Exception
92      */
getPropertyValues( Vector properties )93     public Vector getPropertyValues( Vector properties )
94         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
95         Vector m_propValues = null;
96         if ( m_content != null && properties != null && !properties.isEmpty() ) {
97 
98             int size = properties.size();
99 
100             // Fill info for the properties wanted.
101             Property[] props = new Property[ size ];
102             for ( int index = 0 ; index < size; index++ ) {
103 
104                 // Define property sequence.
105                 Property prop = new Property();
106                 prop.Name = ( String )properties.get( index );
107                 prop.Handle = -1; // n/a
108                 props[ index ] = prop;
109             }
110 
111             // Execute command "getPropertyValues".
112             XRow values =
113                 ( XRow )UnoRuntime.queryInterface(
114                     XRow.class, m_helper.executeCommand( m_content,"getPropertyValues", props ));
115 
116             m_propValues = new Vector();
117 
118             /*
119               Extract values from row object. Note that the
120               first column is 1, not 0.
121               Title: Obtain value of column 1 as string.*/
122             for ( int index = 1 ; index <= size; index++ ) {
123                 Object propertyValue = values.getObject( index, null );
124                 if ( !values.wasNull() && !(propertyValue instanceof com.sun.star.uno.Any ))
125                     m_propValues.add( propertyValue );
126                 else
127                     m_propValues.add( "[ Property not found ]" );
128             }
129         }
130         return m_propValues;
131     }
132 
133     /**
134      *  Get connect URL.
135      *
136      *@return   String  That contains the connect URL
137      */
getContentURL()138     public String getContentURL() {
139         return m_contenturl;
140     }
141 
142     /**
143      * Get the properties.
144      *
145      *@return Vector  That contains the properties
146      */
getProperties()147     public Vector getProperties() {
148         return m_propNames;
149     }
150 
151     /**
152      * Parse arguments
153      *
154      *@param      String[]   Arguments
155      *@exception  java.lang.Exception
156      */
parseArguments( String[] args )157     public void parseArguments( String[] args ) throws java.lang.Exception {
158 
159         for ( int i = 0; i < args.length; i++ ) {
160             if ( args[i].startsWith( "-url=" )) {
161                 m_contenturl    = args[i].substring( 5 );
162             } else if ( args[i].startsWith( "-propNames=" )) {
163                 StringTokenizer tok
164                     = new StringTokenizer( args[i].substring( 11 ), ";" );
165 
166                 while ( tok.hasMoreTokens() )
167                     m_propNames.add( tok.nextToken() );
168 
169             } else if ( args[i].startsWith( "-help" ) ||
170                         args[i].startsWith( "-?" )) {
171                 printCmdLineUsage();
172                 System.exit( 0 );
173             }
174         }
175 
176 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
177             m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
178         }
179 
180         if ( m_propNames.size() == 0 ) {
181             m_propNames.add( "Title" );
182             m_propNames.add( "IsDocument" );
183         }
184     }
185 
186     /**
187      * Print the commands options
188      */
printCmdLineUsage()189     public void printCmdLineUsage() {
190         System.out.println(
191             "Usage   : PropertiesRetriever -url=... -propNames=..." );
192         System.out.println(
193             "Defaults: -url=<currentdir>/data/data.txt -propNames=Title;IsDocument" );
194         System.out.println(
195             "\nExample : -propNames=Title;IsFolder" );
196     }
197 
198     /**
199      *  Create a new connection with the specific args to a running office and
200      *  get the properties values from a resource.
201      *
202      *@param  String[]   Arguments
203      */
main( String args[] )204     public static void main ( String args[] ) {
205         System.out.println( "\n" );
206 		System.out.println(
207             "--------------------------------------------------------------" );
208 		System.out.println(
209             "PropertiesRetriever - obtains property values from a resource." );
210 		System.out.println(
211             "--------------------------------------------------------------" );
212         try {
213             PropertiesRetriever obtProperty = new PropertiesRetriever( args );
214             Vector properties  = obtProperty.getProperties();
215             Vector propertiesValues = obtProperty.getPropertyValues( properties );
216 
217             String tempPrint = "\nProperties of resource " + obtProperty.getContentURL();
218             int size = tempPrint.length();
219             System.out.println( tempPrint );
220             tempPrint = "";
221             for( int i = 0; i < size; i++ ) {
222                 tempPrint += "-";
223             }
224             System.out.println( tempPrint );
225 
226             if ( properties != null && propertiesValues != null )  {
227                 size = properties.size();
228                 for (int index = 0; index < size ; index++ ) {
229                     String property  = ( String )properties.get( index );
230                     Object propValue = propertiesValues.get( index );
231                     System.out.println( property + " : " + propValue );
232                 }
233             }
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 );
240         }
241         System.exit( 0 );
242     }
243 }
244