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.util.ArrayList;
20: import java.util.Collection;
21: import java.util.HashSet;
22: import java.util.Iterator;
23: import java.util.List;
24: import java.util.Set;
25: import java.util.SortedSet;
26: import java.util.TreeSet;
27:
28: import org.compass.core.Resource;
29: import org.compass.core.accessor.Getter;
30: import org.compass.core.mapping.Mapping;
31: import org.compass.core.mapping.osem.AbstractCollectionMapping;
32: import org.compass.core.marshall.MarshallingContext;
33: import org.compass.core.marshall.MarshallingEnvironment;
34:
35: /**
36: * @author kimchy
37: */
38: public class CollectionMappingConverter extends
39: AbstractCollectionMappingConverter {
40:
41: protected int marshallIterateData(Object root,
42: AbstractCollectionMapping colMapping, Resource resource,
43: MarshallingContext context) {
44: Object current = context
45: .getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
46: int count = 0;
47: Mapping elementMapping = colMapping.getElementMapping();
48: Collection col = (Collection) root;
49: for (Iterator it = col.iterator(); it.hasNext();) {
50: Object value = it.next();
51: if (value == null) {
52: continue;
53: }
54: context.setAttribute(
55: MarshallingEnvironment.ATTRIBUTE_CURRENT, current);
56: boolean stored = elementMapping.getConverter().marshall(
57: resource, value, elementMapping, context);
58: if (stored) {
59: count++;
60: }
61: }
62: return count;
63: }
64:
65: protected AbstractCollectionMapping.CollectionType getRuntimeCollectionType(
66: Object root) {
67: if (root instanceof List) {
68: return AbstractCollectionMapping.CollectionType.LIST;
69: } else if (root instanceof SortedSet) {
70: return AbstractCollectionMapping.CollectionType.SORTED_SET;
71: } else if (root instanceof Set) {
72: return AbstractCollectionMapping.CollectionType.SET;
73: } else {
74: throw new IllegalStateException(
75: "Compass does not support collection class ["
76: + root.getClass().getName()
77: + "], please consider using either List or Set implementations");
78: }
79: }
80:
81: protected Object createColObject(Getter getter,
82: AbstractCollectionMapping.CollectionType collectionType,
83: int size) {
84: if (collectionType == AbstractCollectionMapping.CollectionType.LIST) {
85: return new ArrayList(size);
86: } else if (collectionType == AbstractCollectionMapping.CollectionType.SET) {
87: return new HashSet(size);
88: } else if (collectionType == AbstractCollectionMapping.CollectionType.SORTED_SET) {
89: return new TreeSet();
90: } else {
91: throw new IllegalStateException(
92: "Should not happen, internal compass error");
93: }
94: }
95:
96: protected void addValue(Object col, int index, Object value) {
97: ((Collection) col).add(value);
98: }
99: }
|