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