View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.commons.betwixt.strategy;
19  
20  import org.apache.commons.betwixt.ElementDescriptor;
21  import org.apache.commons.betwixt.io.read.ArrayBindAction;
22  import org.apache.commons.betwixt.io.read.BeanBindAction;
23  import org.apache.commons.betwixt.io.read.MappingAction;
24  import org.apache.commons.betwixt.io.read.ReadContext;
25  import org.apache.commons.betwixt.io.read.SimpleTypeBindAction;
26  import org.xml.sax.Attributes;
27  
28  /**
29   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
30   * @version $Revision: 561314 $
31   */
32  public class DefaultActionMappingStrategy extends ActionMappingStrategy {
33      
34      /**
35       * Gets the mapping action to map the given element.
36       * @param namespace not null
37       * @param name not null
38       * @param attributes <code>Attributes</code>, not null
39       * @param context <code>ReadContext</code>, not null
40       * @return <code>MappingAction</code>, not null
41       * @throws Exception
42       */
43      public MappingAction getMappingAction(    
44                              String namespace,
45                              String name,
46                              Attributes attributes,
47                              ReadContext context)
48          throws Exception {
49          MappingAction result = MappingAction.EMPTY;
50              
51          ElementDescriptor activeDescriptor = context.getCurrentDescriptor();
52          if (activeDescriptor != null) {
53              if (activeDescriptor.isHollow()) {
54                  if (isArrayDescriptor(activeDescriptor)) {
55                      result = ArrayBindAction.createMappingAction(activeDescriptor);
56                  } else {
57                      result = BeanBindAction.INSTANCE;
58                  }
59              }
60              else if (activeDescriptor.isSimple())
61              {
62                  result = SimpleTypeBindAction.INSTANCE;
63              }
64              else
65              {
66                  ElementDescriptor[] descriptors 
67                      = activeDescriptor.getElementDescriptors();
68                  if (descriptors.length == 1) {
69                      ElementDescriptor childDescriptor = descriptors[0];
70                      if (childDescriptor.isHollow() 
71                              && isArrayDescriptor(childDescriptor)) {
72                          result = ArrayBindAction.createMappingAction(childDescriptor);
73                      }
74                  }
75              }
76          }
77          return result;
78      }
79      
80      /**
81       * Is the give
82       * @param descriptor <code>ElementDescriptor</code>, possibly null
83       * @return true if the descriptor describes an array property, if null returns false
84       */
85      private boolean isArrayDescriptor(ElementDescriptor descriptor) {
86          boolean result = false;
87          if (descriptor != null) {
88              Class propertyType = descriptor.getPropertyType();
89              if (propertyType != null) {
90                  result = propertyType.isArray();
91              }
92          }
93          return result;
94      }
95  }