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 package com.sun.star.wizards.common;
24 
25 // import com.sun.star.beans.XPropertySet;
26 import com.sun.star.uno.AnyConverter;
27 import com.sun.star.uno.TypeClass;
28 
29 /**
30  * A class for helping with all kinds of numerical conversions.
31  * Optional or named parameters in SO are of the Object type in Java.
32  * These objects must be converted to the right simple value type.
33  * Unfortunately, StarBasic does not know the original desired type,
34  * and a value that should be a "Float" is delivered as "Byte". This class
35  * handles the conversions of these types.<br>
36  * This class does not log warnings (or throws Exceptions) when the precision
37  * of a value is lost.
38  */
39 public class NumericalHelper
40 {
41 
42     public static final int UNKNOWN_TYPE = -32768;
43     public static final int BYTE_TYPE = 0;
44     public static final int SHORT_TYPE = 1;
45     public static final int INT_TYPE = 2;
46     public static final int LONG_TYPE = 3;
47     public static final int FLOAT_TYPE = 4;
48     public static final int DOUBLE_TYPE = 5;
49     public static final int CHAR_TYPE = 6;
50     public static final int STRING_TYPE = -1;
51     public static final int BOOLEAN_TYPE = -2;
52     public static final int ARRAY_TYPE = -3;
53     public static final int SEQUENCE_TYPE = -4;
54     public static final int ASCII_VALUE_0 = 48;
55     public static final int ASCII_VALUE_A = 65;
56     public static final int COUNT_CHARS_IN_ALPHABET = 26;
57     private static final int HEX_BASE = 16;
58     private static final int DEC_BASE = 10;
59     private static final int ASCII_LETTER_A_OFFSET = 55;
60 
61     /**
62      * private c'tor to prevent instantiation
63      */
NumericalHelper()64     private NumericalHelper()
65     {
66         // private c'tor, so no one can instantiate
67     }
68 
69     /**
70      * get the type of an object: returns all types that can possibly converted
71      * with this class.
72      * @param obj an object that is checked for conversion
73      * @return the type of the object
74      */
getType(Object obj)75     public static int getType(Object obj)
76     {
77         try
78         {
79             TypeObject aTypeObject = getTypeObject(obj);
80             return aTypeObject.iType;
81         }
82         catch (com.sun.star.lang.IllegalArgumentException e)
83         {
84             // ignore this one; just return unknown type
85         }
86         return UNKNOWN_TYPE;
87     }
88 
89     /**
90      * get a byte value from the object
91      * @param aValue
92      * @return a byte
93      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
94      */
toByte(Object aValue)95     public static byte toByte(Object aValue)
96             throws com.sun.star.lang.IllegalArgumentException
97     {
98 
99         byte retValue = 0;
100         // boolean hasConversionWarning = false;
101         TypeObject aTypeObject = getTypeObject(aValue);
102         switch (aTypeObject.iType)
103         {
104             case BYTE_TYPE:
105                 retValue = getByte(aTypeObject);
106                 break;
107             case CHAR_TYPE:
108                 retValue = (byte) getChar(aTypeObject);
109                 break;
110             case SHORT_TYPE:
111                 retValue = (byte) getShort(aTypeObject);
112                 break;
113             case INT_TYPE:
114                 retValue = (byte) getInt(aTypeObject);
115                 break;
116             case LONG_TYPE:
117                 retValue = (byte) getLong(aTypeObject);
118                 break;
119             case FLOAT_TYPE:
120                 retValue = (byte) getFloat(aTypeObject);
121                 break;
122             case DOUBLE_TYPE:
123                 retValue = (byte) getDouble(aTypeObject);
124                 break;
125             case STRING_TYPE:
126                 try
127                 {
128                     Byte b = new Byte((String) aTypeObject.aValue);
129                     retValue = b.byteValue();
130                 }
131                 catch (java.lang.NumberFormatException e)
132                 {
133                     throw new com.sun.star.lang.IllegalArgumentException(
134                             "Cannot convert to byte: " + aTypeObject.aValue);
135                 }
136                 break;
137             case BOOLEAN_TYPE:
138                 retValue = getBool(aTypeObject) ? (byte) -1 : (byte) 0;
139                 break;
140             default:
141                 throw new com.sun.star.lang.IllegalArgumentException(
142                         "Cannot convert this type: " + aValue.getClass().getName());
143         }
144         return retValue;
145     }
146 
147     /**
148      * get a char value from the object
149      * @param aValue
150      * @return a char
151      * @throws com.sun.star.lang.IllegalArgumentException  if the object cannot be converted
152      */
toChar(Object aValue)153     public static char toChar(Object aValue)
154             throws com.sun.star.lang.IllegalArgumentException
155     {
156 
157         char retValue = 0;
158         boolean hasConversionWarning = false;
159         TypeObject aTypeObject = getTypeObject(aValue);
160         switch (aTypeObject.iType)
161         {
162             case CHAR_TYPE:
163                 retValue = getChar(aTypeObject);
164                 break;
165             case BYTE_TYPE:
166                 retValue = (char) getByte(aTypeObject);
167                 break;
168             case SHORT_TYPE:
169                 retValue = (char) getShort(aTypeObject);
170                 break;
171             case INT_TYPE:
172                 retValue = (char) getInt(aTypeObject);
173                 break;
174             case LONG_TYPE:
175                 retValue = (char) getLong(aTypeObject);
176                 break;
177             case FLOAT_TYPE:
178                 retValue = (char) getFloat(aTypeObject);
179                 break;
180             case DOUBLE_TYPE:
181                 retValue = (char) getDouble(aTypeObject);
182                 break;
183             case STRING_TYPE:
184                 try
185                 {
186                     String s = (String) aTypeObject.aValue;
187                     if (s.length() > 0)
188                     {
189                         retValue = s.charAt(0);
190                     }
191                     else
192                     {
193                         retValue = (char) 0;
194                     }
195                 }
196                 catch (java.lang.NumberFormatException e)
197                 {
198                     throw new com.sun.star.lang.IllegalArgumentException(
199                             "Cannot convert to char: " + aTypeObject.aValue);
200                 }
201                 break;
202             case BOOLEAN_TYPE:
203                 retValue = getBool(aTypeObject) ? (char) -1 : (char) 0;
204                 break;
205             default:
206                 throw new com.sun.star.lang.IllegalArgumentException(
207                         "Cannot convert this type: " + aValue.getClass().getName());
208         }
209         return retValue;
210     }
211 
212     /**
213      * get a short value from the object
214      * @param aValue
215      * @return a short
216      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
217      */
toShort(Object aValue)218     public static short toShort(Object aValue)
219             throws com.sun.star.lang.IllegalArgumentException
220     {
221         short retValue = 0;
222         TypeObject aTypeObject = getTypeObject(aValue);
223         switch (aTypeObject.iType)
224         {
225             case BYTE_TYPE:
226                 retValue = (short) getByte(aTypeObject);
227                 break;
228             case CHAR_TYPE:
229                 retValue = (byte) getChar(aTypeObject);
230                 break;
231             case SHORT_TYPE:
232                 retValue = getShort(aTypeObject);
233                 break;
234             case INT_TYPE:
235                 retValue = (short) getInt(aTypeObject);
236                 break;
237             case LONG_TYPE:
238                 retValue = (short) getLong(aTypeObject);
239                 break;
240             case FLOAT_TYPE:
241                 retValue = (short) getFloat(aTypeObject);
242                 break;
243             case DOUBLE_TYPE:
244                 retValue = (short) getDouble(aTypeObject);
245                 break;
246             case STRING_TYPE:
247                 try
248                 {
249                     Short s = new Short((String) aTypeObject.aValue);
250                     retValue = s.shortValue();
251                 }
252                 catch (java.lang.NumberFormatException e)
253                 {
254                     throw new com.sun.star.lang.IllegalArgumentException(
255                             "Cannot convert to short: " + aTypeObject.aValue);
256                 }
257                 break;
258             case BOOLEAN_TYPE:
259                 retValue = getBool(aTypeObject) ? (short) -1 : (short) 0;
260                 break;
261             default:
262                 throw new com.sun.star.lang.IllegalArgumentException(
263                         "Cannot convert this type: " + aValue.getClass().getName());
264         }
265         return retValue;
266     }
267 
isValidAndNumerical(Object aValue)268     public static boolean isValidAndNumerical(Object aValue) throws com.sun.star.lang.IllegalArgumentException
269     {
270         if (aValue != null)
271         {
272             if (!AnyConverter.isVoid(aValue))
273             {
274                 return (NumericalHelper.isNumerical(aValue));
275             }
276         }
277         return false;
278     }
279 
isValidAndBoolean(Object aValue)280     public static boolean isValidAndBoolean(Object aValue) throws com.sun.star.lang.IllegalArgumentException
281     {
282         if (aValue != null)
283         {
284             if (!AnyConverter.isVoid(aValue))
285             {
286                 int nType = AnyConverter.getType(aValue).getTypeClass().getValue();
287                 return (nType == TypeClass.BOOLEAN_value);
288             }
289         }
290         return false;
291     }
292 
isValid(Object aValue)293     public static boolean isValid(Object aValue)
294     {
295         if (aValue != null)
296         {
297             if (!AnyConverter.isVoid(aValue))
298             {
299                 return true;
300             }
301         }
302         return false;
303     }
304 
305     /**
306     @param aValue a object this can contain anything
307     @return true, if the parameter aValue is type of real numbers
308     @deprecate, use isRealNumber() instead.
309      */
isNumerical(Object aValue)310     public static boolean isNumerical(Object aValue)
311     {
312         try
313         {
314             TypeObject aTypeObject = getTypeObject(aValue);
315             switch (aTypeObject.iType)
316             {
317                 case BYTE_TYPE:
318                 case CHAR_TYPE:
319                 case SHORT_TYPE:
320                 case INT_TYPE:
321                 case LONG_TYPE:
322                 case DOUBLE_TYPE:
323                 case FLOAT_TYPE:
324                     return true;
325                 default:
326                     return false;
327             }
328         }
329         catch (com.sun.star.lang.IllegalArgumentException e)
330         {
331             return false;
332         }
333     }
334 
335     /**
336     @param _aValue a object this can contain anything
337     @return true, if the parameter aValue is type of real numbers
338 
339     see also http://en.wikipedia.org/wiki/Mathematics
340      */
isRealNumber(Object _aValue)341     public static boolean isRealNumber(Object _aValue)
342     {
343         return isNumerical(_aValue);
344     }
345 
346     /**
347     @param aValue a object this can contain anything
348      * @return true, if the value is type of any integer values. double / float are not(!) integer values
349      * @throws com.sun.star.lang.IllegalArgumentException
350      */
isInteger(Object aValue)351     public static boolean isInteger(Object aValue) throws com.sun.star.lang.IllegalArgumentException
352     {
353         TypeObject aTypeObject = getTypeObject(aValue);
354         switch (aTypeObject.iType)
355         {
356             case BYTE_TYPE:
357             case CHAR_TYPE:
358             case SHORT_TYPE:
359             case INT_TYPE:
360             case LONG_TYPE:
361                 return true;
362             default:
363                 return false;
364         }
365     }
366 
367     /**
368      * Can a given object be converted to a String array?
369      * @param aValue the object to test
370      * @return true, if the object can be converted to a String array.
371      */
isStringArray(Object aValue)372     public static boolean isStringArray(Object aValue)
373     {
374         try
375         {
376             toStringArray(aValue);
377             return true;
378         }
379         catch (com.sun.star.lang.IllegalArgumentException e)
380         {
381             // ignore
382         }
383         return false;
384     }
385 
386     /**
387      * Can a given object be converted to an int array?
388      * @param aValue the object to test
389      * @return true, if the object can be converted to an Integer array.
390      */
isIntegerArray(Object aValue)391     public static boolean isIntegerArray(Object aValue)
392     {
393         try
394         {
395             toIntArray(aValue);
396             return true;
397         }
398         catch (com.sun.star.lang.IllegalArgumentException e)
399         {
400             // ignore
401         }
402         return false;
403     }
404 //    public static int toIntWithErrorMessage(Object _aValue) throws com.sun.star.script.BasicErrorException{
405 //    try {
406 //        return toInt(_aValue);
407 //    }
408 //    catch(com.sun.star.lang.IllegalArgumentException e) {
409 //        DebugHelper.exception(BasicErrorCode.SbERR_CONVERSION, PropertyNames.EMPTY_STRING);
410 //        return 0;
411 //    }}
412 //
413 //
414 //    public static String toStringWithErrorMessage(Object _aValue) throws com.sun.star.script.BasicErrorException{
415 //    try {
416 //        return toString(_aValue);
417 //    }
418 //    catch(com.sun.star.lang.IllegalArgumentException e) {
419 //        DebugHelper.exception(BasicErrorCode.SbERR_CONVERSION, PropertyNames.EMPTY_STRING);
420 //        return PropertyNames.EMPTY_STRING;
421 //    }}
422 //
423 //
424 //    public static int toIntWithErrorMessage(Object _aValue, int _ndefaultValue) throws com.sun.star.script.BasicErrorException{
425 //    try {
426 //        return toInt(_aValue, _ndefaultValue);
427 //    }
428 //    catch(com.sun.star.uno.Exception e) {
429 //        DebugHelper.exception(BasicErrorCode.SbERR_CONVERSION, PropertyNames.EMPTY_STRING);
430 //        return 0;
431 //    }}
432 //
433 //    public static boolean toBooleanWithErrorMessage(Object _oObject, int _nTrueField, int _nFalseField) throws com.sun.star.script.BasicErrorException{
434 //        return toBooleanWithErrorMessage(_oObject, new int[]{_nTrueField}, new int[]{_nFalseField});
435 //    }
436 //
437 //
438 //    public static boolean toBooleanWithErrorMessage(Object _oObject) throws com.sun.star.script.BasicErrorException{
439 //        try{
440 //            return toBoolean(_oObject);
441 //        }
442 //        catch (java.lang.Exception e){
443 //           DebugHelper.exception(BasicErrorCode.SbERR_BAD_ARGUMENT, PropertyNames.EMPTY_STRING);
444 //           return false;
445 //        }
446 //    }
447 //
448 //
449 //    public static boolean toBooleanWithErrorMessage(Object _oObject, int[] _nTrueFields, int[] _nFalseFields) throws com.sun.star.script.BasicErrorException{
450 //    try{
451 //        int nValue = NumericalHelper.toIntWithErrorMessage(_oObject);
452 //        if (ContainerUtilities.FieldInIntTable(_nTrueFields, nValue) > -1){
453 //            return true;
454 //        }
455 //        else if (ContainerUtilities.FieldInIntTable(_nFalseFields, nValue) > -1){
456 //           return false;
457 //        }
458 //        else{
459 //            DebugHelper.exception(BasicErrorCode.SbERR_OUT_OF_RANGE, PropertyNames.EMPTY_STRING);
460 //            return false;
461 //        }
462 //    }catch (java.lang.Exception e){
463 //       DebugHelper.exception(BasicErrorCode.SbERR_OUT_OF_RANGE, PropertyNames.EMPTY_STRING);
464 //        return false;
465 //    }}
466 //
467 //
468 //    public static boolean toBooleanWithErrorMessage(Object _oObject, int _nTrueField, int _nFalseField, boolean _bdefaultValue) throws com.sun.star.script.BasicErrorException{
469 //        return toBooleanWithErrorMessage(_oObject, new int[]{_nTrueField}, new int[]{_nFalseField}, _bdefaultValue);
470 //    }
471 //
472 //
473 //    public static boolean toBooleanWithErrorMessage(Object _oObject, int[] _nTrueFields, int[] _nFalseFields, boolean _bdefaultValue) throws com.sun.star.script.BasicErrorException{
474 //    try{
475 //        if ((_oObject == null) || (AnyConverter.isVoid(_oObject))){
476 //            return _bdefaultValue;
477 //        }
478 //        else{
479 //            int nValue = NumericalHelper.toIntWithErrorMessage(_oObject);
480 //            if (ContainerUtilities.FieldInIntTable(_nTrueFields, nValue) > -1){
481 //                return true;
482 //            }
483 //            else if (ContainerUtilities.FieldInIntTable(_nFalseFields, nValue) > -1){
484 //               return false;
485 //            }
486 //            else{
487 //                DebugHelper.exception(BasicErrorCode.SbERR_OUT_OF_RANGE, PropertyNames.EMPTY_STRING);
488 //                return false;
489 //            }
490 //        }
491 //    }catch (java.lang.Exception e){
492 //       DebugHelper.exception(BasicErrorCode.SbERR_OUT_OF_RANGE, PropertyNames.EMPTY_STRING);
493 //        return false;
494 //    }}
495     /**
496      * get an int value from the object
497      * @param aValue
498      * @return an int
499      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
500      */
toInt(Object aValue)501     public static int toInt(Object aValue)
502             throws com.sun.star.lang.IllegalArgumentException
503     {
504         int retValue = 0;
505         TypeObject aTypeObject = getTypeObject(aValue);
506         switch (aTypeObject.iType)
507         {
508             case BYTE_TYPE:
509                 retValue = (int) getByte(aTypeObject);
510                 break;
511             case CHAR_TYPE:
512                 retValue = (int) getChar(aTypeObject);
513                 break;
514             case SHORT_TYPE:
515                 retValue = (int) getShort(aTypeObject);
516                 break;
517             case INT_TYPE:
518                 retValue = getInt(aTypeObject);
519                 break;
520             case LONG_TYPE:
521                 retValue = (int) getLong(aTypeObject);
522                 break;
523             case FLOAT_TYPE:
524                 retValue = (int) getFloat(aTypeObject);
525                 break;
526             case DOUBLE_TYPE:
527                 retValue = (int) getDouble(aTypeObject);
528                 break;
529             case STRING_TYPE:
530                 try
531                 {
532                     Integer i = new Integer((String) aTypeObject.aValue);
533                     retValue = i.intValue();
534                 }
535                 catch (java.lang.NumberFormatException e)
536                 {
537                     throw new com.sun.star.lang.IllegalArgumentException(
538                             "Cannot convert to int: " + aTypeObject.aValue);
539                 }
540                 break;
541             case BOOLEAN_TYPE:
542                 retValue = getBool(aTypeObject) ? -1 : 0;
543                 break;
544             default:
545                 throw new com.sun.star.lang.IllegalArgumentException(
546                         "Cannot convert this type: " + aValue.getClass().getName());
547         }
548         return retValue;
549     }
550 
551     /**
552      * get a long value from the object
553      * @param aValue
554      * @return a long
555      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
556      */
toLong(Object aValue)557     public static long toLong(Object aValue)
558             throws com.sun.star.lang.IllegalArgumentException
559     {
560         long retValue = 0;
561         TypeObject aTypeObject = getTypeObject(aValue);
562         switch (aTypeObject.iType)
563         {
564             case BYTE_TYPE:
565                 retValue = (long) getByte(aTypeObject);
566                 break;
567             case CHAR_TYPE:
568                 retValue = (long) getChar(aTypeObject);
569                 break;
570             case SHORT_TYPE:
571                 retValue = (long) getShort(aTypeObject);
572                 break;
573             case INT_TYPE:
574                 retValue = (long) getInt(aTypeObject);
575                 break;
576             case LONG_TYPE:
577                 retValue = getLong(aTypeObject);
578                 break;
579             case FLOAT_TYPE:
580                 retValue = (long) getFloat(aTypeObject);
581                 break;
582             case DOUBLE_TYPE:
583                 retValue = (long) getDouble(aTypeObject);
584                 break;
585             case STRING_TYPE:
586                 try
587                 {
588                     Long l = new Long((String) aTypeObject.aValue);
589                     retValue = l.longValue();
590                 }
591                 catch (java.lang.NumberFormatException e)
592                 {
593                     throw new com.sun.star.lang.IllegalArgumentException(
594                             "Cannot convert to short: " + aTypeObject.aValue);
595                 }
596                 break;
597             case BOOLEAN_TYPE:
598                 retValue = getBool(aTypeObject) ? -1 : 0;
599                 break;
600             default:
601                 throw new com.sun.star.lang.IllegalArgumentException(
602                         "Cannot convert this type: " + aValue.getClass().getName());
603         }
604         return retValue;
605     }
606 
607     /**
608      * get a float value from the object
609      * @param aValue
610      * @return a float
611      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
612      */
toFloat(Object aValue)613     public static float toFloat(Object aValue)
614             throws com.sun.star.lang.IllegalArgumentException
615     {
616         float retValue = (float) 0.0;
617         TypeObject aTypeObject = getTypeObject(aValue);
618         switch (aTypeObject.iType)
619         {
620             case BYTE_TYPE:
621                 retValue = (float) getByte(aTypeObject);
622                 break;
623             case CHAR_TYPE:
624                 retValue = (float) getChar(aTypeObject);
625                 break;
626             case SHORT_TYPE:
627                 retValue = (float) getShort(aTypeObject);
628                 break;
629             case INT_TYPE:
630                 retValue = (float) getInt(aTypeObject);
631                 break;
632             case LONG_TYPE:
633                 retValue = (float) getLong(aTypeObject);
634                 break;
635             case FLOAT_TYPE:
636                 retValue = getFloat(aTypeObject);
637                 break;
638             case DOUBLE_TYPE:
639                 retValue = (float) getDouble(aTypeObject);
640                 break;
641             case STRING_TYPE:
642                 try
643                 {
644                     Float f = new Float((String) aTypeObject.aValue);
645                     retValue = f.floatValue();
646                 }
647                 catch (java.lang.NumberFormatException e)
648                 {
649                     throw new com.sun.star.lang.IllegalArgumentException(
650                             "Cannot convert to short: " + aTypeObject.aValue);
651                 }
652                 break;
653             case BOOLEAN_TYPE:
654                 retValue = getBool(aTypeObject) ? (float) -1 : (float) 0;
655                 break;
656             default:
657                 throw new com.sun.star.lang.IllegalArgumentException(
658                         "Cannot convert this type: " + aValue.getClass().getName());
659         }
660         return retValue;
661     }
662 
663     /**
664      * get a double value from the object
665      * @param aValue
666      * @return a double
667      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
668      */
toDouble(Object aValue)669     public static double toDouble(Object aValue)
670             throws com.sun.star.lang.IllegalArgumentException
671     {
672         double retValue = 0.0;
673         TypeObject aTypeObject = getTypeObject(aValue);
674         switch (aTypeObject.iType)
675         {
676             case BYTE_TYPE:
677                 retValue = (double) getByte(aTypeObject);
678                 break;
679             case CHAR_TYPE:
680                 retValue = (double) getChar(aTypeObject);
681                 break;
682             case SHORT_TYPE:
683                 retValue = (double) getShort(aTypeObject);
684                 break;
685             case INT_TYPE:
686                 retValue = (double) getInt(aTypeObject);
687                 break;
688             case LONG_TYPE:
689                 retValue = (double) getLong(aTypeObject);
690                 break;
691             case FLOAT_TYPE:
692                 retValue = (double) getFloat(aTypeObject);
693                 break;
694             case DOUBLE_TYPE:
695                 retValue = getDouble(aTypeObject);
696                 break;
697             case STRING_TYPE:
698                 try
699                 {
700                     Float f = new Float((String) aTypeObject.aValue);
701                     retValue = f.floatValue();
702                 }
703                 catch (java.lang.NumberFormatException e)
704                 {
705                     throw new com.sun.star.lang.IllegalArgumentException(
706                             "Cannot convert to short: " + aTypeObject.aValue);
707                 }
708                 break;
709             case BOOLEAN_TYPE:
710                 retValue = getBool(aTypeObject) ? (double) -1 : (double) 0;
711                 break;
712             default:
713                 throw new com.sun.star.lang.IllegalArgumentException(
714                         "Cannot convert this type: " + aValue.getClass().getName());
715         }
716         return retValue;
717     }
718 
719     /**
720      * get a String value from the object
721      * @param aValue
722      * @return a String
723      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
724      */
toString(Object aValue)725     public static String toString(Object aValue)
726             throws com.sun.star.lang.IllegalArgumentException
727     {
728         String retValue = null;
729         TypeObject aTypeObject = getTypeObject(aValue);
730         switch (aTypeObject.iType)
731         {
732             case BYTE_TYPE:
733                 retValue = aTypeObject.aValue.toString();
734                 break;
735             case CHAR_TYPE:
736                 retValue = aTypeObject.aValue.toString();
737                 break;
738             case SHORT_TYPE:
739                 retValue = aTypeObject.aValue.toString();
740                 break;
741             case INT_TYPE:
742                 retValue = aTypeObject.aValue.toString();
743                 break;
744             case LONG_TYPE:
745                 retValue = aTypeObject.aValue.toString();
746                 break;
747             case FLOAT_TYPE:
748                 retValue = aTypeObject.aValue.toString();
749                 break;
750             case DOUBLE_TYPE:
751                 retValue = aTypeObject.aValue.toString();
752                 break;
753             case STRING_TYPE:
754                 retValue = (String) aTypeObject.aValue;
755                 break;
756             case BOOLEAN_TYPE:
757                 retValue = aTypeObject.aValue.toString();
758                 break;
759             case ARRAY_TYPE:
760                 retValue = new String(toByteArray((aValue)));
761                 break;
762             default:
763                 throw new com.sun.star.lang.IllegalArgumentException(
764                         "Cannot convert this type: " + aValue.getClass().getName());
765         }
766         return retValue;
767     }
768 
769     /**
770      * get a boolean value from the object
771      * @param aValue
772      * @return a boolean
773      * @throws com.sun.star.lang.IllegalArgumentException if the object cannot be converted
774      */
toBoolean(Object aValue)775     public static boolean toBoolean(Object aValue)
776             throws com.sun.star.lang.IllegalArgumentException
777     {
778         boolean retValue = true;
779         TypeObject aTypeObject = getTypeObject(aValue);
780         switch (aTypeObject.iType)
781         {
782             case BYTE_TYPE:
783                 retValue = (((Byte) aTypeObject.aValue).byteValue() != 0);
784                 break;
785             case CHAR_TYPE:
786                 retValue = (((Character) aTypeObject.aValue).charValue() != 0);
787                 break;
788             case SHORT_TYPE:
789                 retValue = (((Short) aTypeObject.aValue).shortValue() != 0);
790                 break;
791             case INT_TYPE:
792                 retValue = (((Integer) aTypeObject.aValue).intValue() != 0);
793                 break;
794             case LONG_TYPE:
795                 retValue = (((Long) aTypeObject.aValue).longValue() != 0);
796                 break;
797             case FLOAT_TYPE:
798                 retValue = (((Float) aTypeObject.aValue).floatValue() != 0);
799                 break;
800             case DOUBLE_TYPE:
801                 retValue = (((Double) aTypeObject.aValue).doubleValue() != 0);
802                 break;
803             case STRING_TYPE:
804                 try
805                 {
806                     Boolean b = Boolean.valueOf((String) aTypeObject.aValue);
807                     retValue = b.booleanValue();
808                 }
809                 catch (java.lang.NumberFormatException e)
810                 {
811                     throw new com.sun.star.lang.IllegalArgumentException(
812                             "Cannot convert to short: " + aTypeObject.aValue);
813                 }
814                 break;
815             case BOOLEAN_TYPE:
816                 retValue = ((Boolean) aTypeObject.aValue).booleanValue();
817                 break;
818             default:
819                 throw new com.sun.star.lang.IllegalArgumentException(
820                         "Cannot convert this type: " + aValue.getClass().getName());
821         }
822         return retValue;
823     }
824 
825     /**
826      * get an int array from an object
827      * @param anArrayValue a value that is constructed into an array
828      * @return an integer array
829      * @throws com.sun.star.lang.IllegalArgumentException
830      */
toIntArray(Object anArrayValue)831     public static int[] toIntArray(Object anArrayValue)
832             throws com.sun.star.lang.IllegalArgumentException
833     {
834         int[] retValue = null;
835         TypeObject aTypeObject = getTypeObject(anArrayValue);
836         if (aTypeObject.iType == SEQUENCE_TYPE)
837         {
838             aTypeObject = convertSequenceToObjectArray(aTypeObject);
839         }
840         if (aTypeObject.iType == ARRAY_TYPE)
841         {
842             Object[] obj = (Object[]) aTypeObject.aValue;
843             retValue = new int[obj.length];
844             for (int i = 0; i < obj.length; i++)
845             {
846                 retValue[i] = toInt(obj[i]);
847             }
848         }
849         else
850         { // object is not really an array
851             retValue = new int[]
852                     {
853                         toInt(anArrayValue)
854                     };
855         }
856         return retValue;
857     }
858 
859     /**
860      * get an byte array from an object
861      * @param anArrayValue a value that is constructed into an array
862      * @return a byte array
863      * @throws com.sun.star.lang.IllegalArgumentException
864      */
toByteArray(Object anArrayValue)865     public static byte[] toByteArray(Object anArrayValue)
866             throws com.sun.star.lang.IllegalArgumentException
867     {
868         byte[] retValue = null;
869         TypeObject aTypeObject = getTypeObject(anArrayValue);
870         if (aTypeObject.iType == SEQUENCE_TYPE)
871         {
872             aTypeObject = convertSequenceToObjectArray(aTypeObject);
873         }
874         if (aTypeObject.iType == ARRAY_TYPE)
875         {
876             Object[] obj = (Object[]) aTypeObject.aValue;
877             retValue = new byte[obj.length];
878             for (int i = 0; i < obj.length; i++)
879             {
880                 retValue[i] = toByte(obj[i]);
881             }
882         }
883         else
884         { // object is not really an array
885             retValue = new byte[]
886                     {
887                         toByte(anArrayValue)
888                     };
889         }
890         return retValue;
891     }
892 
893     /**
894      * get a short array from an object
895      * @param anArrayValue a value that is constructed into an array
896      * @return a short array
897      * @throws com.sun.star.lang.IllegalArgumentException
898      */
toShortArray(Object anArrayValue)899     public static short[] toShortArray(Object anArrayValue)
900             throws com.sun.star.lang.IllegalArgumentException
901     {
902         short[] retValue = null;
903         TypeObject aTypeObject = getTypeObject(anArrayValue);
904         if (aTypeObject.iType == SEQUENCE_TYPE)
905         {
906             aTypeObject = convertSequenceToObjectArray(aTypeObject);
907         }
908         if (aTypeObject.iType == ARRAY_TYPE)
909         {
910             Object[] obj = (Object[]) aTypeObject.aValue;
911             retValue = new short[obj.length];
912             for (int i = 0; i < obj.length; i++)
913             {
914                 retValue[i] = toShort(obj[i]);
915             }
916         }
917         else
918         { // object is not really an array
919             retValue = new short[]
920                     {
921                         toShort(anArrayValue)
922                     };
923         }
924         return retValue;
925     }
926 
927     /**
928      * get a string array from an object
929      * @param anArrayValue a value that is constructed into an array
930      * @return a short array
931      * @throws com.sun.star.lang.IllegalArgumentException
932      */
toStringArray(Object anArrayValue)933     public static String[] toStringArray(Object anArrayValue)
934             throws com.sun.star.lang.IllegalArgumentException
935     {
936         String[] retValue = null;
937         TypeObject aTypeObject = getTypeObject(anArrayValue);
938         if (aTypeObject.iType == SEQUENCE_TYPE)
939         {
940             aTypeObject = convertSequenceToObjectArray(aTypeObject);
941         }
942         if (aTypeObject.iType == ARRAY_TYPE)
943         {
944             Object[] obj = (Object[]) aTypeObject.aValue;
945             retValue = new String[obj.length];
946             for (int i = 0; i < obj.length; i++)
947             {
948                 retValue[i] = toString(obj[i]);
949             }
950         }
951         else
952         { // object is not really an array
953             retValue = new String[]
954                     {
955                         toString(anArrayValue)
956                     };
957         }
958         return retValue;
959     }
960 
961     /**
962      * get an int from an object
963      * @param _aValue a value that is constructed into an int
964      * @param _ndefaultValue the value that is returned, if conversion fails, or if 'aValue' is null
965      * @return an int value
966      * @throws java.lang.Exception
967      */
toInt(Object _aValue, int _ndefaultValue)968     public static int toInt(Object _aValue, int _ndefaultValue) throws Exception
969     {
970         int nreturn = _ndefaultValue;
971         try
972         {
973             if ((_aValue != null) && (!(AnyConverter.isVoid(_aValue))))
974             {
975                 if (isInteger(_aValue))
976                 {
977                     nreturn = toInt(_aValue);
978                 }
979                 else
980                 {
981                     DebugHelper.exception(1/* BasicErrorCode.SbERR_CONVERSION*/, PropertyNames.EMPTY_STRING);
982                 }
983             }
984         }
985         catch (com.sun.star.uno.Exception e)
986         {
987             DebugHelper.exception(1 /*BasicErrorCode.SbERR_METHOD_FAILED*/, PropertyNames.EMPTY_STRING);
988         }
989         return nreturn;
990     }
991 
992     /**
993      * get a long from an object
994      * @param aValue a value that is constructed into a long
995      * @param defaultValue the value that is returned, if conversion fails
996      * @return a long value
997      */
toLong(Object aValue, long defaultValue)998     public static long toLong(Object aValue, long defaultValue)
999     {
1000         try
1001         {
1002             return toLong(aValue);
1003         }
1004         catch (com.sun.star.lang.IllegalArgumentException e)
1005         {
1006             // ignore exception
1007         }
1008         return defaultValue;
1009     }
1010 
1011     /**
1012      * get a float from an object
1013      * @param aValue a value that is constructed into a float
1014      * @param defaultValue the value that is returned, if conversion fails
1015      * @return a long value
1016      */
toFloat(Object aValue, float defaultValue)1017     public static float toFloat(Object aValue, float defaultValue)
1018     {
1019         try
1020         {
1021             return toFloat(aValue);
1022         }
1023         catch (com.sun.star.lang.IllegalArgumentException e)
1024         {
1025             // ignore exception
1026         }
1027         return defaultValue;
1028     }
1029 
1030     /**
1031      * get a double from an object
1032      * @param aValue a value that is constructed into a double
1033      * @param defaultValue the value that is returned, if conversion fails
1034      * @return a double value
1035      */
toDouble(Object aValue, double defaultValue)1036     public static double toDouble(Object aValue, double defaultValue)
1037     {
1038         try
1039         {
1040             return toDouble(aValue);
1041         }
1042         catch (com.sun.star.lang.IllegalArgumentException e)
1043         {
1044             // ignore exception
1045         }
1046         return defaultValue;
1047     }
1048 
1049     /**
1050      * get a string from an object
1051      * @param aValue a value that is constructed into a string
1052      * @param defaultValue the value that is returned, if conversion fails
1053      * @return a string value
1054      */
toString(Object aValue, String defaultValue)1055     public static String toString(Object aValue, String defaultValue)
1056     {
1057         try
1058         {
1059             return toString(aValue);
1060         }
1061         catch (com.sun.star.lang.IllegalArgumentException e)
1062         {
1063             // ignore exception
1064         }
1065         return defaultValue;
1066     }
1067 
1068     /**
1069      * get a boolean from an object
1070      * @param aValue a value that is constructed into a boolean
1071      * @param defaultValue the value that is returned, if conversion fails
1072      * @return a boolean value
1073      */
toBoolean(Object aValue, boolean defaultValue)1074     public static boolean toBoolean(Object aValue, boolean defaultValue)
1075     {
1076         try
1077         {
1078             return toBoolean(aValue);
1079         }
1080         catch (com.sun.star.lang.IllegalArgumentException e)
1081         {
1082             // ignore exception
1083         }
1084         return defaultValue;
1085     }
1086 
1087     /**
1088      * get a int array from an object
1089      * @param anArrayValue  a value that is constructed into an int array
1090      * @param defaultValue the value that is returned, if conversion fails
1091      * @return an int array
1092      */
toIntArray(Object anArrayValue, int[] defaultValue)1093     public static int[] toIntArray(Object anArrayValue, int[] defaultValue)
1094     {
1095         try
1096         {
1097             return toIntArray(anArrayValue);
1098         }
1099         catch (com.sun.star.lang.IllegalArgumentException e)
1100         {
1101             // ignore exception
1102         }
1103         return defaultValue;
1104     }
1105 
1106     /**
1107      * get a short array from an object
1108      * @param anArrayValue a value that is constructed into a short array
1109      * @param defaultValue the value that is returned, if conversion fails
1110      * @return a short array
1111      */
toShortArray(Object anArrayValue, short[] defaultValue)1112     public static short[] toShortArray(Object anArrayValue, short[] defaultValue)
1113     {
1114         try
1115         {
1116             return toShortArray(anArrayValue);
1117         }
1118         catch (com.sun.star.lang.IllegalArgumentException e)
1119         {
1120             // ignore exception
1121         }
1122         return defaultValue;
1123     }
1124 
1125     /**
1126      * get a string array from an object
1127      * @param anArrayValue a value that is constructed into a string array
1128      * @param defaultValue the value that is returned, if conversion fails
1129      * @return a string array
1130      */
toStringArray(Object anArrayValue, String[] defaultValue)1131     public static String[] toStringArray(Object anArrayValue, String[] defaultValue)
1132     {
1133         try
1134         {
1135             return toStringArray(anArrayValue);
1136         }
1137         catch (com.sun.star.lang.IllegalArgumentException e)
1138         {
1139             // ignore exception
1140         }
1141         return defaultValue;
1142     }
1143 
1144     /**
1145      * get a hexadecimal representation from a number
1146      * @param number the number to transform
1147      * @return a String with the hex code of the number
1148      */
getHexStringFromNumber(long number)1149     public static String getHexStringFromNumber(long number)
1150     {
1151         TransformNumToHex num = new TransformNumToHex(number);
1152         return num.getResult();
1153     }
1154 
1155     /**
1156      * Get the roman equivalent to an arabic number, e.g. 17 -> XVII.
1157      * The allowed range for numbers goes from 1 to 3999. These can be
1158      * converted using ASCII letters (3999 -> MMMCMXCIX).
1159      * @param n the arabic number
1160      * @return the roman equivalent as string
1161      * @throws BasicErrorException if the number cannot be converted.
1162      */
1163 //    public static String getRomanEquivalent(int n)
1164 //            throws com.sun.star.script.BasicErrorException {
1165 //        return RomanNumbering.getRomanEquivalent(n);
1166 //    }
1167     /**
1168      * get the type object from the given object
1169      * @param aValue an object representing a (numerical) value; can also be an 'any'
1170      * @return a type object: the object together with the its type information
1171      * @throws com.sun.star.lang.IllegalArgumentException if the object is unknown
1172      */
getTypeObject(Object aValue)1173     private static TypeObject getTypeObject(Object aValue)
1174             throws com.sun.star.lang.IllegalArgumentException
1175     {
1176         TypeObject aTypeObject = new TypeObject();
1177         if (aValue == null || AnyConverter.isVoid(aValue))
1178         {
1179             throw new com.sun.star.lang.IllegalArgumentException("Cannot convert a null object.");
1180         }
1181         int type = AnyConverter.getType(aValue).getTypeClass().getValue();
1182         switch (type)
1183         {
1184             case TypeClass.CHAR_value:
1185                 aTypeObject.iType = CHAR_TYPE;
1186                 aTypeObject.aValue = new Character(AnyConverter.toChar(aValue));
1187                 break;
1188             case TypeClass.BYTE_value:
1189                 aTypeObject.iType = BYTE_TYPE;
1190                 aTypeObject.aValue = new Byte(AnyConverter.toByte(aValue));
1191                 break;
1192             case TypeClass.SHORT_value:
1193                 aTypeObject.iType = SHORT_TYPE;
1194                 aTypeObject.aValue = new Short(AnyConverter.toShort(aValue));
1195                 break;
1196             case TypeClass.LONG_value:
1197                 aTypeObject.iType = INT_TYPE;
1198                 aTypeObject.aValue = new Integer(AnyConverter.toInt(aValue));
1199                 break;
1200             case TypeClass.HYPER_value:
1201                 aTypeObject.iType = LONG_TYPE;
1202                 aTypeObject.aValue = new Long(AnyConverter.toLong(aValue));
1203                 break;
1204             case TypeClass.FLOAT_value:
1205                 aTypeObject.iType = FLOAT_TYPE;
1206                 aTypeObject.aValue = new Float(AnyConverter.toFloat(aValue));
1207                 break;
1208             case TypeClass.DOUBLE_value:
1209                 aTypeObject.iType = DOUBLE_TYPE;
1210                 aTypeObject.aValue = new Double(AnyConverter.toDouble(aValue));
1211                 break;
1212             case TypeClass.STRING_value:
1213                 aTypeObject.iType = STRING_TYPE;
1214                 aTypeObject.aValue = AnyConverter.toString(aValue);
1215                 break;
1216             case TypeClass.BOOLEAN_value:
1217                 aTypeObject.iType = BOOLEAN_TYPE;
1218                 aTypeObject.aValue = Boolean.valueOf(AnyConverter.toBoolean(aValue));
1219                 break;
1220             case TypeClass.ARRAY_value:
1221                 aTypeObject.iType = ARRAY_TYPE;
1222                 aTypeObject.aValue = new Object[]
1223                         {
1224                             AnyConverter.toArray(aValue)
1225                         };
1226                 break;
1227             case TypeClass.SEQUENCE_value:
1228                 aTypeObject.iType = SEQUENCE_TYPE;
1229                 aTypeObject.aValue = aValue;
1230                 break;
1231             default:
1232                 throw new com.sun.star.lang.IllegalArgumentException(
1233                         "Cannot convert this type: " + aValue.getClass().getName());
1234         }
1235         return aTypeObject;
1236     }
1237 
1238     /**
1239      * get the simple byte type
1240      */
getByte(TypeObject typeObject)1241     private static byte getByte(TypeObject typeObject)
1242             throws com.sun.star.lang.IllegalArgumentException
1243     {
1244         if (typeObject.iType != BYTE_TYPE)
1245         {
1246             throw new com.sun.star.lang.IllegalArgumentException(
1247                     "Given argument is not a byte type.");
1248         }
1249         return ((Byte) typeObject.aValue).byteValue();
1250     }
1251 
1252     /**
1253      * get the simple char type
1254      */
getChar(TypeObject typeObject)1255     private static char getChar(TypeObject typeObject)
1256             throws com.sun.star.lang.IllegalArgumentException
1257     {
1258         if (typeObject.iType != CHAR_TYPE)
1259         {
1260             throw new com.sun.star.lang.IllegalArgumentException(
1261                     "Given argument is not a char type.");
1262         }
1263         return ((Character) typeObject.aValue).charValue();
1264     }
1265 
1266     /**
1267      * get the simple short type
1268      */
getShort(TypeObject typeObject)1269     private static short getShort(TypeObject typeObject)
1270             throws com.sun.star.lang.IllegalArgumentException
1271     {
1272         if (typeObject.iType != SHORT_TYPE)
1273         {
1274             throw new com.sun.star.lang.IllegalArgumentException(
1275                     "Given argument is not a short type.");
1276         }
1277         return ((Short) typeObject.aValue).shortValue();
1278     }
1279 
1280     /**
1281      * get the simple int type
1282      * @param typeObject
1283      * @return
1284      * @throws com.sun.star.lang.IllegalArgumentException
1285      */
getInt(TypeObject typeObject)1286     static int getInt(TypeObject typeObject)
1287             throws com.sun.star.lang.IllegalArgumentException
1288     {
1289         if (typeObject.iType != INT_TYPE)
1290         {
1291             throw new com.sun.star.lang.IllegalArgumentException(
1292                     "Given argument is not an int type.");
1293         }
1294         return ((Integer) typeObject.aValue).intValue();
1295     }
1296 
1297     /**
1298      * get the simple float type
1299      * @throws com.sun.star.lang.IllegalArgumentException
1300      */
getFloat(TypeObject typeObject)1301     static float getFloat(TypeObject typeObject)
1302             throws com.sun.star.lang.IllegalArgumentException
1303     {
1304         if (typeObject.iType != FLOAT_TYPE)
1305         {
1306             throw new com.sun.star.lang.IllegalArgumentException(
1307                     "Given argument is not a float type.");
1308         }
1309         return ((Float) typeObject.aValue).floatValue();
1310     }
1311 
1312     /**
1313      * get the simple double type
1314      */
getDouble(TypeObject typeObject)1315     private static double getDouble(TypeObject typeObject)
1316             throws com.sun.star.lang.IllegalArgumentException
1317     {
1318         if (typeObject.iType != DOUBLE_TYPE)
1319         {
1320             throw new com.sun.star.lang.IllegalArgumentException(
1321                     "Given argument is not a double type.");
1322         }
1323         return ((Double) typeObject.aValue).doubleValue();
1324     }
1325 
1326     /**
1327      * get the simple long type
1328      */
getLong(TypeObject typeObject)1329     private static long getLong(TypeObject typeObject)
1330             throws com.sun.star.lang.IllegalArgumentException
1331     {
1332         if (typeObject.iType != LONG_TYPE)
1333         {
1334             throw new com.sun.star.lang.IllegalArgumentException(
1335                     "Given argument is not a long type.");
1336         }
1337         return ((Long) typeObject.aValue).longValue();
1338     }
1339 
1340     /**
1341      * get the simple boolean type
1342      */
getBool(TypeObject typeObject)1343     private static boolean getBool(TypeObject typeObject)
1344             throws com.sun.star.lang.IllegalArgumentException
1345     {
1346         if (typeObject.iType != BOOLEAN_TYPE)
1347         {
1348             throw new com.sun.star.lang.IllegalArgumentException(
1349                     "Given argument is not a boolean type.");
1350         }
1351         return ((Boolean) typeObject.aValue).booleanValue();
1352     }
1353 
1354     /**
1355      * a class to contain a type and a value for easier conversions
1356      */
1357     private static class TypeObject
1358     {
1359 
1360         public int iType;
1361         public Object aValue;
1362     }
1363 
1364     /**
1365      * simple class to construct a hexadecimal value from a long number
1366      */
1367     private static class TransformNumToHex
1368     {
1369 
1370         private StringBuffer val;
1371 
TransformNumToHex(long number)1372         public TransformNumToHex(long number)
1373         {
1374             val = new StringBuffer();
1375             transform(number);
1376         }
1377 
transform(long number)1378         private void transform(long number)
1379         {
1380             int index = (int) (number % HEX_BASE);
1381             number = number / HEX_BASE;
1382             if (index < DEC_BASE)
1383             {
1384                 val.insert(0, index);
1385             }
1386             else
1387             {
1388                 val.insert(0, (char) (ASCII_LETTER_A_OFFSET + index));
1389             }
1390             if (number > 0)
1391             {
1392                 transform(number);
1393             }
1394         }
1395 
getResult()1396         public String getResult()
1397         {
1398             return val.toString();
1399         }
1400     }
1401 
convertSequenceToObjectArray( TypeObject sourceObject)1402     private static TypeObject convertSequenceToObjectArray(
1403             TypeObject sourceObject)
1404             throws com.sun.star.lang.IllegalArgumentException
1405     {
1406         TypeObject destObject = new TypeObject();
1407         Object array = sourceObject.aValue;
1408         destObject.iType = ARRAY_TYPE;
1409         Class c = array.getClass();
1410         Object[] aShortVal = null;
1411         if (c.equals(byte[].class))
1412         {
1413             byte[] vals = (byte[]) array;
1414             aShortVal = new Object[vals.length];
1415             for (int i = 0; i < vals.length; i++)
1416             {
1417                 aShortVal[i] = new Byte(vals[i]);
1418             }
1419         }
1420         else if (c.equals(short[].class))
1421         {
1422             short[] vals = (short[]) array;
1423             aShortVal = new Object[vals.length];
1424             for (int i = 0; i < vals.length; i++)
1425             {
1426                 aShortVal[i] = new Short(vals[i]);
1427             }
1428         }
1429         else if (c.equals(int[].class))
1430         {
1431             int[] vals = (int[]) array;
1432             aShortVal = new Object[vals.length];
1433             for (int i = 0; i < vals.length; i++)
1434             {
1435                 aShortVal[i] = new Integer(vals[i]);
1436             }
1437         }
1438         else if (c.equals(long[].class))
1439         {
1440             long[] vals = (long[]) array;
1441             aShortVal = new Object[vals.length];
1442             for (int i = 0; i < vals.length; i++)
1443             {
1444                 aShortVal[i] = new Long(vals[i]);
1445             }
1446         }
1447         else if (c.equals(float[].class))
1448         {
1449             float[] vals = (float[]) array;
1450             aShortVal = new Object[vals.length];
1451             for (int i = 0; i < vals.length; i++)
1452             {
1453                 aShortVal[i] = new Float(vals[i]);
1454             }
1455         }
1456         else if (c.equals(double[].class))
1457         {
1458             double[] vals = (double[]) array;
1459             aShortVal = new Object[vals.length];
1460             for (int i = 0; i < vals.length; i++)
1461             {
1462                 aShortVal[i] = new Double(vals[i]);
1463             }
1464         }
1465         else if (c.equals(boolean[].class))
1466         {
1467             boolean[] vals = (boolean[]) array;
1468             aShortVal = new Object[vals.length];
1469             for (int i = 0; i < vals.length; i++)
1470             {
1471                 aShortVal[i] = Boolean.valueOf(vals[i]);
1472             }
1473         }
1474         // if nothing did match, try this
1475         if (aShortVal == null)
1476         {
1477             try
1478             {
1479                 aShortVal = (Object[]) array;
1480             }
1481             catch (java.lang.ClassCastException e)
1482             {
1483                 // unknown type cannot be converted
1484                 throw new com.sun.star.lang.IllegalArgumentException(
1485                         "Cannot convert unknown type: '" + e.getMessage() + "'");
1486             }
1487         }
1488         destObject.aValue = aShortVal;
1489         return destObject;
1490     }
1491 
1492 //    public static boolean isObjectNumericRepresentation(Object _oValue, NumberFormatter _oNumberFormatter, XPropertySet _xPropertySet) throws com.sun.star.script.BasicErrorException{
1493 //    try{
1494 //        int nNumberFormat = AnyConverter.toInt(_xPropertySet.getPropertyValue("NumberFormat"));
1495 //        if (AnyConverter.isString(_oValue)){
1496 //            String sCellContent = AnyConverter.toString(_oValue);
1497 //            try{
1498 //                _oNumberFormatter.convertStringToNumber(nNumberFormat, sCellContent);
1499 //                return true;
1500 //            }catch (Exception e){
1501 //                return false;
1502 //            }
1503 //        }
1504 //        else{
1505 //            return true;
1506 //        }
1507 //    }
1508 //    catch (com.sun.star.uno.Exception e){
1509 //        DebugHelper.exception(1 /*BasicErrorCode.SbERR_METHOD_FAILED*/, PropertyNames.EMPTY_STRING);
1510 //        return false;
1511 //    }}
1512     /**
1513      * Helper class for roman numbering
1514      */
1515     private static class RomanNumbering
1516     {
1517 
1518         /** the used roman lettesrs **/
1519         private static final String[] ROMAN_EQUIV = new String[]
1520         {
1521             "I", "V", "X", "L", "C", "D", "M"
1522         };
1523         /** max number that can be converted **/
1524         private static final int MAX_NUMBER = 3999;
1525         /** min number that can be converted **/
1526         private static final int MIN_NUMBER = 1;
1527         /** ASCII code for the number 0 **/
1528         private static final int ASCII_CODE_0 = 48;
1529         /** special number for the conversion algorithm **/
1530         private static final int FOUR = 4;
1531         /** special number for the conversion algorithm **/
1532         private static final int FIVE = 5;
1533         /** special number for the conversion algorithm **/
1534         private static final int NINE = 9;
1535 
1536         /**
1537          * Get the roman equivalent to an arabic number, e.g. 17 -> XVII.
1538          * The allowed range for numbers goes from 1 to 3999. These can be
1539          * converted using ASCII letters (3999 -> MMMCMXCIX).
1540          * @param n the arabic number
1541          * @return the roman equivalent as string
1542          * @throws BasicErrorException if the number cannot be converted.
1543          */
getRomanEquivalent(int n)1544         public static String getRomanEquivalent(int n)
1545                 throws Exception
1546         {
1547             StringBuffer romanNumber = new StringBuffer();
1548             try
1549             {
1550                 if (n > MAX_NUMBER || n < MIN_NUMBER)
1551                 {
1552                     DebugHelper.exception(1 /*BasicErrorCode.SbERR_OUT_OF_RANGE*/, PropertyNames.EMPTY_STRING);
1553                 }
1554                 String number = NumericalHelper.toString(new Integer(n));
1555                 /* converison idea: every digit is written with a maximum of two
1556                  * different roman symbols, using three in total, e.g. CC, CD,
1557                  * DCC, CM for the hundreds (meaning 200, 400, 700 and 900).
1558                  * So every digit is converted separately with regard to the
1559                  * special cases 4 and 9.
1560                  */
1561                 int symbolIndex = 0;
1562                 for (int i = number.length() - 1; i >= 0; i--)
1563                 {
1564                     StringBuffer romanDigit = new StringBuffer();
1565                     int b = (int) number.charAt(i) - ASCII_CODE_0;
1566                     if (b == FOUR)
1567                     { // special case IV
1568                         romanDigit.append(ROMAN_EQUIV[symbolIndex]);
1569                         romanDigit.append(ROMAN_EQUIV[symbolIndex + 1]);
1570                     }
1571                     else if (b == NINE)
1572                     { // special case IX
1573                         romanDigit.append(ROMAN_EQUIV[symbolIndex]);
1574                         romanDigit.append(ROMAN_EQUIV[symbolIndex + 2]);
1575                     }
1576                     else
1577                     {
1578                         if (b >= FIVE)
1579                         { // special case V
1580                             romanDigit.append(ROMAN_EQUIV[symbolIndex + 1]);
1581                             b = b - FIVE;
1582                         }
1583                         for (int j = 0; j < b; j++)
1584                         { // append I's
1585                             romanDigit.append(ROMAN_EQUIV[symbolIndex]);
1586                         }
1587                     }
1588                     // next group of three symbols
1589                     symbolIndex += 2;
1590                     // append in reverse: we are starting at the right
1591                     romanNumber.append(romanDigit.reverse());
1592                 }
1593             }
1594             catch (com.sun.star.lang.IllegalArgumentException e)
1595             {
1596                 DebugHelper.exception(e);
1597             }
1598             // reverse again to get the number
1599             return romanNumber.reverse().toString();
1600         }
1601     }
1602 
representsIntegerNumber(double _dblvalue)1603     public static boolean representsIntegerNumber(double _dblvalue)
1604     {
1605         double dblsecvalue = (double) ((int) _dblvalue);
1606         return Double.compare(_dblvalue, dblsecvalue) == 0;
1607     }
1608 
roundDouble(Double _Dblvalue, int _ndecimals)1609     public static double roundDouble(Double _Dblvalue, int _ndecimals)
1610     {
1611         return roundDouble(_Dblvalue.doubleValue(), _ndecimals);
1612     }
1613 
roundDouble(double _dblvalue, int _ndecimals)1614     public static double roundDouble(double _dblvalue, int _ndecimals)
1615     {
1616         double dblfactor = java.lang.Math.pow(10.0, (double) _ndecimals);
1617         return ((double) ((int) (_dblvalue * dblfactor))) / dblfactor;
1618     }
1619 }
1620