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: */package org.apache.geronimo.kernel.config.xstream;
17:
18: import java.lang.reflect.Method;
19:
20: import com.thoughtworks.xstream.converters.ConversionException;
21: import com.thoughtworks.xstream.converters.Converter;
22: import com.thoughtworks.xstream.converters.MarshallingContext;
23: import com.thoughtworks.xstream.converters.UnmarshallingContext;
24: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
25: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
26:
27: /**
28: * @version $Rev: 549825 $ $Date: 2007-06-22 07:17:31 -0700 (Fri, 22 Jun 2007) $
29: */
30: public class QNameConverter implements Converter {
31: private static final String QNAME_CLASS = "javax.xml.namespace.QName";
32:
33: public boolean canConvert(Class clazz) {
34: return QNAME_CLASS.equals(clazz.getName());
35: }
36:
37: public void marshal(Object object, HierarchicalStreamWriter writer,
38: MarshallingContext marshallingContext) {
39: writer.setValue(object.toString());
40: }
41:
42: public Object unmarshal(HierarchicalStreamReader reader,
43: UnmarshallingContext unmarshallingContext) {
44: Class qnameClass = unmarshallingContext.getRequiredType();
45: if (!canConvert(qnameClass)) {
46: throw new ConversionException(
47: "Unexpected type in unmarshal: "
48: + qnameClass.getName());
49: }
50:
51: String qnameString = reader.getValue();
52: try {
53: Method method = qnameClass.getMethod("valueOf",
54: new Class[] { String.class });
55: Object qname = method.invoke(null,
56: new Object[] { qnameString });
57: return qname;
58: } catch (Exception e) {
59: throw new ConversionException(
60: "Unable to convert value to a qname: "
61: + qnameString, e);
62: }
63: }
64: }
|