01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.compass.core.converter.mapping.osem;
18:
19: import java.lang.reflect.Array;
20:
21: import org.compass.core.Resource;
22: import org.compass.core.accessor.Getter;
23: import org.compass.core.mapping.Mapping;
24: import org.compass.core.mapping.osem.AbstractCollectionMapping;
25: import org.compass.core.marshall.MarshallingContext;
26: import org.compass.core.marshall.MarshallingEnvironment;
27:
28: /**
29: * @author kimchy
30: */
31: public class ArrayMappingConverter extends
32: AbstractCollectionMappingConverter {
33:
34: protected int marshallIterateData(Object root,
35: AbstractCollectionMapping colMapping, Resource resource,
36: MarshallingContext context) {
37: Object current = context
38: .getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
39: int count = 0;
40: int size = Array.getLength(root);
41: Mapping elementMapping = colMapping.getElementMapping();
42: for (int i = 0; i < size; i++) {
43: Object value = Array.get(root, i);
44: if (value == null) {
45: continue;
46: }
47: context.setAttribute(
48: MarshallingEnvironment.ATTRIBUTE_CURRENT, current);
49: boolean stored = elementMapping.getConverter().marshall(
50: resource, value, elementMapping, context);
51: if (stored) {
52: count++;
53: }
54: }
55: return count;
56: }
57:
58: protected AbstractCollectionMapping.CollectionType getRuntimeCollectionType(
59: Object root) {
60: throw new IllegalStateException(
61: "Should not be called, internal compass error");
62: }
63:
64: protected Object createColObject(Getter getter,
65: AbstractCollectionMapping.CollectionType collectionType,
66: int size) {
67: return Array.newInstance(getter.getReturnType()
68: .getComponentType(), size);
69: }
70:
71: protected void addValue(Object col, int index, Object value) {
72: Array.set(col, index, value);
73: }
74: }
|