001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.model.impl;
027:
028: import java.lang.reflect.Field;
029: import java.lang.reflect.Method;
030: import java.lang.reflect.Type;
031:
032: import javax.activation.MimeType;
033:
034: import com.sun.xml.internal.bind.WhiteSpaceProcessor;
035: import com.sun.xml.internal.bind.api.AccessorException;
036: import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
037: import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;
038: import com.sun.xml.internal.bind.v2.model.core.ID;
039: import com.sun.xml.internal.bind.v2.model.nav.Navigator;
040: import com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator;
041: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement;
042: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElementRef;
043: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
044: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfoSet;
045: import com.sun.xml.internal.bind.v2.runtime.FilterTransducer;
046: import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
047: import com.sun.xml.internal.bind.v2.runtime.InlineBinaryTransducer;
048: import com.sun.xml.internal.bind.v2.runtime.MimeTypedTransducer;
049: import com.sun.xml.internal.bind.v2.runtime.SchemaTypeTransducer;
050: import com.sun.xml.internal.bind.v2.runtime.Transducer;
051: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
052:
053: import org.xml.sax.SAXException;
054:
055: /**
056: * {@link ModelBuilder} that works at the run-time by using
057: * the {@code java.lang.reflect} package.
058: *
059: * <p>
060: * This extends {@link ModelBuilder} by providing more functionalities such
061: * as accessing the fields and classes.
062: *
063: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
064: */
065: public class RuntimeModelBuilder extends
066: ModelBuilder<Type, Class, Field, Method> {
067:
068: public RuntimeModelBuilder(
069: RuntimeAnnotationReader annotationReader,
070: String defaultNamespaceRemap) {
071: super (annotationReader, Navigator.REFLECTION,
072: defaultNamespaceRemap);
073: }
074:
075: @Override
076: public RuntimeNonElement getClassInfo(Class clazz,
077: Locatable upstream) {
078: return (RuntimeNonElement) super .getClassInfo(clazz, upstream);
079: }
080:
081: @Override
082: protected RuntimeEnumLeafInfoImpl createEnumLeafInfo(Class clazz,
083: Locatable upstream) {
084: return new RuntimeEnumLeafInfoImpl(this , upstream, clazz);
085: }
086:
087: @Override
088: protected RuntimeClassInfoImpl createClassInfo(Class clazz,
089: Locatable upstream) {
090: return new RuntimeClassInfoImpl(this , upstream, clazz);
091: }
092:
093: @Override
094: public RuntimeElementInfoImpl createElementInfo(
095: RegistryInfoImpl<Type, Class, Field, Method> registryInfo,
096: Method method) throws IllegalAnnotationException {
097: return new RuntimeElementInfoImpl(this , registryInfo, method);
098: }
099:
100: @Override
101: public RuntimeArrayInfoImpl createArrayInfo(Locatable upstream,
102: Type arrayType) {
103: return new RuntimeArrayInfoImpl(this , upstream,
104: (Class) arrayType);
105: }
106:
107: public ReflectionNavigator getNavigator() {
108: return (ReflectionNavigator) nav;
109: }
110:
111: @Override
112: protected RuntimeTypeInfoSetImpl createTypeInfoSet() {
113: return new RuntimeTypeInfoSetImpl(reader);
114: }
115:
116: @Override
117: public RuntimeTypeInfoSet link() {
118: return (RuntimeTypeInfoSet) super .link();
119: }
120:
121: /**
122: * Creates a {@link Transducer} given a reference.
123: *
124: * Used to implement {@link RuntimeNonElementRef#getTransducer()}.
125: * Shouldn't be called from anywhere else.
126: *
127: * TODO: this is not the proper place for this class to be in.
128: */
129: public static Transducer createTransducer(RuntimeNonElementRef ref) {
130: Transducer t = ref.getTarget().getTransducer();
131: RuntimePropertyInfo src = ref.getSource();
132: ID id = src.id();
133:
134: if (id == ID.IDREF)
135: return RuntimeBuiltinLeafInfoImpl.STRING;
136:
137: if (id == ID.ID)
138: t = new IDTransducerImpl(t);
139:
140: MimeType emt = src.getExpectedMimeType();
141: if (emt != null)
142: t = new MimeTypedTransducer(t, emt);
143:
144: if (src.inlineBinaryData())
145: t = new InlineBinaryTransducer(t);
146:
147: if (src.getSchemaType() != null)
148: t = new SchemaTypeTransducer(t, src.getSchemaType());
149:
150: return t;
151: }
152:
153: /**
154: * Transducer implementation for ID.
155: *
156: * This transducer wraps another {@link Transducer} and adds
157: * handling for ID.
158: */
159: private static final class IDTransducerImpl<ValueT> extends
160: FilterTransducer<ValueT> {
161: public IDTransducerImpl(Transducer<ValueT> core) {
162: super (core);
163: }
164:
165: @Override
166: public ValueT parse(CharSequence lexical)
167: throws AccessorException, SAXException {
168: String value = WhiteSpaceProcessor.trim(lexical).toString();
169: UnmarshallingContext.getInstance().addToIdTable(value);
170: return core.parse(value);
171: }
172: }
173: }
|