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. package org.apache.commons.betwixt.strategy;
  17. import org.apache.commons.betwixt.ElementDescriptor;
  18. import org.apache.commons.betwixt.io.read.ArrayBindAction;
  19. import org.apache.commons.betwixt.io.read.BeanBindAction;
  20. import org.apache.commons.betwixt.io.read.MappingAction;
  21. import org.apache.commons.betwixt.io.read.ReadContext;
  22. import org.apache.commons.betwixt.io.read.SimpleTypeBindAction;
  23. import org.xml.sax.Attributes;
  24. /**
  25. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  26. * @version $Revision: 1.2 $
  27. */
  28. public class DefaultActionMappingStrategy extends ActionMappingStrategy {
  29. /**
  30. * Gets the mapping action to map the given element.
  31. * @param namespace not null
  32. * @param name not null
  33. * @param attributes <code>Attributes</code>, not null
  34. * @param context <code>ReadContext</code>, not null
  35. * @return <code>MappingAction</code>, not null
  36. * @throws Exception
  37. */
  38. public MappingAction getMappingAction(
  39. String namespace,
  40. String name,
  41. Attributes attributes,
  42. ReadContext context)
  43. throws Exception {
  44. MappingAction result = MappingAction.EMPTY;
  45. ElementDescriptor activeDescriptor = context.getCurrentDescriptor();
  46. if (activeDescriptor != null) {
  47. if (activeDescriptor.isHollow()) {
  48. if (isArrayDescriptor(activeDescriptor)) {
  49. result = ArrayBindAction.createMappingAction(activeDescriptor);
  50. } else {
  51. result = BeanBindAction.INSTANCE;
  52. }
  53. }
  54. else if (activeDescriptor.isSimple())
  55. {
  56. result = SimpleTypeBindAction.INSTANCE;
  57. }
  58. else
  59. {
  60. ElementDescriptor[] descriptors
  61. = activeDescriptor.getElementDescriptors();
  62. if (descriptors.length == 1) {
  63. ElementDescriptor childDescriptor = descriptors[0];
  64. if (childDescriptor.isHollow()
  65. && isArrayDescriptor(childDescriptor)) {
  66. result = ArrayBindAction.createMappingAction(childDescriptor);
  67. }
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. /**
  74. * Is the give
  75. * @param descriptor <code>ElementDescriptor</code>, possibly null
  76. * @return true if the descriptor describes an array property, if null returns false
  77. */
  78. private boolean isArrayDescriptor(ElementDescriptor descriptor) {
  79. boolean result = false;
  80. if (descriptor != null) {
  81. Class propertyType = descriptor.getPropertyType();
  82. if (propertyType != null) {
  83. result = propertyType.isArray();
  84. }
  85. }
  86. return result;
  87. }
  88. }