001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.impl;
038:
039: import java.util.Collections;
040: import java.util.List;
041:
042: import javax.xml.bind.annotation.XmlList;
043:
044: import com.sun.xml.bind.v2.model.core.ID;
045: import com.sun.xml.bind.v2.model.core.NonElement;
046: import com.sun.xml.bind.v2.model.core.PropertyInfo;
047: import com.sun.xml.bind.v2.model.runtime.RuntimeNonElementRef;
048: import com.sun.xml.bind.v2.model.runtime.RuntimePropertyInfo;
049: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
050: import com.sun.xml.bind.v2.runtime.Transducer;
051: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
052:
053: /**
054: * {@link PropertyInfoImpl} that can only have one type.
055: *
056: * Specifically, {@link AttributePropertyInfoImpl} and {@link ValuePropertyInfoImpl}.
057: *
058: * @author Kohsuke Kawaguchi
059: */
060: abstract class SingleTypePropertyInfoImpl<T, C, F, M> extends
061: PropertyInfoImpl<T, C, F, M> {
062:
063: /**
064: * Computed lazily.
065: *
066: * @see {@link #getTarget()}.
067: */
068: private NonElement<T, C> type;
069:
070: public SingleTypePropertyInfoImpl(
071: ClassInfoImpl<T, C, F, M> classInfo,
072: PropertySeed<T, C, F, M> seed) {
073: super (classInfo, seed);
074: if (this instanceof RuntimePropertyInfo) {
075: Accessor rawAcc = ((RuntimeClassInfoImpl.RuntimePropertySeed) seed)
076: .getAccessor();
077: if (getAdapter() != null && !isCollection())
078: // adapter for a single-value property is handled by accessor.
079: // adapter for a collection property is handled by lister.
080: rawAcc = rawAcc.adapt(((RuntimePropertyInfo) this )
081: .getAdapter());
082: this .acc = rawAcc;
083: } else
084: this .acc = null;
085: }
086:
087: public List<? extends NonElement<T, C>> ref() {
088: return Collections.singletonList(getTarget());
089: }
090:
091: public NonElement<T, C> getTarget() {
092: if (type == null) {
093: assert parent.builder != null : "this method must be called during the build stage";
094: type = parent.builder
095: .getTypeInfo(getIndividualType(), this );
096: }
097: return type;
098: }
099:
100: public PropertyInfo<T, C> getSource() {
101: return this ;
102: }
103:
104: public void link() {
105: super .link();
106:
107: if (!type.isSimpleType() && id() != ID.IDREF) {
108: parent.builder.reportError(new IllegalAnnotationException(
109: Messages.SIMPLE_TYPE_IS_REQUIRED.format(), seed));
110: }
111:
112: if (!isCollection() && seed.hasAnnotation(XmlList.class)) {
113: parent.builder
114: .reportError(new IllegalAnnotationException(
115: Messages.XMLLIST_ON_SINGLE_PROPERTY
116: .format(), this ));
117: }
118: }
119:
120: //
121: //
122: // technically these code belong to runtime implementation, but moving the code up here
123: // allows this to be shared between RuntimeValuePropertyInfoImpl and RuntimeAttributePropertyInfoImpl
124: //
125: //
126:
127: private final Accessor acc;
128: /**
129: * Lazily created.
130: */
131: private Transducer xducer;
132:
133: public Accessor getAccessor() {
134: return acc;
135: }
136:
137: public Transducer getTransducer() {
138: if (xducer == null) {
139: xducer = RuntimeModelBuilder
140: .createTransducer((RuntimeNonElementRef) this );
141: if (xducer == null) {
142: // this situation is checked by by the link method.
143: // avoid repeating the same error by silently recovering
144: xducer = RuntimeBuiltinLeafInfoImpl.STRING;
145: }
146: }
147: return xducer;
148: }
149: }
|