1. /*
  2. * Copyright 1999-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. package org.apache.commons.jxpath;
  17. import java.util.Iterator;
  18. import java.util.Map;
  19. import java.util.Set;
  20. /**
  21. * Implements the DynamicPropertyHandler interface for java.util.Map.
  22. *
  23. * @author Dmitri Plotnikov
  24. * @version $Revision: 1.7 $ $Date: 2004/03/02 01:03:14 $
  25. */
  26. public class MapDynamicPropertyHandler implements DynamicPropertyHandler {
  27. private static final String[] STRING_ARRAY = new String[0];
  28. /**
  29. * Returns string representations of all keys in the map.
  30. */
  31. public String[] getPropertyNames(Object object) {
  32. Map map = (Map) object;
  33. Set set = map.keySet();
  34. String names[] = new String[set.size()];
  35. Iterator it = set.iterator();
  36. for (int i = 0; i < names.length; i++) {
  37. names[i] = String.valueOf(it.next());
  38. }
  39. return names;
  40. }
  41. /**
  42. * Returns the value for the specified key.
  43. */
  44. public Object getProperty(Object object, String propertyName) {
  45. return ((Map) object).get(propertyName);
  46. }
  47. /**
  48. * Sets the specified key value.
  49. */
  50. public void setProperty(Object object, String propertyName, Object value) {
  51. ((Map) object).put(propertyName, value);
  52. }
  53. }