/plugins/XSLT/tags/xslt_0_5_2/xslt/PropertyUtil.java

# · Java · 82 lines · 27 code · 8 blank · 47 comment · 4 complexity · 59e5a77547e4f529c9ab8d60f56a758a MD5 · raw file

  1. /*
  2. * PropertyUtil.java - Utility methods for accessing properties
  3. *
  4. * Copyright (c) 2002 Greg Merrill
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package xslt;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Properties;
  24. /**
  25. * Utility methods for accessing properties.
  26. */
  27. public class PropertyUtil {
  28. /**
  29. * Returns a List of property values whose keys are identical excepct for
  30. * a trailing "." and index.
  31. *
  32. * @param key key of the enumerated property, excluding the trailing "." and
  33. * index. I.e., if you have properties "file.0=file0.txt" and
  34. * "file.1=file1.txt", calling getEnumeratedProperty("file", properties)
  35. * would return a list containing "file0.txt" and "file1.txt".
  36. * @param properties Properties object from which the enumerated property
  37. * should be retrieved
  38. */
  39. public static List getEnumeratedProperty (String key, Properties properties) {
  40. List values = new ArrayList();
  41. int i = 0;
  42. String value;
  43. while ((value = properties.getProperty(calculateKey(key, i++))) != null) {
  44. values.add(value);
  45. }
  46. return values;
  47. }
  48. /**
  49. * Sets a series of property values whose keys are identical excepct for
  50. * a trailing "." and index.
  51. *
  52. * @param key key of the enumerated property (see
  53. * {@link #getEnumeratedProperty})
  54. * @param values values to be assigned to the enumerated property, in order.
  55. * All members of this List must be Strings.
  56. * @param properties Properties object from which the enumerated property
  57. * should be retrieved
  58. */
  59. public static void setEnumeratedProperty (String key, List values, Properties properties) {
  60. List currentValues = getEnumeratedProperty(key, properties);
  61. for (int i=0; i < currentValues.size(); i++) {
  62. properties.remove(calculateKey(key, i));
  63. }
  64. for (int i=0; i < values.size(); i++) {
  65. properties.setProperty(calculateKey(key, i), (String)values.get(i));
  66. }
  67. }
  68. /**
  69. * @return indexed property key
  70. */
  71. private static String calculateKey (String key, int index) {
  72. return key+"."+index;
  73. }
  74. }