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 org.compass.core.Resource;
20: import org.compass.core.converter.ConversionException;
21: import org.compass.core.converter.Converter;
22: import org.compass.core.converter.DelegateConverter;
23: import org.compass.core.converter.ResourcePropertyConverter;
24: import org.compass.core.converter.basic.FormatConverter;
25: import org.compass.core.mapping.Mapping;
26: import org.compass.core.mapping.ResourcePropertyMapping;
27: import org.compass.core.marshall.MarshallingContext;
28:
29: /**
30: * A metadata based format converter, that creates its own intenral format converter based on the format
31: * that it is initalized with.
32: *
33: * @author kimchy
34: */
35: public class MetaDataFormatDelegateConverter implements
36: DelegateConverter, ResourcePropertyConverter {
37:
38: private FormatConverter delegatedConverter;
39:
40: private String format;
41:
42: public MetaDataFormatDelegateConverter(String format) {
43: this .format = format;
44: }
45:
46: /**
47: * Sets a delegated format converter. Will not use the actual passed converter,
48: * but will use {@link org.compass.core.converter.basic.FormatConverter#copy()} to create a new format converter.
49: */
50: public void setDelegatedConverter(Converter delegatedConverter) {
51: if (!(delegatedConverter instanceof FormatConverter)) {
52: throw new IllegalArgumentException(
53: "Meta data format attribute can only work on format converters. The "
54: + "converter ["
55: + delegatedConverter.getClass().getName()
56: + "] is not one.");
57: }
58: this .delegatedConverter = ((FormatConverter) delegatedConverter)
59: .copy();
60: this .delegatedConverter.setFormat(format);
61: }
62:
63: public boolean marshall(Resource resource, Object root,
64: Mapping mapping, MarshallingContext context)
65: throws ConversionException {
66: return delegatedConverter.marshall(resource, root, mapping,
67: context);
68: }
69:
70: public Object unmarshall(Resource resource, Mapping mapping,
71: MarshallingContext context) throws ConversionException {
72: return delegatedConverter
73: .unmarshall(resource, mapping, context);
74: }
75:
76: public Object fromString(String str,
77: ResourcePropertyMapping resourcePropertyMapping)
78: throws ConversionException {
79: return delegatedConverter.fromString(str,
80: resourcePropertyMapping);
81: }
82:
83: public String toString(Object o,
84: ResourcePropertyMapping resourcePropertyMapping)
85: throws ConversionException {
86: return delegatedConverter.toString(o, resourcePropertyMapping);
87: }
88:
89: public boolean canNormalize() {
90: return delegatedConverter.canNormalize();
91: }
92: }
|