001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.type.java5;
019:
020: import java.beans.PropertyDescriptor;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.apache.cxf.aegis.type.Type;
025: import org.apache.cxf.aegis.type.TypeMapping;
026: import org.apache.cxf.aegis.type.basic.BeanTypeInfo;
027: import org.apache.cxf.aegis.util.NamespaceHelper;
028:
029: public class AnnotatedTypeInfo extends BeanTypeInfo {
030: public AnnotatedTypeInfo(TypeMapping tm, Class typeClass, String ns) {
031: super (typeClass, ns);
032: setTypeMapping(tm);
033: initialize();
034: }
035:
036: /**
037: * Override from parent in order to check for IgnoreProperty annotation.
038: */
039: protected void mapProperty(PropertyDescriptor pd) {
040: if (isIgnored(pd)) {
041: return; // do not map ignored properties
042: }
043:
044: String name = pd.getName();
045: if (isAttribute(pd)) {
046: mapAttribute(name, createMappedName(pd));
047: } else if (isElement(pd)) {
048: mapElement(name, createMappedName(pd));
049: }
050: }
051:
052: @Override
053: protected boolean registerType(PropertyDescriptor desc) {
054: XmlAttribute att = desc.getReadMethod().getAnnotation(
055: XmlAttribute.class);
056: if (att != null && att.type() != Type.class) {
057: return false;
058: }
059:
060: XmlElement el = desc.getReadMethod().getAnnotation(
061: XmlElement.class);
062: if (el != null && el.type() != Type.class) {
063: return false;
064: }
065:
066: return super .registerType(desc);
067: }
068:
069: protected boolean isIgnored(PropertyDescriptor desc) {
070: return desc.getReadMethod().isAnnotationPresent(
071: IgnoreProperty.class);
072: }
073:
074: protected boolean isAttribute(PropertyDescriptor desc) {
075: return desc.getReadMethod().isAnnotationPresent(
076: XmlAttribute.class);
077: }
078:
079: protected boolean isElement(PropertyDescriptor desc) {
080: return !isAttribute(desc);
081: }
082:
083: protected boolean isAnnotatedElement(PropertyDescriptor desc) {
084: return desc.getReadMethod().isAnnotationPresent(
085: XmlElement.class);
086: }
087:
088: @Override
089: protected QName createMappedName(PropertyDescriptor desc) {
090: return createQName(desc);
091: }
092:
093: protected QName createQName(PropertyDescriptor desc) {
094: String name = null;
095: String ns = null;
096:
097: XmlType xtype = (XmlType) getTypeClass().getAnnotation(
098: XmlType.class);
099: if (xtype != null) {
100: ns = xtype.namespace();
101: }
102:
103: if (isAttribute(desc)) {
104: XmlAttribute att = desc.getReadMethod().getAnnotation(
105: XmlAttribute.class);
106: name = att.name();
107: if (att.namespace().length() > 0) {
108: ns = att.namespace();
109: }
110: } else if (isAnnotatedElement(desc)) {
111: XmlElement att = desc.getReadMethod().getAnnotation(
112: XmlElement.class);
113: name = att.name();
114: if (att.namespace().length() > 0) {
115: ns = att.namespace();
116: }
117: }
118:
119: if (name == null || name.length() == 0) {
120: name = desc.getName();
121: }
122:
123: if (ns == null || ns.length() == 0) {
124: ns = NamespaceHelper.makeNamespaceFromClassName(
125: getTypeClass().getName(), "http");
126: }
127:
128: return new QName(ns, name);
129: }
130:
131: public boolean isNillable(QName name) {
132: PropertyDescriptor desc = getPropertyDescriptorFromMappedName(name);
133:
134: if (isAnnotatedElement(desc)) {
135: return desc.getReadMethod().getAnnotation(XmlElement.class)
136: .nillable();
137: } else {
138: return super .isNillable(name);
139: }
140: }
141:
142: public int getMinOccurs(QName name) {
143: PropertyDescriptor desc = getPropertyDescriptorFromMappedName(name);
144: if (isAnnotatedElement(desc)) {
145: XmlElement att = desc.getReadMethod().getAnnotation(
146: XmlElement.class);
147: String minOccurs = att.minOccurs();
148: if (minOccurs != null && minOccurs.length() > 0) {
149: return Integer.parseInt(minOccurs);
150: }
151: }
152: return super.getMinOccurs(name);
153: }
154: }
|