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.lang.reflect.Field;
040: import java.lang.reflect.Method;
041: import java.lang.reflect.Type;
042: import java.util.Map;
043:
044: import javax.activation.MimeType;
045:
046: import com.sun.xml.bind.WhiteSpaceProcessor;
047: import com.sun.xml.bind.api.AccessorException;
048: import com.sun.xml.bind.v2.model.annotation.Locatable;
049: import com.sun.xml.bind.v2.model.annotation.RuntimeAnnotationReader;
050: import com.sun.xml.bind.v2.model.core.ID;
051: import com.sun.xml.bind.v2.model.nav.Navigator;
052: import com.sun.xml.bind.v2.model.nav.ReflectionNavigator;
053: import com.sun.xml.bind.v2.model.runtime.RuntimeNonElement;
054: import com.sun.xml.bind.v2.model.runtime.RuntimeNonElementRef;
055: import com.sun.xml.bind.v2.model.runtime.RuntimePropertyInfo;
056: import com.sun.xml.bind.v2.model.runtime.RuntimeTypeInfoSet;
057: import com.sun.xml.bind.v2.runtime.FilterTransducer;
058: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
059: import com.sun.xml.bind.v2.runtime.InlineBinaryTransducer;
060: import com.sun.xml.bind.v2.runtime.MimeTypedTransducer;
061: import com.sun.xml.bind.v2.runtime.SchemaTypeTransducer;
062: import com.sun.xml.bind.v2.runtime.Transducer;
063: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
064: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
065: import com.sun.istack.Nullable;
066:
067: import org.xml.sax.SAXException;
068:
069: /**
070: * {@link ModelBuilder} that works at the run-time by using
071: * the {@code java.lang.reflect} package.
072: *
073: * <p>
074: * This extends {@link ModelBuilder} by providing more functionalities such
075: * as accessing the fields and classes.
076: *
077: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
078: */
079: public class RuntimeModelBuilder extends
080: ModelBuilder<Type, Class, Field, Method> {
081: /**
082: * The {@link JAXBContextImpl} for which the model is built.
083: * Null when created for reflection.
084: */
085: public final @Nullable
086: JAXBContextImpl context;
087:
088: public RuntimeModelBuilder(JAXBContextImpl context,
089: RuntimeAnnotationReader annotationReader,
090: Map<Class, Class> subclassReplacements,
091: String defaultNamespaceRemap) {
092: super (annotationReader, Navigator.REFLECTION,
093: subclassReplacements, defaultNamespaceRemap);
094: this .context = context;
095: }
096:
097: @Override
098: public RuntimeNonElement getClassInfo(Class clazz,
099: Locatable upstream) {
100: return (RuntimeNonElement) super .getClassInfo(clazz, upstream);
101: }
102:
103: @Override
104: public RuntimeNonElement getClassInfo(Class clazz,
105: boolean searchForSuperClass, Locatable upstream) {
106: return (RuntimeNonElement) super .getClassInfo(clazz,
107: searchForSuperClass, upstream);
108: }
109:
110: @Override
111: protected RuntimeEnumLeafInfoImpl createEnumLeafInfo(Class clazz,
112: Locatable upstream) {
113: return new RuntimeEnumLeafInfoImpl(this , upstream, clazz);
114: }
115:
116: @Override
117: protected RuntimeClassInfoImpl createClassInfo(Class clazz,
118: Locatable upstream) {
119: return new RuntimeClassInfoImpl(this , upstream, clazz);
120: }
121:
122: @Override
123: public RuntimeElementInfoImpl createElementInfo(
124: RegistryInfoImpl<Type, Class, Field, Method> registryInfo,
125: Method method) throws IllegalAnnotationException {
126: return new RuntimeElementInfoImpl(this , registryInfo, method);
127: }
128:
129: @Override
130: public RuntimeArrayInfoImpl createArrayInfo(Locatable upstream,
131: Type arrayType) {
132: return new RuntimeArrayInfoImpl(this , upstream,
133: (Class) arrayType);
134: }
135:
136: public ReflectionNavigator getNavigator() {
137: return (ReflectionNavigator) nav;
138: }
139:
140: @Override
141: protected RuntimeTypeInfoSetImpl createTypeInfoSet() {
142: return new RuntimeTypeInfoSetImpl(reader);
143: }
144:
145: @Override
146: public RuntimeTypeInfoSet link() {
147: return (RuntimeTypeInfoSet) super .link();
148: }
149:
150: /**
151: * Creates a {@link Transducer} given a reference.
152: *
153: * Used to implement {@link RuntimeNonElementRef#getTransducer()}.
154: * Shouldn't be called from anywhere else.
155: *
156: * TODO: this is not the proper place for this class to be in.
157: */
158: public static Transducer createTransducer(RuntimeNonElementRef ref) {
159: Transducer t = ref.getTarget().getTransducer();
160: RuntimePropertyInfo src = ref.getSource();
161: ID id = src.id();
162:
163: if (id == ID.IDREF)
164: return RuntimeBuiltinLeafInfoImpl.STRING;
165:
166: if (id == ID.ID)
167: t = new IDTransducerImpl(t);
168:
169: MimeType emt = src.getExpectedMimeType();
170: if (emt != null)
171: t = new MimeTypedTransducer(t, emt);
172:
173: if (src.inlineBinaryData())
174: t = new InlineBinaryTransducer(t);
175:
176: if (src.getSchemaType() != null)
177: t = new SchemaTypeTransducer(t, src.getSchemaType());
178:
179: return t;
180: }
181:
182: /**
183: * Transducer implementation for ID.
184: *
185: * This transducer wraps another {@link Transducer} and adds
186: * handling for ID.
187: */
188: private static final class IDTransducerImpl<ValueT> extends
189: FilterTransducer<ValueT> {
190: public IDTransducerImpl(Transducer<ValueT> core) {
191: super (core);
192: }
193:
194: @Override
195: public ValueT parse(CharSequence lexical)
196: throws AccessorException, SAXException {
197: String value = WhiteSpaceProcessor.trim(lexical).toString();
198: UnmarshallingContext.getInstance().addToIdTable(value);
199: return core.parse(value);
200: }
201: }
202: }
|