01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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://jakarta.apache.org/'>Jakarta Commons Team</a>
30: * @version $Revision: 438373 $
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(String namespace,
44: String name, Attributes attributes, ReadContext context)
45: throws Exception {
46: MappingAction result = MappingAction.EMPTY;
47:
48: ElementDescriptor activeDescriptor = context
49: .getCurrentDescriptor();
50: if (activeDescriptor != null) {
51: if (activeDescriptor.isHollow()) {
52: if (isArrayDescriptor(activeDescriptor)) {
53: result = ArrayBindAction
54: .createMappingAction(activeDescriptor);
55: } else {
56: result = BeanBindAction.INSTANCE;
57: }
58: } else if (activeDescriptor.isSimple()) {
59: result = SimpleTypeBindAction.INSTANCE;
60: } else {
61: ElementDescriptor[] descriptors = activeDescriptor
62: .getElementDescriptors();
63: if (descriptors.length == 1) {
64: ElementDescriptor childDescriptor = descriptors[0];
65: if (childDescriptor.isHollow()
66: && isArrayDescriptor(childDescriptor)) {
67: result = ArrayBindAction
68: .createMappingAction(childDescriptor);
69: }
70: }
71: }
72: }
73: return result;
74: }
75:
76: /**
77: * Is the give
78: * @param descriptor <code>ElementDescriptor</code>, possibly null
79: * @return true if the descriptor describes an array property, if null returns false
80: */
81: private boolean isArrayDescriptor(ElementDescriptor descriptor) {
82: boolean result = false;
83: if (descriptor != null) {
84: Class propertyType = descriptor.getPropertyType();
85: if (propertyType != null) {
86: result = propertyType.isArray();
87: }
88: }
89: return result;
90: }
91: }
|