View Javadoc

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