Coverage report

  %line %branch
org.apache.commons.configuration.PropertyConverter
99% 
100% 

 1  
 /*
 2  
  * Copyright 2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.configuration;
 18  
 
 19  
 import java.awt.Color;
 20  
 import java.math.BigDecimal;
 21  
 import java.math.BigInteger;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.text.ParseException;
 25  
 import java.text.SimpleDateFormat;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Calendar;
 28  
 import java.util.Collection;
 29  
 import java.util.Date;
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 import java.util.Locale;
 33  
 
 34  
 import org.apache.commons.collections.IteratorUtils;
 35  
 import org.apache.commons.collections.iterators.IteratorChain;
 36  
 import org.apache.commons.collections.iterators.SingletonIterator;
 37  
 import org.apache.commons.lang.BooleanUtils;
 38  
 import org.apache.commons.lang.StringUtils;
 39  
 
 40  
 /**
 41  
  * A utility class to convert the configuration properties into any type.
 42  
  *
 43  
  * @author Emmanuel Bourg
 44  
  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
 45  
  * @since 1.1
 46  
  */
 47  0
 public final class PropertyConverter
 48  
 {
 49  
     /**
 50  
      * Convert the specified object into a Boolean.
 51  
      *
 52  
      * @param value the value to convert
 53  
      * @throws ConversionException thrown if the value cannot be converted to a boolean
 54  
      */
 55  
     public static Boolean toBoolean(Object value) throws ConversionException
 56  
     {
 57  1630
         if (value instanceof Boolean)
 58  
         {
 59  627
             return (Boolean) value;
 60  
         }
 61  1003
         else if (value instanceof String)
 62  
         {
 63  957
             Boolean b = BooleanUtils.toBooleanObject((String) value);
 64  957
             if (b == null)
 65  
             {
 66  92
                 throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
 67  
             }
 68  865
             return b;
 69  
         }
 70  
         else
 71  
         {
 72  46
             throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
 73  
         }
 74  
     }
 75  
 
 76  
     /**
 77  
      * Convert the specified object into a Byte.
 78  
      *
 79  
      * @param value the value to convert
 80  
      * @throws ConversionException thrown if the value cannot be converted to a byte
 81  
      */
 82  
     public static Byte toByte(Object value) throws ConversionException
 83  
     {
 84  1064
         if (value instanceof Byte)
 85  
         {
 86  420
             return (Byte) value;
 87  
         }
 88  644
         else if (value instanceof String)
 89  
         {
 90  
             try
 91  
             {
 92  598
                 String string = (String) value;
 93  598
                 if (string.startsWith("0x"))
 94  
                 {
 95  23
                     return new Byte((byte) Integer.parseInt(string.substring(2), 16));
 96  
                 }
 97  
                 else
 98  
                 {
 99  575
                     return new Byte(string);
 100  
                 }
 101  
             }
 102  92
             catch (NumberFormatException e)
 103  
             {
 104  92
                 throw new ConversionException("The value " + value + " can't be converted to a Byte object", e);
 105  
             }
 106  
         }
 107  
         else
 108  
         {
 109  46
             throw new ConversionException("The value " + value + " can't be converted to a Byte object");
 110  
         }
 111  
     }
 112  
 
 113  
     /**
 114  
      * Convert the specified object into a Short.
 115  
      *
 116  
      * @param value the value to convert
 117  
      * @throws ConversionException thrown if the value cannot be converted to a short
 118  
      */
 119  
     public static Short toShort(Object value) throws ConversionException
 120  
     {
 121  1179
         if (value instanceof Short)
 122  
         {
 123  420
             return (Short) value;
 124  
         }
 125  759
         else if (value instanceof String)
 126  
         {
 127  
             try
 128  
             {
 129  713
                 String string = (String) value;
 130  713
                 if (string.startsWith("0x"))
 131  
                 {
 132  23
                     return new Short((short) Integer.parseInt(string.substring(2), 16));
 133  
                 }
 134  
                 else
 135  
                 {
 136  690
                     return new Short(string);
 137  
                 }
 138  
 
 139  
             }
 140  92
             catch (NumberFormatException e)
 141  
             {
 142  92
                 throw new ConversionException("The value " + value + " can't be converted to a Short object", e);
 143  
             }
 144  
         }
 145  
         else
 146  
         {
 147  46
             throw new ConversionException("The value " + value + " can't be converted to a Short object");
 148  
         }
 149  
     }
 150  
 
 151  
     /**
 152  
      * Convert the specified object into an Integer.
 153  
      *
 154  
      * @param value the value to convert
 155  
      * @throws ConversionException thrown if the value cannot be converted to an integer
 156  
      */
 157  
     public static Integer toInteger(Object value) throws ConversionException
 158  
     {
 159  1133
         if (value instanceof Integer)
 160  
         {
 161  466
             return (Integer) value;
 162  
         }
 163  667
         else if (value instanceof String)
 164  
         {
 165  
             try
 166  
             {
 167  621
                 String string = (String) value;
 168  621
                 if (string.startsWith("0x"))
 169  
                 {
 170  23
                     return new Integer((int) Long.parseLong(string.substring(2), 16));
 171  
                 }
 172  
                 else
 173  
                 {
 174  598
                     return new Integer(string);
 175  
                 }
 176  
             }
 177  46
             catch (NumberFormatException e)
 178  
             {
 179  46
                 throw new ConversionException("The value " + value + " can't be converted to an Integer object", e);
 180  
             }
 181  
         }
 182  
         else
 183  
         {
 184  46
             throw new ConversionException("The value " + value + " can't be converted to an Integer object");
 185  
         }
 186  
     }
 187  
 
 188  
     /**
 189  
      * Convert the specified object into a Long.
 190  
      *
 191  
      * @param value the value to convert
 192  
      * @throws ConversionException thrown if the value cannot be converted to a Long
 193  
      */
 194  
     public static Long toLong(Object value) throws ConversionException
 195  
     {
 196  1064
         if (value instanceof Long)
 197  
         {
 198  420
             return (Long) value;
 199  
         }
 200  644
         else if (value instanceof String)
 201  
         {
 202  
             try
 203  
             {
 204  598
                 String string = (String) value;
 205  598
                 if (string.startsWith("0x"))
 206  
                 {
 207  23
                     return new Long(class="keyword">new BigInteger(string.substring(2), 16).longValue());
 208  
                 }
 209  
                 else
 210  
                 {
 211  575
                     return new Long(string);
 212  
                 }
 213  
             }
 214  92
             catch (NumberFormatException e)
 215  
             {
 216  92
                 throw new ConversionException("The value " + value + " can't be converted to a Long object", e);
 217  
             }
 218  
         }
 219  
         else
 220  
         {
 221  46
             throw new ConversionException("The value " + value + " can't be converted to a Long object");
 222  
         }
 223  
     }
 224  
 
 225  
     /**
 226  
      * Convert the specified object into a Float.
 227  
      *
 228  
      * @param value the value to convert
 229  
      * @throws ConversionException thrown if the value cannot be converted to a Float
 230  
      */
 231  
     public static Float toFloat(Object value) throws ConversionException
 232  
     {
 233  1041
         if (value instanceof Float)
 234  
         {
 235  420
             return (Float) value;
 236  
         }
 237  621
         else if (value instanceof String)
 238  
         {
 239  
             try
 240  
             {
 241  575
                 return new Float((String) value);
 242  
             }
 243  92
             catch (NumberFormatException e)
 244  
             {
 245  92
                 throw new ConversionException("The value " + value + " can't be converted to a Float object", e);
 246  
             }
 247  
         }
 248  
         else
 249  
         {
 250  46
             throw new ConversionException("The value " + value + " can't be converted to a Float object");
 251  
         }
 252  
     }
 253  
 
 254  
     /**
 255  
      * Convert the specified object into a Double.
 256  
      *
 257  
      * @param value the value to convert
 258  
      * @throws ConversionException thrown if the value cannot be converted to a Double
 259  
      */
 260  
     public static Double toDouble(Object value) throws ConversionException
 261  
     {
 262  1041
         if (value instanceof Double)
 263  
         {
 264  420
             return (Double) value;
 265  
         }
 266  621
         else if (value instanceof String)
 267  
         {
 268  
             try
 269  
             {
 270  575
                 return new Double((String) value);
 271  
             }
 272  92
             catch (NumberFormatException e)
 273  
             {
 274  92
                 throw new ConversionException("The value " + value + " can't be converted to a Double object", e);
 275  
             }
 276  
         }
 277  
         else
 278  
         {
 279  46
             throw new ConversionException("The value " + value + " can't be converted to a Double object");
 280  
         }
 281  
     }
 282  
 
 283  
     /**
 284  
      * Convert the specified object into a BigInteger.
 285  
      *
 286  
      * @param value the value to convert
 287  
      * @throws ConversionException thrown if the value cannot be converted to a BigInteger
 288  
      */
 289  
     public static BigInteger toBigInteger(Object value) throws ConversionException
 290  
     {
 291  805
         if (value instanceof BigInteger)
 292  
         {
 293  322
             return (BigInteger) value;
 294  
         }
 295  483
         else if (value instanceof String)
 296  
         {
 297  
             try
 298  
             {
 299  437
                 String string = (String) value;
 300  437
                 if (string.startsWith("0x"))
 301  
                 {
 302  23
                     return new BigInteger(string.substring(2), 16);
 303  
                 }
 304  
                 else
 305  
                 {
 306  414
                     return new BigInteger(string);
 307  
                 }
 308  
             }
 309  92
             catch (NumberFormatException e)
 310  
             {
 311  92
                 throw new ConversionException("The value " + value + " can't be converted to a BigInteger object", e);
 312  
             }
 313  
         }
 314  
         else
 315  
         {
 316  46
             throw new ConversionException("The value " + value + " can't be converted to a BigInteger object");
 317  
         }
 318  
     }
 319  
 
 320  
     /**
 321  
      * Convert the specified object into a BigDecimal.
 322  
      *
 323  
      * @param value the value to convert
 324  
      * @throws ConversionException thrown if the value cannot be converted to a BigDecimal
 325  
      */
 326  
     public static BigDecimal toBigDecimal(Object value) throws ConversionException
 327  
     {
 328  782
         if (value instanceof BigDecimal)
 329  
         {
 330  322
             return (BigDecimal) value;
 331  
         }
 332  460
         else if (value instanceof String)
 333  
         {
 334  
             try
 335  
             {
 336  414
                 return new BigDecimal((String) value);
 337  
             }
 338  92
             catch (NumberFormatException e)
 339  
             {
 340  92
                 throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object", e);
 341  
             }
 342  
         }
 343  
         else
 344  
         {
 345  46
             throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object");
 346  
         }
 347  
     }
 348  
 
 349  
     /**
 350  
      * Convert the specified object into an URL.
 351  
      *
 352  
      * @param value the value to convert
 353  
      * @throws ConversionException thrown if the value cannot be converted to an URL
 354  
      */
 355  
     public static URL toURL(Object value) throws ConversionException
 356  
     {
 357  689
         if (value instanceof URL)
 358  
         {
 359  344
             return (URL) value;
 360  
         }
 361  345
         else if (value instanceof String)
 362  
         {
 363  
             try
 364  
             {
 365  299
                 return new URL((String) value);
 366  
             }
 367  46
             catch (MalformedURLException e)
 368  
             {
 369  46
                 throw new ConversionException("The value " + value + " can't be converted to an URL", e);
 370  
             }
 371  
         }
 372  
         else
 373  
         {
 374  46
             throw new ConversionException("The value " + value + " can't be converted to an URL");
 375  
         }
 376  
     }
 377  
 
 378  
     /**
 379  
      * Convert the specified object into a Locale.
 380  
      *
 381  
      * @param value the value to convert
 382  
      * @throws ConversionException thrown if the value cannot be converted to a Locale
 383  
      */
 384  
     public static Locale toLocale(Object value) throws ConversionException
 385  
     {
 386  805
         if (value instanceof Locale)
 387  
         {
 388  322
             return (Locale) value;
 389  
         }
 390  483
         else if (value instanceof String)
 391  
         {
 392  437
             List elements = split((String) value, '_');
 393  437
             int size = elements.size();
 394  
 
 395  437
             if (size >= 1 && (((String) elements.get(0)).length() == 2 || ((String) elements.get(0)).length() == 0))
 396  
             {
 397  391
                 String language = (String) elements.get(0);
 398  391
                 String country = (String) ((size >= 2) ? elements.get(1) : "");
 399  391
                 String variant = (String) ((size >= 3) ? elements.get(2) : "");
 400  
 
 401  391
                 return new Locale(language, country, variant);
 402  
             }
 403  
             else
 404  
             {
 405  46
                 throw new ConversionException("The value " + value + " can't be converted to a Locale");
 406  
             }
 407  
         }
 408  
         else
 409  
         {
 410  46
             throw new ConversionException("The value " + value + " can't be converted to a Locale");
 411  
         }
 412  
     }
 413  
 
 414  
     /**
 415  
      * Split a string on the specified delimiter. To be removed when
 416  
      * commons-lang has a better replacement available (Tokenizer?).
 417  
      *
 418  
      * todo: replace with a commons-lang equivalent
 419  
      *
 420  
      * @param s          the string to split
 421  
      * @param delimiter  the delimiter
 422  
      */
 423  
     public static List split(String s, char delimiter)
 424  
     {
 425  42899
         if (s == null)
 426  
         {
 427  135
             return new ArrayList();
 428  
         }
 429  
 
 430  42764
         List list = new ArrayList();
 431  
 
 432  42764
         StringBuffer token = new StringBuffer();
 433  42764
         int begin = 0;
 434  42764
         int end = 0;
 435  109765
         while (begin <= s.length())
 436  
         {
 437  
             // find the next delimiter
 438  67001
             int index = s.indexOf(delimiter, end);
 439  
 
 440  
             // move the end index at the end of the string if the delimiter is not found
 441  67001
             end = (index != -1) ? index : s.length();
 442  
 
 443  
             // extract the chunk
 444  67001
             String chunk = s.substring(begin , end);
 445  
 
 446  67001
             if (chunk.endsWith("\\") && end != s.length())
 447  
             {
 448  3156
                 token.append(chunk.substring(0, chunk.length() - 1));
 449  3156
                 token.append(delimiter);
 450  
             }
 451  
             else
 452  
             {
 453  
                 // append the chunk to the token
 454  63845
                 token.append(chunk);
 455  
 
 456  
                 // add the token to the list
 457  63845
                 list.add(token.toString().trim());
 458  
 
 459  
                 // reset the token
 460  63845
                 token = new StringBuffer();
 461  
             }
 462  
 
 463  
             // move to the next chunk
 464  67001
             end = end + 1;
 465  67001
             begin = end;
 466  
         }
 467  
 
 468  42764
         return list;
 469  
     }
 470  
 
 471  
     /**
 472  
      * Convert the specified object into a Color. If the value is a String,
 473  
      * the format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:
 474  
      * <ul>
 475  
      *   <li>FF0000 (red)</li>
 476  
      *   <li>0000FFA0 (semi transparent blue)</li>
 477  
      *   <li>#CCCCCC (gray)</li>
 478  
      *   <li>#00FF00A0 (semi transparent green)</li>
 479  
      * </ul>
 480  
      *
 481  
      * @param value the value to convert
 482  
      * @throws ConversionException thrown if the value cannot be converted to a Color
 483  
      */
 484  
     public static Color toColor(Object value) throws ConversionException
 485  
     {
 486  713
         if (value instanceof Color)
 487  
         {
 488  322
             return (Color) value;
 489  
         }
 490  391
         else if (value instanceof String && !StringUtils.isBlank((String) value) && ((String) value).length() >= 6)
 491  
         {
 492  
             try
 493  
             {
 494  345
                 String color = ((String) value).trim();
 495  
 
 496  
                 // remove the leading #
 497  345
                 if (color.startsWith("#"))
 498  
                 {
 499  46
                     color = color.substring(1);
 500  
                 }
 501  
 
 502  345
                 int red = Integer.parseInt(color.substring(0, 2), 16);
 503  299
                 int green = Integer.parseInt(color.substring(2, 4), 16);
 504  299
                 int blue = Integer.parseInt(color.substring(4, 6), 16);
 505  299
                 int alpha = 255;
 506  
 
 507  
                 // parse the transparency
 508  299
                 if (color.length() >= 8)
 509  
                 {
 510  23
                     alpha = Integer.parseInt(color.substring(6, 8), 16);
 511  
                 }
 512  
 
 513  299
                 return new Color(red, green, blue, alpha);
 514  
             }
 515  46
             catch (Exception e)
 516  
             {
 517  46
                 throw new ConversionException("The value " + value + " can't be converted to a Color", e);
 518  
             }
 519  
         }
 520  
         else
 521  
         {
 522  46
             throw new ConversionException("The value " + value + " can't be converted to a Color");
 523  
         }
 524  
     }
 525  
 
 526  
     /**
 527  
      * Convert the specified object into a Calendar.
 528  
      *
 529  
      * @param value  the value to convert
 530  
      * @param format the DateFormat pattern to parse String values
 531  
      * @throws ConversionException thrown if the value cannot be converted to a Calendar
 532  
      */
 533  
     public static Date toDate(Object value, String format) throws ConversionException
 534  
     {
 535  805
         if (value instanceof Date)
 536  
         {
 537  345
             return (Date) value;
 538  
         }
 539  460
         else if (value instanceof Calendar)
 540  
         {
 541  115
             return ((Calendar) value).getTime();
 542  
         }
 543  345
         else if (value instanceof String)
 544  
         {
 545  
             try
 546  
             {
 547  299
                 return new SimpleDateFormat(format).parse((String) value);
 548  
             }
 549  46
             catch (ParseException e)
 550  
             {
 551  46
                 throw new ConversionException("The value " + value + " can't be converted to a Date", e);
 552  
             }
 553  
         }
 554  
         else
 555  
         {
 556  46
             throw new ConversionException("The value " + value + " can't be converted to a Date");
 557  
         }
 558  
     }
 559  
 
 560  
     /**
 561  
      * Convert the specified object into a Calendar.
 562  
      *
 563  
      * @param value  the value to convert
 564  
      * @param format the DateFormat pattern to parse String values
 565  
      * @throws ConversionException thrown if the value cannot be converted to a Calendar
 566  
      */
 567  
     public static Calendar toCalendar(Object value, String format) throws ConversionException
 568  
     {
 569  805
         if (value instanceof Calendar)
 570  
         {
 571  230
             return (Calendar) value;
 572  
         }
 573  575
         else if (value instanceof Date)
 574  
         {
 575  230
             Calendar calendar = Calendar.getInstance();
 576  230
             calendar.setTime((Date) value);
 577  230
             return calendar;
 578  
         }
 579  345
         else if (value instanceof String)
 580  
         {
 581  
             try
 582  
             {
 583  299
                 Calendar calendar = Calendar.getInstance();
 584  299
                 calendar.setTime(new SimpleDateFormat(format).parse((String) value));
 585  253
                 return calendar;
 586  
             }
 587  46
             catch (ParseException e)
 588  
             {
 589  46
                 throw new ConversionException("The value " + value + " can't be converted to a Calendar", e);
 590  
             }
 591  
         }
 592  
         else
 593  
         {
 594  46
             throw new ConversionException("The value " + value + " can't be converted to a Calendar");
 595  
         }
 596  
     }
 597  
 
 598  
     /**
 599  
      * Return an iterator over the simple values of a composite value. The value
 600  
      * specified is handled depending on its type:
 601  
      * <ul>
 602  
      *   <li>Strings are checked for delimiter characters and splitted if necessary.</li>
 603  
      *   <li>For collections the single elements are checked.</li>
 604  
      *   <li>Arrays are treated like collections.</li>
 605  
      *   <li>All other types are directly inserted.</li>
 606  
      *   <li>Recursive combinations are supported, e.g. a collection containing array that contain strings.</li>
 607  
      * </ul>
 608  
      *
 609  
      * @param value     the value to "split"
 610  
      * @param delimiter the delimiter for String values
 611  
      */
 612  
     public static Iterator toIterator(Object value, char delimiter)
 613  
     {
 614  571025
         if (value == null)
 615  
         {
 616  69
             return IteratorUtils.emptyIterator();
 617  
         }
 618  570956
         if (value instanceof String)
 619  
         {
 620  279845
             String s = (String) value;
 621  279845
             if (s.indexOf(delimiter) > 0)
 622  
             {
 623  20444
                 return split((String) value, delimiter).iterator();
 624  
             }
 625  
             else
 626  
             {
 627  259401
                 return new SingletonIterator(value);
 628  
             }
 629  
         }
 630  291111
         else if (value instanceof Collection)
 631  
         {
 632  11983
             return toIterator(((Collection) value).iterator(), delimiter);
 633  
         }
 634  279128
         else if (value.getClass().isArray())
 635  
         {
 636  18101
             return toIterator(IteratorUtils.arrayIterator(value), delimiter);
 637  
         }
 638  261027
         else if (value instanceof Iterator)
 639  
         {
 640  30084
             Iterator iterator = (Iterator) value;
 641  30084
             IteratorChain chain = new IteratorChain();
 642  93472
             while (iterator.hasNext())
 643  
             {
 644  63388
                 chain.addIterator(toIterator(iterator.next(), delimiter));
 645  
             }
 646  30084
             return chain;
 647  
         }
 648  
         else
 649  
         {
 650  230943
             return new SingletonIterator(value);
 651  
         }
 652  
     }
 653  
 
 654  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.