01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.aegis.type.java5;
19:
20: import org.apache.cxf.aegis.Context;
21: import org.apache.cxf.aegis.DatabindingException;
22: import org.apache.cxf.aegis.type.Type;
23: import org.apache.cxf.aegis.util.XmlConstants;
24: import org.apache.cxf.aegis.xml.MessageReader;
25: import org.apache.cxf.aegis.xml.MessageWriter;
26: import org.jdom.Attribute;
27: import org.jdom.Element;
28: import org.jdom.Namespace;
29:
30: public class EnumType extends Type {
31: @SuppressWarnings("unchecked")
32: @Override
33: public Object readObject(MessageReader reader, Context context) {
34: String value = reader.getValue();
35:
36: return Enum.valueOf(getTypeClass(), value);
37: }
38:
39: @Override
40: public void writeObject(Object object, MessageWriter writer,
41: Context context) {
42: writer.writeValue(((Enum) object).toString());
43: }
44:
45: @Override
46: public void setTypeClass(Class typeClass) {
47: if (!typeClass.isEnum()) {
48: throw new DatabindingException(
49: "Type class must be an enum.");
50: }
51:
52: super .setTypeClass(typeClass);
53: }
54:
55: @Override
56: public void writeSchema(Element root) {
57: Namespace xsd = Namespace.getNamespace(XmlConstants.XSD_PREFIX,
58: XmlConstants.XSD);
59:
60: Element simple = new Element("simpleType", xsd);
61: simple.setAttribute(new Attribute("name", getSchemaType()
62: .getLocalPart()));
63: root.addContent(simple);
64:
65: Element restriction = new Element("restriction", xsd);
66: restriction.setAttribute(new Attribute("base",
67: XmlConstants.XSD_PREFIX + ":string"));
68: simple.addContent(restriction);
69:
70: Object[] constants = getTypeClass().getEnumConstants();
71:
72: for (Object constant : constants) {
73: Element enumeration = new Element("enumeration", xsd);
74: enumeration.setAttribute(new Attribute("value",
75: ((Enum) constant).toString()));
76: restriction.addContent(enumeration);
77: }
78: }
79:
80: @Override
81: public boolean isComplex() {
82: return true;
83: }
84: }
|