001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.betwixt.strategy;
019
020 import org.apache.commons.betwixt.ElementDescriptor;
021 import org.apache.commons.betwixt.io.read.ArrayBindAction;
022 import org.apache.commons.betwixt.io.read.BeanBindAction;
023 import org.apache.commons.betwixt.io.read.MappingAction;
024 import org.apache.commons.betwixt.io.read.ReadContext;
025 import org.apache.commons.betwixt.io.read.SimpleTypeBindAction;
026 import org.xml.sax.Attributes;
027
028 /**
029 * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
030 * @version $Revision: 561314 $
031 */
032 public class DefaultActionMappingStrategy extends ActionMappingStrategy {
033
034 /**
035 * Gets the mapping action to map the given element.
036 * @param namespace not null
037 * @param name not null
038 * @param attributes <code>Attributes</code>, not null
039 * @param context <code>ReadContext</code>, not null
040 * @return <code>MappingAction</code>, not null
041 * @throws Exception
042 */
043 public MappingAction getMappingAction(
044 String namespace,
045 String name,
046 Attributes attributes,
047 ReadContext context)
048 throws Exception {
049 MappingAction result = MappingAction.EMPTY;
050
051 ElementDescriptor activeDescriptor = context.getCurrentDescriptor();
052 if (activeDescriptor != null) {
053 if (activeDescriptor.isHollow()) {
054 if (isArrayDescriptor(activeDescriptor)) {
055 result = ArrayBindAction.createMappingAction(activeDescriptor);
056 } else {
057 result = BeanBindAction.INSTANCE;
058 }
059 }
060 else if (activeDescriptor.isSimple())
061 {
062 result = SimpleTypeBindAction.INSTANCE;
063 }
064 else
065 {
066 ElementDescriptor[] descriptors
067 = activeDescriptor.getElementDescriptors();
068 if (descriptors.length == 1) {
069 ElementDescriptor childDescriptor = descriptors[0];
070 if (childDescriptor.isHollow()
071 && isArrayDescriptor(childDescriptor)) {
072 result = ArrayBindAction.createMappingAction(childDescriptor);
073 }
074 }
075 }
076 }
077 return result;
078 }
079
080 /**
081 * Is the give
082 * @param descriptor <code>ElementDescriptor</code>, possibly null
083 * @return true if the descriptor describes an array property, if null returns false
084 */
085 private boolean isArrayDescriptor(ElementDescriptor descriptor) {
086 boolean result = false;
087 if (descriptor != null) {
088 Class propertyType = descriptor.getPropertyType();
089 if (propertyType != null) {
090 result = propertyType.isArray();
091 }
092 }
093 return result;
094 }
095 }